ldc-cocotte/tests/Integration/WithApp/App/Casts/OrderEmployeeCastTest.php

125 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Casts;
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',
]);
});