- Implemented HandlesSuccess and HandlesFailure interfaces in CreateProduct and UpdateProduct actions. - Added tracking of uploaded paths and cleanup on failure. - Enhanced image storage handling to ensure proper cleanup of orphaned files. feat(ranges): add success and failure handling for range actions - Implemented HandlesSuccess and HandlesFailure interfaces in CreateRange and UpdateRange actions. - Added tracking of uploaded paths and cleanup on failure. - Enhanced image storage handling to ensure proper cleanup of orphaned files. feat(slides): add success and failure handling for slide actions - Implemented HandlesSuccess and HandlesFailure interfaces in CreateSlide and UpdateSlide actions. - Added tracking of uploaded paths and cleanup on failure. - Enhanced image storage handling to ensure proper cleanup of orphaned files. chore: introduce TracksPendingStoragePaths trait - Created a new trait to manage tracking of uploaded and obsolete storage paths. - This will streamline the handling of file cleanup across various actions. test: add tests for action failure handling and cleanup - Added tests to verify that uploaded files are cleaned up on action failure. - Ensured that previous files are preserved during rollback scenarios.
137 lines
5.3 KiB
PHP
137 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Contracts\ActionInput;
|
|
use App\Cqs\Actions\Contact\SubmitContactForm;
|
|
use App\Cqs\Actions\Contact\SubmitContactFormInput;
|
|
use App\Lib\PrivateS3Disk;
|
|
use Domain\Models\ValueObjects\AttachedFile;
|
|
use Domain\Models\ValueObjects\ContactFormData;
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
|
|
describe('SubmitContactForm action', function (): void {
|
|
it('builds ContactFormData without touching storage when no file is provided', function (): void {
|
|
// GIVEN
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldNotReceive('storeUploadedOntoDisk');
|
|
$action = new SubmitContactForm($storage, new PrivateS3Disk());
|
|
$orderRef = new OrderReference(Uuid::uuid7());
|
|
$input = new SubmitContactFormInput(
|
|
lastName: 'Doe',
|
|
firstName: 'John',
|
|
email: 'john.doe@example.com',
|
|
subject: 'Question',
|
|
message: 'Bonjour, ceci est un message de test valide.',
|
|
orderRef: $orderRef,
|
|
);
|
|
|
|
// WHEN
|
|
$result = $action($input);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ContactFormData::class)
|
|
->and($result->lastName)->toBe('Doe')
|
|
->and($result->firstName)->toBe('John')
|
|
->and($result->email)->toBe('john.doe@example.com')
|
|
->and($result->subject)->toBe('Question')
|
|
->and($result->orderRef)->toBe($orderRef)
|
|
->and($result->file)->toBeNull();
|
|
});
|
|
|
|
it('uploads attachment to private S3 disk and attaches metadata', function (): void {
|
|
// GIVEN
|
|
$file = UploadedFile::fake()->create('document.pdf', 100, 'application/pdf');
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldReceive('storeUploadedOntoDisk')
|
|
->once()
|
|
->with(
|
|
$file,
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
)
|
|
->andReturn(new StorageFilePath('contact-attachments/document.pdf'));
|
|
|
|
$action = new SubmitContactForm($storage, new PrivateS3Disk());
|
|
$input = new SubmitContactFormInput(
|
|
lastName: 'Doe',
|
|
firstName: 'John',
|
|
email: 'john.doe@example.com',
|
|
subject: 'Question',
|
|
message: 'Bonjour, ceci est un message de test valide.',
|
|
file: $file,
|
|
);
|
|
|
|
// WHEN
|
|
$result = $action($input);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ContactFormData::class)
|
|
->and($result->file)->toBeInstanceOf(AttachedFile::class)
|
|
->and($result->file?->path)->toBe('contact-attachments/document.pdf')
|
|
->and($result->file?->originalName)->toBe('document.pdf')
|
|
->and($result->file?->mimeType)->toBe('application/pdf');
|
|
});
|
|
|
|
it('throws when an invalid input is passed', function (): void {
|
|
// GIVEN
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldNotReceive('storeUploadedOntoDisk');
|
|
$action = new SubmitContactForm($storage, new PrivateS3Disk());
|
|
$invalidInput = new class () implements ActionInput
|
|
{
|
|
};
|
|
|
|
// WHEN & THEN
|
|
expect(
|
|
/** @phpstan-ignore argument.type */
|
|
fn () => $action($invalidInput)
|
|
)
|
|
->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
|
|
it('cleans up private S3 attachment when onFailure is triggered after upload', function (): void {
|
|
// GIVEN
|
|
$file = UploadedFile::fake()->create('document.pdf', 100, 'application/pdf');
|
|
$storedPath = new StorageFilePath('contact-attachments/orphan.pdf');
|
|
$storage = \Mockery::mock(StorageService::class);
|
|
$storage->shouldReceive('storeUploadedOntoDisk')
|
|
->once()
|
|
->andReturn($storedPath);
|
|
$storage->shouldReceive('fileExistsOnDisk')
|
|
->once()
|
|
->with(
|
|
\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'contact-attachments/orphan.pdf'),
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
)
|
|
->andReturnTrue();
|
|
$storage->shouldReceive('removeFromDisk')
|
|
->once()
|
|
->with(
|
|
\Mockery::on(static fn (mixed $p): bool => $p instanceof StorageFilePath && $p->value === 'contact-attachments/orphan.pdf'),
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
)
|
|
->andReturnTrue();
|
|
|
|
$action = new SubmitContactForm($storage, new PrivateS3Disk());
|
|
$input = new SubmitContactFormInput(
|
|
lastName: 'Doe',
|
|
firstName: 'John',
|
|
email: 'john.doe@example.com',
|
|
subject: 'Question',
|
|
message: 'Bonjour, ceci est un message de test valide.',
|
|
file: $file,
|
|
);
|
|
|
|
// WHEN
|
|
$action($input);
|
|
$action->onFailure($input, new \RuntimeException('Mail dispatch failed'));
|
|
|
|
// THEN
|
|
expect(true)->toBeTrue();
|
|
});
|
|
});
|