3 declare(strict_types
=1);
5 namespace Shaarli\Front
;
7 use PHPUnit\Framework\TestCase
;
8 use Shaarli\Config\ConfigManager
;
9 use Shaarli\Container\ShaarliContainer
;
10 use Shaarli\Front\Exception\LoginBannedException
;
11 use Shaarli\Front\Exception\UnauthorizedException
;
12 use Shaarli\Render\PageBuilder
;
13 use Shaarli\Render\PageCacheManager
;
14 use Shaarli\Security\LoginManager
;
15 use Shaarli\Updater\Updater
;
16 use Slim\Http\Request
;
17 use Slim\Http\Response
;
20 class ShaarliMiddlewareTest
extends TestCase
22 /** @var ShaarliContainer */
25 /** @var ShaarliMiddleware */
26 protected $middleware;
28 public function setUp(): void
30 $this->container
= $this->createMock(ShaarliContainer
::class);
32 $this->container
->conf
= $this->createMock(ConfigManager
::class);
33 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
35 $this->middleware
= new ShaarliMiddleware($this->container
);
39 * Test middleware execution with valid controller call
41 public function testMiddlewareExecution(): void
43 $request = $this->createMock(Request
::class);
44 $request->method('getUri')->willReturnCallback(function (): Uri
{
45 $uri = $this->createMock(Uri
::class);
46 $uri->method('getBasePath')->willReturn('/subfolder');
51 $response = new Response();
52 $controller = function (Request
$request, Response
$response): Response
{
53 return $response->withStatus(418); // I'm a tea pot
56 /** @var Response $result */
57 $result = $this->middleware
->__invoke($request, $response, $controller);
59 static::assertInstanceOf(Response
::class, $result);
60 static::assertSame(418, $result->getStatusCode());
64 * Test middleware execution with controller throwing a known front exception
66 public function testMiddlewareExecutionWithFrontException(): void
68 $request = $this->createMock(Request
::class);
69 $request->method('getUri')->willReturnCallback(function (): Uri
{
70 $uri = $this->createMock(Uri
::class);
71 $uri->method('getBasePath')->willReturn('/subfolder');
76 $response = new Response();
77 $controller = function (): void {
78 $exception = new LoginBannedException();
83 $pageBuilder = $this->createMock(PageBuilder
::class);
84 $pageBuilder->method('render')->willReturnCallback(function (string $message): string {
87 $this->container
->pageBuilder
= $pageBuilder;
89 /** @var Response $result */
90 $result = $this->middleware
->__invoke($request, $response, $controller);
92 static::assertInstanceOf(Response
::class, $result);
93 static::assertSame(401, $result->getStatusCode());
94 static::assertContains('error', (string) $result->getBody());
98 * Test middleware execution with controller throwing a not authorized exception
100 public function testMiddlewareExecutionWithUnauthorizedException(): void
102 $request = $this->createMock(Request
::class);
103 $request->method('getUri')->willReturnCallback(function (): Uri
{
104 $uri = $this->createMock(Uri
::class);
105 $uri->method('getBasePath')->willReturn('/subfolder');
110 $response = new Response();
111 $controller = function (): void {
112 throw new UnauthorizedException();
115 /** @var Response $result */
116 $result = $this->middleware
->__invoke($request, $response, $controller);
118 static::assertSame(302, $result->getStatusCode());
119 static::assertSame('/subfolder/login', $result->getHeader('location')[0]);
123 * Test middleware execution with controller throwing a not authorized exception
125 public function testMiddlewareExecutionWithServerExceptionWith(): void
127 $request = $this->createMock(Request
::class);
128 $request->method('getUri')->willReturnCallback(function (): Uri
{
129 $uri = $this->createMock(Uri
::class);
130 $uri->method('getBasePath')->willReturn('/subfolder');
135 $response = new Response();
136 $controller = function (): void {
137 throw new \
Exception();
141 $this->container
->pageBuilder
= $this->createMock(PageBuilder
::class);
142 $this->container
->pageBuilder
->method('render')->willReturnCallback(function (string $message): string {
145 $this->container
->pageBuilder
147 ->willReturnCallback(function (string $key, string $value) use (&$parameters): void {
148 $parameters[$key] = $value;
152 /** @var Response $result */
153 $result = $this->middleware
->__invoke($request, $response, $controller);
155 static::assertSame(500, $result->getStatusCode());
156 static::assertContains('error', (string) $result->getBody());
157 static::assertSame('An unexpected error occurred.', $parameters['message']);
160 public function testMiddlewareExecutionWithUpdates(): void
162 $request = $this->createMock(Request
::class);
163 $request->method('getUri')->willReturnCallback(function (): Uri
{
164 $uri = $this->createMock(Uri
::class);
165 $uri->method('getBasePath')->willReturn('/subfolder');
170 $response = new Response();
171 $controller = function (Request
$request, Response
$response): Response
{
172 return $response->withStatus(418); // I'm a tea pot
175 $this->container
->loginManager
= $this->createMock(LoginManager
::class);
176 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
178 $this->container
->conf
= $this->createMock(ConfigManager
::class);
179 $this->container
->conf
->method('get')->willReturnCallback(function (string $key): string {
183 $this->container
->pageCacheManager
= $this->createMock(PageCacheManager
::class);
184 $this->container
->pageCacheManager
->expects(static::once())->method('invalidateCaches');
186 $this->container
->updater
= $this->createMock(Updater
::class);
187 $this->container
->updater
188 ->expects(static::once())
190 ->willReturn(['update123'])
192 $this->container
->updater
->method('getDoneUpdates')->willReturn($updates = ['update123', 'other']);
193 $this->container
->updater
194 ->expects(static::once())
195 ->method('writeUpdates')
196 ->with('resource.updates', $updates)
199 /** @var Response $result */
200 $result = $this->middleware
->__invoke($request, $response, $controller);
202 static::assertInstanceOf(Response
::class, $result);
203 static::assertSame(418, $result->getStatusCode());