1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace Mezon\Router\Tests\Base; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
10
|
|
|
*/ |
11
|
|
|
abstract class ExtractParametersTestClass extends BaseRouterUnitTestClass |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default setup |
16
|
|
|
* |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
* @see TestCase::setUp() |
19
|
|
|
*/ |
20
|
|
|
public function setUp(): void |
21
|
|
|
{ |
22
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Testing hasParam method |
27
|
|
|
*/ |
28
|
|
|
public function testValidatingParameter(): void |
29
|
|
|
{ |
30
|
|
|
// setup |
31
|
|
|
$router = $this->getRouter(); |
32
|
|
|
$router->addRoute('/catalog/[i:foo]/', function (): void { |
33
|
|
|
// do nothing |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
$router->callRoute('/catalog/1/'); |
37
|
|
|
|
38
|
|
|
// test body and assertions |
39
|
|
|
$this->assertTrue($router->hasParam('foo')); |
40
|
|
|
$this->assertFalse($router->hasParam('unexisting')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Testing parameter extractor. |
45
|
|
|
*/ |
46
|
|
|
public function testValidExtractedParameter(): void |
47
|
|
|
{ |
48
|
|
|
$router = $this->getRouter(); |
49
|
|
|
$router->addRoute('/catalog/[a:cat_id]/', /** |
50
|
|
|
* |
51
|
|
|
* @param string $route |
52
|
|
|
* route |
53
|
|
|
* @param string[] $parameters |
54
|
|
|
*/ |
55
|
|
|
function (string $route, array $parameters): string { |
56
|
|
|
return $parameters['cat_id']; |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
/** @var string $result */ |
60
|
|
|
$result = $router->callRoute('/catalog/foo/'); |
61
|
|
|
|
62
|
|
|
$this->assertEquals('foo', $result); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Testing parameter extractor. |
67
|
|
|
*/ |
68
|
|
|
public function testValidExtractedParameters(): void |
69
|
|
|
{ |
70
|
|
|
$router = $this->getRouter(); |
71
|
|
|
$router->addRoute('/catalog/[a:cat_id]/[i:item_id]', /** |
72
|
|
|
* |
73
|
|
|
* @param string $route |
74
|
|
|
* route |
75
|
|
|
* @param string[] $parameters |
76
|
|
|
*/ |
77
|
|
|
function (string $route, array $parameters): string { |
78
|
|
|
return $parameters['cat_id'] . $parameters['item_id']; |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
/** @var string $result */ |
82
|
|
|
$result = $router->callRoute('catalog/foo/1024'); |
83
|
|
|
|
84
|
|
|
$this->assertEquals('foo1024', $result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Testing multyple routes |
89
|
|
|
*/ |
90
|
|
|
public function testMultyple(): void |
91
|
|
|
{ |
92
|
|
|
// setup |
93
|
|
|
$router = $this->getRouter(); |
94
|
|
|
for ($i = 0; $i < 15; $i ++) { |
95
|
|
|
$router->addRoute('/multiple/' . $i . '/[i:id]', function () { |
96
|
|
|
return 'done!'; |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// test body |
101
|
|
|
/** @var string $result */ |
102
|
|
|
$result = $router->callRoute('/multiple/' . rand(0, 14) . '/12345'); |
103
|
|
|
|
104
|
|
|
// assertions |
105
|
|
|
$this->assertEquals('done!', $result); |
106
|
|
|
$this->assertEquals('12345', $router->getParam('id')); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|