Passed
Push — main ( 50aede...a9f24b )
by Chema
01:07 queued 13s
created

test_priori_post_params_over_get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
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\Router;
9
use Gacela\Router\Routes;
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
    /**
74
     * @dataProvider intProvider
75
     */
76
    public function test_pass_int_params_to_the_action(string $int): void
77
    {
78
        $_SERVER['REQUEST_URI'] = "https://example.org/expected/integer/is/{$int}";
79
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
80
81
        $this->expectOutputString("The 'int' param is '{$int}'!");
82
83
        $router = new Router(static function (Routes $routes): void {
84
            $routes->get('expected/integer/is/{param}', FakeController::class, 'intParamAction');
85
        });
86
        $router->run();
87
    }
88
89
    public function intProvider(): Generator
90
    {
91
        for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) {
92
            $randomInt = (string)random_int(1, 9999);
93
            yield "#{$randomInt}" => ['int' => $randomInt];
94
        }
95
    }
96
97
    /**
98
     * @dataProvider floatProvider
99
     */
100
    public function test_pass_float_params_to_the_action(string $float): void
101
    {
102
        $_SERVER['REQUEST_URI'] = "https://example.org/expected/float/is/{$float}";
103
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
104
105
        $this->expectOutputString("The 'float' param is '{$float}'!");
106
107
        $router = new Router(static function (Routes $routes): void {
108
            $routes->get('expected/float/is/{param}', FakeController::class, 'floatParamAction');
109
        });
110
        $router->run();
111
    }
112
113
    public function floatProvider(): Generator
114
    {
115
        for ($try = 0; $try < self::PROVIDER_TRIES; ++$try) {
116
            $randomFloat = (string)mt_rand();
117
            yield "#{$randomFloat}" => ['float' => $randomFloat];
118
        }
119
    }
120
121
    /**
122
     * @dataProvider boolProvider
123
     */
124
    public function test_pass_bool_params_to_the_action(string $given, string $expected): void
125
    {
126
        $_SERVER['REQUEST_URI'] = "https://example.org/expected/bool/is/{$given}";
127
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
128
129
        $this->expectOutputString("The 'bool' param is '{$expected}'!");
130
131
        $router = new Router(static function (Routes $routes): void {
132
            $routes->get('expected/bool/is/{param}', FakeController::class, 'boolParamAction');
133
        });
134
        $router->run();
135
    }
136
137
    public function boolProvider(): iterable
138
    {
139
        yield 'true' => ['given' => 'true', 'expected' => 'true'];
140
        yield 'false' => ['given' => 'false', 'expected' => 'false'];
141
        yield '1' => ['given' => '1', 'expected' => 'true'];
142
        yield '0' => ['given' => '0', 'expected' => 'false'];
143
    }
144
145
    public function test_priori_post_params_over_get(): void
146
    {
147
        $_SERVER['REQUEST_URI'] = 'https://example.org/expected/uri';
148
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
149
        $_GET['name'] = 'Unexpected!';
150
        $_POST['name'] = 'Expected!';
151
152
        $this->expectOutputString('Expected!');
153
154
        $router = new Router(static function (Routes $routes): void {
155
            $routes->get('expected/uri', FakeControllerWithRequest::class);
156
        });
157
        $router->run();
158
    }
159
}
160