Skip to content

Commit 38b58b8

Browse files
committed
Add Encryption cast test
1 parent f708c90 commit 38b58b8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tests/Casts/EncryptionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Casts;
6+
7+
use Illuminate\Encryption\Encrypter;
8+
use MongoDB\Laravel\Tests\Models\Casting;
9+
use MongoDB\Laravel\Tests\TestCase;
10+
11+
class EncryptionTest extends TestCase
12+
{
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
Casting::truncate();
18+
}
19+
20+
public function testEncryptedString(): void
21+
{
22+
$model = Casting::query()->create(['encryptedString' => 'encrypted']);
23+
24+
self::assertIsString($model->encryptedString);
25+
self::assertEquals('encrypted', $model->encryptedString);
26+
self::assertNotEquals('encrypted', $model->getRawOriginal('encryptedString'));
27+
self::assertEquals('encrypted', app()->make(Encrypter::class)->decryptString($model->getRawOriginal('encryptedString')));
28+
}
29+
}

tests/Models/Casting.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Casting extends Eloquent
3232
'datetimeWithFormatField',
3333
'immutableDatetimeField',
3434
'immutableDatetimeWithFormatField',
35+
'encryptedString',
3536
];
3637

3738
protected $casts = [
@@ -52,5 +53,6 @@ class Casting extends Eloquent
5253
'datetimeWithFormatField' => 'datetime:j.n.Y H:i',
5354
'immutableDatetimeField' => 'immutable_datetime',
5455
'immutableDatetimeWithFormatField' => 'immutable_datetime:j.n.Y H:i',
56+
'encryptedString' => 'encrypted',
5557
];
5658
}

0 commit comments

Comments
 (0)