@@ -570,7 +570,7 @@ public function testParseWithValidMatch(): void
570570 self ::assertArrayHasKey ('type ' , $ lastError );
571571 self ::assertSame (E_USER_DEPRECATED , $ lastError ['type ' ]);
572572 self ::assertArrayHasKey ('message ' , $ lastError );
573- self ::assertSame ('Use regexp instead ' , $ lastError ['message ' ]);
573+ self ::assertSame ('Use pattern instead ' , $ lastError ['message ' ]);
574574 }
575575
576576 public function testParseWithInvalidMatch (): void
@@ -600,6 +600,53 @@ public function testParseWithInvalidMatch(): void
600600 }
601601 }
602602
603+ public function testParseWithPatternWithInvalidPattern (): void
604+ {
605+ try {
606+ (new StringSchema ())->pattern ('test ' );
607+
608+ throw new \Exception ('code should not be reached ' );
609+ } catch (\InvalidArgumentException $ e ) {
610+ self ::assertSame ('Invalid pattern "test" given ' , $ e ->getMessage ());
611+ }
612+ }
613+
614+ public function testParseWithValidPattern (): void
615+ {
616+ $ input = 'aBcDeFg ' ;
617+
618+ $ schema = (new StringSchema ())->pattern ('/^[a-z]+$/i ' );
619+
620+ self ::assertSame ($ input , $ schema ->parse ($ input ));
621+ }
622+
623+ public function testParseWithInvalidPattern (): void
624+ {
625+ $ input = 'a1B2C3d4 ' ;
626+
627+ $ schema = (new StringSchema ())->pattern ('/^[a-z]+$/i ' );
628+
629+ try {
630+ $ schema ->parse ($ input );
631+
632+ throw new \Exception ('code should not be reached ' );
633+ } catch (ErrorsException $ errorsException ) {
634+ self ::assertSame ([
635+ [
636+ 'path ' => '' ,
637+ 'error ' => [
638+ 'code ' => 'string.pattern ' ,
639+ 'template ' => '{{given}} does not pattern {{pattern}} ' ,
640+ 'variables ' => [
641+ 'pattern ' => '/^[a-z]+$/i ' ,
642+ 'given ' => $ input ,
643+ ],
644+ ],
645+ ],
646+ ], $ errorsException ->errors ->jsonSerialize ());
647+ }
648+ }
649+
603650 public function testParseWithRegexpWithInvalidPattern (): void
604651 {
605652 try {
@@ -613,11 +660,21 @@ public function testParseWithRegexpWithInvalidPattern(): void
613660
614661 public function testParseWithValidRegexp (): void
615662 {
663+ error_clear_last ();
664+
616665 $ input = 'aBcDeFg ' ;
617666
618667 $ schema = (new StringSchema ())->regexp ('/^[a-z]+$/i ' );
619668
620669 self ::assertSame ($ input , $ schema ->parse ($ input ));
670+
671+ $ lastError = error_get_last ();
672+
673+ self ::assertNotNull ($ lastError );
674+ self ::assertArrayHasKey ('type ' , $ lastError );
675+ self ::assertSame (E_USER_DEPRECATED , $ lastError ['type ' ]);
676+ self ::assertArrayHasKey ('message ' , $ lastError );
677+ self ::assertSame ('Use pattern instead ' , $ lastError ['message ' ]);
621678 }
622679
623680 public function testParseWithInvalidRegexp (): void
0 commit comments