|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Http\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Config\Routes as RoutesConfig; |
|
8
|
|
|
use Opulence\Http\Requests\Request; |
|
9
|
|
|
use Opulence\Http\Responses\RedirectResponse; |
|
10
|
|
|
use Opulence\Http\Responses\Response; |
|
11
|
|
|
use Opulence\Sessions\ISession; |
|
12
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use SessionHandlerInterface; |
|
15
|
|
|
|
|
16
|
|
|
class AuthenticationTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Authentication - System Under Test */ |
|
19
|
|
|
protected Authentication $sut; |
|
20
|
|
|
|
|
21
|
|
|
/** @var MockObject|ISession */ |
|
22
|
|
|
protected $sessionMock; |
|
23
|
|
|
|
|
24
|
|
|
/** @var MockObject|SessionHandlerInterface */ |
|
25
|
|
|
protected $sessionHandlerMock; |
|
26
|
|
|
|
|
27
|
|
|
/** @var MockObject|RoutesConfig */ |
|
28
|
|
|
protected $routesConfigMock; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->sessionMock = $this->createMock(ISession::class); |
|
33
|
|
|
$this->sessionHandlerMock = $this->createMock(SessionHandlerInterface::class); |
|
34
|
|
|
$this->routesConfigMock = $this->createMock(RoutesConfig::class); |
|
35
|
|
|
|
|
36
|
|
|
$this->sut = new Authentication( |
|
37
|
|
|
$this->sessionMock, |
|
38
|
|
|
$this->sessionHandlerMock, |
|
39
|
|
|
$this->routesConfigMock |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testHandleRedirectsToLoginPathIfUserIsNotLoggedIn() |
|
44
|
|
|
{ |
|
45
|
|
|
$loginPath = '/foo'; |
|
46
|
|
|
|
|
47
|
|
|
$this->routesConfigMock->expects($this->any())->method('getLoginPath')->willReturn($loginPath); |
|
48
|
|
|
|
|
49
|
|
|
$this->sessionMock->expects($this->once())->method('has')->willReturn(false); |
|
50
|
|
|
|
|
51
|
|
|
$requestStub = new Request([], [], [], [], [], [], null); |
|
52
|
|
|
$responseStub = new Response(); |
|
53
|
|
|
|
|
54
|
|
|
$next = function () use ($responseStub) { |
|
55
|
|
|
return $responseStub; |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
/** @var RedirectResponse $actualResult */ |
|
59
|
|
|
$actualResult = $this->sut->handle($requestStub, $next); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertNotSame($responseStub, $actualResult); |
|
62
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $actualResult); |
|
63
|
|
|
$this->assertSame($loginPath, $actualResult->getTargetUrl()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testHandleDoesNothingIfSessionIsSet() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->sessionMock->expects($this->atLeastOnce())->method('has')->willReturn(true); |
|
69
|
|
|
|
|
70
|
|
|
$requestStub = new Request([], [], [], [], [], [], null); |
|
71
|
|
|
$responseStub = new Response(); |
|
72
|
|
|
|
|
73
|
|
|
$next = function () use ($responseStub) { |
|
74
|
|
|
return $responseStub; |
|
75
|
|
|
}; |
|
76
|
|
|
|
|
77
|
|
|
$actualResult = $this->sut->handle($requestStub, $next); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertSame($responseStub, $actualResult); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|