5
5
use Illuminate \Database \Eloquent \Model ;
6
6
use Illuminate \Database \Schema \Blueprint ;
7
7
use Illuminate \Support \Facades \Schema ;
8
+ use Illuminate \Support \Str ;
8
9
use Illuminate \Tests \Integration \Database \DatabaseTestCase ;
9
10
use Illuminate \Translation \ArrayLoader ;
10
11
use Illuminate \Translation \Translator ;
@@ -19,19 +20,56 @@ protected function setUp(): void
19
20
20
21
Schema::create ('users ' , function (Blueprint $ table ) {
21
22
$ table ->increments ('id ' );
23
+ $ table ->string ('uuid ' );
22
24
$ table ->string ('first_name ' );
23
25
});
24
26
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 ' ]);
27
29
}
28
30
29
31
public function testExists ()
30
32
{
31
- $ validator = $ this ->getValidator (['first_name ' => ['John ' , 'Jim ' ]], ['first_name ' => 'exists:users ' ]);
33
+ $ validator = $ this ->getValidator (['first_name ' => ['John ' , 'Taylor ' ]], ['first_name ' => 'exists:users ' ]);
32
34
$ this ->assertFalse ($ validator ->passes ());
33
35
}
34
36
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
+
35
73
public function testImplicitAttributeFormatting ()
36
74
{
37
75
$ translator = new Translator (new ArrayLoader , 'en ' );
@@ -64,3 +102,17 @@ class User extends Model
64
102
public $ timestamps = false ;
65
103
protected $ guarded = ['id ' ];
66
104
}
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