<?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\CmsBundle\Tests\Application\ExclusionReasons;

use Ferienpass\CmsBundle\Application\ExclusionReasonEvent;
use Ferienpass\CmsBundle\Application\ExclusionReasons\NoAccessCode;
use Ferienpass\CmsBundle\Application\ExclusionReasons\NoAccessCodeListener;
use Ferienpass\CoreBundle\ApplicationSystem\ApplicationSystemInterface;
use Ferienpass\CoreBundle\Entity\AccessCodeStrategy;
use Ferienpass\CoreBundle\Entity\EditionTask;
use Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\Entity\Participant\ParticipantInterface;
use PHPUnit\Framework\TestCase;

final class NoAccessCodeListenerTest extends TestCase
{
    public function testDoesNothingWhenNoAccessCodeStrategy(): void
    {
        $task = $this->createMock(EditionTask::class);
        $task->method('getAccessCodeStrategy')->willReturn(null);

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

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

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

        $this->addToAssertionCount(1);
    }

    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 NoAccessCodeListener();
        $listener($event);

        $this->addToAssertionCount(1);
    }

    public function testDoesNothingWhenParticipantIsEnabled(): void
    {
        $participant = $this->createMock(ParticipantInterface::class);

        $strategy = $this->createMock(AccessCodeStrategy::class);
        $strategy->method('isEnabledParticipant')->willReturn(true);

        $task = $this->createMock(EditionTask::class);
        $task->method('getAccessCodeStrategy')->willReturn($strategy);

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

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

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

        $this->addToAssertionCount(1);
    }

    public function testThrowsNoAccessCodeWhenParticipantNotEnabled(): void
    {
        $participant = $this->createMock(ParticipantInterface::class);

        $strategy = $this->createMock(AccessCodeStrategy::class);
        $strategy->method('isEnabledParticipant')->willReturn(false);

        $task = $this->createMock(EditionTask::class);
        $task->method('getAccessCodeStrategy')->willReturn($strategy);

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

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

        $this->expectException(NoAccessCode::class);

        $listener = new NoAccessCodeListener();
        $listener($event);
    }
}
