Passed
Push — main ( 323c81...ae7e9f )
by Chema
07:30 queued 13s
created

ErrorHandlingTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 33
c 0
b 0
f 0
dl 0
loc 66
rs 10

4 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 notMatchesMethodsProvider() 0 8 1
A test_respond_404_status_when_method_does_not_match() 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\Router;
9
use Gacela\Router\Routes;
10
use GacelaTest\Feature\HeaderTestCase;
11
use GacelaTest\Feature\Router\Fixtures\FakeController;
12
use Generator;
13
14
final class ErrorHandlingTest extends HeaderTestCase
15
{
16
    public function test_respond_404_status_when_uri_does_not_match(): void
17
    {
18
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
19
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_OPTIONS;
20
21
        Router::configure(static function (): void {
22
        });
23
24
        self::assertSame([
25
            [
26
                'header' => 'HTTP/1.0 404 Not Found',
27
                'replace' => true,
28
                'response_code' => 0,
29
            ],
30
        ], $this->headers());
31
    }
32
33
    public function test_respond_404_status_when_method_does_not_match(): void
34
    {
35
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
36
        $_SERVER['REQUEST_METHOD'] = 'GET';
37
38
        Router::configure(static function (Routes $routes): void {
39
            $routes->post('expected/uri', FakeController::class, 'basicAction');
40
        });
41
42
        self::assertSame([
43
            [
44
                'header' => 'HTTP/1.0 404 Not Found',
45
                'replace' => true,
46
                'response_code' => 0,
47
            ],
48
        ], $this->headers());
49
    }
50
51
    /**
52
     * @dataProvider notMatchesMethodsProvider
53
     */
54
    public function test_respond_404_status_when_not_matches_match_methods(string $testMethod, array $givenMethods): void
55
    {
56
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
57
        $_SERVER['REQUEST_METHOD'] = $testMethod;
58
59
        Router::configure(static function (Routes $routes) use ($givenMethods): void {
60
            $routes->match($givenMethods, 'expected/uri', FakeController::class, 'basicAction');
61
        });
62
63
        self::assertSame([
64
            [
65
                'header' => 'HTTP/1.0 404 Not Found',
66
                'replace' => true,
67
                'response_code' => 0,
68
            ],
69
        ], $this->headers());
70
    }
71
72
    public function notMatchesMethodsProvider(): Generator
73
    {
74
        yield [Request::METHOD_PUT, [Request::METHOD_GET, Request::METHOD_POST]];
75
        yield [Request::METHOD_OPTIONS, [Request::METHOD_GET, Request::METHOD_POST]];
76
        yield [Request::METHOD_GET, [Request::METHOD_PATCH, Request::METHOD_PUT, Request::METHOD_DELETE, Request::METHOD_POST]];
77
        yield [Request::METHOD_CONNECT, [
78
            Request::METHOD_GET, Request::METHOD_DELETE, Request::METHOD_HEAD, Request::METHOD_OPTIONS,
79
            Request::METHOD_PATCH, Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_TRACE,
80
        ]];
81
    }
82
}
83