2026-11-14 22:41:40 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 13:09:00 +01:00
|
|
|
namespace Tests\Feature\App\Jobs;
|
2026-11-14 22:41:40 +01:00
|
|
|
|
|
|
|
|
use App\Jobs\SendContactMailJob;
|
|
|
|
|
use App\Lib\PrivateS3Disk;
|
|
|
|
|
use App\Mail\SendContactMail;
|
|
|
|
|
use Domain\Models\ValueObjects\AttachedFile;
|
|
|
|
|
use Domain\Models\ValueObjects\ContactFormData;
|
|
|
|
|
use Illuminate\Contracts\Mail\Mailer;
|
2026-11-16 15:04:17 +01:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2026-11-14 22:41:40 +01:00
|
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
|
|
|
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
|
|
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
use function Pest\Laravel\instance;
|
|
|
|
|
|
2026-11-14 22:41:40 +01:00
|
|
|
describe('SendContactMailJob', function (): void {
|
|
|
|
|
it('sends SendContactMail and idempotently deletes attachment when it exists on s3-private', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-16 15:04:17 +01:00
|
|
|
Mail::fake();
|
|
|
|
|
/** @var Mailer $mailer */
|
|
|
|
|
$mailer = Mail::getFacadeRoot();
|
|
|
|
|
|
2026-11-14 22:41:40 +01:00
|
|
|
$data = new ContactFormData(
|
|
|
|
|
lastName: 'Dupont',
|
|
|
|
|
firstName: 'Jean',
|
|
|
|
|
email: 'jean.dupont@gmail.com',
|
|
|
|
|
subject: 'Question avec PJ',
|
|
|
|
|
message: 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
file: new AttachedFile(
|
|
|
|
|
path: 'contact-attachments/piece.pdf',
|
|
|
|
|
originalName: 'piece.pdf',
|
|
|
|
|
mimeType: 'application/pdf',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$job = new SendContactMailJob($data, 'contact@example.com', 'Support');
|
|
|
|
|
$privateS3Disk = new PrivateS3Disk();
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
/** @var StorageService&\Mockery\MockInterface $storageService */
|
|
|
|
|
$storageService = \Mockery::spy(StorageService::class);
|
|
|
|
|
$storageService->allows('fileExistsOnDisk')->andReturn(true);
|
2026-11-14 22:41:40 +01:00
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
// WHEN
|
|
|
|
|
$job->handle($mailer, $storageService, $privateS3Disk);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
Mail::assertSent(
|
|
|
|
|
SendContactMail::class,
|
|
|
|
|
static fn (SendContactMail $mail): bool => $mail->hasTo('contact@example.com') && $mail->data === $data,
|
|
|
|
|
);
|
|
|
|
|
$storageService->shouldHaveReceived('fileExistsOnDisk')
|
2026-11-14 22:41:40 +01:00
|
|
|
->once()
|
|
|
|
|
->with(
|
2026-11-16 15:04:17 +01:00
|
|
|
\Mockery::on(static fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/piece.pdf'),
|
|
|
|
|
\Mockery::on(static fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
|
|
|
);
|
|
|
|
|
$storageService->shouldHaveReceived('removeFromDisk')
|
2026-11-14 22:41:40 +01:00
|
|
|
->once()
|
|
|
|
|
->with(
|
2026-11-16 15:04:17 +01:00
|
|
|
\Mockery::on(static fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/piece.pdf'),
|
|
|
|
|
\Mockery::on(static fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
2026-11-14 22:41:40 +01:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sends SendContactMail and does not call removeFromDisk when attachment does not exist', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-16 15:04:17 +01:00
|
|
|
Mail::fake();
|
|
|
|
|
/** @var Mailer $mailer */
|
|
|
|
|
$mailer = Mail::getFacadeRoot();
|
|
|
|
|
|
2026-11-14 22:41:40 +01:00
|
|
|
$data = new ContactFormData(
|
|
|
|
|
lastName: 'Dupont',
|
|
|
|
|
firstName: 'Jean',
|
|
|
|
|
email: 'jean.dupont@gmail.com',
|
|
|
|
|
subject: 'Question avec PJ',
|
|
|
|
|
message: 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
file: new AttachedFile(
|
|
|
|
|
path: 'contact-attachments/missing.pdf',
|
|
|
|
|
originalName: 'missing.pdf',
|
|
|
|
|
mimeType: 'application/pdf',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$job = new SendContactMailJob($data, 'contact@example.com');
|
|
|
|
|
$privateS3Disk = new PrivateS3Disk();
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
/** @var StorageService&\Mockery\MockInterface $storageService */
|
|
|
|
|
$storageService = \Mockery::spy(StorageService::class);
|
|
|
|
|
$storageService->allows('fileExistsOnDisk')->andReturn(false);
|
2026-11-14 22:41:40 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$job->handle($mailer, $storageService, $privateS3Disk);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
Mail::assertSent(
|
|
|
|
|
SendContactMail::class,
|
|
|
|
|
static fn (SendContactMail $mail): bool => $mail->hasTo('contact@example.com') && $mail->data === $data,
|
|
|
|
|
);
|
|
|
|
|
$storageService->shouldHaveReceived('fileExistsOnDisk')->once();
|
|
|
|
|
$storageService->shouldNotHaveReceived('removeFromDisk');
|
2026-11-14 22:41:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sends SendContactMail and skips storage checks when data has no attachment', function (): void {
|
|
|
|
|
// GIVEN
|
2026-11-16 15:04:17 +01:00
|
|
|
Mail::fake();
|
|
|
|
|
/** @var Mailer $mailer */
|
|
|
|
|
$mailer = Mail::getFacadeRoot();
|
|
|
|
|
|
2026-11-14 22:41:40 +01:00
|
|
|
$data = new ContactFormData(
|
|
|
|
|
lastName: 'Dupont',
|
|
|
|
|
firstName: 'Jean',
|
|
|
|
|
email: 'jean.dupont@gmail.com',
|
|
|
|
|
subject: 'Question sans PJ',
|
|
|
|
|
message: 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$job = new SendContactMailJob($data, 'contact@example.com');
|
|
|
|
|
$privateS3Disk = new PrivateS3Disk();
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
/** @var StorageService&\Mockery\MockInterface $storageService */
|
|
|
|
|
$storageService = \Mockery::spy(StorageService::class);
|
2026-11-14 22:41:40 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$job->handle($mailer, $storageService, $privateS3Disk);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
Mail::assertSent(
|
|
|
|
|
SendContactMail::class,
|
|
|
|
|
static fn (SendContactMail $mail): bool => $mail->hasTo('contact@example.com') && $mail->data === $data,
|
|
|
|
|
);
|
|
|
|
|
$storageService->shouldNotHaveReceived('fileExistsOnDisk');
|
|
|
|
|
$storageService->shouldNotHaveReceived('removeFromDisk');
|
2026-11-14 22:41:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('idempotently deletes attachment on failed() when it exists on s3-private', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$data = new ContactFormData(
|
|
|
|
|
lastName: 'Dupont',
|
|
|
|
|
firstName: 'Jean',
|
|
|
|
|
email: 'jean.dupont@gmail.com',
|
|
|
|
|
subject: 'Question avec PJ',
|
|
|
|
|
message: 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
file: new AttachedFile(
|
|
|
|
|
path: 'contact-attachments/failed.pdf',
|
|
|
|
|
originalName: 'failed.pdf',
|
|
|
|
|
mimeType: 'application/pdf',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$job = new SendContactMailJob($data, 'contact@example.com');
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
/** @var StorageService&\Mockery\MockInterface $storageService */
|
|
|
|
|
$storageService = \Mockery::spy(StorageService::class);
|
|
|
|
|
$storageService->allows('fileExistsOnDisk')->andReturn(true);
|
|
|
|
|
instance(StorageService::class, $storageService);
|
2026-11-14 22:41:40 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$job->failed(new \RuntimeException('SMTP failure'));
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
$storageService->shouldHaveReceived('fileExistsOnDisk')
|
|
|
|
|
->once()
|
|
|
|
|
->with(
|
|
|
|
|
\Mockery::on(static fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/failed.pdf'),
|
|
|
|
|
\Mockery::on(static fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
|
|
|
);
|
|
|
|
|
$storageService->shouldHaveReceived('removeFromDisk')
|
|
|
|
|
->once()
|
|
|
|
|
->with(
|
|
|
|
|
\Mockery::on(static fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/failed.pdf'),
|
|
|
|
|
\Mockery::on(static fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
|
|
|
);
|
2026-11-14 22:41:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('skips removeFromDisk on failed() when attachment does not exist on s3-private', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$data = new ContactFormData(
|
|
|
|
|
lastName: 'Dupont',
|
|
|
|
|
firstName: 'Jean',
|
|
|
|
|
email: 'jean.dupont@gmail.com',
|
|
|
|
|
subject: 'Question avec PJ',
|
|
|
|
|
message: 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
|
|
|
file: new AttachedFile(
|
|
|
|
|
path: 'contact-attachments/already-deleted.pdf',
|
|
|
|
|
originalName: 'already-deleted.pdf',
|
|
|
|
|
mimeType: 'application/pdf',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$job = new SendContactMailJob($data, 'contact@example.com');
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
/** @var StorageService&\Mockery\MockInterface $storageService */
|
|
|
|
|
$storageService = \Mockery::spy(StorageService::class);
|
|
|
|
|
$storageService->allows('fileExistsOnDisk')->andReturn(false);
|
|
|
|
|
instance(StorageService::class, $storageService);
|
2026-11-14 22:41:40 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$job->failed(new \RuntimeException('SMTP failure'));
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
$storageService->shouldHaveReceived('fileExistsOnDisk')->once();
|
|
|
|
|
$storageService->shouldNotHaveReceived('removeFromDisk');
|
2026-11-14 22:41:40 +01:00
|
|
|
});
|
|
|
|
|
});
|