<?php

namespace App\Tests\Controller;

use App\Service\MailerService;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ContactControllerTest extends WebTestCase
{
    public function testContactPageReturnsSuccess(): void
    {
        $client = static::createClient();
        $client->request('GET', '/contact');

        self::assertResponseIsSuccessful();
    }

    public function testContactFormSubmitRedirectsWithSuccess(): void
    {
        $client = static::createClient();

        $mailer = $this->createMock(MailerService::class);
        $mailer->expects(self::once())->method('sendEmail');
        static::getContainer()->set(MailerService::class, $mailer);

        $client->request('POST', '/contact', [
            'name' => 'Dupont',
            'surname' => 'Jean',
            'email' => 'jean@exemple.fr',
            'message' => 'Bonjour, je voudrais des informations.',
        ]);

        self::assertResponseRedirects('/contact');
        $client->followRedirect();
        self::assertSelectorExists('.font-black.text-sm');
    }

    public function testContactFormEmptyFieldsRedirectsWithError(): void
    {
        $client = static::createClient();
        $client->request('POST', '/contact', [
            'name' => '',
            'surname' => 'Jean',
            'email' => 'jean@exemple.fr',
            'message' => 'Bonjour',
        ]);

        self::assertResponseRedirects('/contact');
    }

    public function testContactFormAllEmptyRedirectsWithError(): void
    {
        $client = static::createClient();
        $client->request('POST', '/contact', [
            'name' => '',
            'surname' => '',
            'email' => '',
            'message' => '',
        ]);

        self::assertResponseRedirects('/contact');
    }

    public function testContactFormWhitespaceOnlyRedirectsWithError(): void
    {
        $client = static::createClient();
        $client->request('POST', '/contact', [
            'name' => '   ',
            'surname' => 'Jean',
            'email' => 'jean@exemple.fr',
            'message' => 'Bonjour',
        ]);

        self::assertResponseRedirects('/contact');
    }
}
