/** */ public function testGroupError() { $r = new Router(); $r->group('/member', function ($r) { $r->get('/', function () { throw new Exception(); }); $r->get('/503', function ($router, $req) { return $router->error(503, $req); }); $r->error(500, function () { return 'Member Error'; }); }); $r->error(500, function () { return 'All 500 Error'; }); $r->error(503, function () { return 'All 503 Error'; }); $req = Request::create('/member/'); $res = $r->run($req); $this->assertEquals(500, $res->getStatusCode()); $this->assertEquals('Member Error', $res->getContent()); $req = Request::create('/member/503'); $res = $r->run($req); $this->assertEquals(503, $res->getStatusCode()); $this->assertEquals('All 503 Error', $res->getContent()); }
public function testErrorToController() { $r = new Router(); $r->error(500, 'Fixture\\MomongaController@error500'); $req = Request::create('/'); $ex = new \Exception('Some Error'); $res = $r->error(500, $req, $ex); $this->assertEquals(500, $res->getStatusCode()); $this->assertEquals((string) $ex, $res->getContent()); }
private function error(array $config) { foreach ($config as $status => $controller) { if (!is_int($status)) { continue; } $this->router->error($status, $controller); } }
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()); }
/** */ 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 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()); }
/** @Route('/show/:id') */ public function show(Router $r, Request $req) { return $r->error(403, $req); }