11<?php
22use insolita \ArrayStructureValidator \ArrayStructureValidator ;
3+ use PHPUnit \Framework \Assert ;
34use tests \TestCase ;
45use yii \base \DynamicModel ;
56use yii \base \Model ;
@@ -29,13 +30,13 @@ public function testArrayAndSizeValidation():void
2930 $ model ->addRule (...$ rule );
3031 }
3132 $ model ->validate ();
32- expect ($ model ->hasErrors (), true );
33- expect ($ model ->getFirstError ('notArray ' ))-> equals ( 'notArray ' );
34- expect ($ model ->getFirstError ('emptyArray ' ))-> null ( );
35- expect ($ model ->getFirstError ('emptyArray2 ' ))-> contains ( ' Empty Array2 is too small ' );
36- expect ( $ model ->getFirstError ('hash ' ))-> contains ( ' Hash is too small ' );
37- expect ( $ model ->getFirstError ('hash2 ' ))-> null ( );
38- expect ( $ model ->getFirstError ('indexed ' ))-> contains ( ' Indexed is too large ' );
33+ self :: assertTrue ($ model ->hasErrors ());
34+ self :: assertEquals ($ model ->getFirstError ('notArray ' ), 'notArray ' );
35+ self :: assertNull ($ model ->getFirstError ('emptyArray ' ));
36+ self :: assertNull ($ model ->getFirstError ('hash2 ' ) );
37+ self :: assertContains ( ' Empty Array2 is too small ' , $ model ->getFirstError ('emptyArray2 ' ) );
38+ self :: assertContains ( ' Hash is too small ' , $ model ->getFirstError ('hash ' ) );
39+ self :: assertContains ( ' Indexed is too large ' , $ model ->getFirstError ('indexed ' ));
3940 }
4041
4142 public function testArraySizeValidationWithEachRule ():void
@@ -63,11 +64,11 @@ public function testArraySizeValidationWithEachRule():void
6364 }
6465 $ model ->validate ();
6566 //VarDumper::dump($model->getErrors());
66- expect ($ model ->hasErrors ())-> true ( );
67- expect ($ model ->hasErrors ('a ' ))-> false ( );
68- expect ($ model ->hasErrors ('b ' ))-> true ( );
69- expect ( $ model ->getErrors ('b ' ))-> count ( 1 );
70- expect ( $ model ->getErrors ('c ' ))-> count ( 2 );
67+ self :: assertTrue ($ model ->hasErrors ());
68+ self :: assertFalse ($ model ->hasErrors ('a ' ));
69+ self :: assertTrue ($ model ->hasErrors ('b ' ));
70+ self :: assertCount ( 1 , $ model ->getErrors ('b ' ));
71+ self :: assertCount ( 2 , $ model ->getErrors ('c ' ));
7172 }
7273
7374 public function testWithAssociativeStructure ():void
@@ -132,14 +133,13 @@ public function testWithAssociativeStructure():void
132133 }
133134 $ model ->validate ();
134135 //VarDumper::dump($model->getErrors());
135- expect ($ model ->hasErrors ())->true ();
136- expect ($ model ->hasErrors ('valid ' ))->false ();
137- expect ($ model ->hasErrors ('invalid1 ' ))->true ();
138- expect ($ model ->hasErrors ('invalid2 ' ))->true ();
139- expect ($ model ->hasErrors ('invalid3 ' ))->true ();
140- expect ($ model ->getErrors ('invalid1 ' ))->count (1 );
141- expect ($ model ->getErrors ('invalid2 ' ))->count (count ($ data ['invalid2 ' ]));
142- expect ($ model ->getErrors ('invalid3 ' ))->contains ('Invalid3 contains unexpected items foo,bar ' );
136+ self ::assertTrue ($ model ->hasErrors ());
137+ self ::assertFalse ($ model ->hasErrors ('valid ' ));
138+ self ::assertTrue ($ model ->hasErrors ('invalid1 ' ));
139+ self ::assertTrue ($ model ->hasErrors ('invalid2 ' ));
140+ self ::assertTrue ($ model ->hasErrors ('invalid3 ' ));
141+ self ::assertCount (1 , $ model ->getErrors ('invalid1 ' ));
142+ self ::assertContains ('Invalid3 contains unexpected items foo,bar ' , $ model ->getErrors ('invalid3 ' ));
143143 }
144144
145145 public function testWithIndexedStructureEachOption ():void
@@ -175,8 +175,8 @@ public function testWithIndexedStructureEachOption():void
175175 }
176176 $ model ->validate ();
177177 //VarDumper::dump($model->getErrors());
178- expect ($ model ->hasErrors ('valid ' ))-> false ( );
179- expect ($ model ->hasErrors ('invalid ' ))-> true ( );
178+ self :: assertFalse ($ model ->hasErrors ('valid ' ));
179+ self :: assertTrue ($ model ->hasErrors ('invalid ' ));
180180 }
181181
182182 public function testWithIndexedStructureAndEachValidator ():void
@@ -222,8 +222,8 @@ public function testWithIndexedStructureAndEachValidator():void
222222 }
223223 $ model ->validate ();
224224 //VarDumper::dump($model->getErrors());
225- expect ($ model ->hasErrors ('valid ' ))-> false ( );
226- expect ($ model ->hasErrors ('invalid ' ))-> true ( );
225+ self :: assertFalse ($ model ->hasErrors ('valid ' ));
226+ self :: assertTrue ($ model ->hasErrors ('invalid ' ));
227227 }
228228
229229 public function testMutableImmutable ():void
@@ -256,11 +256,11 @@ public function testMutableImmutable():void
256256 $ model ->addRule (...$ rule );
257257 }
258258
259- expect ($ model ->validate ())-> true ( );
260- expect ($ model ->immutable )-> equals ( $ data ['immutable ' ]);
261- expect ($ model ->mutable )-> equals ( ['x ' => 11 , 'y ' => 'foo ' , 'z ' => 100500 ]);
262- expect ($ model ->mutableIndexed [0 ])-> equals ( ['x ' => 11 , 'y ' => 'foo ' , 'z ' => 100500 ]);
263- expect ($ model ->mutableIndexed [1 ])-> equals ( ['x ' => 12 , 'y ' => 'bar ' , 'z ' => 100500 ]);
259+ self :: assertTrue ($ model ->validate ());
260+ self :: assertEquals ($ model ->immutable , $ data ['immutable ' ]);
261+ self :: assertEquals ($ model ->mutable , ['x ' => 11 , 'y ' => 'foo ' , 'z ' => 100500 ]);
262+ self :: assertEquals ($ model ->mutableIndexed [0 ], ['x ' => 11 , 'y ' => 'foo ' , 'z ' => 100500 ]);
263+ self :: assertEquals ($ model ->mutableIndexed [1 ], ['x ' => 12 , 'y ' => 'bar ' , 'z ' => 100500 ]);
264264 }
265265
266266 public function testWithClosureRules ():void
@@ -274,18 +274,18 @@ public function testWithClosureRules():void
274274 ];
275275 $ closure = function ($ attribute , $ model , $ index , $ baseModel , $ baseAttribute ) {
276276 if ($ baseAttribute === 'hash ' ) {
277- expect ($ index)-> null ( );
277+ self :: assertNull ($ index );
278278 } else {
279- expect (in_array ($ index , [0 , 1 ], true ))-> true ( );
279+ self :: assertTrue (in_array ($ index , [0 , 1 ], true ));
280280 }
281- expect ( $ model )-> isInstanceOf ( DynamicModel::class);
282- expect ( $ model -> hasAttribute ( ' a ' ))-> true ( );
283- expect ($ model ->hasAttribute ('b ' ))-> true ( );
284- expect ($ model ->hasAttribute ('hash ' ))-> false ( );
285- expect ( $ baseModel )-> isInstanceOf (DynamicModel::class );
286- expect ($ baseModel ->hasAttribute ('a ' ))-> false ( );
287- expect ($ baseModel ->hasAttribute ('hash ' ))-> true ( );
288- expect ($ baseModel ->hasAttribute ('indexed ' ))-> true ( );
281+ self :: assertInstanceOf ( DynamicModel::class, $ model );
282+ self :: assertInstanceOf (DynamicModel::class, $ baseModel );
283+ self :: assertTrue ($ model ->hasAttribute ('a ' ) );
284+ self :: assertTrue ($ model ->hasAttribute ('b ' ) );
285+ self :: assertFalse ( $ model -> hasAttribute ( ' hash ' ) );
286+ self :: assertFalse ($ baseModel ->hasAttribute ('a ' ));
287+ self :: assertTrue ($ baseModel ->hasAttribute ('hash ' ));
288+ self :: assertTrue ($ baseModel ->hasAttribute ('indexed ' ));
289289 if ($ model ->b !== 'foo ' ) {
290290 $ model ->addError ($ attribute , $ attribute . ' Fail condition from closure ' );
291291 }
@@ -309,11 +309,11 @@ public function testWithClosureRules():void
309309 foreach ($ rules as $ rule ) {
310310 $ model ->addRule (...$ rule );
311311 }
312- expect ($ model ->validate ())->false ();
313312 //VarDumper::dump($model->errors);
314- expect ($ model ->getErrors ('indexed ' ))->contains ('[1]a Fail condition from closure ' );
315- expect ($ model ->getErrors ('indexed ' ))->contains ('[1]b Fail condition from closure ' );
316- expect ($ model ->hasErrors ('hash ' ))->false ();
313+ self ::assertFalse ($ model ->validate ());
314+ self ::assertFalse ($ model ->hasErrors ('hash ' ));
315+ self ::assertContains ('[1]a Fail condition from closure ' , $ model ->getErrors ('indexed ' ));
316+ self ::assertContains ('[1]b Fail condition from closure ' , $ model ->getErrors ('indexed ' ));
317317 }
318318
319319 public function testNestedArray ():void
@@ -382,20 +382,20 @@ public function testNestedArray():void
382382 foreach ($ rules as $ rule ) {
383383 $ model ->addRule (...$ rule );
384384 }
385- expect ($ model ->validate ())-> true ( );
385+ self :: assertTrue ($ model ->validate ());
386386 //VarDumper::dump($model->getAttributes());
387387 foreach ($ model ->a ['foo ' ] as $ item ) {
388- expect ( $ item )-> hasKey ( 'z ' );
389- expect ( $ item ['z ' ])-> equals ( 100500 );
388+ self :: assertArrayHasKey ( 'z ' , $ item );
389+ self :: assertEquals ( 100500 , $ item ['z ' ]);
390390 }
391391 foreach ($ model ->a ['bar ' ] as $ item ) {
392- expect ( $ item )-> hasKey ( 'a ' );
393- expect ( $ item )-> hasKey ( 'b ' );
394- expect ( $ item )-> hasKey ( 'c ' );
395- expect ( $ item )-> hasKey ( 'x ' );
392+ self :: assertArrayHasKey ( 'a ' , $ item );
393+ self :: assertArrayHasKey ( 'b ' , $ item );
394+ self :: assertArrayHasKey ( 'c ' , $ item );
395+ self :: assertArrayHasKey ( 'x ' , $ item );
396396 }
397- expect ($ model ->b ['bar ' ]['a ' ])-> equals ( null );
398- expect ($ model ->b ['bar ' ]['b ' ])-> equals ( 'foo ' );
397+ self :: assertNull ($ model ->b ['bar ' ]['a ' ]);
398+ self :: assertEquals ($ model ->b ['bar ' ]['b ' ], 'foo ' );
399399 }
400400
401401 public function testErrorMessages ():void
@@ -576,30 +576,30 @@ public function testErrorMessages():void
576576 foreach ($ rules as $ rule ) {
577577 $ model ->addRule (...$ rule );
578578 }
579- expect ($ model ->validate ())-> false ( );
580- expect ( $ model -> getErrors ( ' indexedFullErrors ' ))-> contains ( ' [foo][0]Y cannot be blank. ' );
581- expect ( $ model -> getErrors ( ' indexedFullErrors ' ))-> contains ( ' [foo][1]Y cannot be blank. ' );
582- expect ( $ model -> getErrors ( ' indexedFullErrors ' ))-> contains ( ' [foo][0]X must be no greater than 10. ' );
583- expect ( $ model -> getErrors ( ' indexedFullErrors ' ))-> contains ( ' [bar][0]A must be no less than 2. ' );
584- expect ( $ model -> getErrors ( ' indexedFullErrors ' ))-> contains ( ' [bar][1]B is invalid. ' );
579+ self :: assertFalse ($ model ->validate ());
580+ self :: assertContains ( ' [foo][0]Y cannot be blank. ', $ model -> getErrors ( ' indexedFullErrors ' ) );
581+ self :: assertContains ( ' [foo][1]Y cannot be blank. ', $ model -> getErrors ( ' indexedFullErrors ' ) );
582+ self :: assertContains ( ' [foo][0]X must be no greater than 10. ', $ model -> getErrors ( ' indexedFullErrors ' ) );
583+ self :: assertContains ( ' [bar][0]A must be no less than 2. ', $ model -> getErrors ( ' indexedFullErrors ' ) );
584+ self :: assertContains ( ' [bar][1]B is invalid. ', $ model -> getErrors ( ' indexedFullErrors ' ) );
585585
586- expect ( $ model -> getErrors ( ' indexedStopOnFirst ' ))-> contains ( ' [foo][0]Y cannot be blank. ' );
587- expect ( $ model -> getErrors ( ' indexedStopOnFirst ' ))-> notContains ( ' [foo][1]Y cannot be blank. ' );
588- expect ( $ model -> getErrors ( ' indexedStopOnFirst ' ))-> contains ( ' [foo][0]X must be no greater than 10. ' );
589- expect ( $ model -> getErrors ( ' indexedStopOnFirst ' ))-> contains ( ' [bar][0]A must be no less than 2. ' );
590- expect ( $ model -> getErrors ( ' indexedStopOnFirst ' ))-> notContains ( ' [bar][1]B is invalid. ' );
586+ self :: assertContains ( ' [foo][0]Y cannot be blank. ', $ model -> getErrors ( ' indexedStopOnFirst ' ) );
587+ self :: assertNotContains ( ' [foo][1]Y cannot be blank. ', $ model -> getErrors ( ' indexedStopOnFirst ' ) );
588+ self :: assertContains ( ' [foo][0]X must be no greater than 10. ', $ model -> getErrors ( ' indexedStopOnFirst ' ) );
589+ self :: assertContains ( ' [bar][0]A must be no less than 2. ', $ model -> getErrors ( ' indexedStopOnFirst ' ) );
590+ self :: assertNotContains ( ' [bar][1]B is invalid. ', $ model -> getErrors ( ' indexedStopOnFirst ' ) );
591591
592- expect ( $ model -> getErrors ( ' hashFullErrors ' ))-> contains ( ' [bar]A custom message for required ' );
593- expect ( $ model -> getErrors ( ' hashFullErrors ' ))-> contains ( ' [bar]Id is not a valid email address. ' );
592+ self :: assertContains ( ' [bar]A custom message for required ', $ model -> getErrors ( ' hashFullErrors ' ) );
593+ self :: assertContains ( ' [bar]Id is not a valid email address. ', $ model -> getErrors ( ' hashFullErrors ' ) );
594594
595595 $ compactErrors1 = implode ("\n" ,
596596 ['[bar]A custom message for required ' , '[bar]Id is not a valid email address. ' ]
597597 );
598598 $ compactErrors2 = implode ("\n" ,
599599 ['[bar]Id is not a valid email address. ' , '[bar]A custom message for required ' ]
600600 );
601- $ errors = $ model ->getErrors ('hashCompactErrors ' );
602- expect ($ errors === $ compactErrors1 || $ errors === $ compactErrors2 );
601+ $ errors = implode ( "\n" , $ model ->getErrors ('hashCompactErrors ' ) );
602+ self :: assertTrue ($ errors === $ compactErrors1 || $ errors === $ compactErrors2 );
603603 }
604604
605605 public function testUniqueValidatorNotSupported ()
@@ -629,9 +629,9 @@ public function testValidationWithoutModel()
629629 ]);
630630 $ error = '' ;
631631 $ isValid = $ validator ->validate ([['x ' => 100 ], ['x ' => 5 ], ['x ' => 3 , 'y ' => 4 ]], $ error );
632- expect ($ isValid)-> false ( );
633- expect ( $ error )-> contains ( '[0]Y cannot be blank. ' );
634- expect ( $ error )-> contains ( '[0]X must be no greater than 10. ' );
632+ self :: assertFalse ($ isValid );
633+ self :: assertContains ( '[0]Y cannot be blank. ' , $ error );
634+ self :: assertContains ( '[0]X must be no greater than 10. ' , $ error );
635635 }
636636
637637 public function testScenarioConditionsShouldBeApplied ():void
@@ -686,25 +686,25 @@ public function rules()
686686 $ model ->scenario = 'default ' ;
687687 $ model ->value = ['x ' => '1 ' , 'y ' => null , 'foo ' => '123 ' ];
688688 $ model ->validate ();
689- expect ($ model ->value ['y ' ])-> null ( );
690- expect ($ model ->value [ ' z ' ])-> equals ( ' bar ' );
691- expect ( $ model ->hasErrors ())-> false ( );
689+ self :: assertNull ($ model ->value ['y ' ]);
690+ self :: assertFalse ($ model ->hasErrors () );
691+ self :: assertEquals ( ' bar ' , $ model ->value [ ' z ' ] );
692692
693693 $ model ->value = ['x ' => '1 ' , 'y ' => null , 'foo ' => '1234 ' ];
694694 $ model ->scenario = 'test1 ' ;
695695 $ model ->validate ();
696696 //VarDumper::dump([$model->scenario, $model->getErrors()]);
697- expect ( $ model ->value ['y ' ])-> equals ( 1 );
698- expect ( $ model ->value ['z ' ])-> equals ( ' foo ' );
699- expect ( $ model -> getErrors ( ' value ' ))-> contains ( ' X must be no less than 5. ' );
700- expect ( $ model -> getErrors ( ' value ' ))-> contains ( ' Foo should contain at most 3 characters. ' );
697+ self :: assertEquals ( 1 , $ model ->value ['y ' ]);
698+ self :: assertEquals ( ' foo ' , $ model ->value ['z ' ]);
699+ self :: assertContains ( ' X must be no less than 5. ', $ model -> getErrors ( ' value ' ) );
700+ self :: assertContains ( ' Foo should contain at most 3 characters. ', $ model -> getErrors ( ' value ' ) );
701701
702702 $ model ->scenario = 'test2 ' ;
703703 $ model ->value = ['x ' => '1 ' , 'y ' => null , 'foo ' => '123 ' ];
704704 $ model ->validate ();
705- expect ( $ model ->value ['y ' ])-> equals ( 2 );
706- expect ( $ model ->value ['z ' ])-> equals ( ' foo ' );
707- expect ($ model ->hasErrors ())-> false ( );
705+ self :: assertEquals ( 2 , $ model ->value ['y ' ]);
706+ self :: assertEquals ( ' foo ' , $ model ->value ['z ' ]);
707+ self :: assertFalse ($ model ->hasErrors ());
708708 }
709709
710710 public function testWhenConditionsShouldBeApplied ()
@@ -725,6 +725,14 @@ public function scenarios()
725725
726726 public function rules ()
727727 {
728+ $ checker = function ($ model , $ attribute , $ index , $ baseModel , $ baseAttribute ) {
729+ Assert::assertInstanceOf (DynamicModel::class, $ model );
730+ Assert::assertInstanceOf (Model::class, $ baseModel );
731+ Assert::assertEquals ('z ' , $ attribute );
732+ Assert::assertEquals ('value ' , $ baseAttribute );
733+ Assert::assertNull ($ index );
734+ return $ model ->x > 10 ;
735+ };
728736 return [
729737 [
730738 'value ' ,
@@ -735,17 +743,7 @@ public function rules()
735743 [
736744 'default ' ,
737745 'value ' => 'foo ' ,
738- 'when ' => function (
739- $ model , $ attribute , $ index , $ baseModel ,
740- $ baseAttribute
741- ) {
742- expect ($ model )->isInstanceOf (DynamicModel::class);
743- expect ($ attribute )->equals ('z ' );
744- expect ($ baseModel )->isInstanceOf (Model::class);
745- expect ($ baseAttribute )->equals ('value ' );
746- expect ($ index )->equals (null );
747- return $ model ->x > 10 ;
748- },
746+ 'when ' => $ checker ,
749747 ],
750748 [
751749 'default ' ,
@@ -763,16 +761,16 @@ public function rules()
763761 $ model ->dummy = 'bar ' ;
764762 $ model ->value = ['x ' => 1 ];
765763 $ model ->validate ();
766- expect ( $ model ->value ['z ' ])-> equals ( ' bar ' );
764+ self :: assertEquals ( ' bar ' , $ model ->value ['z ' ]);
767765
768766 $ model ->dummy = 'bar ' ;
769767 $ model ->value = ['x ' => 15 ];
770768 $ model ->validate ();
771- expect ( $ model ->value ['z ' ])-> equals ( ' foo ' );
769+ self :: assertEquals ( ' foo ' , $ model ->value ['z ' ]);
772770
773771 $ model ->dummy = '' ;
774772 $ model ->value = ['x ' => 5 ];
775773 $ model ->validate ();
776- expect ($ model ->value ['z ' ])-> null ( );
774+ self :: assertNull ($ model ->value ['z ' ]);
777775 }
778776}
0 commit comments