ProxiedMethodsFilterTest::expectedMethods()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 114

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManagerTest\ProxyGenerator\Util;
6
7
use PHPUnit\Framework\TestCase;
8
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
9
use ProxyManagerTestAsset\BaseClass;
10
use ProxyManagerTestAsset\ClassWithAbstractMagicMethods;
11
use ProxyManagerTestAsset\ClassWithAbstractProtectedMethod;
12
use ProxyManagerTestAsset\ClassWithAbstractPublicMethod;
13
use ProxyManagerTestAsset\ClassWithCounterConstructor;
14
use ProxyManagerTestAsset\ClassWithFinalMagicMethods;
15
use ProxyManagerTestAsset\ClassWithMagicMethods;
16
use ProxyManagerTestAsset\ClassWithMethodWithByRefVariadicFunction;
17
use ProxyManagerTestAsset\ClassWithMethodWithVariadicFunction;
18
use ProxyManagerTestAsset\EmptyClass;
19
use ProxyManagerTestAsset\HydratedObject;
20
use ProxyManagerTestAsset\LazyLoadingMock;
21
use ReflectionClass;
22
use ReflectionMethod;
23
use function array_map;
24
use function sort;
25
26
/**
27
 * Tests for {@see \ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter}
28
 *
29
 * @covers \ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter
30
 * @group Coverage
31
 */
