ldc-cocotte/tests/Feature/App/Jobs/SendContactMailJobTest.php

208 lines
8.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Jobs;
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;
use Illuminate\Support\Facades\Mail;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\ValueObjects\DiskName;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
use function Pest\Laravel\instance;
describe('SendContactMailJob', function (): void {
it('sends SendContactMail and idempotently deletes attachment when it exists on s3-private', function (): void {
// GIVEN
Mail::fake();
/** @var Mailer $mailer */
$mailer = Mail::getFacadeRoot();
$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();
/** @var StorageService&\Mockery\MockInterface $storageService */
$storageService = \Mockery::spy(StorageService::class);
$storageService->allows('fileExistsOnDisk')->andReturn(true);
// 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')
->once()
->with(
\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')
->once()
->with(
\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),
);
});
it('sends SendContactMail and does not call removeFromDisk when attachment does not exist', function (): void {
// GIVEN
Mail::fake();
/** @var Mailer $mailer */
$mailer = Mail::getFacadeRoot();
$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();
/** @var StorageService&\Mockery\MockInterface $storageService */
$storageService = \Mockery::spy(StorageService::class);
$storageService->allows('fileExistsOnDisk')->andReturn(false);
// 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')->once();
$storageService->shouldNotHaveReceived('removeFromDisk');
});
it('sends SendContactMail and skips storage checks when data has no attachment', function (): void {
// GIVEN
Mail::fake();
/** @var Mailer $mailer */
$mailer = Mail::getFacadeRoot();
$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();
/** @var StorageService&\Mockery\MockInterface $storageService */
$storageService = \Mockery::spy(StorageService::class);
// 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->shouldNotHaveReceived('fileExistsOnDisk');
$storageService->shouldNotHaveReceived('removeFromDisk');
});
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');
/** @var StorageService&\Mockery\MockInterface $storageService */
$storageService = \Mockery::spy(StorageService::class);
$storageService->allows('fileExistsOnDisk')->andReturn(true);
instance(StorageService::class, $storageService);
// WHEN
$job->failed(new \RuntimeException('SMTP failure'));
// THEN
$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),
);
});
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');
/** @var StorageService&\Mockery\MockInterface $storageService */
$storageService = \Mockery::spy(StorageService::class);
$storageService->allows('fileExistsOnDisk')->andReturn(false);
instance(StorageService::class, $storageService);
// WHEN
$job->failed(new \RuntimeException('SMTP failure'));
// THEN
$storageService->shouldHaveReceived('fileExistsOnDisk')->once();
$storageService->shouldNotHaveReceived('removeFromDisk');
});
});