<?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\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Ferienpass\CoreBundle\Entity\ConsentForm;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Repository\ConsentFormRepository;
use PHPUnit\Framework\TestCase;

class ConsentFormRepositoryTest extends TestCase
{
    public function testFindByOfferOrEditionWithNullEdition(): void
    {
        $registry = $this->createStub(ManagerRegistry::class);
        $entityManager = $this->createStub(EntityManagerInterface::class);
        $registry->method('getManagerForClass')->willReturn($entityManager);

        $metadata = new ClassMetadata(ConsentForm::class);
        $entityManager->method('getClassMetadata')->willReturn($metadata);

        $queryBuilder = $this->createStub(QueryBuilder::class);
        $entityManager->method('createQueryBuilder')->willReturn($queryBuilder);

        $expr = $this->createStub(Expr::class);
        $queryBuilder->method('expr')->willReturn($expr);

        $query = $this->createStub(Query::class);
        $queryBuilder->method('getQuery')->willReturn($query);
        $query->method('getResult')->willReturn([]);

        $queryBuilder->method('select')->willReturnSelf();
        $queryBuilder->method('from')->willReturnSelf();
        $queryBuilder->method('where')->willReturnSelf();
        $queryBuilder->method('setParameter')->willReturnSelf();

        $repository = new ConsentFormRepository($registry);

        $offer = $this->createStub(OfferInterface::class);

        // This should not throw a TypeError
        $results = $repository->findByOfferOrEdition($offer, null);

        $this->assertIsArray($results);
    }
}
