<?php

declare(strict_types=1);

namespace App\Tests\Doctrine\Core;

use App\Doctrine\Core\CurrentUserNotificationExtension;
use App\Entity\Access\Access;
use App\Entity\Core\Notification;
use App\Tests\Unit\Doctrine\ExtensionTestCase;

class CurrentUserNotificationExtensionTest extends ExtensionTestCase
{
    private $extension;

    protected function setUp(): void
    {
        parent::setUp();
        $this->extension = new CurrentUserNotificationExtension($this->security);
    }

    public function testSupportsReturnsTrueForNotificationClass()
    {
        $this->assertTrue($this->extension->supports(Notification::class, null));
    }

    public function testSupportsReturnsFalseForOtherClasses()
    {
        $classes = ['OtherClass', Access::class];

        foreach ($classes as $class) {
            $this->assertFalse($this->extension->supports($class, null));
        }
    }

    public function testAddWhere()
    {
        $this->security->method('getUser')->willReturn($this->user);
        $this->queryBuilder->method('getRootAliases')->willReturn(['o']);
        $this->queryBuilder->expects($this->exactly(2))
            ->method('andWhere')
            ->withConsecutive(
                ['o.recipientAccess = :current_access'],
                ['o.availabilityDate IS NULL or o.availabilityDate <= :today']
            )
            ->willReturn($this->queryBuilder);

        $this->queryBuilder->expects($this->exactly(2))
            ->method('setParameter')
            ->withConsecutive(
                ['current_access', $this->user],
                ['today', $this->isInstanceOf(\DateTime::class)]
            )
            ->willReturn($this->queryBuilder);

        // Exécuter la méthode
        $this->invokeAddWhere($this->extension, $this->queryBuilder, Notification::class, null);
    }

    public function testAddWhereWithNullUser()
    {
        $this->security->method('getUser')->willReturn(null);
        $this->queryBuilder->method('getRootAliases')->willReturn(['o']);
        $this->queryBuilder->expects($this->never())
            ->method('andWhere');

        $this->queryBuilder->expects($this->never())
            ->method('setParameter');

        $this->invokeAddWhere($this->extension, $this->queryBuilder, Notification::class, null);
    }
}
