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
/**
* 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)
{
$this->routes = new RouteCollection();
if ($map) {
$this->parseRouteMap($map);
}
$dispatcher = new EventDispatcher();
$dispatcher->connect('core.request', array($this, 'parseRequest'));
$dispatcher->connect('core.request', array($this, 'runBeforeFilters'));
......@@ -253,23 +243,6 @@ class Framework extends HttpKernel
$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
*
......
......@@ -30,14 +30,17 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$test = $this;
$framework = new Framework();
$framework->before(function() use(&$i, $test) {
$test->assertEquals(0, $i);
$i++;
});
$framework->match('/foo', function() use(&$i, $test) {
$test->assertEquals(1, $i);
$i++;
});
$framework->after(function() use(&$i, $test) {
$test->assertEquals(2, $i);
$i++;
......@@ -53,12 +56,13 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
{
$i = 0;
$framework = new Framework(array(
'/foo' => function() use(&$i) {
$framework = new Framework();
$framework->match('/foo', function() use (&$i) {
$i++;
return new Response('foo');
},
));
});
$framework->after(function() use(&$i) {
$i++;
});
......@@ -76,22 +80,27 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$test = $this;
$framework = new Framework();
$framework->before(function() use(&$i, $test) {
$test->assertEquals(0, $i);
$i++;
});
$framework->before(function() use(&$i, $test) {
$test->assertEquals(1, $i);
$i++;
});
$framework->match('/foo', function() use(&$i, $test) {
$test->assertEquals(2, $i);
$i++;
});
$framework->after(function() use(&$i, $test) {
$test->assertEquals(3, $i);
$i++;
});
$framework->after(function() use(&$i, $test) {
$test->assertEquals(4, $i);
$i++;
......
......@@ -25,11 +25,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testNoErrorHandler()
{
$framework = new Framework(array(
'/foo' => function() {
$framework = new Framework();
$framework->match('/foo', function() {
throw new \RuntimeException('foo exception');
},
));
});
try {
$request = Request::create('/foo');
......@@ -42,11 +42,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testOneErrorHandler()
{
$framework = new Framework(array(
'/foo' => function() {
$framework = new Framework();
$framework->match('/foo', function() {
throw new \RuntimeException('foo exception');
},
));
});
$framework->error(function($e) {
return new Response('foo exception handler');
......@@ -58,11 +58,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testMultipleErrorHandlers()
{
$framework = new Framework(array(
'/foo' => function() {
$framework = new Framework();
$framework->match('/foo', function() {
throw new \RuntimeException('foo exception');
},
));
});
$errors = 0;
......@@ -92,11 +92,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testNoResponseErrorHandler()
{
$framework = new Framework(array(
'/foo' => function() {
$framework = new Framework();
$framework->match('/foo', function() {
throw new \RuntimeException('foo exception');
},
));
});
$errors = 0;
......@@ -117,11 +117,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testStringResponseErrorHandler()
{
$framework = new Framework(array(
'/foo' => function() {
$framework = new Framework();
$framework->match('/foo', function() {
throw new \RuntimeException('foo exception');
},
));
});
$framework->error(function($e) {
return 'foo exception handler';
......@@ -133,11 +133,11 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testErrorHandlerException()
{
$framework = new Framework(array(
'/foo' => function() {
$framework = new Framework();
$framework->match('/foo', function() {
throw new \RuntimeException('foo exception');
},
));
});
$framework->error(function($e) {
throw new \RuntimeException('foo exception handler exception');
......
......@@ -27,109 +27,40 @@ class RouterTest extends \PHPUnit_Framework_TestCase
{
public function testMapRouting()
{
$framework = new Framework(array(
'/foo' => function() {
return 'foo';
},
'/bar' => function() {
return 'bar';
},
'/' => function() {
return 'root';
},
));
$this->checkRouteResponse($framework, '/foo', 'foo');
$this->checkRouteResponse($framework, '/bar', 'bar');
$this->checkRouteResponse($framework, '/', 'root');
}
$framework = new Framework();
public function testMapRoutingMethods()
{
$framework = new Framework(array(
'GET /foo' => function() {
$framework->match('/foo', function() {
return 'foo';
},
'PUT|DELETE /bar' => function() {
});
$framework->match('/bar', function() {
return 'bar';
},
'/' => function() {
});
$framework->match('/', 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');
} catch (NotFoundHttpException $expected) {
}
}
public function testMapRoutingParameters()
{
$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, '/bar', 'bar');
$this->checkRouteResponse($framework, '/', 'root');
}
public function testStatusCode()
{
$framework = new Framework(array(
'PUT /created' => function() {
$framework = new Framework();
$framework->put('/created', function() {
return new Response('', 201);
},
'/forbidden' => function() {
});
$framework->match('/forbidden', function() {
return new Response('', 403);
},
'/not_found' => function() {
});
$framework->match('/not_found', function() {
return new Response('', 404);
},
));
});
$request = Request::create('/created', 'put');
$response = $framework->handle($request);
......@@ -146,12 +77,11 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testRedirect()
{
$framework = new Framework(array(
'/redirect' => function() {
$response = new RedirectResponse('/target');
return $response;
},
));
$framework = new Framework();
$framework->match('/redirect', function() {
return new RedirectResponse('/target');
});
$request = Request::create('/redirect');
$response = $framework->handle($request);
......@@ -172,21 +102,27 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testMethodRouting()
{
$framework = new Framework();
$framework->match('/foo', function() {
return 'foo';
});
$framework->match('/bar', function() {
return 'bar';
}, 'GET|POST');
$framework->get('/resource', function() {
return 'get resource';
});
$framework->post('/resource', function() {
return 'post resource';
});
$framework->put('/resource', function() {
return 'put resource';
});
$framework->delete('/resource', function() {
return 'delete resource';
});
......
......@@ -23,14 +23,15 @@ class WebTestCaseTest extends WebTestCase
{
public function createApp()
{
$app = new Framework(array(
'/hello' => function() {
$app = new Framework();
$app->match('/hello', function() {
return 'world';
},
'/html' => function() {
});
$app->match('/html', function() {
return '<h1>title</h1>';
},
));
});
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