44
55namespace Chubbyphp \Tests \Parsing \Unit \Schema ;
66
7+ use Chubbyphp \Parsing \Enum \Uuid ;
78use Chubbyphp \Parsing \ErrorsException ;
89use Chubbyphp \Parsing \Schema \StringSchema ;
10+ use PHPUnit \Framework \Attributes \DataProvider ;
911use PHPUnit \Framework \TestCase ;
1012
1113/**
@@ -680,20 +682,20 @@ public function testParseWithInvalidUrl(): void
680682 }
681683 }
682684
683- public function testParseWithValidUuidV4 (): void
685+ public function testParseWithValidUuid (): void
684686 {
685687 $ input = '960b0533-da17-42d8-a0a4-dd2ab7213caf ' ;
686688
687- $ schema = (new StringSchema ())->uuidV4 ();
689+ $ schema = (new StringSchema ())->uuid ();
688690
689691 self ::assertSame ($ input , $ schema ->parse ($ input ));
690692 }
691693
692- public function testParseWithInvalidUuidV4 (): void
694+ public function testParseWithInvalidUuid (): void
693695 {
694696 $ input = '960b0533-da17-52d8-a0a4-dd2ab7213caf ' ;
695697
696- $ schema = (new StringSchema ())->uuidV4 ();
698+ $ schema = (new StringSchema ())->uuid ();
697699
698700 try {
699701 $ schema ->parse ($ input );
@@ -716,25 +718,60 @@ public function testParseWithInvalidUuidV4(): void
716718 }
717719 }
718720
719- public function testParseWithValidUuidV5 (): void
721+ #[DataProvider('provideParseWithValidUuidsCases ' )]
722+ public function testParseWithValidUuids (Uuid $ version , string $ uuid ): void
720723 {
721- $ input = '960b0533-da17-52d8-a0a4-dd2ab7213caf ' ;
722-
723- $ schema = (new StringSchema ())->uuidV5 ();
724-
725- self ::assertSame ($ input , $ schema ->parse ($ input ));
724+ self ::assertSame ($ uuid , (new StringSchema ())->uuid ($ version )->parse ($ uuid ));
726725 }
727726
728- public function testParseWithInvalidUuidV5 (): void
727+ /**
728+ * @return array<string, array{Uuid, string}>
729+ */
730+ public static function provideParseWithValidUuidsCases (): iterable
729731 {
730- $ input = '960b0533-da17-42d8-a0a4-dd2ab7213caf ' ;
731-
732- $ schema = (new StringSchema ())->uuidV5 ();
732+ return [
733+ 'v1 timestamp + MAC ' => [
734+ Uuid::v1,
735+ '6fa459ea-ee8a-1d13-a3ac-0800200c9a66 ' ,
736+ ],
737+ 'v2 DCE security ' => [
738+ Uuid::v2,
739+ '000003e8-ee8a-2d13-8500-0800200c9a66 ' ,
740+ ],
741+ 'v3 MD5 hash ' => [
742+ Uuid::v3,
743+ '5df41881-3aed-3515-88a7-2f4a814cf09e ' ,
744+ ],
745+ 'v4 random ' => [
746+ Uuid::v4,
747+ 'f47ac10b-58cc-4372-a567-0e02b2c3d479 ' ,
748+ ],
749+ 'v5 SHA-1 hash ' => [
750+ Uuid::v5,
751+ 'a6e4eb18-bba0-5a2d-b0aa-b85e4718e89f ' ,
752+ ],
753+ 'v6 reordered timestamp ' => [
754+ Uuid::v6,
755+ '1d13ee8a-6fa4-659e-a3ac-0800200c9a66 ' ,
756+ ],
757+ 'v7 unix timestamp ' => [
758+ Uuid::v7,
759+ '019490a9-5e00-7d34-b5f6-4a1b2c3d4e5f ' ,
760+ ],
761+ 'v8 custom ' => [
762+ Uuid::v8,
763+ 'c0ffee00-cafe-8bad-beef-deaddeadbeef ' ,
764+ ],
765+ ];
766+ }
733767
768+ #[DataProvider('provideParseWithInvalidUuidsCases ' )]
769+ public function testParseWithInvalidUuids (Uuid $ version , string $ uuid ): void
770+ {
734771 try {
735- $ schema ->parse ($ input );
772+ ( new StringSchema ())-> uuid ( $ version ) ->parse ($ uuid );
736773
737- throw new \ Exception ( ' code should not be reached ' );
774+ self :: fail ( ' Expected ErrorsException was not thrown ' );
738775 } catch (ErrorsException $ errorsException ) {
739776 self ::assertSame ([
740777 [
@@ -743,15 +780,74 @@ public function testParseWithInvalidUuidV5(): void
743780 'code ' => 'string.uuid ' ,
744781 'template ' => 'Invalid uuid {{version}} {{given}} ' ,
745782 'variables ' => [
746- 'version ' => 'v5 ' ,
747- 'given ' => $ input ,
783+ 'version ' => 'v ' . $ version -> value ,
784+ 'given ' => $ uuid ,
748785 ],
749786 ],
750787 ],
751788 ], $ errorsException ->errors ->jsonSerialize ());
752789 }
753790 }
754791
792+ /**
793+ * @return array<string, array{Uuid, string}>
794+ */
795+ public static function provideParseWithInvalidUuidsCases (): iterable
796+ {
797+ return [
798+ 'v1 with invalid variant c ' => [
799+ Uuid::v1,
800+ '6fa459ea-ee8a-1d13-c3ac-0800200c9a66 ' ,
801+ ],
802+ 'v2 with invalid variant 0 ' => [
803+ Uuid::v2,
804+ '000003e8-ee8a-2d13-0500-0800200c9a66 ' ,
805+ ],
806+ 'v3 with invalid hex character g ' => [
807+ Uuid::v3,
808+ '5df41881-3aed-3515-88a7-2f4a814cg09e ' ,
809+ ],
810+ 'v4 with wrong version 5 ' => [
811+ Uuid::v4,
812+ 'f47ac10b-58cc-5372-a567-0e02b2c3d479 ' ,
813+ ],
814+ 'v5 with invalid variant 7 ' => [
815+ Uuid::v5,
816+ 'a6e4eb18-bba0-5a2d-70aa-b85e4718e89f ' ,
817+ ],
818+ 'v6 with wrong version 1 ' => [
819+ Uuid::v6,
820+ '1d13ee8a-6fa4-159e-a3ac-0800200c9a66 ' ,
821+ ],
822+ 'v7 with invalid variant f ' => [
823+ Uuid::v7,
824+ '019490a9-5e00-7d34-f5f6-4a1b2c3d4e5f ' ,
825+ ],
826+ 'v8 with wrong version 0 ' => [
827+ Uuid::v8,
828+ 'c0ffee00-cafe-0bad-beef-deaddeadbeef ' ,
829+ ],
830+ ];
831+ }
832+
833+ public function testParseWithValidUuidV4 (): void
834+ {
835+ $ input = '960b0533-da17-42d8-a0a4-dd2ab7213caf ' ;
836+
837+ $ schema = (new StringSchema ())->uuidV4 ();
838+
839+ self ::assertSame ($ input , $ schema ->parse ($ input ));
840+ }
841+
842+ public function testParseWithValidUuidV5 (): void
843+ {
844+ $ input = '960b0533-da17-52d8-a0a4-dd2ab7213caf ' ;
845+
846+ $ schema = (new StringSchema ())->uuidV5 ();
847+
848+ self ::assertSame ($ input , $ schema ->parse ($ input ));
849+ }
850+
755851 public function testParseWithTrim (): void
756852 {
757853 $ input = ' test ' ;
0 commit comments