ldc-cocotte/tests/Integration/WithApp/App/Actions/Contact/SubmitContactFormTest.php
Jérémy Munsch d1d0585b34
fix(contact): update orderRef to use OrderReference value object
Refactor orderRef handling in contact forms to ensure proper validation and type safety.
2026-11-15 16:45:05 +01:00

97 lines
3.6 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
expect(
/** @phpstan-ignore argument.type */
fn () => $action($invalidInput)
)
// THEN
->toThrow(\InvalidArgumentException::class);
});
});