|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
|
|
5
|
|
|
class RouterTest extends TestCase |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
public static function setUpBeforeClass() |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public static function tearDownAfterClass() |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function tearDown() |
|
23
|
|
|
{ |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testAutoUri() |
|
27
|
|
|
{ |
|
28
|
|
|
//when application run in CLI the first argument will be used as route URI |
|
29
|
|
|
$_SERVER['argv'][1] = ''; |
|
30
|
|
|
|
|
31
|
|
|
$r = new Router(); |
|
32
|
|
|
//remove all all config |
|
33
|
|
|
$r->setRouteConfiguration(array(), false) |
|
34
|
|
|
->setRouteUri() |
|
35
|
|
|
->setRouteSegments() |
|
36
|
|
|
->determineRouteParamsInformation(); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertNull($r->getController()); |
|
39
|
|
|
$this->assertSame('index', $r->getMethod()); |
|
40
|
|
|
$this->assertSame(0, count($r->getArgs())); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testCustomUri() |
|
44
|
|
|
{ |
|
45
|
|
|
$r = new Router(); |
|
46
|
|
|
$r->setRouteUri('users/profile/34/54') |
|
47
|
|
|
->setRouteSegments() |
|
48
|
|
|
->determineRouteParamsInformation(); |
|
49
|
|
|
$this->assertSame('users', $r->getController()); |
|
50
|
|
|
$this->assertSame('profile', $r->getMethod()); |
|
51
|
|
|
$this->assertSame(2, count($r->getArgs())); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testWithCustomConfigControllerMethod() |
|
55
|
|
|
{ |
|
56
|
|
|
$r = new Router(); |
|
57
|
|
|
$r->add('/foo/bar', 'fooController@fooMethod') |
|
|
|
|
|
|
58
|
|
|
->setRouteUri('/foo/bar') |
|
59
|
|
|
->setRouteSegments() |
|
60
|
|
|
->determineRouteParamsInformation(); |
|
61
|
|
|
$this->assertSame('fooController', $r->getController()); |
|
62
|
|
|
$this->assertSame('fooMethod', $r->getMethod()); |
|
63
|
|
|
$this->assertSame(0, count($r->getArgs())); |
|
64
|
|
|
$this->assertNull($r->getModule()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testWithCustomConfigModuleControllerMethod() |
|
68
|
|
|
{ |
|
69
|
|
|
$r = new Router(); |
|
70
|
|
|
$r->add('/foo/bar', 'fooModule#fooController@fooMethod') |
|
|
|
|
|
|
71
|
|
|
->setRouteUri('/foo/bar') |
|
72
|
|
|
->setRouteSegments() |
|
73
|
|
|
->determineRouteParamsInformation(); |
|
74
|
|
|
$this->assertSame('fooController', $r->getController()); |
|
75
|
|
|
$this->assertSame('fooMethod', $r->getMethod()); |
|
76
|
|
|
$this->assertSame('fooModule', $r->getModule()); |
|
77
|
|
|
$this->assertSame(0, count($r->getArgs())); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testWithCustomConfigUsingAnyPattern() |
|
81
|
|
|
{ |
|
82
|
|
|
$r = new Router(); |
|
83
|
|
|
$r->add('/foo/(:any)', 'fooController@fooMethod') |
|
|
|
|
|
|
84
|
|
|
->setRouteUri('/foo/bar123-baz') |
|
85
|
|
|
->setRouteSegments() |
|
86
|
|
|
->determineRouteParamsInformation(); |
|
87
|
|
|
$this->assertSame('fooController', $r->getController()); |
|
88
|
|
|
$this->assertSame('fooMethod', $r->getMethod()); |
|
89
|
|
|
$this->assertSame(1, count($r->getArgs())); |
|
90
|
|
|
$args = $r->getArgs(); |
|
91
|
|
|
$this->assertSame('bar123-baz', $args[0]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testWithCustomConfigUsingNumericPattern() |
|
95
|
|
|
{ |
|
96
|
|
|
$r = new Router(); |
|
97
|
|
|
$r->add('/foo/(:num)', 'fooController@fooMethod') |
|
|
|
|
|
|
98
|
|
|
->setRouteUri('/foo/34') |
|
99
|
|
|
->setRouteSegments() |
|
100
|
|
|
->determineRouteParamsInformation(); |
|
101
|
|
|
$this->assertSame('fooController', $r->getController()); |
|
102
|
|
|
$this->assertSame('fooMethod', $r->getMethod()); |
|
103
|
|
|
$this->assertSame(1, count($r->getArgs())); |
|
104
|
|
|
$args = $r->getArgs(); |
|
105
|
|
|
$this->assertSame('34', $args[0]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testWithCustomConfigUsingAlphaPattern() |
|
109
|
|
|
{ |
|
110
|
|
|
$r = new Router(); |
|
111
|
|
|
$r->add('/foo/(:alpha)', 'fooController@fooMethod') |
|
|
|
|
|
|
112
|
|
|
->setRouteUri('/foo/baz') |
|
113
|
|
|
->setRouteSegments() |
|
114
|
|
|
->determineRouteParamsInformation(); |
|
115
|
|
|
$this->assertSame('fooController', $r->getController()); |
|
116
|
|
|
$this->assertSame('fooMethod', $r->getMethod()); |
|
117
|
|
|
$this->assertSame(1, count($r->getArgs())); |
|
118
|
|
|
$args = $r->getArgs(); |
|
119
|
|
|
$this->assertSame('baz', $args[0]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testWithCustomConfigUsingAlphaNumericPattern() |
|
123
|
|
|
{ |
|
124
|
|
|
$r = new Router(); |
|
125
|
|
|
$r->add('/foo/(:alnum)', 'fooController@fooMethod') |
|
|
|
|
|
|
126
|
|
|
->setRouteUri('/foo/baz123') |
|
127
|
|
|
->setRouteSegments() |
|
128
|
|
|
->determineRouteParamsInformation(); |
|
129
|
|
|
$this->assertSame('fooController', $r->getController()); |
|
130
|
|
|
$this->assertSame('fooMethod', $r->getMethod()); |
|
131
|
|
|
$this->assertSame(1, count($r->getArgs())); |
|
132
|
|
|
$args = $r->getArgs(); |
|
133
|
|
|
$this->assertSame('baz123', $args[0]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testWithCustomConfigUsingMultiplePattern() |
|
137
|
|
|
{ |
|
138
|
|
|
$r = new Router(); |
|
139
|
|
|
$r->add('/foo/(:alpha)/(:num)', 'fooController@fooMethod') |
|
|
|
|
|
|
140
|
|
|
->setRouteUri('/foo/baz/123') |
|
141
|
|
|
->setRouteSegments() |
|
142
|
|
|
->determineRouteParamsInformation(); |
|
143
|
|
|
$this->assertSame('fooController', $r->getController()); |
|
144
|
|
|
$this->assertSame('fooMethod', $r->getMethod()); |
|
145
|
|
|
$this->assertSame(2, count($r->getArgs())); |
|
146
|
|
|
$args = $r->getArgs(); |
|
147
|
|
|
$this->assertSame('baz', $args[0]); |
|
148
|
|
|
$this->assertSame('123', $args[1]); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
} |