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
|
|
|
namespace Tight\Tests; |
27
|
|
|
/** |
28
|
|
|
* Description of RouterTest |
29
|
|
|
* |
30
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
31
|
|
|
*/ |
32
|
|
|
class RouterTest extends \PHPUnit_Framework_TestCase |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
private $router; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @covers Tight\Router::__construct |
39
|
|
|
*/ |
40
|
|
|
public function setUp() { |
41
|
|
|
$this->router = new \Tight\Router(""); |
42
|
|
|
$this->router->get("/hello/", function() { |
43
|
|
|
echo "Hello"; |
44
|
|
|
}); |
45
|
|
|
$this->router->get("/hello/:id", function($id) { |
46
|
|
|
echo "Hello " . $id; |
47
|
|
|
}); |
48
|
|
|
$this->router->post("/world/", function() { |
49
|
|
|
echo "world"; |
50
|
|
|
}); |
51
|
|
|
$this->router->post("/world/:id", function($id) { |
52
|
|
|
echo $id . " world"; |
53
|
|
|
}); |
54
|
|
|
$this->router->map(["get", "post"], "/map/", function() { |
55
|
|
|
echo "map"; |
56
|
|
|
}); |
57
|
|
|
$this->router->update("/upd/", function() { |
58
|
|
|
echo "upd"; |
59
|
|
|
}); |
60
|
|
|
$this->router->delete("/del/", function() { |
61
|
|
|
echo "del"; |
62
|
|
|
}); |
63
|
|
|
$this->router->get("/middle/", function() { |
64
|
|
|
echo "mid1 "; |
65
|
|
|
}, function() { |
66
|
|
|
echo "mid2 "; |
67
|
|
|
}, function() { |
68
|
|
|
echo "end"; |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function tearDown() { |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
* @covers Tight\Router::get |
79
|
|
|
* @covers Tight\Router::__construct |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function testRouterGet() { |
|
|
|
|
82
|
|
|
$output = ""; |
83
|
|
|
$this->router->dispatch("/hello/", "get"); |
84
|
|
|
$this->expectOutputString("Hello"); |
85
|
|
|
$output .= "Hello"; |
86
|
|
|
$this->router->dispatch("/hello/", "post"); |
87
|
|
|
$this->expectOutputString($output . "Page not found"); |
88
|
|
|
$output .= "Page not found"; |
89
|
|
|
$this->router->dispatch("/hello/world", "get"); |
90
|
|
|
$this->expectOutputString($output . "Hello world"); |
91
|
|
|
$output.="Hello world"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @test |
96
|
|
|
* @covers Tight\Router::post |
97
|
|
|
* @covers Tight\Router::dispatch |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function testRouterPost() { |
|
|
|
|
100
|
|
|
$output = ""; |
101
|
|
|
$this->router->dispatch("/world/", "post"); |
102
|
|
|
$this->expectOutputString($output . "world"); |
103
|
|
|
$output .= "world"; |
104
|
|
|
$this->router->dispatch("/world/", "get"); |
105
|
|
|
$this->expectOutputString($output . "Page not found"); |
106
|
|
|
$output .= "Page not found"; |
107
|
|
|
$this->router->dispatch("/world/hello", "post"); |
108
|
|
|
$this->expectOutputString($output . "hello world"); |
109
|
|
|
$output .= "Hello world"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @test |
114
|
|
|
* @covers Tight\Router::map |
115
|
|
|
* @covers Tight\Router::url |
116
|
|
|
* @covers Tight\Router::dispatch |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function testRouterMap() { |
|
|
|
|
119
|
|
|
$output = ""; |
120
|
|
|
$this->router->dispatch("/map/", "post"); |
121
|
|
|
$this->expectOutputString($output . "map"); |
122
|
|
|
$output .= "map"; |
123
|
|
|
$this->router->dispatch("/map/", "get"); |
124
|
|
|
$this->expectOutputString($output . "map"); |
125
|
|
|
$output .= "map"; |
126
|
|
|
$this->router->dispatch("/map/", "options"); |
127
|
|
|
$this->expectOutputString($output . "Page not found"); |
128
|
|
|
$output .= "Page not found"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @test |
133
|
|
|
* @covers Tight\Router::update |
134
|
|
|
* @covers Tight\Router::dispatch |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function testRouteUpdate() { |
|
|
|
|
137
|
|
|
$output = ""; |
138
|
|
|
$this->router->dispatch("/upd/", "update"); |
139
|
|
|
$this->expectOutputString($output . "upd"); |
140
|
|
|
$output .= "upd"; |
141
|
|
|
$this->router->dispatch("/upd/", "get"); |
142
|
|
|
$this->expectOutputString($output . "Page not found"); |
143
|
|
|
$output .= "Page not found"; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @test |
148
|
|
|
* @covers Tight\Router::delete |
149
|
|
|
* @covers Tight\Router::dispatch |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function testRouteDelete() { |
|
|
|
|
152
|
|
|
$output = ""; |
153
|
|
|
$this->router->dispatch("/del/", "delete"); |
154
|
|
|
$this->expectOutputString($output . "del"); |
155
|
|
|
$output .= "del"; |
156
|
|
|
$this->router->dispatch("/del/", "post"); |
157
|
|
|
$this->expectOutputString($output . "Page not found"); |
158
|
|
|
$output .= "Page not found"; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @test |
163
|
|
|
* @covers Tight\Route::setMiddleware |
164
|
|
|
* @covers Tight\Router::dispatch |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public function testRouteMiddleware() { |
|
|
|
|
167
|
|
|
$output = ""; |
168
|
|
|
$this->router->dispatch("/middle/", "get"); |
169
|
|
|
$this->expectOutputString($output . "mid1 mid2 end"); |
170
|
|
|
$output .= "mid1 mid2 end"; |
171
|
|
|
$this->router->dispatch("/middle/", "post"); |
172
|
|
|
$this->expectOutputString($output . "Page not found"); |
173
|
|
|
$output .= "Page not found"; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @test |
178
|
|
|
* @covers Tight\Router::notFound |
179
|
|
|
*/ |
180
|
|
|
public function testRouteNotFound() { |
181
|
|
|
$this->router->notFound(function() { |
182
|
|
|
echo "Error 404"; |
183
|
|
|
}); |
184
|
|
|
$this->router->dispatch("/invalid/url", "post"); |
185
|
|
|
$this->expectOutputString("Error 404"); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.