Passed
Pull Request — main (#13)
by Chema
02:33
created

ErrorHandlingTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
c 0
b 0
f 0
dl 0
loc 110
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A test_respond_404_status_when_uri_does_not_match() 0 15 1
A test_respond_404_status_when_not_matches_match_methods() 0 16 1
A test_handle_handled_exception() 0 23 1
A notMatchesMethodsProvider() 0 8 1
A test_respond_404_status_when_method_does_not_match() 0 16 1
A test_respond_500_status_when_unhandled_exception() 0 16 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Feature\Router;
6
7
use Gacela\Router\Entities\Request;
8
use Gacela\Router\Exceptions\UnhandledException;
9
use Gacela\Router\Handlers;
10
use Gacela\Router\Router;
11
use Gacela\Router\Routes;
12
use GacelaTest\Feature\HeaderTestCase;
13
use GacelaTest\Feature\Router\Fixtures\FakeController;
14
use GacelaTest\Feature\Router\Fixtures\FakeControllerWithUnhandledException;
15
use Generator;
16
17
final class ErrorHandlingTest extends HeaderTestCase
18
{
19
    public function test_respond_404_status_when_uri_does_not_match(): void
20
    {
21
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
22
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_OPTIONS;
23
24
        Router::configure(static function (): void {
25
        });
26
27
        self::assertSame([
28
            [
29
                'header' => 'HTTP/1.0 404 Not Found',
30
                'replace' => true,
31
                'response_code' => 0,
32
            ],
33
        ], $this->headers());
34
    }
35
36
    public function test_respond_404_status_when_method_does_not_match(): void
37
    {
38
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
39
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
40
41
        Router::configure(static function (Routes $routes): void {
42
            $routes->post('expected/uri', FakeController::class, 'basicAction');
43
        });
44
45
        self::assertSame([
46
            [
47
                'header' => 'HTTP/1.0 404 Not Found',
48
                'replace' => true,
49
                'response_code' => 0,
50
            ],
51
        ], $this->headers());
52
    }
53
54
    /**
55
     * @dataProvider notMatchesMethodsProvider
56
     */
57
    public function test_respond_404_status_when_not_matches_match_methods(string $testMethod, array $givenMethods): void
58
    {
59
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
60
        $_SERVER['REQUEST_METHOD'] = $testMethod;
61
62
        Router::configure(static function (Routes $routes) use ($givenMethods): void {
63
            $routes->match($givenMethods, 'expected/uri', FakeController::class, 'basicAction');
64
        });
65
66
        self::assertSame([
67
            [
68
                'header' => 'HTTP/1.0 404 Not Found',
69
                'replace' => true,
70
                'response_code' => 0,
71
            ],
72
        ], $this->headers());
73
    }
74
75
    public function notMatchesMethodsProvider(): Generator
76
    {
77
        yield [Request::METHOD_PUT, [Request::METHOD_GET, Request::METHOD_POST]];
78
        yield [Request::METHOD_OPTIONS, [Request::METHOD_GET, Request::METHOD_POST]];
79
        yield [Request::METHOD_GET, [Request::METHOD_PATCH, Request::METHOD_PUT, Request::METHOD_DELETE, Request::METHOD_POST]];
80
        yield [Request::METHOD_CONNECT, [
81
            Request::METHOD_GET, Request::METHOD_DELETE, Request::METHOD_HEAD, Request::METHOD_OPTIONS,
82
            Request::METHOD_PATCH, Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_TRACE,
83
        ]];
84
    }
85
86
    public function test_respond_500_status_when_unhandled_exception(): void
87
    {
88
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
89
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
90
91
        Router::configure(static function (Routes $routes): void {
92
            $routes->get('expected/uri', FakeControllerWithUnhandledException::class);
93
        });
94
95
        self::assertSame([
96
            [
97
                'header' => 'HTTP/1.1 500 Internal Server Error',
98
                'replace' => true,
99
                'response_code' => 0,
100
            ],
101
        ], $this->headers());
102
    }
103
104
    public function test_handle_handled_exception(): void
105
    {
106
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
107
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
108
109
        Router::configure(static function (Routes $routes, Handlers $handlers): void {
110
            $routes->get('expected/uri', FakeControllerWithUnhandledException::class);
111
112
            $handlers->handle(UnhandledException::class, static function (): string {
113
                header('HTTP/1.1 418 I\'m a teapot');
114
                return 'Handled!';
115
            });
116
        });
117
118
        $this->expectOutputString('Handled!');
119
120
        self::assertSame([
121
            [
122
                'header' => 'HTTP/1.1 418 I\'m a teapot',
123
                'replace' => true,
124
                'response_code' => 0,
125
            ],
126
        ], $this->headers());
127
    }
128
}
129