<?php

declare(strict_types=1);

namespace Ferienpass\CmsBundle\Tests\Application\ExclusionReasons;

use Doctrine\Common\Collections\ArrayCollection;
use Ferienpass\CmsBundle\Application\ExclusionReasonEvent;
use Ferienpass\CmsBundle\Application\ExclusionReasons\LimitReached;
use Ferienpass\CmsBundle\Application\ExclusionReasons\LimitReachedListener;
use Ferienpass\CoreBundle\ApplicationSystem\ApplicationSystemInterface;
use Ferienpass\CoreBundle\Entity\EditionTask;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface;
use PHPUnit\Framework\TestCase;

final class LimitReachedListenerTest extends TestCase
{
    public function testDoesNothingWhenNoTask(): void
    {
        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn(null);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $this->createMock(ParticipantInterface::class),
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testDoesNothingWhenNoMaxApplications(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(null);
        $task->method('isSkipMaxApplications')->willReturn(false);

        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $this->createMock(ParticipantInterface::class),
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testDoesNothingWhenSkipMaxApplications(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(3);
        $task->method('isSkipMaxApplications')->willReturn(true);

        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $this->createMock(ParticipantInterface::class),
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testDoesNothingWhenBelowLimit(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getMaxApplications')->willReturn(3);
        $task->method('isSkipMaxApplications')->willReturn(false);
        $task->method('getId')->willReturn(1);

        $participant = $this->createMock(ParticipantInterface::class);
        $participant->method('getAttendancesNotHidden')->willReturn(new ArrayCollection());
        $participant->method('getFirstname')->willReturn('Max');

        $appSystem = $this->createMock(ApplicationSystemInterface::class);
        $appSystem->method('getTask')->willReturn($task);

        $event = new ExclusionReasonEvent(
            $this->createMock(OfferInterface::class),
            $participant,
            $appSystem
        );

        $listener = new LimitReachedListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }
}
