1
0
Fork 0
mirror of https://github.com/archtechx/virtualcolumn.git synced 2025-12-12 11:14:04 +00:00

Test custom encrypted castable, refactor test

This commit is contained in:
lukinovec 2023-08-08 15:55:20 +02:00
parent 227054231e
commit 2f0eefdb14

View file

@ -2,12 +2,11 @@
namespace Stancl\VirtualColumn\Tests; namespace Stancl\VirtualColumn\Tests;
use Stancl\VirtualColumn\Tests\Etc\EncryptedCast;
use Orchestra\Testbench\TestCase; use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Stancl\VirtualColumn\VirtualColumn; use Stancl\VirtualColumn\VirtualColumn;
use Illuminate\Contracts\Encryption\DecryptException;
class VirtualColumnTest extends TestCase class VirtualColumnTest extends TestCase
{ {
@ -111,32 +110,32 @@ class VirtualColumnTest extends TestCase
/** @test */ /** @test */
public function encrypted_casts_work_with_virtual_column() { public function encrypted_casts_work_with_virtual_column() {
$model = MyModel::create([ // Custom encrypted castables have to be specified in the $customEncryptedCastables static property
'password' => $password = 'foo', MyModel::$customEncryptedCastables = [EncryptedCast::class];
'array' => $array = ['foo', 'bar'],
'collection' => $collection = collect(['foo', 'bar']), /** @var MyModel $model */
'json' => $json = json_encode(['foo', 'bar']), $model = MyModel::create($encryptedAttributes = [
'object' => $object = (object) json_encode(['foo', 'bar']), 'password' => 'foo', // 'encrypted'
// todo1 'custom' => 'foo', 'array' => ['foo', 'bar'], // 'encrypted:array'
'collection' => collect(['foo', 'bar']), // 'encrypted:collection'
'json' => json_encode(['foo', 'bar']), // 'encrypted:json'
'object' => (object) json_encode(['foo', 'bar']), // 'encrypted:object'
'custom' => 'foo', // Custom castable 'EncryptedCast::class'
]); ]);
$attributeGetsCastCorrectly = function (string $attribute, mixed $expectedValue) use ($model): void { foreach($encryptedAttributes as $key => $expectedValue) {
$savedValue = $model->getAttributes()[$attribute]; // Encrypted $savedValue = $model->getAttributes()[$key]; // Encrypted
$this->assertTrue(VirtualColumn::valueEncrypted($savedValue)); $this->assertTrue(MyModel::valueEncrypted($savedValue));
$this->assertNotEquals($savedValue, $expectedValue); $this->assertNotEquals($savedValue, $expectedValue);
$retrievedValue = $model->$attribute; // Decrypted $retrievedValue = $model->$key; // Decrypted
$this->assertEquals($retrievedValue, $expectedValue); $this->assertEquals($retrievedValue, $expectedValue);
}; }
$attributeGetsCastCorrectly('password', $password); // 'encrypted' // Reset static property
$attributeGetsCastCorrectly('array', $array); // 'encrypted:array' VirtualColumn::$customEncryptedCastables = [];
$attributeGetsCastCorrectly('collection', $collection); // 'encrypted:collection'
$attributeGetsCastCorrectly('json', $json); // 'encrypted:json'
$attributeGetsCastCorrectly('object', $object); // 'encrypted:object'
// todo1 $attributeGetsCastCorrectly('custom', $custom); // 'CustomCast::class'
} }
} }
@ -152,7 +151,7 @@ class MyModel extends Model
'collection' => 'encrypted:collection', 'collection' => 'encrypted:collection',
'json' => 'encrypted:json', 'json' => 'encrypted:json',
'object' => 'encrypted:object', 'object' => 'encrypted:object',
// todo1 'custom' => CustomCast::class, 'custom' => EncryptedCast::class,
]; ];
public static function getCustomColumns(): array public static function getCustomColumns(): array