Skip to content

Commit 4d12cba

Browse files
committed
Add Encryption casts tests for array, object, collection
1 parent 38b58b8 commit 4d12cba

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

tests/Casts/EncryptionTest.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Casts;
66

7+
use Illuminate\Database\Eloquent\Casts\Json;
78
use Illuminate\Encryption\Encrypter;
9+
use Illuminate\Support\Collection;
810
use MongoDB\Laravel\Tests\Models\Casting;
911
use MongoDB\Laravel\Tests\TestCase;
1012

@@ -17,13 +19,81 @@ protected function setUp(): void
1719
Casting::truncate();
1820
}
1921

22+
protected function decryptRaw(Casting $model, $key)
23+
{
24+
return app()->make(Encrypter::class)
25+
->decryptString(
26+
$model->getRawOriginal($key)
27+
);
28+
}
29+
2030
public function testEncryptedString(): void
2131
{
2232
$model = Casting::query()->create(['encryptedString' => 'encrypted']);
2333

2434
self::assertIsString($model->encryptedString);
2535
self::assertEquals('encrypted', $model->encryptedString);
2636
self::assertNotEquals('encrypted', $model->getRawOriginal('encryptedString'));
27-
self::assertEquals('encrypted', app()->make(Encrypter::class)->decryptString($model->getRawOriginal('encryptedString')));
37+
self::assertEquals('encrypted', $this->decryptRaw($model, 'encryptedString'));
38+
39+
$model->update(['encryptedString' => 'updated']);
40+
self::assertIsString($model->encryptedString);
41+
self::assertEquals('updated', $model->encryptedString);
42+
self::assertNotEquals('updated', $model->getRawOriginal('encryptedString'));
43+
self::assertEquals('updated', $this->decryptRaw($model, 'encryptedString'));
44+
}
45+
46+
public function testEncryptedArray(): void
47+
{
48+
$expected = ['foo' => 'bar'];
49+
$model = Casting::query()->create(['encryptedArray' => $expected]);
50+
51+
self::assertIsArray($model->encryptedArray);
52+
self::assertEquals($expected, $model->encryptedArray);
53+
self::assertNotEquals($expected, $model->getRawOriginal('encryptedArray'));
54+
self::assertEquals($expected, Json::decode($this->decryptRaw($model, 'encryptedArray')));
55+
56+
$updated = ['updated' => 'array'];
57+
$model->update(['encryptedArray' => $updated]);
58+
self::assertIsArray($model->encryptedArray);
59+
self::assertEquals($updated, $model->encryptedArray);
60+
self::assertNotEquals($updated, $model->getRawOriginal('encryptedArray'));
61+
self::assertEquals($updated, Json::decode($this->decryptRaw($model, 'encryptedArray')));
62+
}
63+
64+
public function testEncryptedObject(): void
65+
{
66+
$expected = (object) ['foo' => 'bar'];
67+
$model = Casting::query()->create(['encryptedObject' => $expected]);
68+
69+
self::assertIsObject($model->encryptedObject);
70+
self::assertEquals($expected, $model->encryptedObject);
71+
self::assertNotEquals($expected, $model->getRawOriginal('encryptedObject'));
72+
self::assertEquals($expected, Json::decode($this->decryptRaw($model, 'encryptedObject'), false));
73+
74+
$updated = (object) ['updated' => 'object'];
75+
$model->update(['encryptedObject' => $updated]);
76+
self::assertIsObject($model->encryptedObject);
77+
self::assertEquals($updated, $model->encryptedObject);
78+
self::assertNotEquals($updated, $model->getRawOriginal('encryptedObject'));
79+
self::assertEquals($updated, Json::decode($this->decryptRaw($model, 'encryptedObject'), false));
80+
}
81+
82+
public function testEncryptedCollection(): void
83+
{
84+
$expected = collect(['foo' => 'bar']);
85+
$model = Casting::query()->create(['encryptedCollection' => $expected]);
86+
87+
self::assertInstanceOf(Collection::class, $model->encryptedCollection);
88+
self::assertEquals($expected, $model->encryptedCollection);
89+
self::assertNotEquals($expected, $model->getRawOriginal('encryptedCollection'));
90+
self::assertEquals($expected, collect(Json::decode($this->decryptRaw($model, 'encryptedCollection'), false)));
91+
92+
$updated = collect(['updated' => 'object']);
93+
$model->update(['encryptedCollection' => $updated]);
94+
self::assertIsObject($model->encryptedCollection);
95+
self::assertEquals($updated, $model->encryptedCollection);
96+
self::assertNotEquals($updated, $model->getRawOriginal('encryptedCollection'));
97+
self::assertEquals($updated, collect(Json::decode($this->decryptRaw($model, 'encryptedCollection'), false)));
2898
}
2999
}

tests/Models/Casting.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Casting extends Eloquent
3333
'immutableDatetimeField',
3434
'immutableDatetimeWithFormatField',
3535
'encryptedString',
36+
'encryptedArray',
37+
'encryptedObject',
38+
'encryptedCollection'
3639
];
3740

3841
protected $casts = [
@@ -54,5 +57,8 @@ class Casting extends Eloquent
5457
'immutableDatetimeField' => 'immutable_datetime',
5558
'immutableDatetimeWithFormatField' => 'immutable_datetime:j.n.Y H:i',
5659
'encryptedString' => 'encrypted',
60+
'encryptedArray' => 'encrypted:array',
61+
'encryptedObject' => 'encrypted:object',
62+
'encryptedCollection' => 'encrypted:collection',
5763
];
5864
}

0 commit comments

Comments
 (0)