<?php

declare(strict_types=1);

namespace App\Tests\Unit\Doctrine\Booking;

use App\Doctrine\Booking\CurrentCoursesExtension;
use App\Entity\Booking\Course;
use App\Tests\Unit\Doctrine\ExtensionTestCase;

class CurrentCoursesExtensionTest extends ExtensionTestCase
{
    private CurrentCoursesExtension $extension;

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

    public function testSupportsReturnsTrueForCourseClass(): void
    {
        $this->assertTrue($this->extension->supports(Course::class, null));
    }

    public function testSupportsReturnsFalseForOtherClasses(): void
    {
        $this->assertFalse($this->extension->supports('OtherClass', null));
    }

    public function testAddWhere(): void
    {
        $this->user->method('getOrganization')->willReturn($this->organization);

        $this->security->method('getUser')->willReturn($this->user);

        $this->queryBuilder->method('getRootAliases')->willReturn(['a']);

        $this->queryBuilder
            ->expects(self::exactly(2))
            ->method('andWhere')
            ->willReturnMap([
                ['a.discr = :discr', $this->queryBuilder],
                ['a.organization = :organization', $this->queryBuilder],
            ]);

        $this->queryBuilder
            ->expects(self::exactly(2))
            ->method('setParameter')
            ->willReturnMap([
                ['discr', 'course', null, $this->queryBuilder],
                ['organization', $this->organization, null, $this->queryBuilder],
            ]);

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

    public function testAddWhereMissingUser(): void
    {
        $this->security->method('getUser')->willReturn(null);

        $this->user->expects(self::never())->method('getOrganization');

        $this->queryBuilder->expects(self::never())->method('getRootAliases');
        $this->queryBuilder->expects(self::never())->method('andWhere');
        $this->queryBuilder->expects(self::never())->method('setParameter');

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

    public function testAddWhereMissingOrganization(): void
    {
        $this->security->method('getUser')->willReturn($this->user);

        $this->user->expects(self::once())->method('getOrganization')->willReturn(null);

        $this->queryBuilder->expects(self::never())->method('getRootAliases');
        $this->queryBuilder->expects(self::never())->method('andWhere');
        $this->queryBuilder->expects(self::never())->method('setParameter');

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