124 lines
3.9 KiB
PHP
124 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\Casts;
|
|
|
|
use App\Casts\OrderLineProductCast;
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Domain\Models\ValueObjects\IncoId;
|
|
use Domain\Models\ValueObjects\OrderLineProduct;
|
|
use Domain\Models\ValueObjects\ProductWeight;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
describe('OrderLineProductCast', function () {
|
|
|
|
it('can cast JSON to OrderLineProduct correctly', function () {
|
|
// GIVEN
|
|
$cast = new OrderLineProductCast();
|
|
$model = new class () extends Model
|
|
{
|
|
};
|
|
|
|
$json = json_encode([
|
|
'inco_id' => 123456,
|
|
'original_id' => Uuid::uuid7()->toString(),
|
|
'name' => 'Poulet Fermier',
|
|
'weight' => 1500,
|
|
'weight_variable' => true,
|
|
'brand' => ProductBrand::loue->value,
|
|
'order_unit' => OrderUnit::PCE->value,
|
|
'price' => 1050,
|
|
'discounted_price' => 950,
|
|
'price_kg_ttc' => 700,
|
|
'tax_rate' => TaxRate::TVA_5_5->percent(),
|
|
'picture' => 'poulet.jpg',
|
|
]);
|
|
|
|
// WHEN
|
|
$vo = $cast->get($model, 'product', $json, []);
|
|
|
|
// THEN
|
|
expect($vo)->toBeInstanceOf(OrderLineProduct::class)
|
|
->and($vo->incoId->value)->toBe(123456)
|
|
->and($vo->name)->toBe('Poulet Fermier')
|
|
->and($vo->weight->value)->toBe(1500)
|
|
->and($vo->weight->isVariable)->toBeTrue()
|
|
->and($vo->brand)->toBe(ProductBrand::loue)
|
|
->and($vo->orderUnit)->toBe(OrderUnit::PCE)
|
|
->and($vo->taxRate)->toBe(TaxRate::TVA_5_5)
|
|
->and($vo->picture)->toBe('poulet.jpg');
|
|
});
|
|
|
|
it('can set OrderLineProduct to JSON correctly', function () {
|
|
// GIVEN
|
|
$cast = new OrderLineProductCast();
|
|
$model = new class () extends Model
|
|
{
|
|
};
|
|
|
|
$vo = new OrderLineProduct(
|
|
incoId: new IncoId(123456),
|
|
originalId: new ProductId(Uuid::uuid7()),
|
|
name: 'Poulet Fermier',
|
|
weight: new ProductWeight(1500, true),
|
|
brand: ProductBrand::loue,
|
|
orderUnit: OrderUnit::PCE,
|
|
price: new Money(1050, new Currency('EUR')),
|
|
discountedPrice: new Money(950, new Currency('EUR')),
|
|
priceKgTtc: new Money(700, new Currency('EUR')),
|
|
taxRate: TaxRate::TVA_5_5,
|
|
picture: 'poulet.jpg'
|
|
);
|
|
|
|
// WHEN
|
|
$json = $cast->set($model, 'product', $vo, []);
|
|
$data = json_decode($json, true);
|
|
|
|
// THEN
|
|
expect($data)
|
|
->toBeArray()
|
|
->toHaveKey('weight', 1500)
|
|
->toHaveKey('weight_variable', true)
|
|
->not->toHaveKey('weight_unit');
|
|
});
|
|
|
|
it('throws exception if value is null on get', function () {
|
|
// GIVEN
|
|
$cast = new OrderLineProductCast();
|
|
$model = new class () extends Model
|
|
{
|
|
};
|
|
|
|
// WHEN / THEN
|
|
$cast->get($model, 'product', null, []);
|
|
})->throws(\InvalidArgumentException::class);
|
|
|
|
it('throws exception if value is not string on get', function () {
|
|
// GIVEN
|
|
$cast = new OrderLineProductCast();
|
|
$model = new class () extends Model
|
|
{
|
|
};
|
|
|
|
// WHEN / THEN
|
|
$cast->get($model, 'product', ['array' => 'invalid'], []);
|
|
})->throws(\InvalidArgumentException::class);
|
|
|
|
it('throws exception if value is not an OrderLineProduct on set', function () {
|
|
// GIVEN
|
|
$cast = new OrderLineProductCast();
|
|
$model = new class () extends Model
|
|
{
|
|
};
|
|
|
|
// WHEN / THEN
|
|
$cast->set($model, 'product', 'not a VO', []);
|
|
})->throws(\InvalidArgumentException::class);
|
|
});
|