コード例 #1
0
ファイル: ConfigRouterTest.php プロジェクト: ranyuen/little
 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());
 }