<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\EventListener;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Event\UserAccountVerifiedEvent;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\EventListener\AttachGuestOrdersOnAccountVerificationListener;
use CpCreation\VitiCore\Cart\Repository\CartRepository;
use CpCreation\VitiCore\User\Model\User;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

final class AttachGuestOrdersOnAccountVerificationListenerTest extends TestCase
{
    public function testItAttachesMatchingGuestOrdersWhenTheAccountIsVerified(): void
    {
        $user = new User();
        $user->setEmail('client@example.com');
        $cartRepository = $this->createMock(CartRepository::class);
        $cartRepository
            ->expects(self::once())
            ->method('attachGuestCompletedOrdersToUserByEmail')
            ->with($user)
            ->willReturn(2);

        $listener = new AttachGuestOrdersOnAccountVerificationListener($cartRepository);

        $listener(new UserAccountVerifiedEvent($user));
    }

    public function testItListensToAccountVerificationEvents(): void
    {
        $attributes = (new \ReflectionClass(AttachGuestOrdersOnAccountVerificationListener::class))
            ->getAttributes(AsEventListener::class);

        self::assertCount(1, $attributes);
        self::assertSame(
            UserAccountVerifiedEvent::class,
            $attributes[0]->newInstance()->event,
        );
    }
}
