3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Front\Exception\ShaarliFrontException
;
10 use Slim\Http\Response
;
12 class ErrorControllerTest
extends TestCase
14 use FrontControllerMockHelper
;
16 /** @var ErrorController */
17 protected $controller;
19 public function setUp(): void
21 $this->createContainer();
23 $this->controller
= new ErrorController($this->container
);
27 * Test displaying error with a ShaarliFrontException: display exception message and use its code for HTTTP code
29 public function testDisplayFrontExceptionError(): void
31 $request = $this->createMock(Request
::class);
32 $response = new Response();
34 $message = 'error message';
37 // Save RainTPL assigned variables
38 $assignedVariables = [];
39 $this->assignTemplateVars($assignedVariables);
41 $result = ($this->controller
)(
44 new class($message, $errorCode) extends ShaarliFrontException
{}
47 static::assertSame($errorCode, $result->getStatusCode());
48 static::assertSame($message, $assignedVariables['message']);
49 static::assertArrayNotHasKey('stacktrace', $assignedVariables);
53 * Test displaying error with any exception (no debug) while logged in:
54 * display full error details
56 public function testDisplayAnyExceptionErrorNoDebugLoggedIn(): void
58 $request = $this->createMock(Request
::class);
59 $response = new Response();
61 // Save RainTPL assigned variables
62 $assignedVariables = [];
63 $this->assignTemplateVars($assignedVariables);
65 $this->container
->loginManager
->method('isLoggedIn')->willReturn(true);
67 $result = ($this->controller
)($request, $response, new \
Exception('abc'));
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);
76 * Test displaying error with any exception (no debug) while logged out:
77 * display standard error without detail
79 public function testDisplayAnyExceptionErrorNoDebug(): void
81 $request = $this->createMock(Request
::class);
82 $response = new Response();
84 // Save RainTPL assigned variables
85 $assignedVariables = [];
86 $this->assignTemplateVars($assignedVariables);
88 $this->container
->loginManager
->method('isLoggedIn')->willReturn(false);
90 $result = ($this->controller
)($request, $response, new \
Exception('abc'));
92 static::assertSame(500, $result->getStatusCode());
93 static::assertSame('An unexpected error occurred.', $assignedVariables['message']);
94 static::assertArrayNotHasKey('text', $assignedVariables);
95 static::assertArrayNotHasKey('stacktrace', $assignedVariables);