testSupports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\AuthorizationChecker;
12
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface as BaseAuthorizationCheckerInterface;
15
use Yarhon\RouteGuardBundle\Routing\RouteContext;
16
use Yarhon\RouteGuardBundle\Security\Test\TestInterface;
17
use Yarhon\RouteGuardBundle\Security\Test\AbstractSymfonySecurityTest;
18
use Yarhon\RouteGuardBundle\Security\TestResolver\SymfonySecurityResolverInterface;
19
use Yarhon\RouteGuardBundle\Security\AuthorizationChecker\SymfonySecurityAuthorizationChecker;
20
21
/**
22
 * @author Yaroslav Honcharuk <[email protected]>
23
 */
24
class SymfonySecurityAuthorizationCheckerTest extends TestCase
25
{
26
    private $baseAuthorizationChecker;
27
28
    private $testResolver;
29
30
    private $checker;
31
32
    public function setUp()
33
    {
34
        $this->baseAuthorizationChecker = $this->createMock(BaseAuthorizationCheckerInterface::class);
35
        $this->testResolver = $this->createMock(SymfonySecurityResolverInterface::class);
36
        $this->checker = new SymfonySecurityAuthorizationChecker($this->baseAuthorizationChecker, $this->testResolver);
37
    }
38
39
    /**
40
     * @dataProvider supportsDataProvider
41
     */
42
    public function testSupports($test, $expected)
43
    {
44
        $this->assertEquals($expected, $this->checker->supports($test));
45
    }
46
47
    public function supportsDataProvider()
48
    {
49
        return [
50
            [$this->createMock(TestInterface::class), false],
51
            [$this->getMockForAbstractClass(AbstractSymfonySecurityTest::class, [['ROLE_USER']]), true],
52
        ];
53
    }
54
55
    /**
56
     * @dataProvider isGrantedDataProvider
57
     */
58
    public function testIsGranted($baseAuthorizationCheckerResult, $expected)
59
    {
60
        $test = $this->getMockForAbstractClass(AbstractSymfonySecurityTest::class, [['ROLE_USER']]);
61
        $routeContext = new RouteContext('index');
62
63
        $this->testResolver->method('resolve')
64
            ->with($test, $routeContext)
65
            ->willReturn(['a', 'b']);
66
67
        $this->baseAuthorizationChecker->method('isGranted')
68
            ->with('a', 'b')
69
            ->willReturn($baseAuthorizationCheckerResult);
70
71
        $this->assertEquals($expected, $this->checker->isGranted($test, $routeContext));
72
    }
73
74
    public function isGrantedDataProvider()
75
    {
76
        return [
77
            [false, false],
78
            [true, true],
79
        ];
80
    }
81
}
82