mirror of
https://github.com/archtechx/virtualcolumn.git
synced 2025-12-12 08:24:04 +00:00
Test casting of all default 'encrypted' castables
This commit is contained in:
parent
885e2112c2
commit
de9d9d7cab
1 changed files with 28 additions and 13 deletions
|
|
@ -7,6 +7,7 @@ use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Crypt;
|
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
|
||||||
{
|
{
|
||||||
|
|
@ -110,23 +111,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(['password' => $password = 'foo']);
|
$model = MyModel::create([
|
||||||
|
'password' => $password = 'foo',
|
||||||
|
'array' => $array = ['foo', 'bar'],
|
||||||
|
'collection' => $collection = collect(['foo', 'bar']),
|
||||||
|
'json' => $json = json_encode(['foo', 'bar']),
|
||||||
|
'object' => $object = (object) json_encode(['foo', 'bar']),
|
||||||
|
// todo1 'custom' => 'foo',
|
||||||
|
]);
|
||||||
|
|
||||||
// Virtual column gets encrypted
|
$attributeGetsCastCorrectly = function (string $attribute, mixed $expectedValue) use ($model): void {
|
||||||
$rawEncryptedPassword = (array) DB::table('my_models')
|
$savedValue = $model->getAttributes()[$attribute]; // Encrypted
|
||||||
->select('data->password')
|
|
||||||
->where('id', $model->id)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
$encryptedPassword = $rawEncryptedPassword[array_key_first((array) $rawEncryptedPassword)];
|
$this->assertTrue(VirtualColumn::valueEncrypted($savedValue));
|
||||||
|
$this->assertNotEquals($savedValue, $expectedValue);
|
||||||
|
|
||||||
$this->assertNotSame($encryptedPassword, $password);
|
$retrievedValue = $model->$attribute; // Decrypted
|
||||||
$this->assertSame(Crypt::decryptString($encryptedPassword), $password);
|
|
||||||
|
|
||||||
// Virtual column gets decrypted
|
$this->assertEquals($retrievedValue, $expectedValue);
|
||||||
$this->assertSame($model->password, $password);
|
};
|
||||||
|
|
||||||
// todo1 Check if *all* encrypted casts work
|
$attributeGetsCastCorrectly('password', $password); // 'encrypted'
|
||||||
|
$attributeGetsCastCorrectly('array', $array); // 'encrypted:array'
|
||||||
|
$attributeGetsCastCorrectly('collection', $collection); // 'encrypted:collection'
|
||||||
|
$attributeGetsCastCorrectly('json', $json); // 'encrypted:json'
|
||||||
|
$attributeGetsCastCorrectly('object', $object); // 'encrypted:object'
|
||||||
|
// todo1 $attributeGetsCastCorrectly('custom', $custom); // 'CustomCast::class'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,7 +147,12 @@ class MyModel extends Model
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
public $timestamps = false;
|
public $timestamps = false;
|
||||||
public $casts = [
|
public $casts = [
|
||||||
'password' => 'encrypted'
|
'password' => 'encrypted',
|
||||||
|
'array' => 'encrypted:array',
|
||||||
|
'collection' => 'encrypted:collection',
|
||||||
|
'json' => 'encrypted:json',
|
||||||
|
'object' => 'encrypted:object',
|
||||||
|
// todo1 'custom' => CustomCast::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function getCustomColumns(): array
|
public static function getCustomColumns(): array
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue