74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Infra\Casts;
|
|
|
|
use Application\Security\Models\ValueObjects\Entities\SecurityAuditLogDetails;
|
|
use Infra\Casts\SecurityAuditLogDetailsCast;
|
|
use Infra\Database\Models\ClientAuditLog;
|
|
|
|
covers(SecurityAuditLogDetailsCast::class);
|
|
|
|
describe('SecurityAuditLogDetailsCast Unit Tests', function (): void {
|
|
it('returns empty details when value is null or empty string', function (): void {
|
|
// GIVEN
|
|
$cast = new SecurityAuditLogDetailsCast();
|
|
$model = new ClientAuditLog();
|
|
|
|
// WHEN
|
|
$resultNull = $cast->get($model, 'details', null, []);
|
|
$resultEmpty = $cast->get($model, 'details', '', []);
|
|
|
|
// THEN
|
|
expect($resultNull->isEmpty())->toBeTrue()
|
|
->and($resultEmpty->isEmpty())->toBeTrue();
|
|
});
|
|
|
|
it('decodes json string into SecurityAuditLogDetails', function (): void {
|
|
// GIVEN
|
|
$cast = new SecurityAuditLogDetailsCast();
|
|
$model = new ClientAuditLog();
|
|
$json = json_encode(['is_active' => 'false', 'role' => 'admin']);
|
|
|
|
// WHEN
|
|
$result = $cast->get($model, 'details', $json, []);
|
|
|
|
// THEN
|
|
expect($result->get('is_active'))->toBe('false')
|
|
->and($result->get('role'))->toBe('admin');
|
|
});
|
|
|
|
it('decodes array into SecurityAuditLogDetails', function (): void {
|
|
// GIVEN
|
|
$cast = new SecurityAuditLogDetailsCast();
|
|
$model = new ClientAuditLog();
|
|
$array = ['is_active' => false, 'count' => 10];
|
|
|
|
// WHEN
|
|
$result = $cast->get($model, 'details', $array, []);
|
|
|
|
// THEN
|
|
expect($result->get('is_active'))->toBe('false')
|
|
->and($result->get('count'))->toBe('10');
|
|
});
|
|
|
|
it('serializes SecurityAuditLogDetails or array to json on set', function (): void {
|
|
// GIVEN
|
|
$cast = new SecurityAuditLogDetailsCast();
|
|
$model = new ClientAuditLog();
|
|
$details = new SecurityAuditLogDetails(['is_active' => 'true']);
|
|
|
|
// WHEN
|
|
$setNull = $cast->set($model, 'details', null, []);
|
|
$setEmpty = $cast->set($model, 'details', SecurityAuditLogDetails::empty(), []);
|
|
$setDetails = $cast->set($model, 'details', $details, []);
|
|
$setArray = $cast->set($model, 'details', ['key' => 'val'], []);
|
|
|
|
// THEN
|
|
expect($setNull)->toBeNull()
|
|
->and($setEmpty)->toBeNull()
|
|
->and($setDetails)->toBe('{"is_active":"true"}')
|
|
->and($setArray)->toBe('{"key":"val"}');
|
|
});
|
|
});
|