vote example

->expects($this->once())
            ->method('vote')
            ->with($token, 'anysubject', ['attr1'])
            ->willReturn(VoterInterface::ACCESS_DENIED);

        $eventDispatcher
            ->expects($this->once())
            ->method('dispatch')
            ->with(new VoteEvent($voter, 'anysubject', ['attr1'], VoterInterface::ACCESS_DENIED), 'debug.security.authorization.vote');

        $sut = new TraceableVoter($voter$eventDispatcher);
        $result = $sut->vote($token, 'anysubject', ['attr1']);

        $this->assertSame(VoterInterface::ACCESS_DENIED, $result);
    }

    public function testSupportsAttributeOnCacheable()
    {
        $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass();
        $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMockForAbstractClass();

        $voter
            ->expects($this->once())
            
private VoterInterface $voter;
    private EventDispatcherInterface $eventDispatcher;

    public function __construct(VoterInterface $voter, EventDispatcherInterface $eventDispatcher)
    {
        $this->voter = $voter;
        $this->eventDispatcher = $eventDispatcher;
    }

    public function vote(TokenInterface $token, mixed $subject, array $attributes): int
    {
        $result = $this->voter->vote($token$subject$attributes);

        $this->eventDispatcher->dispatch(new VoteEvent($this->voter, $subject$attributes$result), 'debug.security.authorization.vote');

        return $result;
    }

    public function getDecoratedVoter(): VoterInterface
    {
        return $this->voter;
    }

    
use Symfony\Component\Security\Core\User\InMemoryUser;

class AuthenticatedVoterTest extends TestCase
{
    /** * @dataProvider getVoteTests */
    public function testVote($authenticated$attributes$expected)
    {
        $voter = new AuthenticatedVoter(new AuthenticationTrustResolver());

        $this->assertSame($expected$voter->vote($this->getToken($authenticated), null, $attributes));
    }

    public static function getVoteTests()
    {
        return [
            ['fully', [], VoterInterface::ACCESS_ABSTAIN],
            ['fully', ['FOO'], VoterInterface::ACCESS_ABSTAIN],
            ['remembered', [], VoterInterface::ACCESS_ABSTAIN],
            ['remembered', ['FOO'], VoterInterface::ACCESS_ABSTAIN],

            ['fully', ['IS_AUTHENTICATED_REMEMBERED'], VoterInterface::ACCESS_GRANTED],
            [
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

class RoleVoterTest extends TestCase
{
    /** * @dataProvider getVoteTests */
    public function testVoteUsingTokenThatReturnsRoleNames($roles$attributes$expected)
    {
        $voter = new RoleVoter();

        $this->assertSame($expected$voter->vote($this->getTokenWithRoleNames($roles), null, $attributes));
    }

    public static function getVoteTests()
    {
        return [
            [[][], VoterInterface::ACCESS_ABSTAIN],
            [[]['FOO'], VoterInterface::ACCESS_ABSTAIN],
            [[]['ROLE_FOO'], VoterInterface::ACCESS_DENIED],
            [['ROLE_FOO']['ROLE_FOO'], VoterInterface::ACCESS_GRANTED],
            [['ROLE_FOO']['FOO', 'ROLE_FOO'], VoterInterface::ACCESS_GRANTED],
            [['ROLE_BAR', 'ROLE_FOO']['ROLE_FOO'], VoterInterface::ACCESS_GRANTED],

            
return $this->strategy->decide(
            $this->collectResults($token$attributes$object)
        );
    }

    /** * @return \Traversable<int, int> */
    private function collectResults(TokenInterface $token, array $attributes, mixed $object): \Traversable
    {
        foreach ($this->getVoters($attributes$object) as $voter) {
            $result = $voter->vote($token$object$attributes);
            if (!\is_int($result) || !(self::VALID_VOTES[$result] ?? false)) {
                throw new \LogicException(sprintf('"%s::vote()" must return one of "%s" constants ("ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN"), "%s" returned.', get_debug_type($voter), VoterInterface::classvar_export($result, true)));
            }

            yield $result;
        }
    }

    /** * @return iterable<mixed, VoterInterface> */
    
use Symfony\Component\Security\Core\Role\RoleHierarchy;

class RoleHierarchyVoterTest extends RoleVoterTest
{
    /** * @dataProvider getVoteTests */
    public function testVoteUsingTokenThatReturnsRoleNames($roles$attributes$expected)
    {
        $voter = new RoleHierarchyVoter(new RoleHierarchy(['ROLE_FOO' => ['ROLE_FOOBAR']]));

        $this->assertSame($expected$voter->vote($this->getTokenWithRoleNames($roles), null, $attributes));
    }

    public static function getVoteTests()
    {
        return array_merge(parent::getVoteTests()[
            [['ROLE_FOO']['ROLE_FOOBAR'], VoterInterface::ACCESS_GRANTED],
        ]);
    }

    /** * @dataProvider getVoteWithEmptyHierarchyTests */
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

class ExpressionVoterTest extends TestCase
{
    /** * @dataProvider getVoteTests */
    public function testVoteWithTokenThatReturnsRoleNames($roles$attributes$expected$tokenExpectsGetRoles = true, $expressionLanguageExpectsEvaluate = true)
    {
        $voter = new ExpressionVoter($this->createExpressionLanguage($expressionLanguageExpectsEvaluate)$this->createTrustResolver()$this->createAuthorizationChecker());

        $this->assertSame($expected$voter->vote($this->getTokenWithRoleNames($roles$tokenExpectsGetRoles), null, $attributes));
    }

    public static function getVoteTests()
    {
        return [
            [[][], VoterInterface::ACCESS_ABSTAIN, false, false],
            [[]['FOO'], VoterInterface::ACCESS_ABSTAIN, false, false],

            [[][self::createExpression()], VoterInterface::ACCESS_DENIED, true, false],

            [['ROLE_FOO'][self::createExpression(), self::createExpression()], VoterInterface::ACCESS_GRANTED],
            [[
$voter[new \stdClass()], VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if attributes were not strings'],

            [$integerVoter[42], VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if attribute is an integer'],
        ];
    }

    /** * @dataProvider getTests */
    public function testVote(VoterInterface $voter, array $attributes$expectedVote$object$message)
    {
        $this->assertEquals($expectedVote$voter->vote($this->token, $object$attributes)$message);
    }

    public function testVoteWithTypeError()
    {
        $this->expectException(\TypeError::class);
        $this->expectExceptionMessage('Should error');
        $voter = new TypeErrorVoterTest_Voter();
        $voter->vote($this->token, new \stdClass()['EDIT']);
    }
}

Home | Imprint | This part of the site doesn't use cookies.