1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Validator\Tests\Rule; |
||
6 | |||
7 | use stdClass; |
||
8 | use Yiisoft\Validator\AttributeTranslator\ArrayAttributeTranslator; |
||
9 | use Yiisoft\Validator\AttributeTranslatorInterface; |
||
10 | use Yiisoft\Validator\AttributeTranslatorProviderInterface; |
||
11 | use Yiisoft\Validator\Rule\StringValue; |
||
12 | use Yiisoft\Validator\Rule\StringValueHandler; |
||
13 | use Yiisoft\Validator\RulesProviderInterface; |
||
14 | use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
||
15 | use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
||
16 | use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
||
17 | use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
||
18 | use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
||
19 | |||
20 | final class StringValueTest extends RuleTestCase |
||
21 | { |
||
22 | use DifferentRuleInHandlerTestTrait; |
||
23 | use RuleWithOptionsTestTrait; |
||
24 | use SkipOnErrorTestTrait; |
||
25 | use WhenTestTrait; |
||
26 | |||
27 | public function testGetName(): void |
||
28 | { |
||
29 | $rule = new StringValue(); |
||
30 | $this->assertSame(StringValue::class, $rule->getName()); |
||
31 | } |
||
32 | |||
33 | public function dataOptions(): array |
||
34 | { |
||
35 | return [ |
||
36 | [ |
||
37 | new StringValue(), |
||
38 | [ |
||
39 | 'message' => [ |
||
40 | 'template' => 'The value must be a string.', |
||
41 | 'parameters' => [], |
||
42 | ], |
||
43 | 'skipOnEmpty' => false, |
||
44 | 'skipOnError' => false, |
||
45 | ], |
||
46 | ], |
||
47 | [ |
||
48 | new StringValue(message: 'Custom message.', skipOnEmpty: true, skipOnError: true), |
||
49 | [ |
||
50 | 'message' => [ |
||
51 | 'template' => 'Custom message.', |
||
52 | 'parameters' => [], |
||
53 | ], |
||
54 | 'skipOnEmpty' => true, |
||
55 | 'skipOnError' => true, |
||
56 | ], |
||
57 | ], |
||
58 | ]; |
||
59 | } |
||
60 | |||
61 | public function dataValidationPassed(): array |
||
62 | { |
||
63 | $rule = new StringValue(); |
||
64 | |||
65 | return [ |
||
66 | 'value: empty string' => ['', [$rule]], |
||
67 | 'value: empty string with whitespaces' => [' ', [$rule]], |
||
68 | 'value: non-empty string' => ['test', [$rule]], |
||
69 | 'value: null, skipOnEmpty: true' => [null, [new StringValue(skipOnEmpty: true)]], |
||
70 | 'value: null, when: custom callable allowing everything except null' => [ |
||
71 | null, |
||
72 | [new StringValue(when: static fn (mixed $value): bool => $value !== null)], |
||
73 | ], |
||
74 | 'value: object providing rules and valid data' => [ |
||
75 | new class () { |
||
76 | #[StringValue] |
||
77 | private string $name = 'test'; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
78 | }, |
||
79 | null, |
||
80 | ], |
||
81 | ]; |
||
82 | } |
||
83 | |||
84 | public function dataValidationFailed(): array |
||
85 | { |
||
86 | $rule = new StringValue(); |
||
87 | $message = 'The value must be a string.'; |
||
88 | |||
89 | return [ |
||
90 | 'value: null' => [null, [$rule], ['' => [$message]]], |
||
91 | 'value: integer' => [1, [$rule], ['' => [$message]]], |
||
92 | 'value: float' => [1.5, [$rule], ['' => [$message]]], |
||
93 | 'value: boolean' => [false, [$rule], ['' => [$message]]], |
||
94 | 'value: array' => [['test'], [$rule], ['' => [$message]]], |
||
95 | 'value: object' => [new stdClass(), [$rule], ['' => [$message]]], |
||
96 | 'value: null, multiple rules' => [ |
||
97 | null, |
||
98 | [ |
||
99 | new StringValue(), |
||
100 | new StringValue(), |
||
101 | ], |
||
102 | ['' => [$message, $message]], |
||
103 | ], |
||
104 | 'value: null, multiple rules, skipOnError: true' => [ |
||
105 | null, |
||
106 | [ |
||
107 | new StringValue(), |
||
108 | new StringValue(skipOnError: true), |
||
109 | ], |
||
110 | ['' => [$message]], |
||
111 | ], |
||
112 | 'value: integer, when: custom callable allowing everything except null' => [ |
||
113 | 1, |
||
114 | [new StringValue(when: static fn (mixed $value): bool => $value !== null)], |
||
115 | ['' => [$message]], |
||
116 | ], |
||
117 | 'value: object providing rules and wrong data' => [ |
||
118 | new class () { |
||
119 | #[StringValue] |
||
120 | private ?string $name = null; |
||
0 ignored issues
–
show
|
|||
121 | }, |
||
122 | null, |
||
123 | ['name' => [$message]], |
||
124 | ], |
||
125 | 'value: boolean, message: custom' => [ |
||
126 | false, |
||
127 | [new StringValue(message: 'Custom message.')], |
||
128 | ['' => ['Custom message.']], |
||
129 | ], |
||
130 | 'value: boolean, message: custom, with parameters' => [ |
||
131 | false, |
||
132 | [new StringValue(message: 'Attribute - {attribute}, type - {type}.')], |
||
133 | ['' => ['Attribute - , type - bool.']], |
||
134 | ], |
||
135 | 'value: boolean, message: custom, with parameters, attribute set' => [ |
||
136 | ['data' => false], |
||
137 | ['data' => new StringValue(message: 'Attribute - {attribute}, type - {type}.')], |
||
138 | ['data' => ['Attribute - data, type - bool.']], |
||
139 | ], |
||
140 | 'value: object providing rules, attribute labels and wrong data' => [ |
||
141 | new class () implements RulesProviderInterface, AttributeTranslatorProviderInterface { |
||
142 | public function __construct( |
||
143 | public ?string $name = null, |
||
144 | ) { |
||
145 | } |
||
146 | |||
147 | public function getAttributeLabels(): array |
||
148 | { |
||
149 | return [ |
||
150 | 'name' => 'Имя', |
||
151 | ]; |
||
152 | } |
||
153 | |||
154 | public function getAttributeTranslator(): ?AttributeTranslatorInterface |
||
155 | { |
||
156 | return new ArrayAttributeTranslator($this->getAttributeLabels()); |
||
157 | } |
||
158 | |||
159 | public function getRules(): array |
||
160 | { |
||
161 | return [ |
||
162 | 'name' => [ |
||
163 | new StringValue(message: '{attribute} плохое.'), |
||
164 | ], |
||
165 | ]; |
||
166 | } |
||
167 | }, |
||
168 | null, |
||
169 | ['name' => ['Имя плохое.']], |
||
170 | ], |
||
171 | ]; |
||
172 | } |
||
173 | |||
174 | public function testSkipOnError(): void |
||
175 | { |
||
176 | $this->testSkipOnErrorInternal(new StringValue(), new StringValue(skipOnError: true)); |
||
177 | } |
||
178 | |||
179 | public function testWhen(): void |
||
180 | { |
||
181 | $when = static fn (mixed $value): bool => $value !== null; |
||
182 | $this->testWhenInternal(new StringValue(), new StringValue(when: $when)); |
||
183 | } |
||
184 | |||
185 | protected function getDifferentRuleInHandlerItems(): array |
||
186 | { |
||
187 | return [StringValue::class, StringValueHandler::class]; |
||
188 | } |
||
189 | |||
190 | protected function getRuleClass(): string |
||
191 | { |
||
192 | return StringValue::class; |
||
193 | } |
||
194 | } |
||
195 |