108 lines
3 KiB
PHP
108 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',
|
||
|
|
]);
|
||
|
|
});
|