<?php

namespace Doctrine\Bundle\PHPCRBundle\Tests\Functional\Form\Type;

use Doctrine\Bundle\PHPCRBundle\Form\Type\DocumentType;
use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\DataFixtures\PHPCR\LoadData;
use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\Document\ReferrerDocument;
use Doctrine\Bundle\PHPCRBundle\Tests\Fixtures\App\Document\TestDocument;
use Doctrine\Bundle\PHPCRBundle\Tests\Functional\BaseTestCase;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Twig\Environment;

class DocumentTypeTest extends BaseTestCase
{
    private DocumentManagerInterface $dm;
    private ReferrerDocument $referrer;

    public function setUp(): void
    {
        self::bootKernel();

        $repositoryManager = $this->getRepositoryManager();
        $repositoryManager->loadFixtures([LoadData::class]);
        $this->dm = $repositoryManager->getDocumentManager();
        $document = $this->dm->find(null, '/test/doc');
        $this->assertNotNull($document, 'fixture loading not working');
        $this->referrer = $this->dm->find(null, '/test/ref');
        $this->assertNotNull($this->referrer, 'fixture loading not working');
    }

    private function createFormBuilder(ReferrerDocument $data): FormBuilderInterface
    {
        return self::getTestContainer()->get('form.factory')->createBuilder(FormType::class, $data, []);
    }

    /**
     * Render a form and return the HTML.
     */
    private function renderForm(FormBuilderInterface $formBuilder): string
    {
        $formView = $formBuilder->getForm()->createView();
        /** @var Environment $twig */
        $twig = self::getTestContainer()->get('twig');

        return $twig->render('form.html.twig', ['form' => $formView]);
    }

    public function testUnfiltered(): void
    {
        $formBuilder = $this->createFormBuilder($this->referrer);

        $formBuilder
            ->add('single', DocumentType::class, [
                'class' => TestDocument::class,
            ])
        ;

        $html = $this->renderForm($formBuilder);
        $this->assertStringContainsString('<select id="form_single" name="form[single]"', $html);
        $this->assertStringContainsString('<option value="/test/doc"', $html);
    }

    public function testFiltered(): void
    {
        $qb = $this->dm
            ->getRepository(TestDocument::class)
            ->createQueryBuilder('e')
        ;
        $this->assertInstanceOf(QueryBuilder::class, $qb);
        $qb->where()->eq()->field('e.text')->literal('thiswillnotmatch');
        $formBuilder = $this->createFormBuilder($this->referrer);

        $formBuilder
            ->add('single', DocumentType::class, [
                'class' => TestDocument::class,
                'query_builder' => $qb,
            ])
        ;

        $html = $this->renderForm($formBuilder);
        $this->assertStringContainsString('<select id="form_single" name="form[single]"', $html);
        $this->assertStringNotContainsString('<option', $html);
    }
}
