2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:21:17 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Casts;
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
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;
|
2026-11-16 12:46:06 +01:00
|
|
|
use Illuminate\Support\Arr;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Money\Currency;
|
|
|
|
|
use Money\Money;
|
2026-11-17 12:15:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
|
|
|
|
|
|
|
|
covers(OrderLineProductCast::class);
|
|
|
|
|
|
|
|
|
|
describe('OrderLineProductCast', function () {
|
|
|
|
|
|
|
|
|
|
it('throws exception when getting null', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$cast = new OrderLineProductCast();
|
|
|
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$cast->get($model, 'product', null, []);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
})->throws(\InvalidArgumentException::class, 'The value cannot be null.');
|
|
|
|
|
|
|
|
|
|
it('throws exception when getting non-string', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$cast = new OrderLineProductCast();
|
|
|
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$cast->get($model, 'product', 123, []);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
})->throws(\InvalidArgumentException::class, 'The value must be a string.');
|
|
|
|
|
|
|
|
|
|
it('gets an order line product', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$cast = new OrderLineProductCast();
|
|
|
|
|
$model = \Mockery::mock(Model::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$uuid = UuidHelper::v7()->toString();
|
2026-11-14 16:06:12 +01:00
|
|
|
|
|
|
|
|
$json = json_encode([
|
|
|
|
|
'inco_id' => 123,
|
|
|
|
|
'original_id' => $uuid,
|
|
|
|
|
'name' => 'Product Name',
|
|
|
|
|
'weight' => 1000,
|
|
|
|
|
'weight_variable' => false,
|
|
|
|
|
'brand' => ProductBrand::loue->value,
|
|
|
|
|
'order_unit' => OrderUnit::PCE->value,
|
|
|
|
|
'price' => '1000',
|
|
|
|
|
'discounted_price' => '800',
|
|
|
|
|
'price_kg_ttc' => '1000',
|
|
|
|
|
'tax_rate' => TaxRate::TVA_5_5->percent(),
|
|
|
|
|
'picture' => 'pic.jpg',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$product = $cast->get($model, 'product', $json, []);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($product->incoId->value)->toBe(123)
|
2026-11-14 16:06:12 +01:00
|
|
|
->and($product->originalId->value->toString())->toBe($uuid)
|
|
|
|
|
->and($product->name)->toBe('Product Name')
|
|
|
|
|
->and($product->weight->value)->toBe(1000)
|
|
|
|
|
->and($product->weight->isVariable)->toBeFalse()
|
|
|
|
|
->and($product->taxRate)->toBe(TaxRate::TVA_5_5)
|
2026-11-16 16:13:14 +01:00
|
|
|
->and($product->picture?->value)->toBe('pic.jpg');
|
2026-11-14 16:06:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('gets an order line product with integer tax rate and null picture', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$cast = new OrderLineProductCast();
|
|
|
|
|
$model = \Mockery::mock(Model::class);
|
2026-11-17 12:15:39 +01:00
|
|
|
$uuid = UuidHelper::v7()->toString();
|
2026-11-14 16:06:12 +01:00
|
|
|
|
|
|
|
|
$json = json_encode([
|
|
|
|
|
'inco_id' => 123,
|
|
|
|
|
'original_id' => $uuid,
|
|
|
|
|
'name' => 'Product Name',
|
|
|
|
|
'weight' => 1000,
|
|
|
|
|
'weight_variable' => false,
|
|
|
|
|
'brand' => ProductBrand::loue->value,
|
|
|
|
|
'order_unit' => OrderUnit::PCE->value,
|
|
|
|
|
'price' => '1000',
|
|
|
|
|
'discounted_price' => '800',
|
|
|
|
|
'price_kg_ttc' => '1000',
|
|
|
|
|
'tax_rate' => 20, // integer from json_decode for 20%
|
|
|
|
|
'picture' => null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$product = $cast->get($model, 'product', $json, []);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($product->taxRate)->toBe(TaxRate::TVA_20)
|
2026-11-14 16:06:12 +01:00
|
|
|
->and($product->picture)->toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws exception when setting invalid type', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$cast = new OrderLineProductCast();
|
|
|
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
2026-11-16 16:13:14 +01:00
|
|
|
/** @phpstan-ignore argument.type */
|
2026-11-14 16:06:12 +01:00
|
|
|
$cast->set($model, 'product', 'invalid', []);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
})->throws(
|
|
|
|
|
\InvalidArgumentException::class,
|
|
|
|
|
'The given value is not an instance of OrderLineProduct.'
|
2026-06-14 21:32:07 +02:00
|
|
|
);
|
|
|
|
|
|
2026-11-14 16:06:12 +01:00
|
|
|
it('sets an order line product', function () {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$cast = new OrderLineProductCast();
|
|
|
|
|
$model = \Mockery::mock(Model::class);
|
|
|
|
|
$currency = new Currency('EUR');
|
|
|
|
|
|
|
|
|
|
$product = new OrderLineProduct(
|
|
|
|
|
incoId: new IncoId(123),
|
2026-11-17 12:15:39 +01:00
|
|
|
originalId: new ProductId(UuidHelper::v7()),
|
2026-11-14 16:06:12 +01:00
|
|
|
name: 'Product Name',
|
|
|
|
|
weight: new ProductWeight(1000, false),
|
|
|
|
|
brand: ProductBrand::cases()[0], // Pick first enum case
|
|
|
|
|
orderUnit: OrderUnit::cases()[0], // Pick first enum case
|
|
|
|
|
price: new Money(1000, $currency),
|
|
|
|
|
discountedPrice: new Money(800, $currency),
|
|
|
|
|
priceKgTtc: new Money(1000, $currency),
|
|
|
|
|
taxRate: TaxRate::TVA_20,
|
|
|
|
|
picture: new StorageFilePath('pic.jpg')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$value = $cast->set($model, 'product', $product, []);
|
|
|
|
|
$data = json_decode($value, true);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 16:13:14 +01:00
|
|
|
assert(is_array($data));
|
2026-11-14 16:06:12 +01:00
|
|
|
expect($value)->toBeJson()
|
2026-11-16 16:13:14 +01:00
|
|
|
->and($data)
|
2026-11-16 12:46:06 +01:00
|
|
|
->toHaveKey('weight', 1000)
|
|
|
|
|
->toHaveKey('weight_variable', false)
|
|
|
|
|
->and($data)->not->toHaveKey('weight_unit')
|
|
|
|
|
->and(Arr::get($data, 'picture'))->toBe('pic.jpg');
|
2026-11-14 16:06:12 +01:00
|
|
|
});
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|