<?php

declare(strict_types=1);

namespace App\Tests\DoctrineExtension\Access\AdditionalExtension;

use App\Doctrine\Access\AdditionalExtension\DateTimeConstraintExtensionAdditional;
use App\Tests\Unit\Doctrine\ExtensionTestCase;

class DateTimeConstraintExtensionAdditionalTest extends ExtensionTestCase
{
    private $extension;

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

    public function testSupportMethodReturnsTrue()
    {
        $this->request->method('isMethod')->with('GET')->willReturn(true);
        $this->request->method('get')->with('_time_constraint', true)->willReturn(true);
        $this->requestStack->method('getMainRequest')->willReturn($this->request);

        $this->assertTrue($this->extension->support('any_name'));
    }

    public function testSupportMethodReturnsFalse()
    {
        $this->request->method('isMethod')->with('GET')->willReturn(false);
        $this->requestStack->method('getMainRequest')->willReturn($this->request);

        $this->assertFalse($this->extension->support('any_name'));
    }

    public function testSupportMethodReturnsFalseWhenTimeConstraintIsFalse()
    {
        $this->request->method('isMethod')->with('GET')->willReturn(true);
        $this->request->method('get')->with('_time_constraint', true)->willReturn(false);
        $this->requestStack->method('getMainRequest')->willReturn($this->request);

        $this->assertFalse($this->extension->support('any_name'));
    }

    public function testAddWhereMethod()
    {
        $this->queryBuilder->method('getRootAliases')->willReturn(['rootAlias']);
        $this->queryBuilder
            ->expects($this->once())
            ->method('innerJoin')
            ->with('rootAlias.organizationFunction', 'organization_function');

        $this->invokeAddWhere($this->extension, $this->queryBuilder);
    }

    public function testAddWhereMethodWithNoRootAlias()
    {
        $this->queryBuilder->method('getRootAliases')->willReturn([]);

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

        $this->invokeAddWhere($this->extension, $this->queryBuilder);
    }
}
