<?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\AdminBundle\Tests\Form;

use Ferienpass\AdminBundle\Form\CompoundType\PeriodType;
use Symfony\Component\Form\Test\TypeTestCase;

class PeriodTypeTest extends TypeTestCase
{
    public function testSubmittingNumberAndUnitYieldsThePeriodString(): void
    {
        $form = $this->factory->create(PeriodType::class, null, ['units' => ['hours', 'days', 'weeks']]);

        $form->submit(['number' => '3', 'unit' => 'days']);

        $this->assertTrue($form->isSynchronized());
        $this->assertSame('3 days', $form->getData());
    }

    public function testAnEmptyNumberMeansNotSet(): void
    {
        $form = $this->factory->create(PeriodType::class, '3 days', ['units' => ['days', 'weeks']]);

        $form->submit(['number' => '', 'unit' => 'days']);

        $this->assertTrue($form->isSynchronized());
        $this->assertNull($form->getData());
    }

    public function testAStoredPeriodPrefillsNumberAndUnit(): void
    {
        $form = $this->factory->create(PeriodType::class, '50 weeks', ['units' => ['weeks', 'months', 'years']]);

        $this->assertSame(50, $form->get('number')->getData());
        $this->assertSame('weeks', $form->get('unit')->getData());
    }

    public function testNullPrefillsTheFirstUnit(): void
    {
        $form = $this->factory->create(PeriodType::class, null, ['units' => ['months', 'years']]);

        $this->assertNull($form->get('number')->getData());
        $this->assertSame('months', $form->get('unit')->getData());
    }
}
