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