<?php

declare(strict_types=1);

namespace App\Tests\Unit\Doctrine\Booking;

use App\Doctrine\Booking\AttendanceBookingReasonExtension;
use App\Entity\Booking\AttendanceBookingReason;
use App\Tests\Unit\Doctrine\ExtensionTestCase;

class AttendanceBookingReasonExtensionTest extends ExtensionTestCase
{
    private AttendanceBookingReasonExtension $extension;

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

    public function testSupportsReturnsTrueForAttendanceBookingReasonClass(): void
    {
        $this->assertTrue($this->extension->supports(AttendanceBookingReason::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($this->once())
            ->method('andWhere')
            ->with('a.organization = :organization')
            ->willReturn($this->queryBuilder);

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

        $this->invokeAddWhere($this->extension, $this->queryBuilder, AttendanceBookingReason::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')->willReturn(['a']);
        $this->queryBuilder->expects(self::never())->method('andWhere');
        $this->queryBuilder->expects(self::never())->method('setParameter');

        $this->invokeAddWhere($this->extension, $this->queryBuilder, AttendanceBookingReason::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')->willReturn(['a']);
        $this->queryBuilder->expects(self::never())->method('andWhere');
        $this->queryBuilder->expects(self::never())->method('setParameter');

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