ldc-boutique-cnc-sable/tests/Unit/App/Casts/EmployeeSitesCastTest.php
Jérémy Munsch 8b96ce90be
test(casts): add tests for EmployeeSitesCast functionality
test(casts): add tests for ShopperEstablishmentCodeCast functionality

test(casts): add tests for ShopperServiceCodeCast functionality

test(casts): add tests for ShopperSiteCodeCast functionality

test(dto): update ProductDtoTest to include additional fields

test(dto): update SettingDtoTest to remove arcadiaSites assertions

test(events): update AnOrderHasBeenPlacedTest to use value objects

test(entities): update OrderTest to use value objects for employee details

test(value-objects): update EmployeeUpsertDataTest to use value objects

test(pagination): update PaginationTest to access value properties

test(services): update AppCustomerAuthServiceTest to use CrmHashcode

test(services): update CrmServiceTest to use CrmHashcode for customer retrieval
2026-10-27 09:58:45 +01:00

105 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
use App\Casts\EmployeeSitesCast;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Database\Eloquent\Model;
it('gets empty array when value is null or empty string', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$resultNull = $cast->get($model, 'employee_sites', null, []);
$resultEmpty = $cast->get($model, 'employee_sites', '', []);
// THEN
expect($resultNull)->toBe([])
->and($resultEmpty)->toBe([]);
});
it('gets array of strings from json string', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
$json = json_encode(['PA', 'LY', 'NA']);
// WHEN
$sites = $cast->get($model, 'employee_sites', $json, []);
// THEN
expect($sites)->toBe(['PA', 'LY', 'NA']);
});
it('gets array of strings from mixed array', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
$input = ['PA', new ShopperSiteCode('LY'), 123];
// WHEN
$sites = $cast->get($model, 'employee_sites', $input, []);
// THEN
expect($sites)->toBe(['PA', 'LY', '123']);
});
it('sets json empty array when value is null', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$value = $cast->set($model, 'employee_sites', null, []);
// THEN
expect($value)->toBe('[]');
});
it('sets json array from array of strings', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$value = $cast->set($model, 'employee_sites', ['PA', 'LY', 'NA'], []);
// THEN
expect($value)->toBe(json_encode(['PA', 'LY', 'NA']));
});
it('sets json array from array of Stringable objects', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$value = $cast->set($model, 'employee_sites', [new ShopperSiteCode('PA'), new ShopperSiteCode('LY')], []);
// THEN
expect($value)->toBe(json_encode(['PA', 'LY']));
});
it('throws exception when setting invalid type', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
// WHEN
expect(fn () => $cast->set($model, 'employee_sites', 12345, []))
// THEN
->toThrow(\UnexpectedValueException::class, 'Employee sites must be an array, string or null.');
});
it('throws exception when setting array with non-convertible items', function (): void {
// GIVEN
$cast = new EmployeeSitesCast();
$model = \Mockery::mock(Model::class);
// WHEN
expect(fn () => $cast->set($model, 'employee_sites', [new \stdClass()], []))
// THEN
->toThrow(\UnexpectedValueException::class, 'Each employee site must be convertable to string.');
});