2026-11-14 22:00:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:21:17 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Services;
|
|
|
|
|
|
2026-11-14 22:00:11 +01:00
|
|
|
use App\Mail\RegateOrderMail;
|
|
|
|
|
use App\Services\MailRegateService;
|
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
use Domain\Enums\Units\OrderUnit;
|
|
|
|
|
use Domain\Events\Dto\OrderPlacedLine;
|
|
|
|
|
use Domain\Exceptions\OrderTunnel\RegateExportFailedException;
|
|
|
|
|
use Domain\Models\ValueObjects\IncoId;
|
|
|
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
|
|
|
use Domain\Ports\Services\CsvGenerator;
|
|
|
|
|
use Illuminate\Contracts\Mail\Mailer;
|
|
|
|
|
use Illuminate\Mail\PendingMail;
|
|
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
use Money\Money;
|
2026-11-17 12:15:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-11-14 22:00:11 +01:00
|
|
|
|
|
|
|
|
describe('MailRegateService', function (): void {
|
|
|
|
|
it('generates expected CSV rows and sends RegateOrderMail to configured regate address', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
Mail::fake();
|
|
|
|
|
Config::set('mail.to.regate', 'regate@example.com');
|
|
|
|
|
|
2026-11-17 12:15:39 +01:00
|
|
|
$uuid = UuidHelper::v7();
|
2026-11-14 22:00:11 +01:00
|
|
|
$reference = new OrderReference($uuid);
|
|
|
|
|
$orderReferenceValue = (string) $reference;
|
|
|
|
|
$employeeMatricule = new Matricule('1234567');
|
2026-11-16 16:13:14 +01:00
|
|
|
$pickupDay = CarbonImmutable::parse('2025-01-01');
|
2026-11-14 22:00:11 +01:00
|
|
|
|
|
|
|
|
$line1 = new OrderPlacedLine(
|
|
|
|
|
productName: 'Product 1',
|
|
|
|
|
productIncoId: new IncoId(45),
|
|
|
|
|
productWeightIsVariable: false,
|
|
|
|
|
productPriceKgTtc: null,
|
|
|
|
|
productOrderUnit: OrderUnit::PCE,
|
|
|
|
|
quantity: 2,
|
|
|
|
|
totalTtc: Money::EUR(1000),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$line2 = new OrderPlacedLine(
|
|
|
|
|
productName: 'Product 2',
|
|
|
|
|
productIncoId: new IncoId(46),
|
|
|
|
|
productWeightIsVariable: false,
|
|
|
|
|
productPriceKgTtc: null,
|
|
|
|
|
productOrderUnit: OrderUnit::COLIS,
|
|
|
|
|
quantity: 1,
|
|
|
|
|
totalTtc: Money::EUR(2000),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$line3 = new OrderPlacedLine(
|
|
|
|
|
productName: 'Product 3',
|
|
|
|
|
productIncoId: new IncoId(47),
|
|
|
|
|
productWeightIsVariable: true,
|
|
|
|
|
productPriceKgTtc: Money::EUR(1000),
|
|
|
|
|
productOrderUnit: OrderUnit::KGM,
|
|
|
|
|
quantity: 3,
|
|
|
|
|
totalTtc: Money::EUR(3000),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$csvGenerator = \Mockery::mock(CsvGenerator::class);
|
|
|
|
|
$csvGenerator->shouldReceive('generate')
|
|
|
|
|
->once()
|
|
|
|
|
->with(
|
|
|
|
|
\Mockery::type('array'),
|
|
|
|
|
\Mockery::on(function (array $rows) use ($orderReferenceValue): bool {
|
|
|
|
|
return count($rows) === 3
|
2026-11-16 16:13:14 +01:00
|
|
|
&& is_array($rows[0])
|
|
|
|
|
&& is_array($rows[1])
|
|
|
|
|
&& is_array($rows[2])
|
|
|
|
|
&& $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;
|
2026-11-14 22:00:11 +01:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
->andReturn('csv_content');
|
|
|
|
|
|
|
|
|
|
/** @var Mailer $mailer */
|
|
|
|
|
$mailer = Mail::getFacadeRoot();
|
|
|
|
|
$service = new MailRegateService($csvGenerator, $mailer);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->processOrder($reference, $employeeMatricule, $pickupDay, [$line1, $line2, $line3]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
Mail::assertSent(RegateOrderMail::class, function (RegateOrderMail $mail) use ($orderReferenceValue): bool {
|
|
|
|
|
$attachments = $mail->attachments();
|
|
|
|
|
|
|
|
|
|
return $mail->hasTo('regate@example.com')
|
|
|
|
|
&& count($attachments) === 1
|
|
|
|
|
&& str_contains((string) $attachments[0]->as, sprintf('BTQ-%s-', $orderReferenceValue));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('throws RegateExportFailedException when mailer throws', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
Config::set('mail.to.regate', 'regate@example.com');
|
|
|
|
|
|
2026-11-17 12:15:39 +01:00
|
|
|
$reference = new OrderReference(UuidHelper::v7());
|
2026-11-14 22:00:11 +01:00
|
|
|
$employeeMatricule = new Matricule('1234567');
|
2026-11-16 16:13:14 +01:00
|
|
|
$pickupDay = CarbonImmutable::parse('2025-01-01');
|
2026-11-14 22:00:11 +01:00
|
|
|
|
|
|
|
|
$csvGenerator = \Mockery::mock(CsvGenerator::class);
|
|
|
|
|
$csvGenerator->shouldReceive('generate')->once()->andReturn('csv_content');
|
|
|
|
|
|
|
|
|
|
$pendingMail = \Mockery::mock(PendingMail::class);
|
|
|
|
|
$pendingMail->shouldReceive('send')
|
|
|
|
|
->once()
|
|
|
|
|
->with(\Mockery::type(RegateOrderMail::class))
|
|
|
|
|
->andThrow(new \RuntimeException('SMTP connection refused'));
|
|
|
|
|
|
|
|
|
|
$mailer = \Mockery::mock(Mailer::class);
|
|
|
|
|
$mailer->shouldReceive('to')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn($pendingMail);
|
|
|
|
|
|
|
|
|
|
$service = new MailRegateService($csvGenerator, $mailer);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn () => $service->processOrder($reference, $employeeMatricule, $pickupDay, []);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(RegateExportFailedException::class, 'SMTP connection refused');
|
|
|
|
|
});
|
|
|
|
|
});
|