Commit 2bff0c98 authored by Igor Wiedler's avatar Igor Wiedler

remove routeMap syntax, use methods instead

parent dff88b8b
...@@ -34,21 +34,11 @@ class Framework extends HttpKernel ...@@ -34,21 +34,11 @@ class Framework extends HttpKernel
/** /**
* Constructor. * Constructor.
*
* Takes a route map argument (assoc array). The key is (optional) a pipe '|'
* delimited list of HTTP methods followed by a single space ' ', followed by
* the path pattern to match. The value is a callable.
*
* @param array $map Route map, mapping patterns to callables
*/ */
public function __construct(array $map = null) public function __construct(array $map = null)
{ {
$this->routes = new RouteCollection(); $this->routes = new RouteCollection();
if ($map) {
$this->parseRouteMap($map);
}
$dispatcher = new EventDispatcher(); $dispatcher = new EventDispatcher();
$dispatcher->connect('core.request', array($this, 'parseRequest')); $dispatcher->connect('core.request', array($this, 'parseRequest'));
$dispatcher->connect('core.request', array($this, 'runBeforeFilters')); $dispatcher->connect('core.request', array($this, 'runBeforeFilters'));
...@@ -80,7 +70,7 @@ class Framework extends HttpKernel ...@@ -80,7 +70,7 @@ class Framework extends HttpKernel
* @param string $pattern Matched route pattern * @param string $pattern Matched route pattern
* @param mixed $to Callback that returns the response when matched * @param mixed $to Callback that returns the response when matched
* @param string $method Matched HTTP methods, multiple can be supplied, * @param string $method Matched HTTP methods, multiple can be supplied,
* delimited by a pipe character '|', eg. 'GET|POST'. * delimited by a pipe character '|', eg. 'GET|POST'.
* *
* @return $this * @return $this
*/ */
...@@ -253,23 +243,6 @@ class Framework extends HttpKernel ...@@ -253,23 +243,6 @@ class Framework extends HttpKernel
$this->handle($request)->send(); $this->handle($request)->send();
} }
/**
* Parse a route map and create routes
*
* @see __construct()
*/
protected function parseRouteMap(array $map) {
foreach ($map as $pattern => $to) {
$method = null;
if (false !== strpos($pattern, ' ')) {
list($method, $pattern) = explode(' ', $pattern, 2);
}
$this->match($pattern, $to, $method);
}
}
/** /**
* Handler for core.request * Handler for core.request
* *
......
...@@ -30,14 +30,17 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -30,14 +30,17 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$test = $this; $test = $this;
$framework = new Framework(); $framework = new Framework();
$framework->before(function() use(&$i, $test) { $framework->before(function() use(&$i, $test) {
$test->assertEquals(0, $i); $test->assertEquals(0, $i);
$i++; $i++;
}); });
$framework->match('/foo', function() use(&$i, $test) { $framework->match('/foo', function() use(&$i, $test) {
$test->assertEquals(1, $i); $test->assertEquals(1, $i);
$i++; $i++;
}); });
$framework->after(function() use(&$i, $test) { $framework->after(function() use(&$i, $test) {
$test->assertEquals(2, $i); $test->assertEquals(2, $i);
$i++; $i++;
...@@ -53,12 +56,13 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -53,12 +56,13 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
{ {
$i = 0; $i = 0;
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() use(&$i) {
$i++; $framework->match('/foo', function() use (&$i) {
return new Response('foo'); $i++;
}, return new Response('foo');
)); });
$framework->after(function() use(&$i) { $framework->after(function() use(&$i) {
$i++; $i++;
}); });
...@@ -76,22 +80,27 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -76,22 +80,27 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$test = $this; $test = $this;
$framework = new Framework(); $framework = new Framework();
$framework->before(function() use(&$i, $test) { $framework->before(function() use(&$i, $test) {
$test->assertEquals(0, $i); $test->assertEquals(0, $i);
$i++; $i++;
}); });
$framework->before(function() use(&$i, $test) { $framework->before(function() use(&$i, $test) {
$test->assertEquals(1, $i); $test->assertEquals(1, $i);
$i++; $i++;
}); });
$framework->match('/foo', function() use(&$i, $test) { $framework->match('/foo', function() use(&$i, $test) {
$test->assertEquals(2, $i); $test->assertEquals(2, $i);
$i++; $i++;
}); });
$framework->after(function() use(&$i, $test) { $framework->after(function() use(&$i, $test) {
$test->assertEquals(3, $i); $test->assertEquals(3, $i);
$i++; $i++;
}); });
$framework->after(function() use(&$i, $test) { $framework->after(function() use(&$i, $test) {
$test->assertEquals(4, $i); $test->assertEquals(4, $i);
$i++; $i++;
......
...@@ -25,11 +25,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -25,11 +25,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{ {
public function testNoErrorHandler() public function testNoErrorHandler()
{ {
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() {
throw new \RuntimeException('foo exception'); $framework->match('/foo', function() {
}, throw new \RuntimeException('foo exception');
)); });
try { try {
$request = Request::create('/foo'); $request = Request::create('/foo');
...@@ -42,11 +42,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -42,11 +42,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testOneErrorHandler() public function testOneErrorHandler()
{ {
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() {
throw new \RuntimeException('foo exception'); $framework->match('/foo', function() {
}, throw new \RuntimeException('foo exception');
)); });
$framework->error(function($e) { $framework->error(function($e) {
return new Response('foo exception handler'); return new Response('foo exception handler');
...@@ -58,11 +58,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -58,11 +58,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testMultipleErrorHandlers() public function testMultipleErrorHandlers()
{ {
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() {
throw new \RuntimeException('foo exception'); $framework->match('/foo', function() {
}, throw new \RuntimeException('foo exception');
)); });
$errors = 0; $errors = 0;
...@@ -92,11 +92,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -92,11 +92,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testNoResponseErrorHandler() public function testNoResponseErrorHandler()
{ {
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() {
throw new \RuntimeException('foo exception'); $framework->match('/foo', function() {
}, throw new \RuntimeException('foo exception');
)); });
$errors = 0; $errors = 0;
...@@ -117,11 +117,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -117,11 +117,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testStringResponseErrorHandler() public function testStringResponseErrorHandler()
{ {
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() {
throw new \RuntimeException('foo exception'); $framework->match('/foo', function() {
}, throw new \RuntimeException('foo exception');
)); });
$framework->error(function($e) { $framework->error(function($e) {
return 'foo exception handler'; return 'foo exception handler';
...@@ -133,11 +133,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -133,11 +133,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testErrorHandlerException() public function testErrorHandlerException()
{ {
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() {
throw new \RuntimeException('foo exception'); $framework->match('/foo', function() {
}, throw new \RuntimeException('foo exception');
)); });
$framework->error(function($e) { $framework->error(function($e) {
throw new \RuntimeException('foo exception handler exception'); throw new \RuntimeException('foo exception handler exception');
......
...@@ -27,109 +27,40 @@ class RouterTest extends \PHPUnit_Framework_TestCase ...@@ -27,109 +27,40 @@ class RouterTest extends \PHPUnit_Framework_TestCase
{ {
public function testMapRouting() public function testMapRouting()
{ {
$framework = new Framework(array( $framework = new Framework();
'/foo' => function() {
return 'foo'; $framework->match('/foo', function() {
}, return 'foo';
'/bar' => function() { });
return 'bar';
}, $framework->match('/bar', function() {
'/' => function() { return 'bar';
return 'root'; });
},
)); $framework->match('/', function() {
return 'root';
});
$this->checkRouteResponse($framework, '/foo', 'foo'); $this->checkRouteResponse($framework, '/foo', 'foo');
$this->checkRouteResponse($framework, '/bar', 'bar'); $this->checkRouteResponse($framework, '/bar', 'bar');
$this->checkRouteResponse($framework, '/', 'root'); $this->checkRouteResponse($framework, '/', 'root');
} }
public function testMapRoutingMethods() public function testStatusCode()
{ {
$framework = new Framework(array( $framework = new Framework();
'GET /foo' => function() {
return 'foo';
},
'PUT|DELETE /bar' => function() {
return 'bar';
},
'/' => function() {
return 'root';
},
));
// foo route
$this->checkRouteResponse($framework, '/foo', 'foo');
// bar route
$this->checkRouteResponse($framework, '/bar', 'bar', 'put');
$this->checkRouteResponse($framework, '/bar', 'bar', 'delete');
// root route
$this->checkRouteResponse($framework, '/', 'root');
$this->checkRouteResponse($framework, '/', 'root', 'post');
$this->checkRouteResponse($framework, '/', 'root', 'put');
$this->checkRouteResponse($framework, '/', 'root', 'delete');
try {
$request = Request::create('/bar');
$framework->handle($request);
$this->fail('Framework must reject HTTP GET method to /bar');
} catch (NotFoundHttpException $expected) {
}
try {
$request = Request::create('/bar', 'post');
$framework->handle($request);
$this->fail('Framework must reject HTTP POST method to /bar'); $framework->put('/created', function() {
} catch (NotFoundHttpException $expected) { return new Response('', 201);
} });
}
public function testMapRoutingParameters() $framework->match('/forbidden', function() {
{ return new Response('', 403);
$framework = new Framework(array( });
'/hello' => function() {
return "Hello anon";
},
'/hello/{name}' => function($name) {
return "Hello $name";
},
'/goodbye/{name}' => function($name) {
return "Goodbye $name";
},
'/tell/{name}/{message}' => function($message, $name) {
return "Message for $name: $message";
},
'/' => function() {
return 'root';
},
));
$this->checkRouteResponse($framework, '/hello', 'Hello anon');
$this->checkRouteResponse($framework, '/hello/alice', 'Hello alice');
$this->checkRouteResponse($framework, '/hello/bob', 'Hello bob');
$this->checkRouteResponse($framework, '/goodbye/alice', 'Goodbye alice');
$this->checkRouteResponse($framework, '/goodbye/bob', 'Goodbye bob');
$this->checkRouteResponse($framework, '/tell/bob/secret', 'Message for bob: secret');
$this->checkRouteResponse($framework, '/', 'root');
}
public function testStatusCode() $framework->match('/not_found', function() {
{ return new Response('', 404);
$framework = new Framework(array( });
'PUT /created' => function() {
return new Response('', 201);
},
'/forbidden' => function() {
return new Response('', 403);
},
'/not_found' => function() {
return new Response('', 404);
},
));
$request = Request::create('/created', 'put'); $request = Request::create('/created', 'put');
$response = $framework->handle($request); $response = $framework->handle($request);
...@@ -146,12 +77,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase ...@@ -146,12 +77,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testRedirect() public function testRedirect()
{ {
$framework = new Framework(array( $framework = new Framework();
'/redirect' => function() {
$response = new RedirectResponse('/target'); $framework->match('/redirect', function() {
return $response; return new RedirectResponse('/target');
}, });
));
$request = Request::create('/redirect'); $request = Request::create('/redirect');
$response = $framework->handle($request); $response = $framework->handle($request);
...@@ -172,21 +102,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase ...@@ -172,21 +102,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testMethodRouting() public function testMethodRouting()
{ {
$framework = new Framework(); $framework = new Framework();
$framework->match('/foo', function() { $framework->match('/foo', function() {
return 'foo'; return 'foo';
}); });
$framework->match('/bar', function() { $framework->match('/bar', function() {
return 'bar'; return 'bar';
}, 'GET|POST'); }, 'GET|POST');
$framework->get('/resource', function() { $framework->get('/resource', function() {
return 'get resource'; return 'get resource';
}); });
$framework->post('/resource', function() { $framework->post('/resource', function() {
return 'post resource'; return 'post resource';
}); });
$framework->put('/resource', function() { $framework->put('/resource', function() {
return 'put resource'; return 'put resource';
}); });
$framework->delete('/resource', function() { $framework->delete('/resource', function() {
return 'delete resource'; return 'delete resource';
}); });
......
...@@ -23,14 +23,15 @@ class WebTestCaseTest extends WebTestCase ...@@ -23,14 +23,15 @@ class WebTestCaseTest extends WebTestCase
{ {
public function createApp() public function createApp()
{ {
$app = new Framework(array( $app = new Framework();
'/hello' => function() {
return 'world'; $app->match('/hello', function() {
}, return 'world';
'/html' => function() { });
return '<h1>title</h1>';
}, $app->match('/html', function() {
)); return '<h1>title</h1>';
});
return $app; return $app;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment