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'); }); });