aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/front/controller/visitor/ErrorNotFoundControllerTest.php
blob: a1cbbecfc60f182b76df5dda79c86c3ce91d0b6f (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
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller\Visitor;

use Shaarli\TestCase;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Uri;

class ErrorNotFoundControllerTest extends TestCase
{
    use FrontControllerMockHelper;

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

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

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

    /**
     * Test displaying 404 error
     */
    public function testDisplayNotFoundError(): void
    {
        $request = $this->createMock(Request::class);
        $request->expects(static::once())->method('getRequestTarget')->willReturn('/');
        $request->method('getUri')->willReturnCallback(function (): Uri {
            $uri = $this->createMock(Uri::class);
            $uri->method('getBasePath')->willReturn('/subfolder');

            return $uri;
        });

        $response = new Response();

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

        $result = ($this->controller)(
            $request,
            $response
        );

        static::assertSame(404, $result->getStatusCode());
        static::assertSame('404', (string) $result->getBody());
        static::assertSame('Requested page could not be found.', $assignedVariables['error_message']);
    }

    /**
     * Test displaying 404 error from REST API
     */
    public function testDisplayNotFoundErrorFromAPI(): void
    {
        $request = $this->createMock(Request::class);
        $request->expects(static::once())->method('getRequestTarget')->willReturn('/sufolder/api/v1/links');
        $request->method('getUri')->willReturnCallback(function (): Uri {
            $uri = $this->createMock(Uri::class);
            $uri->method('getBasePath')->willReturn('/subfolder');

            return $uri;
        });

        $response = new Response();

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

        $result = ($this->controller)($request, $response);

        static::assertSame(404, $result->getStatusCode());
        static::assertSame([], $assignedVariables);
    }
}