<?php

namespace App\Tests\Unit\Service\ServiceIterator;

use App\Doctrine\Access\AccessExtensionInterface;
use App\Service\ServiceIterator\CurrentAccessExtensionIterator;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;

class CurrentAccessExtensionIteratorTest extends TestCase
{
    /**
     * @see CurrentAccessExtensionIterator::addWhere()
     */
    public function testAddWhere(): void
    {
        $queryBuilder = $this->getMockBuilder(QueryBuilder::class)
            ->disableOriginalConstructor()
            ->getMock();

        $mocker = $this->getMockBuilder(AccessExtensionInterface::class);

        $ext1 = $mocker->getMock();
        $ext1->method('support')->willReturn(false);
        $ext1->expects($this->never())->method('addWhere')->with($queryBuilder);

        $ext2 = $mocker->getMock();
        $ext2->method('support')->with('foo')->willReturn(true);
        $ext2->expects($this->once())->method('addWhere')->with($queryBuilder);

        $ext3 = $mocker->getMock();
        $ext3->method('support')->willReturn(false);
        $ext1->expects($this->never())->method('addWhere')->with($queryBuilder);

        $extensions = [$ext1, $ext2, $ext3];

        $iterator = $this->getMockBuilder(CurrentAccessExtensionIterator::class)
            ->setConstructorArgs([$extensions])
            ->setMethodsExcept(['addWhere'])
            ->getMock();

        $iterator->addWhere($queryBuilder, 'foo');
    }
}
