ldc-cocotte/tests/Integration/WithApp/App/Casts/EmployeeSitesCastTest.php
Jérémy Munsch 5197e796c0
test(casts): add tests for various cast classes
- Implement tests for DiscountValueCast to validate percentage and amount handling.
- Add tests for EmailCast to ensure proper email handling.
- Create tests for EmployeeSitesCast to validate JSON and array handling.
- Implement tests for MatriculeCast to validate matricule handling.
- Add tests for MoneyCast to ensure proper money handling.
- Create tests for NutritionalValuesCast to validate nutritional values handling.
- Implement tests for OrderEmployeeCast to validate employee data handling.
- Add tests for OrderLineProductCast to validate order line product handling.
- Create tests for OrderReferenceCast to validate order reference handling.
- Implement tests for ShopperEstablishmentCodeCast to validate establishment code handling.
- Add tests for ShopperServiceCodeCast to validate service code handling.
- Create tests for ShopperSiteCodeCast to validate site code handling.
2026-11-15 18:56:19 +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.');
});