Completed
Pull Request — master (#50)
by Ron
04:24 queued 01:49
created

HandleCommandVoterTest::provideTestVoteData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.8571
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace League\Tactician\Bundle\Tests\Security\Voter;
4
5
use League\Tactician\Bundle\Security\Voter\HandleCommandVoter;
6
use League\Tactician\Bundle\Tests\Fake\FakeCommand;
7
use Mockery;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
11
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
12
use Symfony\Component\Security\Core\Role\Role;
13
14
/**
15
 * Unit test for the handle command voter
16
 *
17
 * @author Ron Rademaker
18
 */
19
class HandleCommandVoterTest extends TestCase
20
{
21
    /**
22
     * Tests the vote method.
23
     *
24
     * @param string $attribute
25
     * @param mixed $subject
26
     * @param array $roles
27
     * @param array $mapping
28
     * @param int $expected
29
     *
30
     * @dataProvider provideTestVoteData
31
     */
32
    public function testVote(string $attribute, $subject, array $roles, array $mapping, int $expected)
33
    {
34
        $voter = new HandleCommandVoter($mapping);
35
        $tokenMock = Mockery::mock(TokenInterface::class);
36
        $tokenMock->shouldReceive('getRoles')->andReturn($roles);
37
38
        $this->assertEquals($expected, $voter->vote($tokenMock, $subject, [$attribute]));
39
    }
40
41
    /**
42
     * Gets the testdata for the vote test.
43
     *
44
     * @return array
45
     */
46
    public function provideTestVoteData()
47
    {
48
        return [
49
            // Testcase: default access is false
50
            ['handle', new FakeCommand, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_DENIED],
51
52
            // Testcase: abstain when not handling a command, but using the handle attribute
53
            ['handle', null, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_ABSTAIN],
54
55
            // Testcase: abstain when not handling a command and not using the handle attribute
56
            ['create', null, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_ABSTAIN],
57
58
            // Testcase: abstain when not handling a command
59
            ['create', new FakeCommand, [new Role('ROLE_ADMIN')], [FakeCommand::class => ['ROLE_ADMIN']], VoterInterface::ACCESS_ABSTAIN],
60
61
            // Testcase: default is unrelated to roles
62
            ['handle', new FakeCommand, [new Role('ROLE_ADMIN')], [], VoterInterface::ACCESS_DENIED],
63
64
            // Testcase: deny access if incorrect role
65
            ['handle', new FakeCommand, [new Role('ROLE_ADMIN')], [FakeCommand::class => ['ROLE_USER']], VoterInterface::ACCESS_DENIED],
66
67
            // Testcase: grant access if the user has the configure role
68
            ['handle', new FakeCommand, [new Role('ROLE_USER')], [FakeCommand::class => ['ROLE_USER']], VoterInterface::ACCESS_GRANTED],
69
70
            // Testcase: grant access if the user has one of the configure roles
71
            ['handle', new FakeCommand, [new Role('ROLE_USER')], [FakeCommand::class => ['ROLE_USER', 'ROLE_TWO']], VoterInterface::ACCESS_GRANTED],
72
73
            // Testcase: grant access if the user has one of the configure roles, but also another role
74
            ['handle', new FakeCommand, [new Role('ROLE_USER', new Role('ROLE_THREE'))], [FakeCommand::class => ['ROLE_USER', 'ROLE_TWO']], VoterInterface::ACCESS_GRANTED],
0 ignored issues
show
Unused Code introduced by
The call to Role::__construct() has too many arguments starting with new \Symfony\Component\S...Role\Role('ROLE_THREE').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
76
            // Testcase: deny access if the command is not in the mapping (i.e. a default deny access case)
77
            ['handle', new FakeCommand, [new Role('ROLE_USER')], ['someOtherCommand' => ['ROLE_USER']], VoterInterface::ACCESS_DENIED],
78
        ];
79
    }
80
}
81