Test Failed
Push — master ( 3abf2e...da9b67 )
by Elijah
03:54
created

PermissionVoterTest::getTestsNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eloyekunle\PermissionsBundle\Tests\Security;
13
14
use Eloyekunle\PermissionsBundle\Model\RoleInterface;
15
use Eloyekunle\PermissionsBundle\Security\PermissionsVoter;
16
use Eloyekunle\PermissionsBundle\Tests\TestRole;
17
use Eloyekunle\PermissionsBundle\Tests\TestUser;
18
use Eloyekunle\PermissionsBundle\Util\PermissionsHandler;
19
use Eloyekunle\PermissionsBundle\Util\PermissionsHandlerInterface;
20
use PHPUnit\Framework\TestCase;
21
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
22
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
23
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
24
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
25
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
26
27
class PermissionVoterTest extends TestCase
28
{
29
    /** @var TokenInterface */
30
    protected $token;
31
32
    /** @var PermissionsVoter */
33
    protected $voter;
34
35
    /** @var AccessDecisionManagerInterface */
36
    protected $accessDecisionManager;
37
38
    /** @var PermissionsHandlerInterface */
39
    protected $permissionsHandler;
40
41
    protected function setUp()
42
    {
43
        $this->accessDecisionManager = new AccessDecisionManager([new RoleVoter()]);
44
        $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('S...nInterface')->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Securi...on\Token\TokenInterface of property $token.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
        $this->permissionsHandler = new PermissionsHandler(__DIR__.'/../Fixtures');
46
47
        $this->voter = $this->createPermissionsVoter();
48
    }
49
50
    public function getTests()
51
    {
52
        return [
53
            [['delete users'], VoterInterface::ACCESS_DENIED, new \StdClass(), ''],
54
            [['review articles'], VoterInterface::ACCESS_ABSTAIN, new \StdClass(), 'Access Abstain if permission is not declared in modules.'],
55
        ];
56
    }
57
58
    public function getTestsNew()
59
    {
60
        return [
61
            [['delete users'], new \StdClass()],
62
            [['review articles'], new \StdClass(), VoterInterface::ACCESS_ABSTAIN],
63
        ];
64
    }
65
66
    /**
67
     * @dataProvider getTests
68
     *
69
     * @param array $attributes
70
     * @param $expectedVote
71
     * @param $subject
72
     * @param $message
73
     */
74
    public function testVote(array $attributes, $expectedVote, $subject, $message)
75
    {
76
        $voter = new PermissionsVoter(new AccessDecisionManager(), $this->permissionsHandler);
77
        $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
78
79
        $this->assertSame($expectedVote, $voter->vote($token, $subject, $attributes), $message);
80
    }
81
82
    /**
83
     * @dataProvider getTestsNew
84
     *
85
     * @param array $attributes
86
     * @param $subject
87
     * @param $expectedVote
88
     */
89
    public function testSuperAdminPass(array $attributes, $subject, $expectedVote = VoterInterface::ACCESS_GRANTED)
90
    {
91
        $this->assertSame($expectedVote, $this->voter->vote($this->getToken([RoleInterface::ROLE_SUPER_ADMIN]), $subject, $attributes));
92
    }
93
94
    protected function createPermissionsVoter()
95
    {
96
        return new PermissionsVoter($this->accessDecisionManager, $this->permissionsHandler);
97
    }
98
99
    protected function getToken(array $roles)
100
    {
101
        foreach ($roles as $i => $role) {
102
            $roles[$i] = $this->createMockRole()->setRole($role);
103
        }
104
        $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
105
        $token->expects($this->any())
106
            ->method('getRoles')
107
            ->will($this->returnValue($roles));
108
109
        return $token;
110
    }
111
112
    protected function createMockRole()
113
    {
114
        return new TestRole();
115
    }
116
117
    protected function createMockUser()
118
    {
119
        return new TestUser();
120
    }
121
}
122