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 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 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 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 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 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()); }
/** */ 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()); }
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()); }
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()); }