<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Repository;

use Chill\PersonBundle\DataFixtures\Helper\RandomPersonHelperTrait;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\TicketBundle\Repository\PersonTicketACLAwareRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @internal
 *
 * @coversNothing
 */
class PersonTicketACLAwareRepositoryTest extends KernelTestCase
{
    use RandomPersonHelperTrait;

    private PersonTicketACLAwareRepository $repository;
    private EntityManagerInterface $entityManager;

    protected function setUp(): void
    {
        self::bootKernel();
        $this->repository = self::getContainer()->get(PersonTicketACLAwareRepository::class);
        $this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
    }

    public function testFindPersonPreviouslyAssociatedWithCallerWithPerson()
    {
        $person = $this->getRandomPerson(self::getContainer()->get(EntityManagerInterface::class));

        $actual = $this->repository->findPersonPreviouslyAssociatedWithCaller($person);

        self::assertIsList($actual);
    }

    public function testFindPersonPreviouslyAssociatedWithCallerWithThirdParty()
    {
        $thirdParty = $this->entityManager->createQuery(
            sprintf('SELECT t FROM %s t', ThirdParty::class)
        )
            ->setMaxResults(1)
            ->getSingleResult();

        if (null === $thirdParty) {
            throw new \RuntimeException('the third party table seems to be empty');
        }

        $actual = $this->repository->findPersonPreviouslyAssociatedWithCaller($thirdParty);

        self::assertIsList($actual);
    }
}
