Test Failed
Pull Request — main (#22)
by Chema
02:31
created

test_mandatory_and_optional_argument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
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'] = Request::METHOD_GET;
21
22
        $this->expectOutputString('Expected!');
23
24
        $router = new Router(static function (Routes $routes): void {
25
            $routes->get('expected/uri', FakeController::class, 'basicAction');
26
        });
27
        $router->run();
28
    }
29
30
    public function test_respond_only_the_first_match(): void
31
    {
32
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
33
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
34
35
        $this->expectOutputString('Expected!');
36
37
        $router = new Router(static function (Routes $routes): void {
38
            $routes->get('expected/uri', FakeController::class, 'basicAction');
39
            $routes->get('expected/{param}', FakeController::class, 'stringParamAction');
40
        });
41
        $router->run();
42
    }
43
44
    public function test_optional_argument(): void
45
    {
46
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional';
47
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
48
49
        $this->expectOutputString('Expected!');
50
51
        $router = new Router(static function (Routes $routes): void {
52
            $routes->get('optional/{param?}', FakeController::class, 'basicAction');
53
        });
54
        $router->run();
55
    }
56
57
    public function test_multiple_optional_argument(): void
58
    {
59
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional';
60
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
61
62
        $this->expectOutputString('Expected!');
63
64
        $router = new Router(static function (Routes $routes): void {
65
            $routes->get('optional/{param1?}/{param2?}', FakeController::class, 'basicAction');
66
        });
67
        $router->run();
68
    }
69
70
    public function test_mandatory_and_optional_argument(): void
71
    {
72
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/bob';
73
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
74
75
        $this->expectOutputString("The 'string' param is 'bob'!");
76
77
        Router::configure(static function (Routes $routes): void {
0 ignored issues
show
Bug Best Practice introduced by
The method Gacela\Router\Router::configure() is not static, but was called statically. ( Ignorable by Annotation )

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

77
        Router::/** @scrutinizer ignore-call */ 
78
                configure(static function (Routes $routes): void {
Loading history...
78
            $routes->get('optional/{param}/{param?}', FakeController::class, 'stringParamAction');
79
        });
80
    }
81
82
    public function test_thrown_exception_when_method_does_not_exist(): void
83
    {
84
        $this->expectException(UnsupportedHttpMethodException::class);
85
86
        $router = new Router(static function (Routes $routes): void {
87
            $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

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