- Removed fully qualified names for better readability. - Streamlined code by using direct class references.
218 lines
6.5 KiB
PHP
218 lines
6.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Events;
|
|
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Events\Dto\OrderPlacedLine;
|
|
use Domain\Events\Dto\OrderPlacedShop;
|
|
use Domain\Events\Dto\RegateOrderCsvGenerated;
|
|
use Domain\Events\Dto\SalariesHaveBeenSynced;
|
|
use Domain\Models\ValueObjects\DeliveryAddress;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Domain\Models\ValueObjects\IncoId;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Domain\Models\ValueObjects\OrderLine;
|
|
use Domain\Models\ValueObjects\OrderLineProduct;
|
|
use Domain\Models\ValueObjects\ProductWeight;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
describe('OrderPlacedLine', function () {
|
|
it('creates OrderPlacedLine correctly', function () {
|
|
// GIVEN
|
|
$productName = 'Product';
|
|
$incoId = new IncoId(1);
|
|
$unit = OrderUnit::PCE;
|
|
$price = new Money(2000, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$line = new OrderPlacedLine($productName, $incoId, false, null, $unit, 2, $price);
|
|
|
|
// THEN
|
|
expect($line->quantity)->toBe(2);
|
|
});
|
|
|
|
it('throws exception if quantity is zero or less for OrderPlacedLine', function () {
|
|
// GIVEN
|
|
$productName = 'Product';
|
|
$incoId = new IncoId(1);
|
|
$unit = OrderUnit::PCE;
|
|
$price = new Money(2000, new Currency('EUR'));
|
|
|
|
// WHEN
|
|
$action = fn () => new OrderPlacedLine($productName, $incoId, false, null, $unit, 0, $price);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Quantity must be greater than 0');
|
|
});
|
|
|
|
it('creates OrderPlacedLine from OrderLine', function () {
|
|
// GIVEN
|
|
$product = new OrderLineProduct(
|
|
incoId: new IncoId(42),
|
|
originalId: new ProductId(Uuid::uuid7()),
|
|
name: 'Poulet rôti',
|
|
weight: new ProductWeight(1000, false),
|
|
brand: ProductBrand::none,
|
|
orderUnit: OrderUnit::PCE,
|
|
price: Money::EUR(1000),
|
|
discountedPrice: Money::EUR(1000),
|
|
priceKgTtc: Money::EUR(1200),
|
|
taxRate: TaxRate::TVA_20,
|
|
picture: null
|
|
);
|
|
|
|
$line = new OrderLine(
|
|
product: $product,
|
|
quantity: 3,
|
|
unitPriceHt: Money::EUR(1000),
|
|
unitDiscount: Money::EUR(0),
|
|
unitTax: Money::EUR(200),
|
|
totalHt: Money::EUR(3000),
|
|
totalDiscount: Money::EUR(0),
|
|
totalTax: Money::EUR(600),
|
|
totalTtc: Money::EUR(3600)
|
|
);
|
|
|
|
// WHEN
|
|
$dto = OrderPlacedLine::fromOrderLine($line);
|
|
|
|
// THEN
|
|
expect($dto->productName)->toBe('Poulet rôti')
|
|
->and($dto->productIncoId->value)->toBe(42)
|
|
->and($dto->quantity)->toBe(3)
|
|
->and($dto->totalTtc->getAmount())->toBe('3600')
|
|
->and($dto->productOrderUnit)->toBe(OrderUnit::PCE);
|
|
});
|
|
});
|
|
|
|
describe('OrderPlacedShop', function () {
|
|
it('creates OrderPlacedShop correctly', function () {
|
|
// GIVEN
|
|
$name = 'Name';
|
|
$address = 'Addr';
|
|
$zip = '12345';
|
|
$city = 'City';
|
|
|
|
// WHEN
|
|
$shop = new OrderPlacedShop($name, $address, $zip, $city);
|
|
|
|
// THEN
|
|
expect($shop->name)->toBe('Name');
|
|
});
|
|
|
|
it('creates OrderPlacedShop from DeliveryAddress', function () {
|
|
// GIVEN
|
|
$address = new DeliveryAddress(
|
|
name: 'Boutique Paris',
|
|
address: '10 avenue des Champs',
|
|
postcode: '75008',
|
|
city: 'Paris'
|
|
);
|
|
|
|
// WHEN
|
|
$dto = OrderPlacedShop::fromDeliveryAddress($address);
|
|
|
|
// THEN
|
|
expect($dto->name)->toBe('Boutique Paris')
|
|
->and($dto->address)->toBe('10 avenue des Champs')
|
|
->and($dto->postcode)->toBe('75008')
|
|
->and($dto->city)->toBe('Paris');
|
|
});
|
|
|
|
it('throws exception if shop name is empty', function () {
|
|
// GIVEN
|
|
$name = ' ';
|
|
$address = 'Addr';
|
|
$zip = '12345';
|
|
$city = 'City';
|
|
|
|
// WHEN
|
|
$action = fn () => new OrderPlacedShop($name, $address, $zip, $city);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Shop name is required');
|
|
});
|
|
|
|
it('throws exception if shop address is empty', function () {
|
|
// GIVEN
|
|
$name = 'Name';
|
|
$address = '';
|
|
$zip = '12345';
|
|
$city = 'City';
|
|
|
|
// WHEN
|
|
$action = fn () => new OrderPlacedShop($name, $address, $zip, $city);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Shop address is required');
|
|
});
|
|
|
|
it('throws exception if shop postcode is empty', function () {
|
|
// GIVEN
|
|
$name = 'Name';
|
|
$address = 'Addr';
|
|
$zip = '';
|
|
$city = 'City';
|
|
|
|
// WHEN
|
|
$action = fn () => new OrderPlacedShop($name, $address, $zip, $city);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Shop postcode is required');
|
|
});
|
|
|
|
it('throws exception if shop city is empty', function () {
|
|
// GIVEN
|
|
$name = 'Name';
|
|
$address = 'Addr';
|
|
$zip = '12345';
|
|
$city = ' ';
|
|
|
|
// WHEN
|
|
$action = fn () => new OrderPlacedShop($name, $address, $zip, $city);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Shop city is required');
|
|
});
|
|
});
|
|
|
|
describe('RegateOrderCsvGenerated', function () {
|
|
it('creates RegateOrderCsvGenerated correctly', function () {
|
|
// GIVEN
|
|
$csvContent = 'csv';
|
|
$fileName = 'file.csv';
|
|
|
|
// WHEN
|
|
$dto = new RegateOrderCsvGenerated($csvContent, $fileName);
|
|
|
|
// THEN
|
|
expect($dto->csvContent)->toBe('csv')
|
|
->and($dto->fileName)->toBe('file.csv');
|
|
});
|
|
});
|
|
|
|
describe('SalariesHaveBeenSynced', function () {
|
|
it('creates SalariesHaveBeenSynced correctly', function () {
|
|
// GIVEN
|
|
$matricules = [new Matricule('1234567')];
|
|
|
|
// WHEN
|
|
$dto = new SalariesHaveBeenSynced($matricules);
|
|
|
|
// THEN
|
|
expect($dto->matricules)->toBe($matricules);
|
|
});
|
|
|
|
it('throws exception if SalariesHaveBeenSynced gets non-matricule', function () {
|
|
// GIVEN
|
|
/** @var array<Matricule> $matricules */
|
|
$matricules = ['1234567'];
|
|
|
|
// WHEN
|
|
$action = fn () => new SalariesHaveBeenSynced($matricules);
|
|
// THEN
|
|
expect($action)->toThrow(\UnexpectedValueException::class);
|
|
});
|
|
});
|