Passed
Push — main ( 80dd2f...323c81 )
by Chema
01:10 queued 13s
created

RouterMatchTest::anyHttpMethodProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\UnsupportedHttpMethodException;
9
use Gacela\Router\Router;
10
use Gacela\Router\Routes;
11
use GacelaTest\Feature\Router\Fixtures\FakeController;
12
use Generator;
13
use PHPUnit\Framework\TestCase;
14
15
final class RouterMatchTest extends TestCase
16
{
17
    public function test_respond_when_everything_matches(): void
18
    {
19
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
20
        $_SERVER['REQUEST_METHOD'] = 'GET';
21
22
        $this->expectOutputString('Expected!');
23
24
        Router::configure(static function (Routes $routes): void {
25
            $routes->get('expected/uri', FakeController::class, 'basicAction');
26
        });
27
    }
28
29
    public function test_respond_only_the_first_match(): void
30
    {
31
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
32
        $_SERVER['REQUEST_METHOD'] = 'GET';
33
34
        $this->expectOutputString('Expected!');
35
36
        Router::configure(static function (Routes $routes): void {
37
            $routes->get('expected/uri', FakeController::class, 'basicAction');
38
            $routes->get('expected/{param}', FakeController::class, 'stringParamAction');
39
        });
40
    }
41
42
    public function test_optional_argument(): void
43
    {
44
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional';
45
        $_SERVER['REQUEST_METHOD'] = 'GET';
46
47
        $this->expectOutputString('Expected!');
48
49
        Router::configure(static function (Routes $routes): void {
50
            $routes->get('optional/{param?}', FakeController::class, 'basicAction');
51
        });
52
    }
53
54
    public function test_multiple_optional_argument(): void
55
    {
56
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional';
57
        $_SERVER['REQUEST_METHOD'] = 'GET';
58
59
        $this->expectOutputString('Expected!');
60
61
        Router::configure(static function (Routes $routes): void {
62
            $routes->get('optional/{param1?}/{param2?}', FakeController::class, 'basicAction');
63
        });
64
    }
65
66
    public function test_thrown_exception_when_method_does_not_exist(): void
67
    {
68
        $this->expectException(UnsupportedHttpMethodException::class);
69
70
        Router::configure(static function (Routes $routes): void {
71
            $routes->invalidName('', FakeController::class);
0 ignored issues
show
Bug introduced by
The method invalidName() does not exist on Gacela\Router\Routes. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            $routes->/** @scrutinizer ignore-call */ 
72
                     invalidName('', FakeController::class);
Loading history...
72
        });
73
    }
74
75
    /**
76
     * @dataProvider anyHttpMethodProvider
77
     */
78
    public function test_any_http_method(string $httpMethod): void
79
    {
80
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
81
        $_SERVER['REQUEST_METHOD'] = $httpMethod;
82
83
        $this->expectOutputString('Expected!');
84
85
        Router::configure(static function (Routes $routes): void {
86
            $routes->any('expected/uri', FakeController::class, 'basicAction');
87
        });
88
    }
89
90
    public function anyHttpMethodProvider(): Generator
91
    {
92
        yield ['METHOD_GET' => Request::METHOD_GET];
93
        yield ['METHOD_CONNECT' => Request::METHOD_CONNECT];
94
        yield ['METHOD_DELETE' => Request::METHOD_DELETE];
95
        yield ['METHOD_HEAD' => Request::METHOD_HEAD];
96
        yield ['METHOD_OPTIONS' => Request::METHOD_OPTIONS];
97
        yield ['METHOD_PATCH' => Request::METHOD_PATCH];
98
        yield ['METHOD_POST' => Request::METHOD_POST];
99
        yield ['METHOD_PUT' => Request::METHOD_PUT];
100
        yield ['METHOD_TRACE' => Request::METHOD_TRACE];
101
    }
102
103
    /**
104
     * @dataProvider matchesMethodsProvider
105
     */
106
    public function test_match_matches_all_its_methods(string $testMethod, array $givenMethods): void
107
    {
108
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
109
        $_SERVER['REQUEST_METHOD'] = $testMethod;
110
111
        $this->expectOutputString('Expected!');
112
113
        Router::configure(static function (Routes $routes) use ($givenMethods): void {
114
            $routes->match($givenMethods, 'expected/uri', FakeController::class, 'basicAction');
115
        });
116
    }
117
118
    public function matchesMethodsProvider(): Generator
119
    {
120
        yield [Request::METHOD_GET, [Request::METHOD_GET, Request::METHOD_POST]];
121
        yield [Request::METHOD_POST, [Request::METHOD_GET, Request::METHOD_POST]];
122
        yield [Request::METHOD_PATCH, [Request::METHOD_PATCH, Request::METHOD_PUT]];
123
        yield [Request::METHOD_PUT, [Request::METHOD_PATCH, Request::METHOD_PUT]];
124
    }
125
126
    /**
127
     * @dataProvider notMatchesMethodsProvider
128
     */
129
    public function test_match_does_not_matches_other_methods(string $testMethod, array $givenMethods): void
130
    {
131
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
132
        $_SERVER['REQUEST_METHOD'] = $testMethod;
133
134
        $this->expectOutputString('');
135
136
        Router::configure(static function (Routes $routes) use ($givenMethods): void {
137
            $routes->match($givenMethods, 'expected/uri', FakeController::class, 'basicAction');
138
        });
139
    }
140
141
    public function notMatchesMethodsProvider(): Generator
142
    {
143
        yield [Request::METHOD_PUT, [Request::METHOD_GET, Request::METHOD_POST]];
144
        yield [Request::METHOD_OPTIONS, [Request::METHOD_GET, Request::METHOD_POST]];
145
        yield [Request::METHOD_GET, [Request::METHOD_PATCH, Request::METHOD_PUT, Request::METHOD_DELETE, Request::METHOD_POST]];
146
        yield [Request::METHOD_CONNECT, [
147
            Request::METHOD_GET, Request::METHOD_DELETE, Request::METHOD_HEAD, Request::METHOD_OPTIONS,
148
            Request::METHOD_PATCH, Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_TRACE,
149
        ]];
150
    }
151
}
152