|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]). |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Tight\Tests; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Description of RouterTest |
|
31
|
|
|
* |
|
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
|
33
|
|
|
*/ |
|
34
|
|
|
class RouterTest extends \PHPUnit_Framework_TestCase |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
private $router; |
|
38
|
|
|
private $a = "a"; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @covers Tight\Router::__construct |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setUp() { |
|
44
|
|
|
$this->router = new \Tight\Router("/"); |
|
45
|
|
|
$this->router->get("/hello/", function() { |
|
46
|
|
|
return "Hello"; |
|
47
|
|
|
}); |
|
48
|
|
|
$this->router->get("/hello/:id", function($id) { |
|
49
|
|
|
return "Hello " . $id; |
|
50
|
|
|
}); |
|
51
|
|
|
$this->router->post("/world/", function() { |
|
52
|
|
|
return "world"; |
|
53
|
|
|
}); |
|
54
|
|
|
$this->router->post("/world/:id", function($id) { |
|
55
|
|
|
return $id . " world"; |
|
56
|
|
|
}); |
|
57
|
|
|
$this->router->map(["get", "post"], "/map/", function() { |
|
58
|
|
|
return "map"; |
|
59
|
|
|
}); |
|
60
|
|
|
$this->router->update("/upd/", function() { |
|
61
|
|
|
return "upd"; |
|
62
|
|
|
}); |
|
63
|
|
|
$this->router->delete("/del/", function() { |
|
64
|
|
|
return "del"; |
|
65
|
|
|
}); |
|
66
|
|
|
$this->router->get("/middle/", function() { |
|
67
|
|
|
return "mid1 "; |
|
68
|
|
|
}, function() { |
|
69
|
|
|
return "mid2 "; |
|
70
|
|
|
}, function() { |
|
71
|
|
|
return "end"; |
|
72
|
|
|
}); |
|
73
|
|
|
$this->router->notFound(function() { |
|
74
|
|
|
return "Error 404: Page not found"; |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function tearDown() { |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @test |
|
84
|
|
|
* @covers \Tight\Router::dispatch |
|
85
|
|
|
* @covers \Tight\Router::getRoutes |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testRouterDispatch() { |
|
88
|
|
|
$firstRoute = $this->router->getRoutes()[0]; |
|
89
|
|
|
$actual = $this->router->dispatch("/hello/", "get"); |
|
90
|
|
|
$expected = $firstRoute->dispatch(); |
|
91
|
|
|
$this->assertEquals($actual, $expected); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @test |
|
96
|
|
|
* @covers Tight\Router::notFound |
|
97
|
|
|
* @covers \Tight\Router::dispatchNotFound |
|
98
|
|
|
* @depends testRouterDispatch |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testRouterNotFound() { |
|
101
|
|
|
$expected = $this->router->dispatch("/invalid/url/", "post"); |
|
102
|
|
|
$this->assertEquals($expected, "Error 404: Page not found"); |
|
103
|
|
|
return $expected; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @test |
|
108
|
|
|
* @covers Tight\Router::get |
|
109
|
|
|
* @covers Tight\Router::__construct |
|
110
|
|
|
* @depends testRouterNotFound |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testRouterGet($notFound) { |
|
113
|
|
|
$this->assertEquals($this->router->dispatch("/hello/", "get"), "Hello"); |
|
114
|
|
|
$this->assertEquals($this->router->dispatch("/hello/", "post"), $notFound); |
|
115
|
|
|
$this->assertEquals($this->router->dispatch("/hello/world", "get"), "Hello world"); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @test |
|
120
|
|
|
* @covers Tight\Router::post |
|
121
|
|
|
* @depends testRouterNotFound |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testRouterPost($notFound) { |
|
124
|
|
|
$this->assertEquals($this->router->dispatch("/world/", "post"), "world"); |
|
125
|
|
|
$this->assertEquals($this->router->dispatch("/world/", "get"), $notFound); |
|
126
|
|
|
$this->assertEquals($this->router->dispatch("/world/hello", "post"), "hello world"); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @test |
|
131
|
|
|
* @covers Tight\Router::map |
|
132
|
|
|
* @covers Tight\Router::url |
|
133
|
|
|
* @depends testRouterNotFound |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testRouterMap($notFound) { |
|
136
|
|
|
$this->assertEquals($this->router->dispatch("/map/", "post"), "map"); |
|
137
|
|
|
$this->assertEquals($this->router->dispatch("/map/", "get"), "map"); |
|
138
|
|
|
$this->assertEquals($this->router->dispatch("/map/", "options"), $notFound); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @test |
|
143
|
|
|
* @covers Tight\Router::update |
|
144
|
|
|
* @depends testRouterNotFound |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testRouteUpdate($notFound) { |
|
147
|
|
|
$this->assertEquals($this->router->dispatch("/upd/", "update"), "upd"); |
|
148
|
|
|
$this->assertEquals($this->router->dispatch("/upd/", "get"), $notFound); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @test |
|
153
|
|
|
* @covers Tight\Router::delete |
|
154
|
|
|
* @depends testRouterNotFound |
|
155
|
|
|
*/ |
|
156
|
|
|
public function testRouteDelete($notFound) { |
|
157
|
|
|
$this->assertEquals($this->router->dispatch("/del/", "delete"), "del"); |
|
158
|
|
|
$this->assertEquals($this->router->dispatch("/del/", "post"), $notFound); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @test |
|
163
|
|
|
* @covers Tight\Route::setMiddleware |
|
164
|
|
|
* @depends testRouterNotFound |
|
165
|
|
|
*/ |
|
166
|
|
|
public function testRouteMiddleware($notFound) { |
|
167
|
|
|
$this->assertEquals($this->router->dispatch("/middle/", "get"), "mid1 mid2 end"); |
|
168
|
|
|
$this->assertEquals($this->router->dispatch("/middle/", "post"), $notFound); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @test |
|
173
|
|
|
*/ |
|
174
|
|
|
public function testRouterGetRequestUrn() { |
|
|
|
|
|
|
175
|
|
|
$this->assertEquals($_SERVER['REQUEST_URI'], $this->router->getRequestUrn()); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @test |
|
180
|
|
|
* @covers \Tight\Router::runMvc |
|
181
|
|
|
* @covers \Tight\Router::registerClasses |
|
182
|
|
|
*/ |
|
183
|
|
|
public function testRouterMvc() { |
|
184
|
|
|
/* $this->router->runMvc(); |
|
|
|
|
|
|
185
|
|
|
$this->expectOutputString("Page not found"); */ |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.