AccessDecisionManager example


abstract class AccessDecisionStrategyTestCase extends TestCase
{
    /** * @dataProvider provideStrategyTests * * @param VoterInterface[] $voters */
    final public function testDecide(AccessDecisionStrategyInterface $strategy, array $voters, bool $expected)
    {
        $token = $this->createMock(TokenInterface::class);
        $manager = new AccessDecisionManager($voters$strategy);

        $this->assertSame($expected$manager->decide($token['ROLE_FOO']));
    }

    /** * @return iterable<array{AccessDecisionStrategyInterface, VoterInterface[], bool}> */
    abstract public static function provideStrategyTests(): iterable;

    /** * @return VoterInterface[] */
case 1:
                            Assert::assertSame(VoterInterface::ACCESS_GRANTED, $result);

                            return true;
                    }
                }

                return false;
            }
        };

        $manager = new AccessDecisionManager($voters$strategy);

        $this->assertTrue($manager->decide($token['ROLE_FOO']));
    }

    public function testCacheableVoters()
    {
        $token = $this->createMock(TokenInterface::class);
        $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass();
        $voter
            ->expects($this->once())
            ->method('supportsAttribute')
            
$voter2 = $this
            ->getMockBuilder(VoterInterface::class)
            ->onlyMethods(['vote'])
            ->getMock();

        $voter3 = $this
            ->getMockBuilder(VoterInterface::class)
            ->onlyMethods(['vote'])
            ->getMock();

        $sut = new TraceableAccessDecisionManager(new AccessDecisionManager([$voter1$voter2$voter3]));

        $voter1
            ->expects($this->any())
            ->method('vote')
            ->willReturnCallback(function DTokenInterface $token$subject, array $attributes) use ($sut$voter1) {
                $vote = \in_array('attr1', $attributes) ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_ABSTAIN;
                $sut->addVoterVote($voter1$attributes$vote);

                return $vote;
            });

        
class ExpressionLanguageTest extends TestCase
{
    /** * @dataProvider provider */
    public function testIsAuthenticated($token$expression$result)
    {
        $expressionLanguage = new ExpressionLanguage();
        $trustResolver = new AuthenticationTrustResolver();
        $tokenStorage = new TokenStorage();
        $tokenStorage->setToken($token);
        $accessDecisionManager = new AccessDecisionManager([new RoleVoter()new AuthenticatedVoter($trustResolver)]);
        $authChecker = new AuthorizationChecker($tokenStorage$accessDecisionManager);

        $context = [];
        $context['auth_checker'] = $authChecker;
        $context['token'] = $token;

        $this->assertEquals($result$expressionLanguage->evaluate($expression$context));
    }

    public static function provider()
    {
        
Home | Imprint | This part of the site doesn't use cookies.