<?php

declare(strict_types=1);

namespace App\Tests\Doctrine\Organization;

use App\Doctrine\Organization\CurrentParentOrganizationExtension;
use App\Entity\Access\Access;
use App\Entity\Organization\Organization;
use App\Entity\Organization\OrganizationAddressPostal;
use App\Entity\Organization\OrganizationArticle;
use App\Entity\Organization\Subdomain;
use App\Tests\Unit\Doctrine\ExtensionTestCase;

class CurrentParentOrganizationExtensionTest extends ExtensionTestCase
{
    private $extension;

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

    public function testSupportsReturnsTrueForSupportedClasses()
    {
        $supportedClasses = [OrganizationAddressPostal::class, OrganizationArticle::class, Subdomain::class];
        foreach ($supportedClasses as $class) {
            $this->assertTrue($this->extension->supports($class, null));
        }
    }

    public function testSupportsReturnsFalseForOtherClasses()
    {
        $classes = ['OtherClass', Access::class, Organization::class];
        foreach ($classes as $class) {
            $this->assertFalse($this->extension->supports($class, null));
        }
    }

    public function testAddWhere()
    {
        $this->user->method('getOrganization')->willReturn($this->organization);
        $this->organization->method('getId')->willReturn(1);
        $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', 1)
            ->willReturn($this->queryBuilder);

        $this->invokeAddWhere($this->extension, $this->queryBuilder, OrganizationAddressPostal::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, OrganizationAddressPostal::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');

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