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

107 lines
3 KiB
PHP

<?php
declare(strict_types=1);
use App\Casts\DeliveryAddressCast;
use Domain\Models\ValueObjects\DeliveryAddress;
use Illuminate\Database\Eloquent\Model;
it('gets a default delivery address when value is null', function () {
// GIVEN
$cast = new DeliveryAddressCast();
$model = \Mockery::mock(Model::class);
// WHEN
$address = $cast->get($model, 'address', null, []);
// THEN
expect($address)->toBeInstanceOf(DeliveryAddress::class)
->and($address->name)->toBe('Non renseigné')
->and($address->address)->toBe('Non renseigné')
->and($address->postcode)->toBe('00000')
->and($address->city)->toBe('Non renseigné');
});
it('throws exception when getting invalid value type', function () {
// GIVEN
$cast = new DeliveryAddressCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'address', 123, []);
// THEN
})->throws(\InvalidArgumentException::class, 'The value must be a string.');
it('gets a delivery address from json string', function () {
// GIVEN
$cast = new DeliveryAddressCast();
$model = \Mockery::mock(Model::class);
$json = json_encode([
'name' => 'John',
'address' => '123 Street',
'postcode' => '12345',
'city' => 'City',
'phone' => '0102030405',
'latitude' => '48.0',
'longitude' => '2.0',
]);
// WHEN
$address = $cast->get($model, 'address', $json, []);
// THEN
expect($address)->toBeInstanceOf(DeliveryAddress::class)
->and($address->name)->toBe('John')
->and($address->address)->toBe('123 Street')
->and($address->postcode)->toBe('12345')
->and($address->city)->toBe('City')
->and($address->phone)->toBe('0102030405')
->and($address->latitude)->toBe('48.0')
->and($address->longitude)->toBe('2.0');
});
it('throws exception when setting invalid type', function () {
// GIVEN
$cast = new DeliveryAddressCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'address', 'invalid', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The value must be an instance of DeliveryAddress.'
);
it('sets a delivery address', function () {
// GIVEN
$cast = new DeliveryAddressCast();
$model = \Mockery::mock(Model::class);
$address = new DeliveryAddress(
name: 'John',
address: '123 Street',
postcode: '12345',
city: 'City',
phone: '0102030405',
latitude: '48.0',
longitude: '2.0'
);
// WHEN
$value = $cast->set($model, 'address', $address, []);
// THEN
expect($value)->toBeJson()
->and(json_decode($value, true))->toMatchArray([
'name' => 'John',
'address' => '123 Street',
'postcode' => '12345',
'city' => 'City',
'phone' => '0102030405',
'latitude' => '48.0',
'longitude' => '2.0',
]);
});