a tiny mvc framework for php using php-activerecord
1<?php
2
3require(__DIR__ . "/../lib/halfmoon.php");
4
5class RouterTest extends PHPUnit_Framework_TestCase {
6 public function testSetupRoutes() {
7 HalfMoon\Router::clearRoutes();
8
9 $this->assertEquals(0, count(HalfMoon\Router::getRoutes()));
10 $this->assertEquals(0, count(HalfMoon\Router::getRootRoutes()));
11
12 $added_routes = 0;
13 $added_root_routes = 0;
14
15 HalfMoon\Router::addRoute(array(
16 "url" => "logout",
17 "controller" => "login",
18 "action" => "logout2",
19 "conditions" => array(
20 "hostname" => "/^www(\d+)\.example\.(com|net)$/i"),
21 ));
22 $added_routes++;
23
24 HalfMoon\Router::addRoute(array(
25 "url" => "logout",
26 "controller" => "login",
27 "action" => "logout",
28 ));
29 $added_routes++;
30
31 HalfMoon\Router::addRoute(array(
32 "url" => "*globby",
33 "controller" => "login",
34 "action" => "globtest",
35 "conditions" => array(
36 "globby" => "/^globte.t$/"
37 ),
38 ));
39 $added_routes++;
40
41 HalfMoon\Router::addRoute(array(
42 "url" => "stub/:controller/:action/:id",
43 "action" => "stub_:action",
44 ));
45 $added_routes++;
46
47 HalfMoon\Router::addRoute(array(
48 "url" => "zero_test/:id",
49 "controller" => "zero_test",
50 "action" => "show",
51 "conditions" => array("id" => "/^[0-9]+$/"),
52 ));
53 $added_routes++;
54
55 HalfMoon\Router::addRoute(array(
56 "url" => ":tag/:message",
57 "controller" => "messages",
58 "action" => "show_tagged",
59 "conditions" => array("tag" => "/^[A-Za-z0-9_\-]+$/",
60 "message" => "/^\d+$/")
61 ));
62 $added_routes++;
63
64 HalfMoon\Router::addRoute(array(
65 "url" => ":controller/:action/*globbed",
66 "conditions" => array("controller" => "someglob",
67 "action" => "/^[a-z]+$/"),
68 ));
69 $added_routes++;
70
71
72 HalfMoon\Router::addRootRoute(array(
73 "controller" => "root2",
74 "conditions" => array("hostname" => "www.example2.com"),
75 ));
76 $added_root_routes++;
77
78 HalfMoon\Router::addRootRoute(array(
79 "controller" => "root",
80 ));
81 $added_root_routes++;
82
83
84 $this->assertEquals($added_routes,
85 count(HalfMoon\Router::getRoutes()));
86 $this->assertEquals($added_root_routes,
87 count(HalfMoon\Router::getRootRoutes()));
88 }
89
90 /**
91 * @depends testSetupRoutes
92 */
93 public function testBasicRouteParsing() {
94 $route = HalfMoon\Router::routeRequest(
95 $this->request_for("/logout"));
96
97 $this->assertEquals("login", $route["controller"]);
98 $this->assertEquals("logout", $route["action"]);
99 $this->assertFalse(array_key_exists("id", $route));
100 }
101
102 /**
103 * @depends testSetupRoutes
104 */
105 public function testWildcardRouting() {
106 $route = HalfMoon\Router::routeRequest(
107 $this->request_for("/stub/something/blah"));
108
109 $this->assertEquals("something", $route["controller"]);
110 $this->assertEquals("stub_blah", $route["action"]);
111 $this->assertEmpty($route["id"]);
112
113 $route = HalfMoon\Router::routeRequest(
114 $this->request_for("/stub/something"));
115
116 $this->assertEquals("something", $route["controller"]);
117 $this->assertEquals("stub_index", $route["action"]);
118 $this->assertEmpty($route["id"]);
119 }
120
121 /**
122 * @depends testSetupRoutes
123 */
124 public function testConditionsMatching() {
125 $route = HalfMoon\Router::routeRequest(
126 $this->request_for("/matchingtag/50"));
127
128 $this->assertEquals("messages", $route["controller"]);
129 $this->assertEquals("show_tagged", $route["action"]);
130 $this->assertFalse(array_key_exists("id", $route));
131 }
132
133 /**
134 * @depends testSetupRoutes
135 * @expectedException HalfMoon\RoutingException
136 */
137 public function testConditionsFail() {
138 $route = HalfMoon\Router::routeRequest(
139 $this->request_for("/matchingtag/50asdf"));
140 }
141
142 /**
143 * @depends testSetupRoutes
144 * @expectedException HalfMoon\RoutingException
145 */
146 public function testPathCountMismatch() {
147 $route = HalfMoon\Router::routeRequest(
148 $this->request_for("/matchingtag/50/1/2/3/4"));
149 }
150
151 /**
152 * @depends testSetupRoutes
153 */
154 public function testGlobRouting() {
155 $route = HalfMoon\Router::routeRequest(
156 $this->request_for("/someglob/blah/this/is/a/test"));
157
158 $this->assertEquals("someglob", $route["controller"]);
159 $this->assertEquals("blah", $route["action"]);
160 $this->assertEquals("this/is/a/test", $route["globbed"]);
161
162 $route = HalfMoon\Router::routeRequest(
163 $this->request_for("/globtest/"));
164
165 $this->assertEquals("globtest", $route["globby"]);
166 $this->assertEquals("login", $route["controller"]);
167 $this->assertEquals("globtest", $route["action"]);
168 }
169
170 /**
171 * @depends testSetupRoutes
172 */
173 public function testRoutingWithHostname() {
174 $route = HalfMoon\Router::routeRequest(
175 $this->request_for("http://www5.example.com/logout"));
176
177 $this->assertEquals("login", $route["controller"]);
178 $this->assertEquals("logout2", $route["action"]);
179 $this->assertFalse(array_key_exists("id", $route));
180 }
181
182 /**
183 * @depends testSetupRoutes
184 */
185 public function testRootRouting() {
186 $route = HalfMoon\Router::routeRequest(
187 $this->request_for("/"));
188
189 $this->assertEquals("root", $route["controller"]);
190 $this->assertEquals("index", $route["action"]);
191 $this->assertFalse(array_key_exists("id", $route));
192 }
193
194 /**
195 * @depends testSetupRoutes
196 */
197 public function testRootRoutingWithHostname() {
198 $route = HalfMoon\Router::routeRequest(
199 $this->request_for("http://www.example2.com/"));
200
201 $this->assertEquals("root2", $route["controller"]);
202 $this->assertEquals("index", $route["action"]);
203 $this->assertFalse(array_key_exists("id", $route));
204
205 /* shouldn't raise exception */
206 HalfMoon\Router::addRootRoute(array(
207 "controller" => "root2",
208 "conditions" => array("hostname" => "www.example3.com"),
209 ));
210 }
211
212 /**
213 * @depends testSetupRoutes
214 * @expectedException \HalfMoon\HalfMoonException
215 */
216 public function testRootRoutingDuplicate() {
217 HalfMoon\Router::addRootRoute(array(
218 "controller" => "root3",
219 ));
220 }
221
222 /**
223 * @depends testSetupRoutes
224 * @expectedException \HalfMoon\HalfMoonException
225 */
226 public function testRootRoutingDuplicateConditions() {
227 HalfMoon\Router::addRootRoute(array(
228 "controller" => "root3",
229 "conditions" => array("hostname" => "www.example2.com"),
230 ));
231 }
232
233 /**
234 * @depends testSetupRoutes
235 */
236 public function testZeroRouting() {
237 $route = HalfMoon\Router::routeRequest(
238 $this->request_for("http://www.example2.com/zero_test/0"));
239
240 $this->assertEquals("zero_test", $route["controller"]);
241 $this->assertEquals("show", $route["action"]);
242 $this->assertEquals("0", $route["id"]);
243 }
244
245
246 private function request_for($url) {
247 if (preg_match("/^\//", $url))
248 $url = "http://www.example.com" . $url;
249
250 $req = new HalfMoon\Request($url, array(), array(), array(),
251 microtime(true));
252 return $req;
253 }
254}
255
256?>