1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP: Nelson Martell Library file |
5
|
|
|
* |
6
|
|
|
* Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io) |
7
|
|
|
* |
8
|
|
|
* Licensed under The MIT License (MIT) |
9
|
|
|
* For full copyright and license information, please see the LICENSE |
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
11
|
|
|
* |
12
|
|
|
* @copyright 2016-2021 Nelson Martell |
13
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
14
|
|
|
* @since 0.6.0 |
15
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
16
|
|
|
* */ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace NelsonMartell\Test\DataProviders; |
21
|
|
|
|
22
|
|
|
use NelsonMartell\IConvertibleToString; |
23
|
|
|
use NelsonMartell\IMagicPropertiesContainer; |
24
|
|
|
use NelsonMartell\ICustomPrefixedPropertiesContainer; |
25
|
|
|
use NelsonMartell\IStrictPropertiesContainer; |
26
|
|
|
use NelsonMartell\PropertiesHandler; |
27
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\WithMagicPropertiesBaseClass; |
28
|
|
|
use NelsonMartell\Type; |
29
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\A; |
30
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\B; |
31
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\C; |
32
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\ToString; |
33
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\WithSomeMethodsChildClass; |
34
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\WithSomeMethodsClass; |
35
|
|
|
use NelsonMartell\Test\DataProviders\ExampleClass\WithManyMagicPropertiesClass; |
36
|
|
|
use NelsonMartell\Test\Helpers\ConstructorMethodTester; |
37
|
|
|
use NelsonMartell\Test\Helpers\HasReadOnlyProperties; |
38
|
|
|
use NelsonMartell\Test\Helpers\HasUnaccesibleProperties; |
39
|
|
|
use NelsonMartell\Test\Helpers\ImplementsIConvertibleToString; |
40
|
|
|
use NelsonMartell\Test\Helpers\ImplementsIStrictPropertiesContainer; |
41
|
|
|
use NelsonMartell\Test\TestCase\TypeTest; |
42
|
|
|
use InvalidArgumentException; |
43
|
|
|
use stdClass; |
44
|
|
|
|
45
|
|
|
use function NelsonMartell\typeof; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Data providers for NelsonMartell\Test\TestCase\TypeTest. |
49
|
|
|
* |
50
|
|
|
* @author Nelson Martell <[email protected]> |
51
|
|
|
* @since 0.6.0 |
52
|
|
|
* @internal |
53
|
|
|
* */ |
54
|
|
|
trait TypeTestProvider |
55
|
|
|
{ |
56
|
|
|
use ConstructorMethodTester; |
57
|
|
|
use HasReadOnlyProperties; |
58
|
|
|
use HasUnaccesibleProperties; |
59
|
|
|
use ImplementsIConvertibleToString; |
60
|
|
|
use ImplementsIStrictPropertiesContainer; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
* @since 1.0.0-dev |
66
|
|
|
*/ |
67
|
|
|
public function unaccesiblePropertiesProvider(): array |
68
|
|
|
{ |
69
|
|
|
$obj = new Type($this); |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
'$reflectionObject' => [$obj, 'reflectionObject'], |
73
|
|
|
'$namespace with case changed' => [$obj, 'Namespace'], |
74
|
|
|
'$name with case changed' => [$obj, 'Name'], |
75
|
|
|
'$shortName with case changed' => [$obj, 'ShortName'], |
76
|
|
|
'$methods with case changed' => [$obj, 'Methods'], |
77
|
|
|
'$vars with case changed' => [$obj, 'Vars'], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Provides valid arguments for constructor. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function goodConstructorArgumentsProvider(): array |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
'NULL' => [null], |
90
|
|
|
'integer' => [1], |
91
|
|
|
'double' => [1.9999], |
92
|
|
|
'string' => ['str'], |
93
|
|
|
ToString::class => [new ToString()], |
94
|
|
|
'array' => [[]], |
95
|
|
|
'stdClass' => [new \stdClass()], |
96
|
|
|
__CLASS__ => [$this], |
97
|
|
|
|
98
|
|
|
'string: ' . ToString::class => [ToString::class, true], |
99
|
|
|
'string: ' . stdClass::class => [stdClass::class, true], |
100
|
|
|
'non string as `$obj` arg' => [new stdClass(), true], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function toStringProvider(): array |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
'NULL' => ['NULL', typeof(null)], |
108
|
|
|
'integer' => ['integer', typeof(1)], |
109
|
|
|
'double' => ['double', typeof(1.9999)], |
110
|
|
|
'string' => ['string', typeof('str')], |
111
|
|
|
'array' => ['array', typeof([])], |
112
|
|
|
'stdClass' => ['object (stdClass)', typeof(new \stdClass())], |
113
|
|
|
__CLASS__ => ['object (NelsonMartell\Test\TestCase\TypeTest)', typeof($this)], |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function badConstructorArgumentsProvider(): array |
118
|
|
|
{ |
119
|
|
|
return [ |
120
|
|
|
'not existing class' => [InvalidArgumentException::class, 'Hola', true], |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function canBeStringProvider(): array |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
'NULL' => [null], |
128
|
|
|
'integer' => [1], |
129
|
|
|
'double' => [1.9999], |
130
|
|
|
'string' => ['str'], |
131
|
|
|
ToString::class => [new ToString()], |
132
|
|
|
'string: ' . ToString::class => [ToString::class, true], |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function canNotBeStringProvider(): array |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
|
|
'array' => [[]], |
140
|
|
|
'stdClass' => [new \stdClass()], |
141
|
|
|
__CLASS__ => [$this], |
142
|
|
|
'string: ' . stdClass::class => [stdClass::class, true], |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function readonlyPropertiesProvider(): array |
151
|
|
|
{ |
152
|
|
|
$obj = new Type($this); |
153
|
|
|
|
154
|
|
|
return [ |
155
|
|
|
[$obj, 'name', __CLASS__], |
156
|
|
|
[$obj, 'shortName', 'TypeTest'], |
157
|
|
|
[$obj, 'namespace', 'NelsonMartell\Test\TestCase'], |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function objectInstanceProvider(): array |
162
|
|
|
{ |
163
|
|
|
return [[new Type($this)]]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* [ |
168
|
|
|
* (bool) if should match, |
169
|
|
|
* (mixed) type, |
170
|
|
|
* (array) objects |
171
|
|
|
* ] |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function methodIsProvider(): array |
175
|
|
|
{ |
176
|
|
|
return [ |
177
|
|
|
[true, (bool) true, [true, false]], |
178
|
|
|
[false, (bool) false, [true, false, 0]], |
179
|
|
|
[true, 123, [11, 0, -34]], |
180
|
|
|
[false, 123, [11, 0, -34.456]], |
181
|
|
|
[true, 1.23, [11.0, 0.0, -34.6]], |
182
|
|
|
[false, 1.23, [11.0, 0, -34.4]], |
183
|
|
|
[true, '', ['', '0', 'i am not a string']], |
184
|
|
|
[false, '', [11.2, '0', true]], |
185
|
|
|
[true, null, [null, null]], |
186
|
|
|
[false, null, [[], null, false]], |
187
|
|
|
[true, new stdClass(), [new stdClass(), new stdClass()]], |
188
|
|
|
[false, new stdClass(), [[], new stdClass(), true]], |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* [ |
194
|
|
|
* (bool) if should match, |
195
|
|
|
* (mixed) type, |
196
|
|
|
* (array) objects |
197
|
|
|
* ] |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public function methodIsInProvider(): array |
201
|
|
|
{ |
202
|
|
|
return [ |
203
|
|
|
[true, (bool) true, [true, false, 1, 'string']], |
204
|
|
|
[false, (bool) false, ['true', 'false', 0, 1]], |
205
|
|
|
[true, 123, [11, 0, -34]], |
206
|
|
|
[false, 123, [11.2, '0', true]], |
207
|
|
|
[true, 1.23, [11, 0.5, -34]], |
208
|
|
|
[false, 1.23, [11, '0', true]], |
209
|
|
|
[true, '', [11, '0', -34]], |
210
|
|
|
[false, '', [11.2, 0, true]], |
211
|
|
|
[true, null, [null, true, 4]], |
212
|
|
|
[false, null, [[], 'null', false]], |
213
|
|
|
[true, new stdClass(), [new stdClass(), new A(), 0]], |
214
|
|
|
[false, new stdClass(), [[], 'stdClass', true]], |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* [ |
220
|
|
|
* (mixed) object, |
221
|
|
|
* (array<string>) interfaces |
222
|
|
|
* ] |
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function getInterfacesProvider(): array |
227
|
|
|
{ |
228
|
|
|
return [ |
229
|
|
|
[new A(), [IStrictPropertiesContainer::class]], |
230
|
|
|
[new B(), [IStrictPropertiesContainer::class]], |
231
|
|
|
[new C(), [ICustomPrefixedPropertiesContainer::class, IStrictPropertiesContainer::class]], |
232
|
|
|
[new ToString(), [IConvertibleToString::class, 'Stringable']], |
233
|
|
|
[new WithMagicPropertiesBaseClass(), [IMagicPropertiesContainer::class, IStrictPropertiesContainer::class]], |
234
|
|
|
['string', []], |
235
|
|
|
]; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* [ |
240
|
|
|
* (mixed) object, |
241
|
|
|
* (array<string>) interfaces |
242
|
|
|
* ] |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
|
|
public function getTraitsProvider(): array |
247
|
|
|
{ |
248
|
|
|
return [ |
249
|
|
|
[new A(), [PropertiesHandler::class]], |
250
|
|
|
[new B(), []], |
251
|
|
|
[new C(), []], |
252
|
|
|
[new ToString(), []], |
253
|
|
|
[new Type(TypeTest::class, true), [ |
254
|
|
|
TypeTestProvider::class, |
255
|
|
|
], |
256
|
|
|
], |
257
|
|
|
['string', []], |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* [ |
263
|
|
|
* (mixed) object, |
264
|
|
|
* (array<string>) interfaces |
265
|
|
|
* ] |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function getRecursiveTraitsProvider(): array |
270
|
|
|
{ |
271
|
|
|
return [ |
272
|
|
|
[new A(), [PropertiesHandler::class]], |
273
|
|
|
[new B(), [PropertiesHandler::class]], |
274
|
|
|
[new C(), [PropertiesHandler::class]], |
275
|
|
|
[new Type(TypeTest::class, true), [ |
276
|
|
|
ConstructorMethodTester::class, |
277
|
|
|
HasReadOnlyProperties::class, |
278
|
|
|
HasUnaccesibleProperties::class, |
279
|
|
|
ImplementsIConvertibleToString::class, |
280
|
|
|
ImplementsIStrictPropertiesContainer::class, |
281
|
|
|
TypeTestProvider::class, |
282
|
|
|
], |
283
|
|
|
], |
284
|
|
|
[new ToString(), []], |
285
|
|
|
['string', []], |
286
|
|
|
]; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* <mixed, string> |
291
|
|
|
* |
292
|
|
|
* @return array |
293
|
|
|
*/ |
294
|
|
|
public function hasPropertyProvider(): array |
295
|
|
|
{ |
296
|
|
|
return [ |
297
|
|
|
[new A(), 'attribute1'], |
298
|
|
|
[new A(), 'property1'], |
299
|
|
|
[new Type(A::class, true), 'attribute4'], |
300
|
|
|
[new B(), 'property2'], |
301
|
|
|
[new B(), 'attribute2'], |
302
|
|
|
|
303
|
|
|
// Magic properties |
304
|
|
|
[new WithMagicPropertiesBaseClass(), 'baseProperty', true, true], |
305
|
|
|
|
306
|
|
|
[new WithManyMagicPropertiesClass(), 'UpperCaseProperty', false, true], |
307
|
|
|
[new WithManyMagicPropertiesClass(), 'UpperCaseWithDifferentSetterProperty', false, true], |
308
|
|
|
[new WithManyMagicPropertiesClass(), 'propertyWithLongClass', false, true], |
309
|
|
|
'Magic property after propertyWithLongClass' => [new WithManyMagicPropertiesClass(), 'anotherPropertyAfterPropertyWithLongClass', false, true], |
310
|
|
|
]; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* <mixed, string> |
315
|
|
|
* |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
|
|
public function hasNotPropertyProvider(): array |
319
|
|
|
{ |
320
|
|
|
$obj = new stdClass(); |
321
|
|
|
|
322
|
|
|
$obj->testProperty = 0; |
323
|
|
|
|
324
|
|
|
return [ |
325
|
|
|
['string', ''], |
326
|
|
|
[new A(), 'xyz'], |
327
|
|
|
[new A(), 'Property1'], |
328
|
|
|
[new B(), 'property5'], |
329
|
|
|
'should ignore dinamic properties' => [$obj, 'testProperty'], |
330
|
|
|
'With $includeMagic = false' => [new WithMagicPropertiesBaseClass(), 'baseProperty', true, false], |
331
|
|
|
]; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* |
336
|
|
|
* |
337
|
|
|
* @return array[] mixed => string |
338
|
|
|
*/ |
339
|
|
|
public function hasMethodProvider(): array |
340
|
|
|
{ |
341
|
|
|
$obj = new WithSomeMethodsClass(); |
342
|
|
|
$obk = new WithSomeMethodsChildClass(); |
343
|
|
|
|
344
|
|
|
return [ |
345
|
|
|
'private' => [$obj, 'privateMethod'], |
346
|
|
|
'protected' => [$obj, 'protectedMethod'], |
347
|
|
|
'public' => [$obj, 'publicMethod'], |
348
|
|
|
// 'magic' => [$obj, 'magicMethod'], // FIXME Implement detection of magic methods |
|
|
|
|
349
|
|
|
|
350
|
|
|
'private in parent' => [$obk, 'privateMethod'], |
351
|
|
|
'protected in parent' => [$obk, 'protectedMethod'], |
352
|
|
|
'public in parent' => [$obk, 'publicMethod'], |
353
|
|
|
// 'magic in parent' => [$obk, 'magicMethod'], // FIXME Implement detection of magic methods |
|
|
|
|
354
|
|
|
]; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function hasNotMethodProvider(): array |
358
|
|
|
{ |
359
|
|
|
$obj = new WithSomeMethodsClass(); |
360
|
|
|
|
361
|
|
|
return [ |
362
|
|
|
[$obj, 'someMethodThatIsNotDefined'], |
363
|
|
|
]; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.