<?php

namespace App\Tests\Unit\Service\Security;

use App\Entity\Access\Access;
use App\Service\Security\SwitchUser;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;

class SwitchUserTest extends TestCase
{
    /**
     * @see SwitchUser::isAllowedToSwitch()
     */
    public function testIsAllowedToSwitch(): void
    {
        // Cannot mock SwitchUser because of https://github.com/sebastianbergmann/phpunit/issues/3886
        $switchUser = new SwitchUser();

        $children = $this->getMockBuilder(Access::class)->getMock();

        $user = $this->getMockBuilder(Access::class)->getMock();
        $user->method('getChildren')
            ->willReturn(new ArrayCollection([$children]));

        $this->assertTrue($switchUser->isAllowedToSwitch($user, $children));
    }

    /**
     * @see SwitchUser::isAllowedToSwitch()
     */
    public function testIsNotAllowedToSwitch(): void
    {
        // Cannot mock SwitchUser because of https://github.com/sebastianbergmann/phpunit/issues/3886
        $switchUser = new SwitchUser();

        $children = $this->getMockBuilder(Access::class)->getMock();
        $user = $this->getMockBuilder(Access::class)->getMock();
        $user
            ->expects($this->once())
            ->method('getChildren')
            ->willReturn(new ArrayCollection());

        $this->assertFalse($switchUser->isAllowedToSwitch($user, $children));
    }
}
