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

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Ferienpass\CoreBundle\Entity\Offer\Base;
use Ferienpass\CoreBundle\EventListener\Doctrine\Offer\SetHostListener;
use Ferienpass\CoreBundle\Repository\HostRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;

final class DoctrineOfferSetHostListenerTest extends TestCase
{
    public function testReturnsEarlyWhenOfferAlreadyHasHosts(): void
    {
        $hosts = $this->createMock(HostRepository::class);
        $hosts->expects(self::never())->method('findByUser');

        $security = $this->createMock(Security::class);
        $security->expects(self::never())->method('getUser');

        $listener = new SetHostListener($hosts, $security);

        $offer = new Base();
        $offer->addHost(new \Ferienpass\CoreBundle\Entity\Host());

        $args = new PrePersistEventArgs($offer, $this->createMock(EntityManagerInterface::class));
        $listener->prePersist($offer, $args);
    }

    public function testReturnsEarlyWhenUserIsNotFerienpassUser(): void
    {
        $hosts = $this->createMock(HostRepository::class);
        $hosts->expects(self::never())->method('findByUser');

        $security = $this->createMock(Security::class);
        $security->method('getUser')->willReturn(null);

        $listener = new SetHostListener($hosts, $security);

        $offer = new Base();

        $args = new PrePersistEventArgs($offer, $this->createMock(EntityManagerInterface::class));
        $listener->prePersist($offer, $args);
    }
}
