Completed
Push — master ( 231cf5...6b8ce2 )
by John
03:01
created

willThrowAccessDeniedExceptionWhenAuthorizationCheckerReturnsFalse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\Security;
10
11
use KleijnWeb\SwaggerBundle\Security\RequestAuthorizationListener;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
15
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
class RequestAuthorizationListenerTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var AuthorizationCheckerInterface
24
     */
25
    private $authorizationChecker;
26
27
    /**
28
     * @var RequestAuthorizationListener
29
     */
30
    private $listener;
31
32
    protected function setUp()
33
    {
34
        $this->authorizationChecker = $this->getMockForAbstractClass(AuthorizationCheckerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForAbstrac...heckerInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...zationCheckerInterface> of property $authorizationChecker.

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...
35
        $this->listener = new RequestAuthorizationListener($this->authorizationChecker);
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function willNotHandleUnlessMasterRequest()
42
    {
43
        $this->listener->handle($this->createKernelEventWithRequest(new Request(), false));
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function willInvokeAuthorizationCheckerWithCorrectAttributeAndRequest()
50
    {
51
        $request  = new Request();
52
53
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
54
        $mock = $this->authorizationChecker;
55
        $mock
56
            ->expects($this->once())
57
            ->method('isGranted')
58
            ->with(RequestAuthorizationListener::ATTRIBUTE, $request)
59
            ->willReturn(true);
60
61
        $this->listener->handle($this->createKernelEventWithRequest($request));
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function willThrowAccessDeniedExceptionWhenAuthorizationCheckerReturnsFalse()
68
    {
69
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
70
        $mock = $this->authorizationChecker;
71
        $mock
72
            ->expects($this->once())
73
            ->method('isGranted')
74
            ->willReturn(false);
75
76
        $this->expectException(AccessDeniedException::class);
77
        $this->listener->handle($this->createKernelEventWithRequest(new Request()));
78
    }
79
80
    private function createKernelEventWithRequest(Request $request, $isMaster = true): GetResponseEvent
81
    {
82
        $mock = $this->getMockBuilder(GetResponseEvent::class)
83
            ->disableOriginalConstructor()
84
            ->getMock();
85
        $mock
86
            ->expects($this->any())
87
            ->method('getRequest')
88
            ->willReturn($request);
89
        $mock
90
            ->expects($this->any())
91
            ->method('isMasterRequest')
92
            ->willReturn($isMaster);
93
94
        return $mock;
95
    }
96
}
97