]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/front/ShaarliMiddlewareTest.php
Bump elliptic from 6.4.1 to 6.5.3
[github/shaarli/Shaarli.git] / tests / front / ShaarliMiddlewareTest.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front;
6
7 use PHPUnit\Framework\TestCase;
8 use Shaarli\Config\ConfigManager;
9 use Shaarli\Container\ShaarliContainer;
10 use Shaarli\Front\Exception\LoginBannedException;
11 use Shaarli\Render\PageBuilder;
12 use Slim\Http\Request;
13 use Slim\Http\Response;
14
15 class 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 }