218 lines
8.1 KiB
PHP
218 lines
8.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Feature\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\Mail\Mailables\Address;
|
||
|
|
use Illuminate\Mail\PendingMail;
|
||
|
|
use Uwil\Storage\Contracts\StorageService;
|
||
|
|
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
||
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
||
|
|
|
||
|
|
describe('SendContactMailJob', function (): void {
|
||
|
|
it('sends SendContactMail and idempotently deletes attachment when it exists on s3-private', function (): void {
|
||
|
|
/** @var \Tests\TestCase $this */
|
||
|
|
// 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/piece.pdf',
|
||
|
|
originalName: 'piece.pdf',
|
||
|
|
mimeType: 'application/pdf',
|
||
|
|
),
|
||
|
|
);
|
||
|
|
|
||
|
|
$job = new SendContactMailJob($data, 'contact@example.com', 'Support');
|
||
|
|
$privateS3Disk = new PrivateS3Disk();
|
||
|
|
|
||
|
|
$pendingMail = \Mockery::mock(PendingMail::class);
|
||
|
|
$pendingMail->shouldReceive('send')
|
||
|
|
->once()
|
||
|
|
->with(\Mockery::on(fn (mixed $mail): bool => $mail instanceof SendContactMail && $mail->data === $data));
|
||
|
|
|
||
|
|
$mailer = \Mockery::mock(Mailer::class);
|
||
|
|
$mailer->shouldReceive('to')
|
||
|
|
->once()
|
||
|
|
->with(\Mockery::on(
|
||
|
|
fn (mixed $address): bool => $address instanceof Address
|
||
|
|
&& $address->address === 'contact@example.com'
|
||
|
|
&& $address->name === 'Support'
|
||
|
|
))
|
||
|
|
->andReturn($pendingMail);
|
||
|
|
|
||
|
|
$storageService = \Mockery::mock(StorageService::class);
|
||
|
|
$storageService->shouldReceive('fileExistsOnDisk')
|
||
|
|
->once()
|
||
|
|
->with(
|
||
|
|
\Mockery::on(fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/piece.pdf'),
|
||
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
||
|
|
)
|
||
|
|
->andReturn(true);
|
||
|
|
$storageService->shouldReceive('removeFromDisk')
|
||
|
|
->once()
|
||
|
|
->with(
|
||
|
|
\Mockery::on(fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/piece.pdf'),
|
||
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
||
|
|
);
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$job->handle($mailer, $storageService, $privateS3Disk);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect(true)->toBeTrue();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('sends SendContactMail and does not call removeFromDisk when attachment does not exist', function (): void {
|
||
|
|
/** @var \Tests\TestCase $this */
|
||
|
|
// 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/missing.pdf',
|
||
|
|
originalName: 'missing.pdf',
|
||
|
|
mimeType: 'application/pdf',
|
||
|
|
),
|
||
|
|
);
|
||
|
|
|
||
|
|
$job = new SendContactMailJob($data, 'contact@example.com');
|
||
|
|
$privateS3Disk = new PrivateS3Disk();
|
||
|
|
|
||
|
|
$pendingMail = \Mockery::mock(PendingMail::class);
|
||
|
|
$pendingMail->shouldReceive('send')->once();
|
||
|
|
|
||
|
|
$mailer = \Mockery::mock(Mailer::class);
|
||
|
|
$mailer->shouldReceive('to')->once()->andReturn($pendingMail);
|
||
|
|
|
||
|
|
$storageService = \Mockery::mock(StorageService::class);
|
||
|
|
$storageService->shouldReceive('fileExistsOnDisk')
|
||
|
|
->once()
|
||
|
|
->andReturn(false);
|
||
|
|
$storageService->shouldNotReceive('removeFromDisk');
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$job->handle($mailer, $storageService, $privateS3Disk);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect(true)->toBeTrue();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('sends SendContactMail and skips storage checks when data has no attachment', function (): void {
|
||
|
|
/** @var \Tests\TestCase $this */
|
||
|
|
// GIVEN
|
||
|
|
$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();
|
||
|
|
|
||
|
|
$pendingMail = \Mockery::mock(PendingMail::class);
|
||
|
|
$pendingMail->shouldReceive('send')->once();
|
||
|
|
|
||
|
|
$mailer = \Mockery::mock(Mailer::class);
|
||
|
|
$mailer->shouldReceive('to')->once()->andReturn($pendingMail);
|
||
|
|
|
||
|
|
$storageService = \Mockery::mock(StorageService::class);
|
||
|
|
$storageService->shouldNotReceive('fileExistsOnDisk');
|
||
|
|
$storageService->shouldNotReceive('removeFromDisk');
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$job->handle($mailer, $storageService, $privateS3Disk);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect(true)->toBeTrue();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('idempotently deletes attachment on failed() when it exists on s3-private', function (): void {
|
||
|
|
/** @var \Tests\TestCase $this */
|
||
|
|
// 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');
|
||
|
|
|
||
|
|
$this->mock(StorageService::class, function ($mock): void {
|
||
|
|
$mock->shouldReceive('fileExistsOnDisk')
|
||
|
|
->once()
|
||
|
|
->with(
|
||
|
|
\Mockery::on(fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/failed.pdf'),
|
||
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
||
|
|
)
|
||
|
|
->andReturn(true);
|
||
|
|
$mock->shouldReceive('removeFromDisk')
|
||
|
|
->once()
|
||
|
|
->with(
|
||
|
|
\Mockery::on(fn (StorageFilePath $path): bool => $path->value === 'contact-attachments/failed.pdf'),
|
||
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$job->failed(new \RuntimeException('SMTP failure'));
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect(true)->toBeTrue();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('skips removeFromDisk on failed() when attachment does not exist on s3-private', function (): void {
|
||
|
|
/** @var \Tests\TestCase $this */
|
||
|
|
// 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');
|
||
|
|
|
||
|
|
$this->mock(StorageService::class, function ($mock): void {
|
||
|
|
$mock->shouldReceive('fileExistsOnDisk')
|
||
|
|
->once()
|
||
|
|
->andReturn(false);
|
||
|
|
$mock->shouldNotReceive('removeFromDisk');
|
||
|
|
});
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$job->failed(new \RuntimeException('SMTP failure'));
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect(true)->toBeTrue();
|
||
|
|
});
|
||
|
|
});
|