@@ -891,6 +891,95 @@ public function testTransliterateStrict(string $value, string $expected): void
891
891
{
892
892
$ this ->assertSame ($ expected , Str::transliterate ($ value , '? ' , true ));
893
893
}
894
+
895
+ public function testItCanFreezeUuids ()
896
+ {
897
+ $ this ->assertNotSame ((string ) Str::uuid (), (string ) Str::uuid ());
898
+ $ this ->assertNotSame (Str::uuid (), Str::uuid ());
899
+
900
+ $ uuid = Str::freezeUuids ();
901
+
902
+ $ this ->assertSame ($ uuid , Str::uuid ());
903
+ $ this ->assertSame (Str::uuid (), Str::uuid ());
904
+ $ this ->assertSame ((string ) $ uuid , (string ) Str::uuid ());
905
+ $ this ->assertSame ((string ) Str::uuid (), (string ) Str::uuid ());
906
+
907
+ Str::createUuidsNormally ();
908
+
909
+ $ this ->assertNotSame (Str::uuid (), Str::uuid ());
910
+ $ this ->assertNotSame ((string ) Str::uuid (), (string ) Str::uuid ());
911
+ }
912
+
913
+ public function testItCanFreezeUuidsInAClosure ()
914
+ {
915
+ $ uuids = [];
916
+
917
+ $ uuid = Str::freezeUuids (function ($ uuid ) use (&$ uuids ) {
918
+ $ uuids [] = $ uuid ;
919
+ $ uuids [] = Str::uuid ();
920
+ $ uuids [] = Str::uuid ();
921
+ });
922
+
923
+ $ this ->assertSame ($ uuid , $ uuids [0 ]);
924
+ $ this ->assertSame ((string ) $ uuid , (string ) $ uuids [0 ]);
925
+ $ this ->assertSame ((string ) $ uuids [0 ], (string ) $ uuids [1 ]);
926
+ $ this ->assertSame ($ uuids [0 ], $ uuids [1 ]);
927
+ $ this ->assertSame ((string ) $ uuids [0 ], (string ) $ uuids [1 ]);
928
+ $ this ->assertSame ($ uuids [1 ], $ uuids [2 ]);
929
+ $ this ->assertSame ((string ) $ uuids [1 ], (string ) $ uuids [2 ]);
930
+ $ this ->assertNotSame (Str::uuid (), Str::uuid ());
931
+ $ this ->assertNotSame ((string ) Str::uuid (), (string ) Str::uuid ());
932
+
933
+ Str::createUuidsNormally ();
934
+ }
935
+
936
+ public function testItCanSpecifyASquenceOfUuidsToUtilise ()
937
+ {
938
+ Str::createUuidsUsingSequence ([
939
+ 0 => ($ zeroth = Str::uuid ()),
940
+ 1 => ($ first = Str::uuid ()),
941
+ // just generate a random one here...
942
+ 3 => ($ third = Str::uuid ()),
943
+ // continue to generate random uuids...
944
+ ]);
945
+
946
+ $ retrieved = Str::uuid ();
947
+ $ this ->assertSame ($ zeroth , $ retrieved );
948
+ $ this ->assertSame ((string ) $ zeroth , (string ) $ retrieved );
949
+
950
+ $ retrieved = Str::uuid ();
951
+ $ this ->assertSame ($ first , $ retrieved );
952
+ $ this ->assertSame ((string ) $ first , (string ) $ retrieved );
953
+
954
+ $ retrieved = Str::uuid ();
955
+ $ this ->assertFalse (in_array ($ retrieved , [$ zeroth , $ first , $ third ], true ));
956
+ $ this ->assertFalse (in_array ((string ) $ retrieved , [(string ) $ zeroth , (string ) $ first , (string ) $ third ], true ));
957
+
958
+ $ retrieved = Str::uuid ();
959
+ $ this ->assertSame ($ third , $ retrieved );
960
+ $ this ->assertSame ((string ) $ third , (string ) $ retrieved );
961
+
962
+ $ retrieved = Str::uuid ();
963
+ $ this ->assertFalse (in_array ($ retrieved , [$ zeroth , $ first , $ third ], true ));
964
+ $ this ->assertFalse (in_array ((string ) $ retrieved , [(string ) $ zeroth , (string ) $ first , (string ) $ third ], true ));
965
+
966
+ Str::createUuidsNormally ();
967
+ }
968
+
969
+ public function testItCanSpecifyAFallbackForASequence ()
970
+ {
971
+ Str::createUuidsUsingSequence ([Str::uuid (), Str::uuid ()], fn () => throw new \Exception ('Out of Uuids. ' ));
972
+ Str::uuid ();
973
+ Str::uuid ();
974
+
975
+ try {
976
+ $ this ->expectExceptionMessage ('Out of Uuids. ' );
977
+ Str::uuid ();
978
+ $ this ->fail ();
979
+ } finally {
980
+ Str::createUuidsNormally ();
981
+ }
982
+ }
894
983
}
895
984
896
985
class StringableObjectStub
0 commit comments