<?php

declare(strict_types=1);

namespace App\Tests\Functional\Module\Core;

use App\Module\Core\Domain\Entity\User;
use App\Shared\Domain\Contract\NotifierInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @internal
 */
final class NotifierTest extends KernelTestCase
{
    public function testNotifyPersistsANotificationForTheUser(): void
    {
        self::bootKernel();
        $em       = self::getContainer()->get(EntityManagerInterface::class);
        $notifier = self::getContainer()->get(NotifierInterface::class);

        $user = $em->getRepository(User::class)->findOneBy(['username' => 'alice']);
        self::assertNotNull($user);

        $title = 'Titre-'.uniqid('', true);
        $notifier->notify($user, 'task_assigned', $title, 'Message');

        $count = (int) $em->createQuery(
            'SELECT COUNT(n.id) FROM App\Module\Core\Domain\Entity\Notification n WHERE n.user = :u AND n.title = :t'
        )->setParameter('u', $user)->setParameter('t', $title)->getSingleScalarResult();

        self::assertSame(1, $count);
    }
}
