Passed
Push — main ( deaffd...dea3f5 )
by Chema
55s queued 13s
created

test_pass_optional_string_params_to_the_action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Feature\Router;
6
7
use Gacela\Router\Configure\Routes;
8
use Gacela\Router\Entities\Request;
9
use Gacela\Router\Router;
10
use GacelaTest\Feature\Router\Fixtures\FakeController;
11
use GacelaTest\Feature\Router\Fixtures\FakeControllerWithRequest;
12
use Generator;
13
use PHPUnit\Framework\TestCase;
14
15
final class RouterParamTest extends TestCase
16
{
17
    private const PROVIDER_TRIES = 10;
18
19
    public function test_pass_many_params_to_the_action(): void
20
    {
21
        $params = ['foo', 'bar', 'baz'];
22
23
        $_SERVER['REQUEST_URI'] = "https://example.org/{$params[0]}/{$params[1]}/{$params[2]}";
24
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
25
26
        $this->expectOutputString("The params are '{$params[0]}', '{$params[1]}' and '{$params[2]}'!");
27
28
        $router = new Router(static function (Routes $routes): void {
29
            $routes->get('{firstParam}/{secondParam}/{thirdParam}', FakeController::class, 'manyParamsAction');
30
        });
31
        $router->run();
32
    }
33
34
    public function test_pass_associated_params_by_name_to_the_action(): void
35
    {
36
        $params = ['foo', 'bar', 'baz'];
37
38
        $_SERVER['REQUEST_URI'] = "https://example.org/{$params[0]}/{$params[1]}/{$params[2]}";
39
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
40
41
        $this->expectOutputString("The params are '{$params[1]}', '{$params[0]}' and '{$params[2]}'!");
42
43
        $router = new Router(static function (Routes $routes): void {
44
            $routes->get('{secondParam}/{firstParam}/{thirdParam}', FakeController::class, 'manyParamsAction');
45
        });
46
        $router->run();
47
    }
48
49
    /**
50
     * @dataProvider stringProvider
51
     */
52
    public function test_pass_string_params_to_the_action(string $string): void
53
    {
54
        $_SERVER['REQUEST_URI'] = "https://example.org/expected/string/is/{$string}";
55
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
56
57
        $this->expectOutputString("The 'string' param is '{$string}'!");
58
59
        $router = new Router(static function (Routes $routes): void {
60
            $routes->get('expected/string/is/{param}', FakeController::class, 'stringParamAction');
61
        });
62
        $router->run();
63
    }
64
65
    public function stringProvider(): Generator
66
    {
67
        for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) {
68
            $randomString = 'word-' . mt_rand();
69
            yield $randomString => ['string' => $randomString];
70
        }
71
    }
72
73
    public function test_do_not_pass_optional_string_params_to_the_action(): void
74
    {
75
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/string/is';
76
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
77
78
        $this->expectOutputString("The optional 'string' param is 'bob'!");
79
80
        $router = new Router(static function (Routes $routes): void {
81
            $routes->get('expected/string/is/{param?}', FakeController::class, 'optionalStringParamAction');
82
        });
83
        $router->run();
84
    }
85
86
    public function test_pass_optional_string_params_to_the_action(): void
87
    {
88
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/string/is/alice';
89
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
90
91
        $this->expectOutputString("The optional 'string' param is 'alice'!");
92
93
        $router = new Router(static function (Routes $routes): void {
94
            $routes->get('expected/string/is/{param?}', FakeController::class, 'optionalStringParamAction');
95
        });
96
        $router->run();
97
    }
98
99
    /**
100
     * @dataProvider intProvider
101
     */
102
    public function test_pass_int_params_to_the_action(string $int): void
103
    {
104
        $_SERVER['REQUEST_URI'] = "https://example.org/expected/integer/is/{$int}";
105
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
106
107
        $this->expectOutputString("The 'int' param is '{$int}'!");
108
109
        $router = new Router(static function (Routes $routes): void {
110
            $routes->get('expected/integer/is/{param}', FakeController::class, 'intParamAction');
111
        });
112
        $router->run();
113
    }
114
115
    public function intProvider(): Generator
116
    {
117
        for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) {
118
            $randomInt = (string)random_int(1, 9999);
119
            yield "#{$randomInt}" => ['int' => $randomInt];
120
        }
121
    }
122
123
    /**
124
     * @dataProvider floatProvider
125
     */
126
    public function test_pass_float_params_to_the_action(string $float): void
127
    {
128
        $_SERVER['REQUEST_URI'] = "https://example.org/expected/float/is/{$float}";
129
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
130
131
        $this->expectOutputString("The 'float' param is '{$float}'!");
132
133
        $router = new Router(static function (Routes $routes): void {
134
            $routes->get('expected/float/is/{param}', FakeController::class, 'floatParamAction');
135
        });
136
        $router->run();
137
    }
138
139
    public function floatProvider(): Generator
140
    {
141
        for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) {
142
            $randomFloat = (string)mt_rand();
143
            yield "#{$randomFloat}" => ['float' => $randomFloat];
144
        }
145
    }
146
147
    /**
148
     * @dataProvider boolProvider
149
     */
150
    public function test_pass_bool_params_to_the_action(string $given, string $expected): void
151
    {
152
        $_SERVER['REQUEST_URI'] = "https://example.org/expected/bool/is/{$given}";
153
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
154
155
        $this->expectOutputString("The 'bool' param is '{$expected}'!");
156
157
        $router = new Router(static function (Routes $routes): void {
158
            $routes->get('expected/bool/is/{param}', FakeController::class, 'boolParamAction');
159
        });
160
        $router->run();
161
    }
162
163
    public function boolProvider(): iterable
164
    {
165
        yield 'true' => ['given' => 'true', 'expected' => 'true'];
166
        yield 'false' => ['given' => 'false', 'expected' => 'false'];
167
        yield '1' => ['given' => '1', 'expected' => 'true'];
168
        yield '0' => ['given' => '0', 'expected' => 'false'];
169
    }
170
171
    public function test_priori_post_params_over_get(): void
172
    {
173
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
174
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
175
        $_GET['name'] = 'Unexpected!';
176
        $_POST['name'] = 'Expected!';
177
178
        $this->expectOutputString('Expected!');
179
180
        $router = new Router(static function (Routes $routes): void {
181
            $routes->get('expected/uri', FakeControllerWithRequest::class);
182
        });
183
        $router->run();
184
    }
185
}
186