ldc-cocotte/tests/Integration/WithApp/App/Casts/OrderEmployeeCastTest.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

123 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Casts\OrderEmployeeCast;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderEmployee;
use Illuminate\Database\Eloquent\Model;
it('gets null when value is null', function () {
// GIVEN
$cast = new OrderEmployeeCast();
$model = \Mockery::mock(Model::class);
// WHEN
$employee = $cast->get($model, 'employee', null, []);
// THEN
expect($employee)->toBeNull();
});
it('throws exception when getting invalid value type', function () {
// GIVEN
$cast = new OrderEmployeeCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'employee', 123, []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The value must be a string.'
);
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
it('gets an order employee from json string', function () {
// GIVEN
$cast = new OrderEmployeeCast();
$model = \Mockery::mock(Model::class);
$json = json_encode([
'matricule' => '1234567',
'firstname' => 'John',
'lastname' => 'Doe',
'facility' => 'FAC01',
'site' => 'SITE',
'service' => '101',
]);
// WHEN
$employee = $cast->get($model, 'employee', $json, []);
// THEN
expect($employee)->toBeInstanceOf(OrderEmployee::class)
->and($employee->matricule)->toBeInstanceOf(Matricule::class)
->and($employee->matricule->value)->toBe('1234567')
->and($employee->firstname)->toBe('John')
->and($employee->lastname)->toBe('Doe')
->and($employee->facility)->toBeInstanceOf(ShopperEstablishmentCode::class)
->and($employee->facility->value)->toBe('FAC01')
->and($employee->site)->toBeInstanceOf(ShopperSiteCode::class)
->and($employee->site->value)->toBe('SITE')
->and($employee->service)->toBeInstanceOf(ShopperServiceCode::class)
->and($employee->service->value)->toBe('101');
});
it('sets null when value is null', function () {
// GIVEN
$cast = new OrderEmployeeCast();
$model = \Mockery::mock(Model::class);
// WHEN
$value = $cast->set($model, 'employee', null, []);
// THEN
expect($value)->toBeNull();
});
it('throws exception when setting invalid type', function () {
// GIVEN
$cast = new OrderEmployeeCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'employee', 'invalid', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value is not an instance of OrderEmployee.'
);
it('sets an order employee', function () {
// GIVEN
$cast = new OrderEmployeeCast();
$model = \Mockery::mock(Model::class);
$employee = new OrderEmployee(
matricule: new Matricule('1234567'),
firstname: 'John',
lastname: 'Doe',
facility: new ShopperEstablishmentCode('FAC01'),
site: new ShopperSiteCode('SITE'),
service: new ShopperServiceCode('101')
);
// WHEN
$value = $cast->set($model, 'employee', $employee, []);
// THEN
expect($value)->toBeJson()
->and(json_decode($value, true))->toMatchArray([
'matricule' => '1234567',
'firstname' => 'John',
'lastname' => 'Doe',
'facility' => 'FAC01',
'site' => 'SITE',
'service' => '101',
]);
});