2026-11-14 16:06:12 +01:00
|
|
|
<?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;
|
2026-11-14 23:15:10 +01:00
|
|
|
use Domain\Models\ValueObjects\OrderReference;
|
2026-11-14 16:06:12 +01:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2026-11-14 23:15:10 +01:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2026-11-14 16:06:12 +01:00
|
|
|
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');
|
2026-11-14 23:15:10 +01:00
|
|
|
$action = new SubmitContactForm($storage, new PrivateS3Disk());
|
|
|
|
|
$orderRef = new OrderReference(Uuid::uuid7());
|
|
|
|
|
$input = new SubmitContactFormInput(
|
2026-11-14 16:06:12 +01:00
|
|
|
lastName: 'Doe',
|
|
|
|
|
firstName: 'John',
|
|
|
|
|
email: 'john.doe@example.com',
|
|
|
|
|
subject: 'Question',
|
|
|
|
|
message: 'Bonjour, ceci est un message de test valide.',
|
2026-11-14 23:15:10 +01:00
|
|
|
orderRef: $orderRef,
|
2026-11-14 16:06:12 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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')
|
2026-11-14 23:15:10 +01:00
|
|
|
->and($result->orderRef)->toBe($orderRef)
|
2026-11-14 16:06:12 +01:00
|
|
|
->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);
|
|
|
|
|
});
|
|
|
|
|
});
|