]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/front/controller/visitor/ErrorControllerTest.php
Display error details even with dev.debug set to false
[github/shaarli/Shaarli.git] / tests / front / controller / visitor / ErrorControllerTest.php
CommitLineData
0c6fdbe1
A
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Visitor;
6
0c6fdbe1 7use Shaarli\Front\Exception\ShaarliFrontException;
a5a9cf23 8use Shaarli\TestCase;
0c6fdbe1
A
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class ErrorControllerTest extends TestCase
13{
14 use FrontControllerMockHelper;
15
16 /** @var ErrorController */
17 protected $controller;
18
19 public function setUp(): void
20 {
21 $this->createContainer();
22
23 $this->controller = new ErrorController($this->container);
24 }
25
26 /**
27 * Test displaying error with a ShaarliFrontException: display exception message and use its code for HTTTP code
28 */
29 public function testDisplayFrontExceptionError(): void
30 {
31 $request = $this->createMock(Request::class);
32 $response = new Response();
33
34 $message = 'error message';
35 $errorCode = 418;
36
37 // Save RainTPL assigned variables
38 $assignedVariables = [];
39 $this->assignTemplateVars($assignedVariables);
40
41 $result = ($this->controller)(
42 $request,
43 $response,
44 new class($message, $errorCode) extends ShaarliFrontException {}
45 );
46
47 static::assertSame($errorCode, $result->getStatusCode());
48 static::assertSame($message, $assignedVariables['message']);
49 static::assertArrayNotHasKey('stacktrace', $assignedVariables);
50 }
51
52 /**
cfdd2094
A
53 * Test displaying error with any exception (no debug) while logged in:
54 * display full error details
55 */
56 public function testDisplayAnyExceptionErrorNoDebugLoggedIn(): void
57 {
58 $request = $this->createMock(Request::class);
59 $response = new Response();
60
61 // Save RainTPL assigned variables
62 $assignedVariables = [];
63 $this->assignTemplateVars($assignedVariables);
64
65 $this->container->loginManager->method('isLoggedIn')->willReturn(true);
66
67 $result = ($this->controller)($request, $response, new \Exception('abc'));
68
69 static::assertSame(500, $result->getStatusCode());
70 static::assertSame('Error: abc', $assignedVariables['message']);
71 static::assertContainsPolyfill('Please report it on Github', $assignedVariables['text']);
72 static::assertArrayHasKey('stacktrace', $assignedVariables);
73 }
74
75 /**
76 * Test displaying error with any exception (no debug) while logged out:
77 * display standard error without detail
0c6fdbe1
A
78 */
79 public function testDisplayAnyExceptionErrorNoDebug(): void
80 {
81 $request = $this->createMock(Request::class);
82 $response = new Response();
83
84 // Save RainTPL assigned variables
85 $assignedVariables = [];
86 $this->assignTemplateVars($assignedVariables);
87
cfdd2094
A
88 $this->container->loginManager->method('isLoggedIn')->willReturn(false);
89
0c6fdbe1
A
90 $result = ($this->controller)($request, $response, new \Exception('abc'));
91
92 static::assertSame(500, $result->getStatusCode());
93 static::assertSame('An unexpected error occurred.', $assignedVariables['message']);
cfdd2094 94 static::assertArrayNotHasKey('text', $assignedVariables);
0c6fdbe1
A
95 static::assertArrayNotHasKey('stacktrace', $assignedVariables);
96 }
97}