<?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\Consent;
use Ferienpass\CoreBundle\Entity\ConsentForm;
use Ferienpass\CoreBundle\Entity\User;
use Ferienpass\CoreBundle\Repository\ConsentRepository;
use PHPUnit\Framework\TestCase;

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

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

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

        $expr = new Expr();
        $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('addSelect')->willReturnSelf();
        $queryBuilder->method('from')->willReturnSelf();
        $queryBuilder->method('leftJoin')->willReturnSelf();
        $queryBuilder->method('where')->willReturnSelf();
        $queryBuilder->method('andWhere')->willReturnSelf();
        $queryBuilder->method('setParameter')->willReturnSelf();

        $repository = new ConsentRepository($registry);

        $form = $this->createStub(ConsentForm::class);
        $form->method('getConsentText')->willReturn(null);

        $user = $this->createStub(User::class);

        // Verification of method calls
        $queryBuilder->expects($this->atLeastOnce())->method('addSelect')->with($this->logicalOr('f', 't'));
        $queryBuilder->expects($this->atLeastOnce())->method('leftJoin')->with($this->logicalOr('c.consentForm', 'c.consentText'), $this->logicalOr('f', 't'));

        $results = $repository->findSignatures([$form], $user, []);

        $this->assertIsArray($results);
    }

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

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

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

        $expr = new Expr();
        $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('addSelect')->willReturnSelf();
        $queryBuilder->method('from')->willReturnSelf();
        $queryBuilder->method('leftJoin')->willReturnSelf();
        $queryBuilder->method('where')->willReturnSelf();
        $queryBuilder->method('andWhere')->willReturnSelf();
        $queryBuilder->method('setParameter')->willReturnSelf();

        $repository = new ConsentRepository($registry);

        $consentText = $this->createStub(\Ferienpass\CoreBundle\Entity\ConsentText::class);
        $consentText->method('getHash')->willReturn('abc');

        $form = $this->createStub(ConsentForm::class);
        $form->method('getConsentText')->willReturn($consentText);

        $user = $this->createStub(User::class);

        // Verification of method calls
        $queryBuilder->expects($this->exactly(3))->method('setParameter')->with($this->logicalOr('forms', 'hashes', 'user'), $this->anything())->willReturnSelf();

        $results = $repository->findSignatures([$form], $user, []);

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