ldc-cocotte/tests/Integration/WithApp/App/Services/MailRegateServiceTest.php

133 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Services;
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;
use Shared\Helpers\UuidHelper;
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');
$uuid = UuidHelper::v7();
$reference = new OrderReference($uuid);
$orderReferenceValue = (string) $reference;
$employeeMatricule = new Matricule('1234567');
$pickupDay = CarbonImmutable::parse('2025-01-01');
$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
&& 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;
})
)
->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');
$reference = new OrderReference(UuidHelper::v7());
$employeeMatricule = new Matricule('1234567');
$pickupDay = CarbonImmutable::parse('2025-01-01');
$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');
});
});