149 lines
5 KiB
PHP
149 lines
5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\App\Http\Controllers\Fo\Pages;
|
|
|
|
use App\Dto\ShopDto;
|
|
use App\Lib\PrivateS3Disk;
|
|
use App\Mail\SendContactMail;
|
|
use App\Models\Page;
|
|
use App\Models\Shop;
|
|
use Database\Seeders\ContentSeeder;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\View;
|
|
use Uwil\Storage\Contracts\StorageService;
|
|
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
|
|
describe('ContactController', function (): void {
|
|
beforeEach(function (): void {
|
|
Config::set('mail.to.contact', 'test@example.com');
|
|
});
|
|
|
|
it('returns 404 when no default shop is configured', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$this->seed(ContentSeeder::class);
|
|
View::share('PageList', Page::getCached()
|
|
->mapWithKeys(fn ($page) => [
|
|
$page->slug->value() => $page,
|
|
]));
|
|
Cache::flush();
|
|
|
|
// WHEN
|
|
$response = $this->get('/contactez-nous');
|
|
|
|
// THEN
|
|
$response->assertStatus(404);
|
|
});
|
|
|
|
it('displays contact page with default shop and page content', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$this->seed(ContentSeeder::class);
|
|
View::share('PageList', Page::getCached()
|
|
->mapWithKeys(fn ($page) => [
|
|
$page->slug->value() => $page,
|
|
]));
|
|
Cache::flush();
|
|
|
|
$shop = new Shop();
|
|
$shop->name = 'Boutique Cocotte';
|
|
$shop->address = '1 rue des Poules';
|
|
$shop->postcode = '75000';
|
|
$shop->city = 'Paris';
|
|
$shop->default = true;
|
|
$shop->save();
|
|
|
|
// WHEN
|
|
$response = $this->get('/contactez-nous');
|
|
|
|
// THEN
|
|
$response->assertStatus(200);
|
|
$response->assertViewIs('fo.pages.contact');
|
|
$response->assertViewHas('magasin', fn ($magasin) => $magasin instanceof ShopDto && $magasin->name === 'Boutique Cocotte');
|
|
});
|
|
|
|
it('submits contact form and sends email', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
Mail::fake();
|
|
|
|
$data = [
|
|
'first_name' => 'Jean',
|
|
'last_name' => 'Dupont',
|
|
'email' => 'jean.dupont@gmail.com',
|
|
'subject' => 'Question',
|
|
'order_ref' => '123456',
|
|
'message' => 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
|
'ok_cgv' => '1',
|
|
];
|
|
|
|
// WHEN
|
|
$response = $this->from('/contactez-nous')->post('/contactez-nous', $data);
|
|
|
|
// THEN
|
|
$response->assertRedirect('/contactez-nous');
|
|
$response->assertSessionHas('status', true);
|
|
Mail::assertQueued(SendContactMail::class);
|
|
});
|
|
|
|
it('submits contact form with an attachment uploaded via StorageService', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
Mail::fake();
|
|
$file = UploadedFile::fake()->create('piece.pdf', 100, 'application/pdf');
|
|
$this->mock(StorageService::class, function ($mock): void {
|
|
$mock->shouldReceive('storeUploadedOntoDisk')
|
|
->once()
|
|
->with(
|
|
\Mockery::type(UploadedFile::class),
|
|
\Mockery::on(fn (DiskName $disk): bool => $disk->value === PrivateS3Disk::DISK_NAME),
|
|
)
|
|
->andReturn(new StorageFilePath('contact-attachments/piece.pdf'));
|
|
});
|
|
|
|
$data = [
|
|
'first_name' => 'Jean',
|
|
'last_name' => 'Dupont',
|
|
'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' => $file,
|
|
'ok_cgv' => '1',
|
|
];
|
|
|
|
// WHEN
|
|
$response = $this->from('/contactez-nous')->post('/contactez-nous', $data);
|
|
|
|
// THEN
|
|
$response->assertRedirect('/contactez-nous');
|
|
$response->assertSessionHas('status', true);
|
|
Mail::assertQueued(
|
|
SendContactMail::class,
|
|
fn (SendContactMail $mail): bool => $mail->data->file?->path === 'contact-attachments/piece.pdf'
|
|
);
|
|
});
|
|
|
|
it('redirects with validation errors when form data is invalid', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$this->seed(ContentSeeder::class);
|
|
View::share('PageList', Page::getCached()
|
|
->mapWithKeys(fn ($page) => [
|
|
$page->slug->value() => $page,
|
|
]));
|
|
Cache::flush();
|
|
|
|
// WHEN
|
|
$response = $this->from('/contactez-nous')->post('/contactez-nous', []);
|
|
|
|
// THEN
|
|
$response->assertStatus(302);
|
|
$response->assertSessionHasErrors(['first_name', 'last_name', 'email', 'subject', 'message', 'ok_cgv']);
|
|
});
|
|
});
|