RoleVoter example

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()
    {
        
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
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],
            [[
Home | Imprint | This part of the site doesn't use cookies.