<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\CoreBundle\Tests\Entity;

use Ferienpass\CoreBundle\Entity\ConsentForm;
use PHPUnit\Framework\TestCase;

final class ConsentFormTest extends TestCase
{
    public function testDefaultCompulsoryIsCompulsory(): void
    {
        $form = new ConsentForm();

        self::assertSame('compulsory', $form->getCompulsory());
    }

    public function testSetText(): void
    {
        $form = new ConsentForm();
        $form->setText('hello');

        self::assertSame('hello', $form->getText());
    }

    public function testSetTextCreatesNewConsentText(): void
    {
        $form = new ConsentForm();
        $form->setText('text');

        self::assertNotNull($form->getConsentText());
        self::assertSame('text', $form->getConsentText()->getText());
    }

    public function testSetTextDoesNotCreateNewConsentTextWhenSame(): void
    {
        $form = new ConsentForm();
        $form->setText('same text');
        $original = $form->getConsentText();

        $form->setText('same text');

        self::assertSame($original, $form->getConsentText());
    }

    public function testSetTextNullClearsConsentText(): void
    {
        $form = new ConsentForm();
        $form->setText('some text');
        $form->setText(null);

        self::assertNull($form->getText());
        self::assertNull($form->getConsentText());
    }

    public function testSetCompulsory(): void
    {
        $form = new ConsentForm();
        $form->setCompulsory('opt-in');

        self::assertSame('opt-in', $form->getCompulsory());
    }

    public function testSetCompulsoryNullDoesNothing(): void
    {
        $form = new ConsentForm();
        $form->setCompulsory(null);

        self::assertSame('compulsory', $form->getCompulsory()); // unchanged
    }

    public function testSetTitle(): void
    {
        $form = new ConsentForm();
        $form->setTitle('My Consent Title');

        self::assertSame('My Consent Title', $form->getTitle());
    }

    public function testGetTitleNullByDefault(): void
    {
        $form = new ConsentForm();

        self::assertNull($form->getTitle());
    }

    public function testSetSubject(): void
    {
        $form = new ConsentForm();
        $form->setSubject('user');

        self::assertSame('user', $form->getSubject());
    }

    public function testDefaultSubjectIsParticipant(): void
    {
        $form = new ConsentForm();

        self::assertSame('participant', $form->getSubject());
    }

    public function testSignaturesInitiallyEmpty(): void
    {
        $form = new ConsentForm();

        self::assertCount(0, $form->getSignatures());
    }

    public function testSetConsentText(): void
    {
        $form = new ConsentForm();
        $text = new \Ferienpass\CoreBundle\Entity\ConsentText('some text');
        $form->setConsentText($text);

        self::assertSame($text, $form->getConsentText());
    }

    public function testSetOffer(): void
    {
        $form = new ConsentForm();
        $offer = $this->createStub(\Ferienpass\CoreBundle\Entity\Offer\OfferInterface::class);
        $form->setOffer($offer);

        self::assertSame($offer, $form->getOffer());
    }

    public function testGetOfferNullByDefault(): void
    {
        $form = new ConsentForm();

        self::assertNull($form->getOffer());
    }

    public function testSetEdition(): void
    {
        $form = new ConsentForm();
        $edition = $this->createStub(\Ferienpass\CoreBundle\Entity\Edition::class);
        $form->setEdition($edition);

        self::assertSame($edition, $form->getEdition());
    }

    public function testGetEditionNullByDefault(): void
    {
        $form = new ConsentForm();

        self::assertNull($form->getEdition());
    }

    public function testSetTextChangingTextCreatesNewConsentText(): void
    {
        $form = new ConsentForm();
        $form->setText('first text');
        $first = $form->getConsentText();

        $form->setText('second text');
        $second = $form->getConsentText();

        self::assertNotSame($first, $second);
        self::assertSame('second text', $form->getText());
    }
}
