<?php

namespace App\Tests\Unit\Service\ServiceIterator;

use App\Entity\Access\Access;
use App\Service\Access\OptionalsRolesInterface;
use App\Service\ServiceIterator\OptionalsRolesIterator;
use PHPUnit\Framework\TestCase;

class OptionalsRolesIteratorTest extends TestCase
{
    /**
     * @see OptionalsRolesIterator::getOptionalsRoles()
     */
    public function testGetOptionalsRoles(): void
    {
        $access = $this->getMockBuilder(Access::class)->getMock();

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

        $role1 = $mocker->getMock();
        $role1->method('support')->willReturn(false);

        $role2 = $mocker->getMock();
        $role2->method('support')->with($access)->willReturn(true);
        $role2->expects($this->once())->method('getRole')->willReturn('ROLE2');

        $role3 = $mocker->getMock();
        $role3->method('support')->willReturn(false);

        $role4 = $mocker->getMock();
        $role4->method('support')->with($access)->willReturn(true);
        $role4->expects($this->once())->method('getRole')->willReturn('ROLE4');

        $roles = [$role1, $role2, $role3, $role4];

        $iterator = $this->getMockBuilder(OptionalsRolesIterator::class)
            ->setConstructorArgs([$roles])
            ->setMethodsExcept(['getOptionalsRoles'])
            ->getMock();

        $actualRoles = $iterator->getOptionalsRoles($access);

        $this->assertEquals(['ROLE2', 'ROLE4'], $actualRoles);
    }

    /**
     * @see OptionalsRolesIterator::getOptionalsRoles()
     */
    public function testGetOptionalsRolesNotFound(): void
    {
        $access = $this->getMockBuilder(Access::class)->getMock();

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

        $role1 = $mocker->getMock();
        $role1->method('support')->willReturn(false);

        $role2 = $mocker->getMock();
        $role2->method('support')->willReturn(false);

        $roles = [$role1, $role2];

        $iterator = $this->getMockBuilder(OptionalsRolesIterator::class)
            ->setConstructorArgs([$roles])
            ->setMethodsExcept(['getOptionalsRoles'])
            ->getMock();

        $actualRoles = $iterator->getOptionalsRoles($access);

        $this->assertEquals([], $actualRoles);
    }
}
