aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/visitor/ErrorControllerTest.php
blob: e18a6fa2faa278c99eb0071766cabd272ecf6036 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Visitor;

use Shaarli\Front\Exception\ShaarliFrontException;
use Shaarli\TestCase;
use Slim\Http\Request;
use Slim\Http\Response;

class ErrorControllerTest extends TestCase
{
    use FrontControllerMockHelper;

    /** @var ErrorController */
    protected $controller;

    public function setUp(): void
    {
        $this->createContainer();

        $this->controller = new ErrorController($this->container);
    }

    /**
     * Test displaying error with a ShaarliFrontException: display exception message and use its code for HTTTP code
     */
    public function testDisplayFrontExceptionError(): void
    {
        $request = $this->createMock(Request::class);
        $response = new Response();

        $message = 'error message';
        $errorCode = 418;

        // Save RainTPL assigned variables
        $assignedVariables = [];
        $this->assignTemplateVars($assignedVariables);

        $result = ($this->controller)(
            $request,
            $response,
            new class($message, $errorCode) extends ShaarliFrontException {}
        );

        static::assertSame($errorCode, $result->getStatusCode());
        static::assertSame($message, $assignedVariables['message']);
        static::assertArrayNotHasKey('stacktrace', $assignedVariables);
    }

    /**
     * Test displaying error with any exception (no debug) while logged in:
     * display full error details
     */
    public function testDisplayAnyExceptionErrorNoDebugLoggedIn(): void
    {
        $request = $this->createMock(Request::class);
        $response = new Response();

        // Save RainTPL assigned variables
        $assignedVariables = [];
        $this->assignTemplateVars($assignedVariables);

        $this->container->loginManager->method('isLoggedIn')->willReturn(true);

        $result = ($this->controller)($request, $response, new \Exception('abc'));

        static::assertSame(500, $result->getStatusCode());
        static::assertSame('Error: abc', $assignedVariables['message']);
        static::assertContainsPolyfill('Please report it on Github', $assignedVariables['text']);
        static::assertArrayHasKey('stacktrace', $assignedVariables);
    }

    /**
     * Test displaying error with any exception (no debug) while logged out:
     * display standard error without detail
     */
    public function testDisplayAnyExceptionErrorNoDebug(): void
    {
        $request = $this->createMock(Request::class);
        $response = new Response();

        // Save RainTPL assigned variables
        $assignedVariables = [];
        $this->assignTemplateVars($assignedVariables);

        $this->container->loginManager->method('isLoggedIn')->willReturn(false);

        $result = ($this->controller)($request, $response, new \Exception('abc'));

        static::assertSame(500, $result->getStatusCode());
        static::assertSame('An unexpected error occurred.', $assignedVariables['message']);
        static::assertArrayNotHasKey('text', $assignedVariables);
        static::assertArrayNotHasKey('stacktrace', $assignedVariables);
    }
}