@@ -31,15 +31,17 @@ protected function setUp(): void
31
31
$ table ->text ('secret_object ' )->nullable ();
32
32
$ table ->text ('secret_collection ' )->nullable ();
33
33
});
34
+
35
+ Model::$ encrypter = null ;
34
36
}
35
37
36
38
public function testStringsAreCastable ()
37
39
{
38
- $ this ->encrypter ->expects ('encryptString ' )
39
- ->with ('this is a secret string ' )
40
+ $ this ->encrypter ->expects ('encrypt ' )
41
+ ->with ('this is a secret string ' , false )
40
42
->andReturn ('encrypted-secret-string ' );
41
- $ this ->encrypter ->expects ('decryptString ' )
42
- ->with ('encrypted-secret-string ' )
43
+ $ this ->encrypter ->expects ('decrypt ' )
44
+ ->with ('encrypted-secret-string ' , false )
43
45
->andReturn ('this is a secret string ' );
44
46
45
47
/** @var \Illuminate\Tests\Integration\Database\EncryptedCast $subject */
@@ -56,11 +58,11 @@ public function testStringsAreCastable()
56
58
57
59
public function testArraysAreCastable ()
58
60
{
59
- $ this ->encrypter ->expects ('encryptString ' )
60
- ->with ('{"key1":"value1"} ' )
61
+ $ this ->encrypter ->expects ('encrypt ' )
62
+ ->with ('{"key1":"value1"} ' , false )
61
63
->andReturn ('encrypted-secret-array-string ' );
62
- $ this ->encrypter ->expects ('decryptString ' )
63
- ->with ('encrypted-secret-array-string ' )
64
+ $ this ->encrypter ->expects ('decrypt ' )
65
+ ->with ('encrypted-secret-array-string ' , false )
64
66
->andReturn ('{"key1":"value1"} ' );
65
67
66
68
/** @var \Illuminate\Tests\Integration\Database\EncryptedCast $subject */
@@ -77,11 +79,11 @@ public function testArraysAreCastable()
77
79
78
80
public function testJsonIsCastable ()
79
81
{
80
- $ this ->encrypter ->expects ('encryptString ' )
81
- ->with ('{"key1":"value1"} ' )
82
+ $ this ->encrypter ->expects ('encrypt ' )
83
+ ->with ('{"key1":"value1"} ' , false )
82
84
->andReturn ('encrypted-secret-json-string ' );
83
- $ this ->encrypter ->expects ('decryptString ' )
84
- ->with ('encrypted-secret-json-string ' )
85
+ $ this ->encrypter ->expects ('decrypt ' )
86
+ ->with ('encrypted-secret-json-string ' , false )
85
87
->andReturn ('{"key1":"value1"} ' );
86
88
87
89
/** @var \Illuminate\Tests\Integration\Database\EncryptedCast $subject */
@@ -101,12 +103,12 @@ public function testObjectIsCastable()
101
103
$ object = new \stdClass ();
102
104
$ object ->key1 = 'value1 ' ;
103
105
104
- $ this ->encrypter ->expects ('encryptString ' )
105
- ->with ('{"key1":"value1"} ' )
106
+ $ this ->encrypter ->expects ('encrypt ' )
107
+ ->with ('{"key1":"value1"} ' , false )
106
108
->andReturn ('encrypted-secret-object-string ' );
107
- $ this ->encrypter ->expects ('decryptString ' )
109
+ $ this ->encrypter ->expects ('decrypt ' )
108
110
->twice ()
109
- ->with ('encrypted-secret-object-string ' )
111
+ ->with ('encrypted-secret-object-string ' , false )
110
112
->andReturn ('{"key1":"value1"} ' );
111
113
112
114
/** @var \Illuminate\Tests\Integration\Database\EncryptedCast $object */
@@ -124,12 +126,12 @@ public function testObjectIsCastable()
124
126
125
127
public function testCollectionIsCastable ()
126
128
{
127
- $ this ->encrypter ->expects ('encryptString ' )
128
- ->with ('{"key1":"value1"} ' )
129
+ $ this ->encrypter ->expects ('encrypt ' )
130
+ ->with ('{"key1":"value1"} ' , false )
129
131
->andReturn ('encrypted-secret-collection-string ' );
130
- $ this ->encrypter ->expects ('decryptString ' )
132
+ $ this ->encrypter ->expects ('decrypt ' )
131
133
->twice ()
132
- ->with ('encrypted-secret-collection-string ' )
134
+ ->with ('encrypted-secret-collection-string ' , false )
133
135
->andReturn ('{"key1":"value1"} ' );
134
136
135
137
/** @var \Illuminate\Tests\Integration\Database\EncryptedCast $subject */
@@ -144,6 +146,39 @@ public function testCollectionIsCastable()
144
146
'secret_collection ' => 'encrypted-secret-collection-string ' ,
145
147
]);
146
148
}
149
+
150
+ public function testCustomEncrypterCanBeSpecified ()
151
+ {
152
+ $ customEncrypter = $ this ->mock (Encrypter::class);
153
+
154
+ $ this ->assertNull (Model::$ encrypter );
155
+
156
+ Model::encryptUsing ($ customEncrypter );
157
+
158
+ $ this ->assertSame ($ customEncrypter , Model::$ encrypter );
159
+
160
+ $ this ->encrypter ->expects ('encrypt ' )
161
+ ->never ();
162
+ $ this ->encrypter ->expects ('decrypt ' )
163
+ ->never ();
164
+ $ customEncrypter ->expects ('encrypt ' )
165
+ ->with ('this is a secret string ' , false )
166
+ ->andReturn ('encrypted-secret-string ' );
167
+ $ customEncrypter ->expects ('decrypt ' )
168
+ ->with ('encrypted-secret-string ' , false )
169
+ ->andReturn ('this is a secret string ' );
170
+
171
+ /** @var \Illuminate\Tests\Integration\Database\EncryptedCast $subject */
172
+ $ subject = EncryptedCast::create ([
173
+ 'secret ' => 'this is a secret string ' ,
174
+ ]);
175
+
176
+ $ this ->assertSame ('this is a secret string ' , $ subject ->secret );
177
+ $ this ->assertDatabaseHas ('encrypted_casts ' , [
178
+ 'id ' => $ subject ->id ,
179
+ 'secret ' => 'encrypted-secret-string ' ,
180
+ ]);
181
+ }
147
182
}
148
183
149
184
/**
0 commit comments