generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDefinitionValidatorTest.php
More file actions
385 lines (358 loc) · 14.1 KB
/
DefinitionValidatorTest.php
File metadata and controls
385 lines (358 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
declare(strict_types=1);
namespace Yiisoft\Definitions\Tests\Unit\Helpers;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;
use Yiisoft\Definitions\ArrayDefinition;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use Yiisoft\Definitions\Helpers\DefinitionValidator;
use Yiisoft\Definitions\Reference;
use Yiisoft\Definitions\Tests\Support\Car;
use Yiisoft\Definitions\Tests\Support\CarFactory;
use Yiisoft\Definitions\Tests\Support\ColorPink;
use Yiisoft\Definitions\Tests\Support\GearBox;
use Yiisoft\Definitions\Tests\Support\MagicCall;
use Yiisoft\Definitions\Tests\Support\Phone;
use Yiisoft\Definitions\Tests\Support\ReadonlyProperty;
use Yiisoft\Definitions\Tests\Support\Recorder;
use Yiisoft\Definitions\Tests\Support\UTF8User;
use Yiisoft\Definitions\ValueDefinition;
final class DefinitionValidatorTest extends TestCase
{
public function testIntegerKeyOfArray(): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage(
'Invalid definition: invalid key in array definition. Only string keys are allowed, got 0.',
);
DefinitionValidator::validate([
ArrayDefinition::CLASS_NAME => Phone::class,
'RX',
]);
}
public static function dataInvalidClass(): array
{
return [
[42, 'Invalid definition: class name must be a non-empty string, got int.'],
['', 'Invalid definition: class name must be a non-empty string.'],
];
}
#[DataProvider('dataInvalidClass')]
public function testInvalidClass($class, string $message): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage($message);
DefinitionValidator::validate([
ArrayDefinition::CLASS_NAME => $class,
]);
}
public static function dataInvalidProperty(): array
{
$object1 = new class {
public bool $visible = true;
private bool $invisible = true;
};
return [
[stdClass::class, '$', 'Invalid definition: class "stdClass" does not have any public properties.'],
[
$object1::class,
'$1',
sprintf(
'Invalid definition: class "%s" does not have the public property with name "1". Possible properties to set: "visible".',
$object1::class,
),
],
[
$object1::class,
'$invisible',
sprintf(
'Invalid definition: property "%s" must be public and writable.',
$object1::class . '::$invisible',
),
],
[
UTF8User::class,
'$имя',
sprintf(
'Invalid definition: property "%s" must be public and writable.',
UTF8User::class . '::$имя',
),
],
];
}
#[DataProvider('dataInvalidProperty')]
public function testInvalidProperty(string $object, string $property, string $message): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage($message);
DefinitionValidator::validate([
ArrayDefinition::CLASS_NAME => $object,
$property => [],
]);
}
public static function dataInvalidDefinitionWithoutClass(): array
{
return [
[[]],
[[stdClass::class]],
];
}
#[DataProvider('dataInvalidDefinitionWithoutClass')]
public function testWithoutClass($definition): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage('Invalid definition: no class name specified.');
DefinitionValidator::validate($definition);
}
public static function dataInvalidStringDefinition(): array
{
return [
['', 'Invalid definition: class name must be a non-empty string.'],
[' ', 'Invalid definition: class name must be a non-empty string.'],
];
}
#[DataProvider('dataInvalidStringDefinition')]
public function testInvalidStringDefinition(string $definition, string $message): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage($message);
DefinitionValidator::validate($definition);
}
public function testInvalidConstructor(): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage(
'Invalid definition: incorrect constructor arguments. Expected array, got string.',
);
DefinitionValidator::validate([
ArrayDefinition::CLASS_NAME => Phone::class,
ArrayDefinition::CONSTRUCTOR => 'Kiradzu',
]);
}
public static function dataInvalidMethodCalls(): array
{
return [
[
Phone::class,
['addApp()' => 'Browser'],
'Invalid definition: incorrect method "addApp()" arguments. Expected array, got "string". Probably you should wrap them into square brackets.',
],
[
Phone::class,
['deleteApp()' => ['Browser']],
sprintf(
'Invalid definition: class "%s" does not have the public method with name "deleteApp". Possible methods to call: "getName", "getVersion", "getColors", "addApp", "getApps", "getId", "setId", "setId777", "withAuthor", "getAuthor", "withCountry", "getCountry"',
Phone::class,
),
],
[
stdClass::class,
['addApp()' => ['Browser']],
'Invalid definition: class "stdClass" does not have the public method with name "addApp". No public methods available to call.',
],
[
Recorder::class,
['__call()' => ['test magic method']],
sprintf('Invalid definition: method "%s::__call()" must be public.', Recorder::class),
],
];
}
#[DataProvider('dataInvalidMethodCalls')]
public function testInvalidMethodCalls(string $class, array $methodCalls, string $message): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage($message);
DefinitionValidator::validate(
array_merge([
ArrayDefinition::CLASS_NAME => $class,
], $methodCalls),
);
}
public static function dataErrorOnPropertyOrMethodTypo(): array
{
return [
['dev', true, 'Invalid definition: key "dev" is not allowed. Did you mean "$dev"?'],
['setId', [42], 'Invalid definition: key "setId" is not allowed. Did you mean "setId()"?'],
['()setId', [42], 'Invalid definition: key "()setId" is not allowed. Did you mean "setId()"?'],
[
'set()Id',
[42],
sprintf(
'Invalid definition: class "%s" does not have the public method with name "set". Possible methods to call: "getName", "getVersion", "getColors", "addApp", "getApps", "getId", "setId", "setId777", "withAuthor", "getAuthor", "withCountry", "getCountry"',
Phone::class,
),
],
[
' set()Id',
[42],
sprintf(
'Invalid definition: class "%s" does not have the public method with name " set". Possible methods to call: "getName", "getVersion", "getColors", "addApp", "getApps", "getId", "setId", "setId777", "withAuthor", "getAuthor", "withCountry", "getCountry"',
Phone::class,
),
],
[
'getCountryPrivate',
[42],
sprintf(
'Invalid definition: key "getCountryPrivate" is not allowed. Method "%s" must be public to be able to be called.',
Phone::class . '::getCountryPrivate()',
),
],
[
'country',
[42],
sprintf(
'Invalid definition: key "country" is not allowed. Property "%s" must be public to be able to be called.',
Phone::class . '::$country',
),
],
[
' country',
[42],
sprintf(
'Invalid definition: key " country" is not allowed. Property "%s" must be public to be able to be called.',
Phone::class . '::$country',
),
],
[
'()test',
[42],
'Invalid definition: key "()test" is not allowed. The key may be a call of a method or a setting of a property.',
],
[
'var$',
true,
'Invalid definition: key "var$" is not allowed. The key may be a call of a method or a setting of a property.',
],
[
' var$',
true,
'Invalid definition: key " var$" is not allowed. The key may be a call of a method or a setting of a property.',
],
[
'100$',
true,
'Invalid definition: key "100$" is not allowed. The key may be a call of a method or a setting of a property.',
],
[
'test-тест',
true,
'Invalid definition: key "test-тест" is not allowed. The key may be a call of a method or a setting of a property.',
],
];
}
#[DataProvider('dataErrorOnPropertyOrMethodTypo')]
public function testErrorOnPropertyOrMethodTypo(string $key, $value, string $errorMessage): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage($errorMessage);
DefinitionValidator::validate([
'class' => Phone::class,
'__construct()' => ['name' => 'hello'],
'$dev' => true,
'setId()' => [24],
$key => $value,
]);
}
public static function dataValidate(): array
{
return [
'ready-object' => [new stdClass()],
'reference' => [Reference::to('test')],
'class-name' => [Car::class],
'callable' => [CarFactory::create(...)],
'array-definition' => [['class' => ColorPink::class]],
'magic method reference' => [['class' => Recorder::class, 'add()' => ['test magic method']]],
'magic only call reference' => [['class' => MagicCall::class, 'add()' => ['test magic method']]],
'magic property reference' => [['class' => Recorder::class, '$add' => ['test magic property']]],
'multiple call methods' => [
[
'class' => Recorder::class,
'test()' => [1],
'test()_2' => [2],
'test()_3' => [3],
],
],
];
}
#[DataProvider('dataValidate')]
public function testValidate($definition, ?string $id = null): void
{
DefinitionValidator::validate($definition, $id);
$this->expectNotToPerformAssertions();
}
public static function dataValidateArrayDefinition(): array
{
return [
[['class' => ColorPink::class], 'pink_color'],
[['class' => ColorPink::class], null],
[[], ColorPink::class],
];
}
#[DataProvider('dataValidateArrayDefinition')]
public function testValidateArrayDefinition(array $definition, ?string $id): void
{
DefinitionValidator::validateArrayDefinition($definition, $id);
$this->assertSame(1, 1);
}
public function testInteger(): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage('Invalid definition: 42');
DefinitionValidator::validate(42);
}
public function testDefinitionInArguments(): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessageMatches(
'/^Only references are allowed in constructor arguments, a definition object was provided: (\\\\|)' .
preg_quote(ValueDefinition::class) .
'.*/',
);
DefinitionValidator::validate([
'class' => GearBox::class,
'__construct()' => [
new ValueDefinition(56),
],
]);
}
public static function dataIncorrectMethodName(): array
{
return [
[
[
'class' => Phone::class,
'addApp()hm()' => ['name' => 'hello'],
],
'Invalid definition: key "addApp()hm()" is not allowed. The key may be a call of a method or a setting of a property.',
],
[
[
'class' => UTF8User::class,
'имя()aa()' => ['значение'],
],
'Invalid definition: key "имя()aa()" is not allowed. The key may be a call of a method or a setting of a property.',
],
];
}
#[DataProvider('dataIncorrectMethodName')]
public function testIncorrectMethodName(array $config, string $message): void
{
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage($message);
DefinitionValidator::validate($config);
}
public function testReadonlyProperty(): void
{
$definition = [
'class' => ReadonlyProperty::class,
'$var' => 'test',
];
$this->expectException(InvalidConfigException::class);
$this->expectExceptionMessage(
'Invalid definition: property "Yiisoft\Definitions\Tests\Support\ReadonlyProperty::$var" must be public and writable.',
);
DefinitionValidator::validate($definition);
}
}