55use Illuminate \Database \Eloquent \Model ;
66use Illuminate \Database \Schema \Blueprint ;
77use Illuminate \Support \Facades \Schema ;
8+ use Illuminate \Support \Str ;
89use Illuminate \Tests \Integration \Database \DatabaseTestCase ;
910use Illuminate \Translation \ArrayLoader ;
1011use Illuminate \Translation \Translator ;
@@ -19,19 +20,56 @@ protected function setUp(): void
1920
2021 Schema::create ('users ' , function (Blueprint $ table ) {
2122 $ table ->increments ('id ' );
23+ $ table ->string ('uuid ' );
2224 $ table ->string ('first_name ' );
2325 });
2426
25- User::create (['first_name ' => 'John ' ]);
26- User::create (['first_name ' => 'John ' ]);
27+ User::create (['uuid ' => ( string ) Str:: uuid (), ' first_name ' => 'John ' ]);
28+ User::create (['uuid ' => ( string ) Str:: uuid (), ' first_name ' => 'Jim ' ]);
2729 }
2830
2931 public function testExists ()
3032 {
31- $ validator = $ this ->getValidator (['first_name ' => ['John ' , 'Jim ' ]], ['first_name ' => 'exists:users ' ]);
33+ $ validator = $ this ->getValidator (['first_name ' => ['John ' , 'Taylor ' ]], ['first_name ' => 'exists:users ' ]);
3234 $ this ->assertFalse ($ validator ->passes ());
3335 }
3436
37+ public function testUnique ()
38+ {
39+ $ validator = $ this ->getValidator (['first_name ' => 'John ' ], ['first_name ' => 'unique: ' .User::class]);
40+ $ this ->assertFalse ($ validator ->passes ());
41+
42+ $ validator = $ this ->getValidator (['first_name ' => 'John ' ], ['first_name ' => 'unique: ' .User::class.',first_name,1 ' ]);
43+ $ this ->assertTrue ($ validator ->passes ());
44+
45+ $ validator = $ this ->getValidator (['first_name ' => 'Taylor ' ], ['first_name ' => 'unique: ' .User::class]);
46+ $ this ->assertTrue ($ validator ->passes ());
47+ }
48+
49+ public function testUniqueWithCustomModelKey ()
50+ {
51+ $ _SERVER ['CUSTOM_MODEL_KEY_NAME ' ] = 'uuid ' ;
52+
53+ $ validator = $ this ->getValidator (['first_name ' => 'John ' ], ['first_name ' => 'unique: ' .UserWithUuid::class]);
54+ $ this ->assertFalse ($ validator ->passes ());
55+
56+ $ user = UserWithUuid::where ('first_name ' , 'John ' )->first ();
57+
58+ $ validator = $ this ->getValidator (['first_name ' => 'John ' ], ['first_name ' => 'unique: ' .UserWithUuid::class.',first_name, ' .$ user ->uuid ]);
59+ $ this ->assertTrue ($ validator ->passes ());
60+
61+ $ validator = $ this ->getValidator (['first_name ' => 'John ' ], ['first_name ' => 'unique:users,first_name, ' .$ user ->uuid .',uuid ' ]);
62+ $ this ->assertTrue ($ validator ->passes ());
63+
64+ $ validator = $ this ->getValidator (['first_name ' => 'John ' ], ['first_name ' => 'unique:users,first_name, ' .$ user ->uuid .',id ' ]);
65+ $ this ->assertFalse ($ validator ->passes ());
66+
67+ $ validator = $ this ->getValidator (['first_name ' => 'Taylor ' ], ['first_name ' => 'unique: ' .UserWithUuid::class]);
68+ $ this ->assertTrue ($ validator ->passes ());
69+
70+ unset($ _SERVER ['CUSTOM_MODEL_KEY_NAME ' ]);
71+ }
72+
3573 public function testImplicitAttributeFormatting ()
3674 {
3775 $ translator = new Translator (new ArrayLoader , 'en ' );
@@ -64,3 +102,17 @@ class User extends Model
64102 public $ timestamps = false ;
65103 protected $ guarded = ['id ' ];
66104}
105+
106+ class UserWithUuid extends Model
107+ {
108+ protected $ table = 'users ' ;
109+ public $ timestamps = false ;
110+ protected $ guarded = ['id ' ];
111+ protected $ keyType = 'string ' ;
112+ public $ incrementing = false ;
113+
114+ public function getKeyName ()
115+ {
116+ return 'uuid ' ;
117+ }
118+ }
0 commit comments