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