1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Helper; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Yiisoft\Validator\Helper\RulesNormalizer; |
9
|
|
|
use Yiisoft\Validator\Result; |
10
|
|
|
use Yiisoft\Validator\Rule\Number; |
11
|
|
|
use Yiisoft\Validator\RuleInterface; |
12
|
|
|
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility; |
13
|
|
|
|
14
|
|
|
final class RulesNormalizerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function dataNormalize(): array |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
|
|
'null' => [[], null], |
20
|
|
|
'object' => [ |
21
|
|
|
[ |
22
|
|
|
'name' => ['required'], |
23
|
|
|
'age' => ['number'], |
24
|
|
|
'number' => ['number'], |
25
|
|
|
], |
26
|
|
|
new ObjectWithDifferentPropertyVisibility(), |
27
|
|
|
], |
28
|
|
|
'class-string' => [ |
29
|
|
|
[ |
30
|
|
|
'name' => ['required'], |
31
|
|
|
'age' => ['number'], |
32
|
|
|
'number' => ['number'], |
33
|
|
|
], |
34
|
|
|
ObjectWithDifferentPropertyVisibility::class, |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @dataProvider dataNormalize |
41
|
|
|
* |
42
|
|
|
* More cases are covered in {@see ValidatorTest}. |
43
|
|
|
* |
44
|
|
|
* @see ValidatorTest::testDataAndRulesCombinations() |
45
|
|
|
* @see ValidatorTest::testRulesPropertyVisibility() |
46
|
|
|
* @see ValidatorTest::testWithEmptyArrayOfRules() |
47
|
|
|
* @see ValidatorTest::testDiverseTypes() |
48
|
|
|
* @see ValidatorTest::testNullAsDataSet() |
49
|
|
|
* @see ValidatorTest::testValidateWithSingleRule() |
50
|
|
|
*/ |
51
|
|
|
public function testNormalizeWithArrayResult( |
52
|
|
|
array $expected, |
53
|
|
|
callable|iterable|object|string|null $rules, |
54
|
|
|
mixed $data = null |
55
|
|
|
): void { |
56
|
|
|
$rules = RulesNormalizer::normalize($rules, $data); |
57
|
|
|
|
58
|
|
|
$result = []; |
59
|
|
|
foreach ($rules as $attributeName => $attributeRules) { |
60
|
|
|
$result[$attributeName] = []; |
61
|
|
|
foreach ($attributeRules as $rule) { |
62
|
|
|
$result[$attributeName][] = $rule->getName(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->assertSame($expected, $result); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function dataNormalizeList(): array |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
[ |
73
|
|
|
[], |
74
|
|
|
[], |
75
|
|
|
], |
76
|
|
|
[ |
77
|
|
|
['callback'], |
78
|
|
|
static fn () => new Result(), |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
['number'], |
82
|
|
|
new Number(), |
83
|
|
|
], |
84
|
|
|
[ |
85
|
|
|
['number', 'callback'], |
86
|
|
|
[new Number(), static fn () => new Result()], |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider dataNormalizeList |
93
|
|
|
*/ |
94
|
|
|
public function testNormalizeList(array $expected, iterable|callable|RuleInterface $rules): void |
95
|
|
|
{ |
96
|
|
|
$rules = RulesNormalizer::normalizeList($rules); |
97
|
|
|
|
98
|
|
|
$result = []; |
99
|
|
|
foreach ($rules as $rule) { |
100
|
|
|
$result[] = $rule->getName(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->assertSame($expected, $result); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|