Permalink
Browse files

Merge branch 'feature/route-callback-args' into develop

  • Loading branch information...
2 parents 9ef5ce5 + ef3d485 commit 63bbd0948b461a87a74a9588bc1b81629b3cd282 @trq trq committed Feb 4, 2013
Showing with 130 additions and 31 deletions.
  1. +7 −1 lib/Proem/Routing/RouteManager.php
  2. +123 −30 lib/Proem/Routing/Tests/RouteManagerTest.php
@@ -187,7 +187,13 @@ public function route()
// If match found, return matching route.
if ($route->process($this->request) !== false) {
- return $route;
+ if ($route->hasCallback()) {
+ $arguments = $route->getPayload();
+ array_unshift($arguments, $this->request);
+ return call_user_func_array($route->getCallback(), $arguments);
+ } else {
+ return $route;
+ }
} else {
// Recurse through the next route.
return $this->route();
@@ -86,19 +86,24 @@ public function testSingleRoute()
$request = m::mock('Proem\Http\Request');
$request->shouldReceive('getMethod')
- ->once();
+ ->once()
- $request->shouldReceive('getScheme')
+ ->shouldReceive('getScheme')
->once()
->andReturn('http');
$route = m::mock('Proem\Routing\RouteInterface');
$route->shouldReceive('process')
->once()
- ->andReturn(true);
- $route->shouldReceive('getOptions')
+ ->andReturn(true)
+
+ ->shouldReceive('getOptions')
+ ->once()
+ ->andReturn([])
+
+ ->shouldReceive('hasCallback')
->once()
- ->andReturn([]);
+ ->andReturn(false);
$routeManager = new RouteManager($request);
@@ -121,16 +126,26 @@ public function testMultipleMatchingRoutes()
$route = m::mock('Proem\Routing\RouteInterface');
$route->shouldReceive('process')
->once()
- ->andReturn(true);
- $route->shouldReceive('getOptions')
- ->once();
+ ->andReturn(true)
+
+ ->shouldReceive('getOptions')
+ ->once()
+
+ ->shouldReceive('hasCallback')
+ ->once()
+ ->andReturn(false);
$route2 = m::mock('Proem\Routing\RouteInterface');
$route2->shouldReceive('process')
->once()
- ->andReturn(true);
- $route2->shouldReceive('getOptions')
- ->once();
+ ->andReturn(true)
+
+ ->shouldReceive('getOptions')
+ ->once()
+
+ ->shouldReceive('hasCallback')
+ ->once()
+ ->andReturn(false);
$routeManager = new RouteManager($request);
@@ -159,29 +174,41 @@ public function testMultipleMatchingAndFailingRoutes()
$route = m::mock('Proem\Routing\RouteInterface');
$route->shouldReceive('process')
->once()
- ->andReturn(true); // match
- $route->shouldReceive('getOptions')
- ->once();
+ ->andReturn(true) // match
+
+ ->shouldReceive('getOptions')
+ ->once()
+
+ ->shouldReceive('hasCallback')
+ ->once()
+ ->andReturn(false);
$route2 = m::mock('Proem\Routing\RouteInterface');
$route2->shouldReceive('process')
->once()
- ->andReturn(false); // fail
- $route2->shouldReceive('getOptions')
+ ->andReturn(false) // fail
+
+ ->shouldReceive('getOptions')
->once();
$route3 = m::mock('Proem\Routing\RouteInterface');
$route3->shouldReceive('process')
->once()
- ->andReturn(true); // match
- $route3->shouldReceive('getOptions')
- ->once();
+ ->andReturn(true) // match
+
+ ->shouldReceive('getOptions')
+ ->once()
+
+ ->shouldReceive('hasCallback')
+ ->once()
+ ->andReturn(false);
$route4 = m::mock('Proem\Routing\RouteInterface');
$route4->shouldReceive('process')
->once()
- ->andReturn(false); // fail
- $route4->shouldReceive('getOptions')
+ ->andReturn(false) // fail
+
+ ->shouldReceive('getOptions')
->once();
$routeManager = new RouteManager($request);
@@ -214,17 +241,27 @@ public function testSpecificMethodTakesPrecedence()
$with = m::mock('Proem\Routing\RouteInterface');
$with->shouldReceive('process')
->twice()
- ->andReturn(true);
- $with->shouldReceive('getOptions')
+ ->andReturn(true)
+
+ ->shouldReceive('getOptions')
+ ->twice()
+ ->andReturn(['method' => 'GET'])
+
+ ->shouldReceive('hasCallback')
->twice()
- ->andReturn(['method' => 'GET']);
+ ->andReturn(false);
$without = m::mock('Proem\Routing\RouteInterface');
$without->shouldReceive('process')
->twice()
- ->andReturn(true);
- $without->shouldReceive('getOptions')
- ->twice();
+ ->andReturn(true)
+
+ ->shouldReceive('getOptions')
+ ->twice()
+
+ ->shouldReceive('hasCallback')
+ ->twice()
+ ->andReturn(false);
$routeManager = new RouteManager($request);
@@ -264,10 +301,15 @@ public function testSpecificMethodOfSameNameTakesPrecedence()
$with = m::mock('Proem\Routing\RouteInterface');
$with->shouldReceive('process')
->twice()
- ->andReturn(true);
- $with->shouldReceive('getOptions')
+ ->andReturn(true)
+
+ ->shouldReceive('getOptions')
+ ->twice()
+ ->andReturn(['method' => 'GET'])
+
+ ->shouldReceive('hasCallback')
->twice()
- ->andReturn(['method' => 'GET']);
+ ->andReturn(false);
$without = m::mock('Proem\Routing\RouteInterface');
$without->shouldReceive('getOptions');
@@ -379,4 +421,55 @@ public function testCanSwitchSchemeDefault()
$this->assertInstanceOf('\Proem\Routing\Route', $result);
}
+
+ public function testCanHandleCallbackRoute()
+ {
+ $request = \Proem\Http\Request::create('/foo');
+ $result = (new RouteManager($request))
+ ->attach('r1', new \Proem\Routing\Route('/foo', [], function() { return "bar"; }))
+ ->route();
+
+ $this->assertEquals('bar', $result);
+ }
+
+ public function testCallbackGetsRequest()
+ {
+ $request = \Proem\Http\Request::create('/foo');
+ $result = (new RouteManager($request))
+ ->attach('r1', new \Proem\Routing\Route('/foo', [], function($req) { return $req; }))
+ ->route();
+
+ $this->assertInstanceOf('\Proem\Http\Request', $result);
+ }
+
+ public function testCallbackGetsNamedArgs()
+ {
+ $request = \Proem\Http\Request::create('/view/trq');
+ list($action, $username) = (new RouteManager($request))
+ ->attach('r1', new \Proem\Routing\Route('/{action}/{username}', [], function($req, $a, $u) { return [$a, $u]; }))
+ ->route();
+
+ $this->assertEquals('view', $action);
+ $this->assertEquals('trq', $username);
+ }
+
+ public function testCallbackGetsArgsOfType()
+ {
+ $request = \Proem\Http\Request::create('/user/123');
+ list($action, $id) = (new RouteManager($request))
+ ->attach('r1', new \Proem\Routing\Route(
+ '/{action}/{id}',
+ ['filters' => [
+ 'action' => '{alpha}',
+ 'id' => '{int}'
+ ]],
+ function($req, $action, $id) {
+ return [$action, $id];
+ }
+ ))
+ ->route();
+
+ $this->assertEquals('user', $action);
+ $this->assertEquals(123, $id);
+ }
}

0 comments on commit 63bbd09

Please sign in to comment.