<?php

declare(strict_types=1);

namespace App\Tests\Unit\Doctrine\Education;

use App\Doctrine\Education\CurrentEducationTimingExtension;
use App\Entity\Access\Access;
use App\Entity\Education\EducationTiming;
use App\Entity\Organization\Organization;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;

class CurrentEducationTimingExtensionTest extends TestCase
{
    private $security;
    private $queryBuilder;
    private $extension;
    private $user;
    private $organization;

    protected function setUp(): void
    {
        $this->security = $this->createMock(Security::class);
        $this->queryBuilder = $this->createMock(QueryBuilder::class);
        $this->user = $this->createMock(Access::class);
        $this->organization = $this->createMock(Organization::class);

        $this->extension = new CurrentEducationTimingExtension($this->security);
    }

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

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

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

    public function testAddWhere()
    {
        $this->user->method('getOrganization')->willReturn($this->organization);
        $this->security->method('getUser')->willReturn($this->user);
        $this->queryBuilder->method('getRootAliases')->willReturn(['o']);

        $this->queryBuilder->expects($this->once())
            ->method('andWhere')
            ->with('o.organization = :organization')
            ->willReturn($this->queryBuilder);

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

        $reflection = new \ReflectionClass($this->extension);
        $method = $reflection->getMethod('addWhere');
        $method->setAccessible(true);
        $method->invoke($this->extension, $this->queryBuilder, EducationTiming::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');

        $reflection = new \ReflectionClass($this->extension);
        $method = $reflection->getMethod('addWhere');
        $method->setAccessible(true);
        $method->invoke($this->extension, $this->queryBuilder, EducationTiming::class, null);
    }

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

        $reflection = new \ReflectionClass($this->extension);
        $method = $reflection->getMethod('addWhere');
        $method->setAccessible(true);
        $method->invoke($this->extension, $this->queryBuilder, EducationTiming::class, null);
    }
}
