public function testStackAnnotation() { Router::plugin('Ranyuen\\Little\\Plugin\\ControllerAnnotationRouter'); $r = new Router(); $r->stack('Fixture\\FirstMiddleware'); $r->stack('Fixture\\SecondMiddleware'); $r->registerController('Fixture\\StackController'); $res = $r->run(Request::create('/g/')); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals(' b1 b2 b3 /g/ a3 a2 a1', $res->getContent()); }
public function testControllerArgs() { $c = new Container(['test' => $this]); $c->bind('Fixture\\Momonga', 'momonga', function ($c) { return new Momonga(); }); $r = new Router($c); $r->get('/', 'Fixture\\MomongaController@argtest'); $req = Request::create('/'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('Momonga argtest', $res->getContent()); }
/** */ public function testDiByType() { $test = $this; $c = new Container(); $c->bind('Fixture\\Momonga', 'momonga', function ($c) { return new Momonga(); }); $r = new Router($c); $r->get('/', function (Momonga $m) use($test, $c) { $test->assertSame($c['momonga'], $m); return ''; }); $req = Request::create('/'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); }
private function group(array $config) { foreach ($config as $path => $val) { $this->router->group($path, function (Router $r) use($val) { (new ConfigRouter($r))->routeByConfig($val); }); } }
/** */ public function testInternalServerError() { $test = $this; $someError = new Exception(); $r = new Router(); $r->get('/raise', function () use($someError) { throw $someError; }); $r->error(500, function ($ex) use($test, $someError) { $test->assertSame($someError, $ex); return 'Internal Server Error'; }); $req = Request::create('/raise'); $res = $r->run($req); $this->assertEquals(500, $res->getStatusCode()); $this->assertEquals('Internal Server Error', $res->getContent()); }
public function testRegisterController() { Router::plugin('Ranyuen\\Little\\Plugin\\ControllerAnnotationRouter'); $r = new Router(); $r->registerController('Fixture\\BlogController'); $req = Request::create('/blog/2'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('blog index GET 2', $res->getContent()); $req = Request::create('/blog/'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('blog index GET 1', $res->getContent()); $res = $r->run('blog_index', $req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('blog index GET 1', $res->getContent()); $req = Request::create('/blog/show/mOmonga'); $res = $r->run($req); $this->assertEquals(403, $res->getStatusCode()); $this->assertEquals('blog 404', $res->getContent()); $req = Request::create('/blog/show/mOmonga', 'POST'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('POST mOmonga', $res->getContent()); }
public function testParamCondition() { $r = new Router(); $r->get('/:id', function () { return ''; })->assert('id', '42'); $req = Request::create('/42'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $req = Request::create('/41'); $res = $r->run($req); $this->assertEquals(404, $res->getStatusCode()); $r = new Router(); $r->get('/:id', function () { return ''; })->assert('id', '/\\A\\d+\\z/'); $req = Request::create('/42'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $req = Request::create('/id'); $res = $r->run($req); $this->assertEquals(404, $res->getStatusCode()); }
public function testRouteByConfig() { Router::plugin('Ranyuen\\Little\\Plugin\\ConfigRouter'); $r = new Router(); $r->routeByConfig(['map' => [['/', function () { return 'index'; }]], 'error' => [500 => function () { return 'index 500'; }], 'group' => ['/blog' => ['map' => [['/:page?', function ($page = 1) { return "blog index {$page}"; }, 'assert' => ['page' => '/\\A\\d+\\z/'], 'name' => 'blog_index'], ['/show/:id', function (Router $r, Request $req) { return $r->error(404, $req); }]], 'error' => [404 => function () { return 'blog 404'; }]]]]); $req = Request::create('/'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('index', $res->getContent()); $req = Request::create('/'); $res = $r->error(500, $req, new Exception()); $this->assertEquals(500, $res->getStatusCode()); $this->assertEquals('index 500', $res->getContent()); $req = Request::create('/blog/2'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('blog index 2', $res->getContent()); $req = Request::create('/blog/'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('blog index 1', $res->getContent()); $res = $r->run('blog_index', $req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('blog index 1', $res->getContent()); $req = Request::create('/blog/show/mOmonga'); $res = $r->run($req); $this->assertEquals(404, $res->getStatusCode()); $this->assertEquals('blog 404', $res->getContent()); }
/** * Add a child router. * * @param string $path Path DSL. * @param Router $router Child router. * * @return void */ public function addGroup($path, Router $router) { if (in_array([$path, $router], $this->routes, true)) { return; } $this->routes[] = [$path, $router]; $router->registerParent($this->facade); }
public function test503() { $r = new Router(); $r->get('/', function () { throw new ServiceUnavailable(); }); $r->error(503, function () { return 'RequestEntityTooLarge'; }); $res = $r->run(Request::create('/')); $this->assertEquals(503, $res->getStatusCode()); }
/** * Name this route. * * @param string $name Name. * * @return this */ public function name($name) { $this->router->addRoute($this, $name); return $this; }
public function testAssretAndDefaults() { $r = new Router(); $r->get('/:id?', function ($id = 42) { return (string) $id; })->assert('id', '/\\d+/'); $req = Request::create('/index'); $res = $r->run($req); $this->assertEquals(404, $res->getStatusCode()); $req = Request::create('/'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $this->assertEquals('42', $res->getContent()); }
/** @Route('/show/:id') */ public function show(Router $r, Request $req) { return $r->error(403, $req); }
/** * Handles a Request to convert it to a Response. * * When $catch is true, the implementation must catch all exceptions * and do its best to convert them to a Response instance. * * @param Request $req A Request instance * @param int $type The type of the request * (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) * @param bool $catch Whether to catch exceptions or not * * @return Response A Response instance * * @throws \Exception When an Exception occurs during processing * * @SuppressWarnings(PHPMD) */ public function handle(Request $req, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { return $this->router->toResponse($this->route->response($this->dp)); }
public function testSeparateRouter() { $child = new Router(); $child->get('/', function () { return ''; }); $r = new Router(); $r->group('/user', $child); $req = Request::create('/user/'); $res = $r->run($req); $this->assertEquals(200, $res->getStatusCode()); $req = Request::create('/'); $res = $child->run($req); $this->assertEquals(200, $res->getStatusCode()); $req = Request::create('/user/'); $res = $child->run($req); $this->assertEquals(404, $res->getStatusCode()); $req = Request::create('/'); $res = $r->run($req); $this->assertEquals(404, $res->getStatusCode()); }