<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\Form\CompoundType;

use Ferienpass\AdminBundle\Form\CompoundType\PostalAddressesType;
use Ferienpass\CoreBundle\Entity\PostalAddress;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PostalAddressesTypeTest extends TestCase
{
    public function testDeleteEmptyClosure(): void
    {
        $requestStack = $this->createStub(RequestStack::class);
        $requestStack->method('getCurrentRequest')->willReturn(new Request());

        $type = new PostalAddressesType($requestStack);
        $resolver = new OptionsResolver();
        $type->configureOptions($resolver);

        $options = $resolver->resolve();
        $deleteEmpty = $options['delete_empty'];

        $this->assertIsCallable($deleteEmpty);

        $postalAddress = new PostalAddress();
        
        $this->assertTrue($deleteEmpty($postalAddress));

        $postalAddress->setStreet('Street');
        $this->assertFalse($deleteEmpty($postalAddress));

        $postalAddress->setStreet(null);
        $postalAddress->setPostalCode('12345');
        $this->assertFalse($deleteEmpty($postalAddress));

        $postalAddress->setPostalCode(null);
        $postalAddress->setCity('City');
        $this->assertFalse($deleteEmpty($postalAddress));
    }

    public function testDeleteEmptyClosureWithLiveComponentAdminRoute(): void
    {
        $requestStack = $this->createStub(RequestStack::class);
        $request = new Request();
        $request->attributes->set('_route', 'live_component_admin');
        $requestStack->method('getCurrentRequest')->willReturn($request);

        $type = new PostalAddressesType($requestStack);
        $resolver = new OptionsResolver();
        $type->configureOptions($resolver);

        $options = $resolver->resolve();
        $deleteEmpty = $options['delete_empty'];

        $postalAddress = new PostalAddress();
        
        $this->assertFalse($deleteEmpty($postalAddress));
    }

    public function testGetParent(): void
    {
        $requestStack = $this->createStub(RequestStack::class);
        $type = new PostalAddressesType($requestStack);

        $this->assertSame(\Symfony\UX\LiveComponent\Form\Type\LiveCollectionType::class, $type->getParent());
    }
}
