<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Form;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Site\ProductReviewSubmissionType;
use CpCreation\VitiCore\Marketing\DTO\ProductReviewSubmission;
use Symfony\Component\Form\Test\TypeTestCase;

final class ProductReviewSubmissionTypeTest extends TypeTestCase
{
    public function testOnlyAsksForTheProductRatingAndComment(): void
    {
        $form = $this->factory->create(ProductReviewSubmissionType::class, null, [
            'product_choices' => ['Produit' => 'product-id'],
        ]);

        self::assertFalse($form->has('authorDisplayName'));
        self::assertFalse($form->has('title'));
        self::assertTrue($form->has('productId'));
        self::assertTrue($form->has('rating'));
        self::assertTrue($form->has('comment'));
        self::assertFalse($form->has('captcha'));
    }

    public function testMapsEmptyRequiredFieldsWithoutATypeError(): void
    {
        $form = $this->factory->create(ProductReviewSubmissionType::class, null, [
            'product_choices' => ['Produit' => 'product-id'],
        ]);

        $form->submit([
            'productId' => '',
            'rating' => '5',
            'comment' => '',
        ]);

        $submission = $form->getData();

        self::assertInstanceOf(ProductReviewSubmission::class, $submission);
        self::assertNull($submission->productId);
        self::assertNull($submission->comment);
    }

    public function testBusinessEligibleReviewDoesNotRequireAnAntiRobotProof(): void
    {
        $form = $this->factory->create(ProductReviewSubmissionType::class, null, [
            'product_choices' => ['Produit' => 'product-id'],
        ]);

        $form->submit([
            'productId' => 'product-id',
            'rating' => '5',
            'comment' => 'Très bon produit.',
        ]);

        self::assertTrue($form->isValid());
    }
}
