|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\RulesProvider; |
|
6
|
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\Deprecated; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use ReflectionProperty; |
|
10
|
|
|
use Traversable; |
|
11
|
|
|
use Yiisoft\Validator\Rule\HasLength; |
|
12
|
|
|
use Yiisoft\Validator\Rule\IsTrue; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Number; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Required; |
|
15
|
|
|
use Yiisoft\Validator\RuleInterface; |
|
16
|
|
|
use Yiisoft\Validator\RulesProvider\AttributesRulesProvider; |
|
17
|
|
|
use Yiisoft\Validator\Tests\Support\Data\ObjectWithDifferentPropertyVisibility; |
|
18
|
|
|
|
|
19
|
|
|
final class AttributesRulesProviderTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function dataBase(): array |
|
22
|
|
|
{ |
|
23
|
|
|
return [ |
|
24
|
|
|
'class-name' => [ |
|
25
|
|
|
[ |
|
26
|
|
|
'name' => [Required::class], |
|
27
|
|
|
'age' => [Number::class], |
|
28
|
|
|
'number' => [Number::class], |
|
29
|
|
|
], |
|
30
|
|
|
ObjectWithDifferentPropertyVisibility::class, |
|
31
|
|
|
], |
|
32
|
|
|
'object' => [ |
|
33
|
|
|
[ |
|
34
|
|
|
'name' => [Required::class], |
|
35
|
|
|
'age' => [Number::class], |
|
36
|
|
|
'number' => [Number::class], |
|
37
|
|
|
], |
|
38
|
|
|
new ObjectWithDifferentPropertyVisibility(), |
|
39
|
|
|
], |
|
40
|
|
|
'object with no properties' => [ |
|
41
|
|
|
[], |
|
42
|
|
|
new class () { |
|
43
|
|
|
}, |
|
44
|
|
|
], |
|
45
|
|
|
'object with properties without rule attributes' => [ |
|
46
|
|
|
[ |
|
47
|
|
|
'title' => [HasLength::class], |
|
48
|
|
|
], |
|
49
|
|
|
new class () { |
|
50
|
|
|
#[Deprecated(reason: 'test reason', replacement: 'test replacement')] |
|
51
|
|
|
private int $viewsCount = 1; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
private bool $active = true; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
#[HasLength(max: 255)] |
|
56
|
|
|
private string $title = 'Test title'; |
|
|
|
|
|
|
57
|
|
|
}, |
|
58
|
|
|
], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @dataProvider dataBase |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testBase(array $expectedRuleClassNames, string|object $source): void |
|
66
|
|
|
{ |
|
67
|
|
|
$rulesProvider = new AttributesRulesProvider($source); |
|
68
|
|
|
$ruleClassNames = $this->convertRulesToClassNames($rulesProvider->getRules()); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertSame($expectedRuleClassNames, $ruleClassNames); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function dataPropertyVisibility(): array |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
'class-name-and-private-protected-public' => [ |
|
77
|
|
|
[ |
|
78
|
|
|
'name' => [Required::class], |
|
79
|
|
|
'age' => [Number::class], |
|
80
|
|
|
'number' => [Number::class], |
|
81
|
|
|
], |
|
82
|
|
|
ObjectWithDifferentPropertyVisibility::class, |
|
83
|
|
|
ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC, |
|
84
|
|
|
], |
|
85
|
|
|
'class-name-and-private' => [ |
|
86
|
|
|
[ |
|
87
|
|
|
'number' => [Number::class], |
|
88
|
|
|
], |
|
89
|
|
|
ObjectWithDifferentPropertyVisibility::class, |
|
90
|
|
|
ReflectionProperty::IS_PRIVATE, |
|
91
|
|
|
], |
|
92
|
|
|
'class-name-and-protected' => [ |
|
93
|
|
|
[ |
|
94
|
|
|
'age' => [Number::class], |
|
95
|
|
|
], |
|
96
|
|
|
ObjectWithDifferentPropertyVisibility::class, |
|
97
|
|
|
ReflectionProperty::IS_PROTECTED, |
|
98
|
|
|
], |
|
99
|
|
|
'class-name-and-public' => [ |
|
100
|
|
|
[ |
|
101
|
|
|
'name' => [Required::class], |
|
102
|
|
|
], |
|
103
|
|
|
ObjectWithDifferentPropertyVisibility::class, |
|
104
|
|
|
ReflectionProperty::IS_PUBLIC, |
|
105
|
|
|
], |
|
106
|
|
|
'object-and-private-protected-public' => [ |
|
107
|
|
|
[ |
|
108
|
|
|
'name' => [Required::class], |
|
109
|
|
|
'age' => [Number::class], |
|
110
|
|
|
'number' => [Number::class], |
|
111
|
|
|
], |
|
112
|
|
|
new ObjectWithDifferentPropertyVisibility(), |
|
113
|
|
|
ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC, |
|
114
|
|
|
], |
|
115
|
|
|
'object-and-private' => [ |
|
116
|
|
|
[ |
|
117
|
|
|
'number' => [Number::class], |
|
118
|
|
|
], |
|
119
|
|
|
new ObjectWithDifferentPropertyVisibility(), |
|
120
|
|
|
ReflectionProperty::IS_PRIVATE, |
|
121
|
|
|
], |
|
122
|
|
|
'object-and-protected' => [ |
|
123
|
|
|
[ |
|
124
|
|
|
'age' => [Number::class], |
|
125
|
|
|
], |
|
126
|
|
|
new ObjectWithDifferentPropertyVisibility(), |
|
127
|
|
|
ReflectionProperty::IS_PROTECTED, |
|
128
|
|
|
], |
|
129
|
|
|
'object-and-public' => [ |
|
130
|
|
|
[ |
|
131
|
|
|
'name' => [Required::class], |
|
132
|
|
|
], |
|
133
|
|
|
new ObjectWithDifferentPropertyVisibility(), |
|
134
|
|
|
ReflectionProperty::IS_PUBLIC, |
|
135
|
|
|
], |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @dataProvider dataPropertyVisibility |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testPropertyVisibility( |
|
143
|
|
|
array $expectedRuleClassNames, |
|
144
|
|
|
string|object $source, |
|
145
|
|
|
int $propertyVisibility, |
|
146
|
|
|
): void { |
|
147
|
|
|
$rulesProvider = new AttributesRulesProvider($source, $propertyVisibility); |
|
148
|
|
|
|
|
149
|
|
|
$ruleClassNames = $this->convertRulesToClassNames($rulesProvider->getRules()); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertSame($expectedRuleClassNames, $ruleClassNames); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function dataStaticProperties(): array |
|
155
|
|
|
{ |
|
156
|
|
|
return [ |
|
157
|
|
|
[ |
|
158
|
|
|
['a' => [Required::class]], |
|
159
|
|
|
new class () { |
|
160
|
|
|
#[Required] |
|
161
|
|
|
public int $a = 1; |
|
162
|
|
|
#[IsTrue] |
|
163
|
|
|
public static bool $b = false; |
|
164
|
|
|
}, |
|
165
|
|
|
true, |
|
166
|
|
|
], |
|
167
|
|
|
[ |
|
168
|
|
|
['a' => [Required::class], 'b' => [IsTrue::class]], |
|
169
|
|
|
new class () { |
|
170
|
|
|
#[Required] |
|
171
|
|
|
public int $a = 1; |
|
172
|
|
|
#[IsTrue] |
|
173
|
|
|
public static bool $b = false; |
|
174
|
|
|
}, |
|
175
|
|
|
false, |
|
176
|
|
|
], |
|
177
|
|
|
]; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @dataProvider dataStaticProperties |
|
182
|
|
|
*/ |
|
183
|
|
|
public function testStaticProperties( |
|
184
|
|
|
array $expectedRuleClassNames, |
|
185
|
|
|
object $source, |
|
186
|
|
|
bool $skipStaticProperties, |
|
187
|
|
|
): void { |
|
188
|
|
|
$rulesProvider = new AttributesRulesProvider($source, skipStaticProperties: $skipStaticProperties); |
|
189
|
|
|
|
|
190
|
|
|
$ruleClassNames = $this->convertRulesToClassNames($rulesProvider->getRules()); |
|
191
|
|
|
|
|
192
|
|
|
$this->assertSame($expectedRuleClassNames, $ruleClassNames); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
private function convertRulesToClassNames(iterable $rules): array |
|
196
|
|
|
{ |
|
197
|
|
|
$classNames = []; |
|
198
|
|
|
foreach ($rules as $attribute => $attributeRules) { |
|
199
|
|
|
$classNames[$attribute] = array_map( |
|
200
|
|
|
static fn (RuleInterface $rule): string => $rule::class, |
|
201
|
|
|
$attributeRules instanceof Traversable ? iterator_to_array($attributeRules) : $attributeRules |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $classNames; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|