44
55namespace Yiisoft \Validator \Tests \Rule ;
66
7+ use PHPUnit \Framework \TestCase ;
8+ use stdClass ;
79use Yiisoft \Validator \Rule \AtLeast ;
8- use Yiisoft \Validator \SerializableRuleInterface ;
10+ use Yiisoft \Validator \Tests \Stub \FakeValidatorFactory ;
11+ use Yiisoft \Validator \Validator ;
912
10- final class AtLeastTest extends AbstractRuleTest
13+ final class AtLeastTest extends TestCase
1114{
1215 public function testGetName (): void
1316 {
1417 $ rule = new AtLeast ([]);
1518 $ this ->assertSame ('atLeast ' , $ rule ->getName ());
1619 }
1720
18- public function optionsDataProvider (): array
21+ public function dataOptions (): array
1922 {
2023 return [
2124 [
2225 new AtLeast (['attr1 ' , 'attr2 ' ]),
2326 [
2427 'attributes ' => [
25- 'attr1 ' , 'attr2 ' ,
28+ 'attr1 ' ,
29+ 'attr2 ' ,
2630 ],
2731 'min ' => 1 ,
2832 'message ' => [
@@ -37,7 +41,8 @@ public function optionsDataProvider(): array
3741 new AtLeast (['attr1 ' , 'attr2 ' ], min: 2 ),
3842 [
3943 'attributes ' => [
40- 'attr1 ' , 'attr2 ' ,
44+ 'attr1 ' ,
45+ 'attr2 ' ,
4146 ],
4247 'min ' => 2 ,
4348 'message ' => [
@@ -51,8 +56,149 @@ public function optionsDataProvider(): array
5156 ];
5257 }
5358
54- protected function getRule (): SerializableRuleInterface
59+ /**
60+ * @dataProvider dataOptions
61+ */
62+ public function testOptions (AtLeast $ rule , array $ expectedOptions ): void
5563 {
56- return new AtLeast ([]);
64+ $ options = $ rule ->getOptions ();
65+ $ this ->assertSame ($ expectedOptions , $ options );
66+ }
67+
68+ public function dataValidationPassed (): array
69+ {
70+ return [
71+ [
72+ new class () {
73+ public $ attr1 = 1 ;
74+ public $ attr2 = null ;
75+ },
76+ [new AtLeast (['attr1 ' , 'attr2 ' ])],
77+ ],
78+ [
79+ new class () {
80+ public $ attr1 = null ;
81+ public $ attr2 = 1 ;
82+ },
83+ [new AtLeast (['attr2 ' ])],
84+ ],
85+ [
86+ ['attr1 ' => 1 , 'attr2 ' => null ],
87+ [new AtLeast (['attr1 ' , 'attr2 ' ])],
88+ ],
89+ [
90+ ['attr1 ' => null , 'attr2 ' => 1 ],
91+ [new AtLeast (['attr2 ' ])],
92+ ],
93+ [
94+ new class () {
95+ public $ obj ;
96+
97+ public function __construct ()
98+ {
99+ $ this ->obj = new class () {
100+ public $ attr1 = 1 ;
101+ public $ attr2 = null ;
102+ };
103+ }
104+ },
105+ ['obj ' => new AtLeast (['attr1 ' , 'attr2 ' ])],
106+ ],
107+ [
108+ new class () {
109+ public $ obj ;
110+
111+ public function __construct ()
112+ {
113+ $ this ->obj = new class () {
114+ public $ attr1 = null ;
115+ public $ attr2 = 1 ;
116+ };
117+ }
118+ },
119+ ['obj ' => new AtLeast (['attr2 ' ])],
120+ ],
121+ [
122+ ['obj ' => ['attr1 ' => 1 , 'attr2 ' => null ]],
123+ ['obj ' => new AtLeast (['attr1 ' , 'attr2 ' ])],
124+ ],
125+ [
126+ ['obj ' => ['attr1 ' => null , 'attr2 ' => 1 ]],
127+ ['obj ' => new AtLeast (['attr2 ' ])],
128+ ],
129+ ];
130+ }
131+
132+ /**
133+ * @dataProvider dataValidationPassed
134+ */
135+ public function testValidationPassed (mixed $ data , array $ rules ): void
136+ {
137+ $ result = $ this ->createValidator ()->validate ($ data , $ rules );
138+
139+ $ this ->assertTrue ($ result ->isValid ());
140+ }
141+
142+ public function dataValidationFailed (): array
143+ {
144+ return [
145+ [
146+ new class () {
147+ public $ attr1 = 1 ;
148+ public $ attr2 = null ;
149+ },
150+ [new AtLeast (['attr2 ' ])],
151+ ['' => ['The model is not valid. Must have at least "1" filled attributes. ' ]],
152+ ],
153+ [
154+ new class () {
155+ public $ attr1 = 1 ;
156+ public $ attr2 = null ;
157+ },
158+ [new AtLeast (['attr1 ' , 'attr2 ' ], min: 2 )],
159+ ['' => ['The model is not valid. Must have at least "2" filled attributes. ' ]],
160+ ],
161+ ];
162+ }
163+
164+ /**
165+ * @dataProvider dataValidationFailed
166+ */
167+ public function testValidationFailed (object $ object , array $ rules , array $ errorMessagesIndexedByPath ): void
168+ {
169+ $ result = $ this ->createValidator ()->validate ($ object , $ rules );
170+
171+ $ this ->assertFalse ($ result ->isValid ());
172+ $ this ->assertSame ($ errorMessagesIndexedByPath , $ result ->getErrorMessagesIndexedByPath ());
173+ }
174+
175+ public function testCustomErrorMessage (): void
176+ {
177+ $ object = new class () {
178+ public $ attr1 = 1 ;
179+ public $ attr2 = null ;
180+ };
181+ $ rules = [new AtLeast (['attr1 ' , 'attr2 ' ], min: 2 , message: 'Custom error ' )];
182+
183+ $ result = $ this ->createValidator ()->validate ($ object , $ rules );
184+
185+ $ this ->assertFalse ($ result ->isValid ());
186+ $ this ->assertSame (
187+ ['' => ['Custom error ' ]],
188+ $ result ->getErrorMessagesIndexedByPath ()
189+ );
190+ }
191+
192+ private function createValidator (): Validator
193+ {
194+ return FakeValidatorFactory::make ();
195+ }
196+
197+ private function createObject (mixed $ attr1 , mixed $ attr2 ): stdClass
198+ {
199+ $ object = new stdClass ();
200+ $ object ->attr1 = $ attr1 ;
201+ $ object ->attr2 = $ attr2 ;
202+ return $ object ;
57203 }
58204}
0 commit comments