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

use Doctrine\ORM\EntityManagerInterface;
use Ferienpass\CoreBundle\Message\CommitAttendances as CommitAttendancesCommand;
use Ferienpass\CoreBundle\Message\FetchPmPaymentStatus as FetchPmPaymentStatusCommand;
use Ferienpass\CoreBundle\MessageHandler\CommitAttendances;
use Ferienpass\CoreBundle\MessageHandler\FetchPmPaymentStatus;
use Ferienpass\CoreBundle\Repository\AttendanceRepository;
use Ferienpass\CoreBundle\Repository\EditionRepository;
use Ferienpass\CoreBundle\Repository\PaymentRepository;
use Ferienpass\CoreBundle\Settings\IntegrationsManager;
use FOS\HttpCacheBundle\CacheManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Workflow\WorkflowInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class MoreHandlersTest extends TestCase
{
    // ---- CommitAttendances ----

    public function testCommitAttendancesDoesNothingWhenEditionNotFound(): void
    {
        $editions = $this->createMock(EditionRepository::class);
        $editions->method('find')->willReturn(null);

        $attendances = $this->createMock(AttendanceRepository::class);

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(self::never())->method('dispatch');

        $cacheManager = $this->createMock(CacheManager::class);

        $handler = new CommitAttendances($attendances, $editions, $bus, $cacheManager, 'demo');
        $handler(new CommitAttendancesCommand(Uuid::v7()->toRfc4122(), [Uuid::v7()->toRfc4122()], true), Uuid::v7());
    }

    // ---- FetchPmPaymentStatus ----

    public function testFetchPmPaymentStatusReturnsEarlyWhenEmptyTxid(): void
    {
        $client = $this->createMock(HttpClientInterface::class);
        $client->expects(self::never())->method('request');

        $em = $this->createMock(EntityManagerInterface::class);
        $payments = $this->createMock(PaymentRepository::class);
        $workflow = $this->createMock(WorkflowInterface::class);
        $integrations = $this->createMock(IntegrationsManager::class);

        $handler = new FetchPmPaymentStatus($client, $em, $payments, $workflow, $integrations);
        $handler(new FetchPmPaymentStatusCommand(''));
    }

    public function testFetchPmPaymentStatusReturnsEarlyWhenZeroTxid(): void
    {
        $client = $this->createMock(HttpClientInterface::class);
        $client->expects(self::never())->method('request');

        $em = $this->createMock(EntityManagerInterface::class);
        $payments = $this->createMock(PaymentRepository::class);
        $workflow = $this->createMock(WorkflowInterface::class);
        $integrations = $this->createMock(IntegrationsManager::class);

        $handler = new FetchPmPaymentStatus($client, $em, $payments, $workflow, $integrations);
        $handler(new FetchPmPaymentStatusCommand('0'));
    }
}