32
final class ProxiedMethodsFilterTest extends TestCase
33
{
34
    /**
35
     * @param array<int, string>|null $excludes
36
     * @param string[]                $expectedMethods
37
     *
38
     * @dataProvider expectedMethods
39
     */
40
    public function testFiltering(ReflectionClass $reflectionClass, ?array $excludes, array $expectedMethods) : void
41
    {
42
        $filtered = ProxiedMethodsFilter::getProxiedMethods($reflectionClass, $excludes);
43
44
        $keys = array_map(
45
            static function (ReflectionMethod $method) : string {
46
                return $method->getName();
47
            },
48
            $filtered
49
        );
50
51
        sort($keys);
52
        sort($expectedMethods);
53
54
        self::assertSame($keys, $expectedMethods);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<ProxyManagerTest\...oxiedMethodsFilterTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
    }
56
57
    /**
58
     * @param array<int, string>|null $excludes
59
     * @param string[]                $expectedMethods
60
     *
61
     * @dataProvider expectedAbstractPublicMethods
62
     */
63
    public function testFilteringOfAbstractPublic(
64
        ReflectionClass $reflectionClass,
65
        ?array $excludes,
66
        array $expectedMethods
67
    ) : void {
68
        $filtered = ProxiedMethodsFilter::getAbstractProxiedMethods($reflectionClass, $excludes);
69
70
        $keys = array_map(
71
            static function (ReflectionMethod $method) : string {
72
                return $method->getName();
73
            },
74
            $filtered
75
        );
76
77
        sort($keys);
78
        sort($expectedMethods);
79
80
        self::assertSame($keys, $expectedMethods);
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<ProxyManagerTest\...oxiedMethodsFilterTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
    }
82
83
    /**
84
     * Data provider
85
     *
86
     * @return ReflectionClass[][]|null[][]|string[][][]
87
     */
88
    public function expectedMethods() : array
89
    {
90
        return [
91
            [
92
                new ReflectionClass(BaseClass::class),
93
                null,
94
                [
95
                    'privatePropertyGetter',
96
                    'protectedPropertyGetter',
97
                    'publicArrayHintedMethod',
98
                    'publicByReferenceMethod',
99
                    'publicByReferenceParameterMethod',
100
                    'publicMethod',
101
                    'publicPropertyGetter',
102
                    'publicTypeHintedMethod',
103
                ],
104
            ],
105
            [
106
                new ReflectionClass(EmptyClass::class),
107
                null,
108
                [],
109
            ],
110
            [
111
                new ReflectionClass(LazyLoadingMock::class),
112
                null,
113
                [
114
                    'getProxyInitializer',
115
                    'getWrappedValueHolderValue',
116
                    'initializeProxy',
117
                    'isProxyInitialized',
118
                    'setProxyInitializer',
119
                ],
120
            ],
121
            [
122
                new ReflectionClass(LazyLoadingMock::class),
123
                [],
124
                [
125
                    'getProxyInitializer',
126
                    'getWrappedValueHolderValue',
127
                    'initializeProxy',
128
                    'isProxyInitialized',
129
                    'setProxyInitializer',
130
                ],
131
            ],
132
            [
133
                new ReflectionClass(HydratedObject::class),
134
                ['doFoo'],
135
                ['__get'],
136
            ],
137
            [
138
                new ReflectionClass(HydratedObject::class),
139
                ['Dofoo'],
140
                ['__get'],
141
            ],
142
            [
143
                new ReflectionClass(HydratedObject::class),
144
                [],
145
                ['doFoo', '__get'],
146
            ],
147
            [
148
                new ReflectionClass(ClassWithAbstractProtectedMethod::class),
149
                null,
150
                [],
151
            ],
152
            [
153
                new ReflectionClass(ClassWithAbstractPublicMethod::class),
154
                null,
155
                ['publicAbstractMethod'],
156
            ],
157
            [
158
                new ReflectionClass(ClassWithAbstractPublicMethod::class),
159
                ['publicAbstractMethod'],
160
                [],
161
            ],
162
            [
163
                new ReflectionClass(ClassWithAbstractMagicMethods::class),
164
                null,
165
                [],
166
            ],
167
            [
168
                new ReflectionClass(ClassWithAbstractMagicMethods::class),
169
                [],
170
                [
171
                    '__clone',
172
                    '__get',
173
                    '__isset',
174
                    '__set',
175
                    '__sleep',
176
                    '__unset',
177
                    '__wakeup',
178
                ],
179
            ],
180
            [
181
                new ReflectionClass(ClassWithMethodWithVariadicFunction::class),
182
                null,
183
                ['foo', 'buz'],
184
            ],
185
            [
186
                new ReflectionClass(ClassWithMethodWithByRefVariadicFunction::class),
187
                null,
188
                ['tuz'],
189
            ],
190
            'final magic methods' => [
191
                new ReflectionClass(ClassWithFinalMagicMethods::class),
192
                null,
193
                [],
194
            ],
195
            'non-final constructor is to be skipped' => [
196
                new ReflectionClass(ClassWithCounterConstructor::class),
197
                null,
198
                ['getAmount'],
199
            ],
200
        ];
201
    }
202
203
    /**
204
     * Data provider
205
     *
206
     * @return ReflectionClass[][]|null[][]|string[][][]
207
     */
208
    public function expectedAbstractPublicMethods() : array
209
    {
210
        return [
211
            [
212
                new ReflectionClass(BaseClass::class),
213
                null,
214
                [],
215
            ],
216
            [
217
                new ReflectionClass(EmptyClass::class),
218
                null,
219
                [],
220
            ],
221
            [
222
                new ReflectionClass(ClassWithAbstractProtectedMethod::class),
223
                null,
224
                [],
225
            ],
226
            [
227
                new ReflectionClass(ClassWithAbstractPublicMethod::class),
228
                null,
229
                ['publicAbstractMethod'],
230
            ],
231
            [
232
                new ReflectionClass(ClassWithAbstractPublicMethod::class),
233
                ['publicAbstractMethod'],
234
                [],
235
            ],
236
            [
237
                new ReflectionClass(ClassWithMagicMethods::class),
238
                [],
239
                [],
240
            ],
241
            [
242
                new ReflectionClass(ClassWithAbstractMagicMethods::class),
243
                null,
244
                [],
245
            ],
246
            [
247
                new ReflectionClass(ClassWithAbstractMagicMethods::class),
248
                [],
249
                [
250
                    '__clone',
251
                    '__get',
252
                    '__isset',
253
                    '__set',
254
                    '__sleep',
255
                    '__unset',
256
                    '__wakeup',
257
                ],
258
            ],
259
        ];
260
    }
261
}
262