<?php

declare(strict_types=1);

namespace App\Tests\Doctrine\Core;

use App\Doctrine\Core\CurrentUserNotificationUserExtension;
use App\Entity\Access\Access;
use App\Entity\Core\NotificationUser;
use App\Tests\Unit\Doctrine\ExtensionTestCase;

class CurrentUserNotificationUserExtensionTest extends ExtensionTestCase
{
    private $extension;

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

    public function testSupportsReturnsTrueForNotificationUserClass()
    {
        $this->assertTrue($this->extension->supports(NotificationUser::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->once())
            ->method('andWhere')
            ->with('o.access = :current_access')
            ->willReturn($this->queryBuilder);

        $this->queryBuilder->expects($this->once())
            ->method('setParameter')
            ->with('current_access', $this->user)
            ->willReturn($this->queryBuilder);

        $this->invokeAddWhere($this->extension, $this->queryBuilder, NotificationUser::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, NotificationUser::class, null);
    }
}
