Passed
Push — master ( 80fed1...32ef61 )
by Peter
09:46
created

testHandleCallsNextIfEnforcingCasbinRulesSucceed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Middleware;
6
7
use AbterPhp\Framework\TestDouble\Session\MockSessionFactory;
8
use Casbin\Enforcer;
9
use Casbin\Exceptions\CasbinException;
10
use Opulence\Http\Requests\Request;
11
use Opulence\Http\Responses\RedirectResponse;
12
use Opulence\Http\Responses\Response;
13
use Opulence\Http\Responses\ResponseHeaders;
14
use Opulence\Sessions\ISession;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
18
class AuthorizationTest extends TestCase
19
{
20
    protected const RESOURCE = 'foo';
21
    protected const ROLE     = 'bar';
22
    protected const USERNAME = 'baz';
23
24
    /** @var Authorization - System Under Test */
25
    protected $sut;
26
27
    /** @var MockObject|ISession */
28
    protected $sessionMock;
29
30
    /** @var MockObject|Enforcer */
31
    protected $enforcerMock;
32
33
    /** @var array<string,string> */
34
    protected $parametersStub = ['resource' => self::RESOURCE, 'role' => self::ROLE];
35
36
    /** @var array<string,string> */
37
    protected $sessionDataStub = ['username' => self::USERNAME];
38
39
    public function setUp(): void
40
    {
41
        $this->sessionMock = MockSessionFactory::create($this, $this->sessionDataStub);
42
43
        $this->enforcerMock = $this->createMock(Enforcer::class);
44
45
        $this->sut = new Authorization(
46
            $this->sessionMock,
0 ignored issues
show
Bug introduced by
It seems like $this->sessionMock can also be of type null; however, parameter $session of AbterPhp\Admin\Http\Midd...rization::__construct() does only seem to accept Opulence\Sessions\ISession, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            /** @scrutinizer ignore-type */ $this->sessionMock,
Loading history...
47
            $this->enforcerMock
48
        );
49
50
        $this->sut->setParameters($this->parametersStub);
51
    }
52
53
    public function testHandleCallsNextIfEnforcingCasbinRulesSucceed()
54
    {
55
        $this->enforcerMock->expects($this->once())->method('enforce')->willReturn(true);
56
57
        $requestStub  = new Request([], [], [], [], [], [], null);
58
        $responseStub = new Response();
59
60
        $next = function () use ($responseStub) {
61
            return $responseStub;
62
        };
63
64
        $actualResult = $this->sut->handle($requestStub, $next);
65
66
        $this->assertSame($responseStub, $actualResult);
67
    }
68
69
    public function testHandleRedirectsTo403OnCasbinRulesEnforcingFailure()
70
    {
71
        $this->enforcerMock
72
            ->expects($this->once())
73
            ->method('enforce')
74
            ->willReturn(false);
75
76
        $requestStub  = new Request([], [], [], [], [], [], null);
77
        $responseStub = new Response();
78
79
        $next = function () use ($responseStub) {
80
            return $responseStub;
81
        };
82
83
        /** @var RedirectResponse $actualResult */
84
        $actualResult = $this->sut->handle($requestStub, $next);
85
86
        $this->assertNotSame($responseStub, $actualResult);
87
        $this->assertInstanceOf(RedirectResponse::class, $actualResult);
88
        $this->assertSame(ResponseHeaders::HTTP_TEMPORARY_REDIRECT, $actualResult->getStatusCode());
89
        $this->assertSame(Authorization::PATH_403, $actualResult->getTargetUrl());
90
    }
91
92
    public function testHandleRedirectsTo403OnCasbinRulesEnforcingError()
93
    {
94
        $this->enforcerMock
95
            ->expects($this->once())
96
            ->method('enforce')
97
            ->willThrowException(new CasbinException());
98
99
        $requestStub  = new Request([], [], [], [], [], [], null);
100
        $responseStub = new Response();
101
102
        $next = function () use ($responseStub) {
103
            return $responseStub;
104
        };
105
106
        /** @var RedirectResponse $actualResult */
107
        $actualResult = $this->sut->handle($requestStub, $next);
108
109
        $this->assertNotSame($responseStub, $actualResult);
110
        $this->assertInstanceOf(RedirectResponse::class, $actualResult);
111
        $this->assertSame(ResponseHeaders::HTTP_TEMPORARY_REDIRECT, $actualResult->getStatusCode());
112
        $this->assertSame(Authorization::PATH_403, $actualResult->getTargetUrl());
113
    }
114
115
    public function testHandleThrowsExceptionOnUnexpectedException()
116
    {
117
        $this->expectException(\Exception::class);
118
119
        $this->enforcerMock
120
            ->expects($this->once())
121
            ->method('enforce')
122
            ->willThrowException(new \Exception());
123
124
        $requestStub  = new Request([], [], [], [], [], [], null);
125
        $responseStub = new Response();
126
127
        $next = function () use ($responseStub) {
128
            return $responseStub;
129
        };
130
131
        $this->sut->handle($requestStub, $next);
132
    }
133
}
134