<?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\Entity\Attendance;
use Ferienpass\CoreBundle\Entity\Edition;
use Ferienpass\CoreBundle\Entity\Offer\Base;
use Ferienpass\CoreBundle\Entity\Participant\BaseParticipant;
use Ferienpass\CoreBundle\Message\DeleteAttendance as DeleteAttendanceCommand;
use Ferienpass\CoreBundle\MessageHandler\DeleteAttendance;
use Ferienpass\CoreBundle\Repository\AttendanceRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Uid\Uuid;

final class DeleteAttendanceTest extends TestCase
{
    public function testDoesNothingWhenAttendanceNotFound(): void
    {
        $repo = $this->createMock(AttendanceRepository::class);
        $repo->method('findOneBy')->willReturn(null);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects(self::never())->method('remove');

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

        $handler = new DeleteAttendance($repo, $em, $bus);
        $handler(new DeleteAttendanceCommand('999'), Uuid::v7());
    }

    public function testRemovesAttendanceAndDispatchesMessages(): void
    {
        $edition = new Edition();
        $offer = new Base();
        $offer->setEdition($edition);

        $participant = new BaseParticipant();
        $attendance = new Attendance($offer, $participant);

        $repo = $this->createMock(AttendanceRepository::class);
        $repo->method('findOneBy')->willReturn($attendance);

        $em = $this->createMock(EntityManagerInterface::class);
        $em->expects(self::once())->method('remove')->with($attendance);

        $bus = $this->createMock(MessageBusInterface::class);
        $bus->expects(self::exactly(2))->method('dispatch')->willReturn(new Envelope(new \stdClass()));

        $handler = new DeleteAttendance($repo, $em, $bus);
        $handler(new DeleteAttendanceCommand(Uuid::v7()->toRfc4122()), Uuid::v7());
    }
}
