ldc-boutique-cnc-sable/tests/Unit/Domain/Services/RegateServiceTest.php

90 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Services;
use Carbon\CarbonImmutable;
use Domain\Enums\Units\OrderUnit as UnitsOrderUnit;
use Domain\Events\Dto\OrderPlacedLine;
use Domain\Events\Dto\RegateOrderCsvGenerated;
use Domain\Models\ValueObjects\IncoId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Ports\Services\CsvGenerator;
use Domain\Services\RegateService;
use Mockery;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Shared\DDD\DomainEventDispatcher;
describe('RegateService', function (): void {
it('processes order and generates csv', function (): void {
// GIVEN
$csvGenerator = Mockery::mock(CsvGenerator::class);
$dispatcher = Mockery::mock(DomainEventDispatcher::class);
$service = new RegateService($csvGenerator, $dispatcher);
$uuid = Uuid::uuid7();
$reference = new OrderReference($uuid);
$orderReferenceValue = (string) $reference;
$employeeMatricule = new Matricule('1234567');
$pickupDay = CarbonImmutable::create(2025, 1, 1);
$line1 = new OrderPlacedLine(
productName: 'Product 1',
productIncoId: new IncoId(45),
productWeightIsVariable: false,
productPriceKgTtc: null,
productOrderUnit: UnitsOrderUnit::PCE,
quantity: 2,
totalTtc: Money::EUR(1000)
);
$line2 = new OrderPlacedLine(
productName: 'Product 2',
productIncoId: new IncoId(46),
productWeightIsVariable: false,
productPriceKgTtc: null,
productOrderUnit: UnitsOrderUnit::COLIS,
quantity: 1,
totalTtc: Money::EUR(2000)
);
$line3 = new OrderPlacedLine(
productName: 'Product 3',
productIncoId: new IncoId(47),
productWeightIsVariable: true,
productPriceKgTtc: Money::EUR(1000),
productOrderUnit: UnitsOrderUnit::KGM,
quantity: 3,
totalTtc: Money::EUR(3000)
);
$csvGenerator->shouldReceive('generate')->once()->with(
Mockery::type('array'),
Mockery::on(function (array $rows) use ($orderReferenceValue): bool {
return count($rows) === 3
&& $rows[0][5] === 'UB'
&& $rows[1][5] === 'UEA'
&& $rows[2][5] === 'KGM'
&& $rows[0][0] === '45'
&& $rows[0][7] === '01/01/2025'
&& $rows[0][9] === $orderReferenceValue;
})
)->andReturn('csv_content');
$dispatcher->shouldReceive('dispatch')->once()->with(Mockery::on(function (object $event) use ($orderReferenceValue): bool {
return $event instanceof RegateOrderCsvGenerated
&& $event->csvContent === 'csv_content'
&& str_contains($event->fileName, sprintf('BTQ-%s-', $orderReferenceValue));
}));
// WHEN
$service->processOrder($reference, $employeeMatricule, $pickupDay, [$line1, $line2, $line3]);
// THEN
expect(true)->toBeTrue();
});
});