<?php

declare(strict_types=1);

namespace App\Tests\Unit\Doctrine\Core;

use App\Doctrine\Core\AllowedAddressPostalExtension;
use App\Entity\Access\Access;
use App\Entity\Core\AddressPostal;
use App\Tests\Unit\Doctrine\ExtensionTestCase;
use Symfony\Component\Security\Core\User\UserInterface;

class AllowedAddressPostalExtensionTest extends ExtensionTestCase
{
    private AllowedAddressPostalExtension $extension;

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

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

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

        foreach ($classes as $class) {
            $this->assertFalse($this->extension->supports($class, 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('innerJoin')
            ->with('a.organizationAddressPostal', 'organization_address_postal')
            ->willReturn($this->queryBuilder);

        $this->queryBuilder->expects($this->once())
            ->method('andWhere')
            ->with('organization_address_postal.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, AddressPostal::class, null);
    }

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

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

        $this->queryBuilder->expects($this->never())
            ->method('andWhere');

        $this->queryBuilder->expects($this->never())
            ->method('setParameter');

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

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

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

        $this->queryBuilder->expects($this->never())
            ->method('andWhere');

        $this->queryBuilder->expects($this->never())
            ->method('setParameter');

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

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

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

        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('No root alias defined.');

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