@@ -515,17 +515,73 @@ public function testSkipValidation()
515515 ->insert ($ data ));
516516 }
517517
518- public function testValidationSkipsNonExistingFields ()
518+ public function testCleanValidationRemovesAllWhenNoDataProvided ()
519519 {
520- $ model = new ValidModel ($ this ->db );
520+ $ model = new Model ($ this ->db );
521+ $ cleaner = $ this ->getPrivateMethodInvoker ($ model , 'cleanValidationRules ' );
522+
523+ $ rules = [
524+ 'name ' => 'required ' ,
525+ 'foo ' => 'bar ' ,
526+ ];
527+
528+ $ rules = $ cleaner ($ rules , null );
529+
530+ $ this ->assertEmpty ($ rules );
531+ }
532+
533+ public function testCleanValidationRemovesOnlyForFieldsNotProvided ()
534+ {
535+ $ model = new Model ($ this ->db );
536+ $ cleaner = $ this ->getPrivateMethodInvoker ($ model , 'cleanValidationRules ' );
537+
538+ $ rules = [
539+ 'name ' => 'required ' ,
540+ 'foo ' => 'required ' ,
541+ ];
521542
522543 $ data = [
523- 'description ' => 'some great marketing stuff ' ,
524- 'id ' => 42 ,
525- 'token ' => 42 ,
544+ 'foo ' => 'bar ' ,
545+ ];
546+
547+ $ rules = $ cleaner ($ rules , $ data );
548+
549+ $ this ->assertTrue (array_key_exists ('foo ' , $ rules ));
550+ $ this ->assertFalse (array_key_exists ('name ' , $ rules ));
551+ }
552+
553+ public function testCleanValidationReturnsAllWhenAllExist ()
554+ {
555+ $ model = new Model ($ this ->db );
556+ $ cleaner = $ this ->getPrivateMethodInvoker ($ model , 'cleanValidationRules ' );
557+
558+ $ rules = [
559+ 'name ' => 'required ' ,
560+ 'foo ' => 'required ' ,
526561 ];
527562
528- $ this ->assertEquals (5 , $ model ->insert ($ data ));
563+ $ data = [
564+ 'foo ' => 'bar ' ,
565+ 'name ' => null ,
566+ ];
567+
568+ $ rules = $ cleaner ($ rules , $ data );
569+
570+ $ this ->assertTrue (array_key_exists ('foo ' , $ rules ));
571+ $ this ->assertTrue (array_key_exists ('name ' , $ rules ));
572+ }
573+
574+ public function testValidationPassesWithMissingFields ()
575+ {
576+ $ model = new ValidModel ();
577+
578+ $ data = [
579+ 'foo ' => 'bar ' ,
580+ ];
581+
582+ $ result = $ model ->validate ($ data );
583+
584+ $ this ->assertTrue ($ result );
529585 }
530586
531587 //--------------------------------------------------------------------
@@ -893,6 +949,7 @@ public function testUpdateWithValidation()
893949 $ data ['description ' ] = 'This is a second test! ' ;
894950 unset($ data ['name ' ]);
895951
896- $ this ->assertTrue ($ model ->update ($ id , $ data ));
952+ $ result = $ model ->update ($ id , $ data );
953+ $ this ->assertTrue ($ result );
897954 }
898955}
0 commit comments