aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/ShaarliMiddlewareTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-26 11:41:10 +0100
committerGitHub <noreply@github.com>2020-01-26 11:41:10 +0100
commitc653ae3bfb11f663a52f55817e6d02a66d0852c8 (patch)
treed5a03a3e425180f1fafb646c8ec68a4b8458d0b3 /tests/front/ShaarliMiddlewareTest.php
parent1410dce2db310e71b5e683b0871c2f28d8807844 (diff)
parent27ceea2aeeed69b43fef4ebff35ec8004fcc2e45 (diff)
downloadShaarli-c653ae3bfb11f663a52f55817e6d02a66d0852c8.tar.gz
Shaarli-c653ae3bfb11f663a52f55817e6d02a66d0852c8.tar.zst
Shaarli-c653ae3bfb11f663a52f55817e6d02a66d0852c8.zip
Render login page through Slim controller (#1401)
Render login page through Slim controller
Diffstat (limited to 'tests/front/ShaarliMiddlewareTest.php')
-rw-r--r--tests/front/ShaarliMiddlewareTest.php70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php
new file mode 100644
index 00000000..80974f37
--- /dev/null
+++ b/tests/front/ShaarliMiddlewareTest.php
@@ -0,0 +1,70 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Container\ShaarliContainer;
10use Shaarli\Front\Exception\LoginBannedException;
11use Shaarli\Render\PageBuilder;
12use Slim\Http\Request;
13use Slim\Http\Response;
14
15class ShaarliMiddlewareTest extends TestCase
16{
17 /** @var ShaarliContainer */
18 protected $container;
19
20 /** @var ShaarliMiddleware */
21 protected $middleware;
22
23 public function setUp(): void
24 {
25 $this->container = $this->createMock(ShaarliContainer::class);
26 $this->middleware = new ShaarliMiddleware($this->container);
27 }
28
29 public function testMiddlewareExecution(): void
30 {
31 $request = $this->createMock(Request::class);
32 $response = new Response();
33 $controller = function (Request $request, Response $response): Response {
34 return $response->withStatus(418); // I'm a tea pot
35 };
36
37 /** @var Response $result */
38 $result = $this->middleware->__invoke($request, $response, $controller);
39
40 static::assertInstanceOf(Response::class, $result);
41 static::assertSame(418, $result->getStatusCode());
42 }
43
44 public function testMiddlewareExecutionWithException(): void
45 {
46 $request = $this->createMock(Request::class);
47 $response = new Response();
48 $controller = function (): void {
49 $exception = new LoginBannedException();
50
51 throw new $exception;
52 };
53
54 $pageBuilder = $this->createMock(PageBuilder::class);
55 $pageBuilder->method('render')->willReturnCallback(function (string $message): string {
56 return $message;
57 });
58 $this->container->pageBuilder = $pageBuilder;
59
60 $conf = $this->createMock(ConfigManager::class);
61 $this->container->conf = $conf;
62
63 /** @var Response $result */
64 $result = $this->middleware->__invoke($request, $response, $controller);
65
66 static::assertInstanceOf(Response::class, $result);
67 static::assertSame(401, $result->getStatusCode());
68 static::assertContains('error', (string) $result->getBody());
69 }
70}