Completed
Pull Request — master (#50)
by Ron
03:24
created

HandleCommandVoterTest::provideTestVoteData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 10
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 decision manager returns true and the command is in the mapping
68
            ['handle', new FakeCommand, [new Role('ROLE_USER')], [FakeCommand::class => ['ROLE_USER']], VoterInterface::ACCESS_GRANTED],
69
70
            // Testcase: deny access if the command is not in the mapping (i.e. a default deny access case)
71
            ['handle', new FakeCommand, [new Role('ROLE_USER')], ['someOtherCommand' => ['ROLE_USER']], VoterInterface::ACCESS_DENIED],
72
        ];
73
    }
74
}
75