<?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 Ferienpass\CoreBundle\Entity\Offer\OfferInterface;
use Ferienpass\CoreBundle\EventListener\Doctrine\Offer\VariantListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class DoctrineOfferListenersTest extends TestCase
{
    public function testVariantListenerDoesNotDispatchWhenNoVariants(): void
    {
        $offer = $this->createMock(OfferInterface::class);
        $offer->method('hasVariants')->willReturn(false);

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

        $listener = new VariantListener($bus);
        $listener->postUpdate($offer);
    }

    public function testVariantListenerDispatchesWhenOfferHasVariants(): void
    {
        $offer = $this->createMock(OfferInterface::class);
        $offer->method('hasVariants')->willReturn(true);
        $offer->method('getId')->willReturn(42);

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

        $listener = new VariantListener($bus);
        $listener->postUpdate($offer);
    }
}
