Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

testVoteNotSupportedAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Security\Authorization;
12
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
15
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
17
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
18
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
19
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
20
use Yarhon\RouteGuardBundle\ExpressionLanguage\ExpressionDecorator;
21
use Yarhon\RouteGuardBundle\Security\Authorization\SensioSecurityExpressionVoter;
22
23
/**
24
 * @author Yaroslav Honcharuk <[email protected]>
25
 */
26
class SensioSecurityExpressionVoterTest extends TestCase
27
{
28
    private $expressionLanguage;
29
30
    private $trustResolver;
31
32
    private $authChecker;
33
34
    private $roleHierarchy;
35
36
    private $voter;
37
38
    public function setUp()
39
    {
40
        $this->expressionLanguage = $this->createMock(ExpressionLanguage::class);
41
        $this->trustResolver = $this->createMock(AuthenticationTrustResolverInterface::class);
42
        $this->authChecker = $this->createMock(AuthorizationCheckerInterface::class);
43
        $this->roleHierarchy = $this->createMock(RoleHierarchyInterface::class);
44
45
        $this->voter = new SensioSecurityExpressionVoter($this->expressionLanguage, $this->trustResolver, $this->authChecker);
46
    }
47
48
    public function testVoteNotSupportedAttribute()
49
    {
50
        $token = $this->createMock(TokenInterface::class);
51
52
        $result = $this->voter->vote($token, null, ['test']);
53
54
        $this->assertSame(VoterInterface::ACCESS_ABSTAIN, $result);
55
    }
56
57
    public function testVote()
58
    {
59
        $token = $this->createMock(TokenInterface::class);
60
61
        $token->method('getRoles')
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        $token->/** @scrutinizer ignore-call */ 
62
                method('getRoles')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            ->willReturn([]);
63
64
        $expressionDecorator = $this->createMock(ExpressionDecorator::class);
65
66
        $expressionDecorator->method('getVariables')
67
            ->willReturn(['token' => 'asd', 'custom' => 'test']);
68
69
        $expressionDecorator->method('getExpression')
70
            ->willReturn('expression');
71
72
        $expectedVariables = [
73
            'token' => $token,
74
            'custom' => 'test',
75
            'user' => null,
76
            'object' => null,
77
            'subject' => null,
78
            'roles' => [],
79
            'trust_resolver' => $this->trustResolver,
80
            'auth_checker' => $this->authChecker,
81
        ];
82
83
        $this->expressionLanguage->expects($this->once())
84
            ->method('evaluate')
85
            ->with('expression', $expectedVariables)
86
            ->willReturn(true);
87
88
        $result = $this->voter->vote($token, null, [$expressionDecorator]);
89
90
        $this->assertSame(VoterInterface::ACCESS_GRANTED, $result);
91
    }
92
93
    public function testGetVariableNames()
94
    {
95
        $expected = [
96
            'token',
97
            'user',
98
            'object',
99
            'subject',
100
            'roles',
101
            'trust_resolver',
102
            'auth_checker',
103
            'request',
104
        ];
105
106
        $this->assertSame($expected, SensioSecurityExpressionVoter::getVariableNames());
107
    }
108
}
109