예제 #1
0
 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());
 }
예제 #2
0
 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());
 }
예제 #3
0
 /**
  */
 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());
 }
예제 #4
0
 private function group(array $config)
 {
     foreach ($config as $path => $val) {
         $this->router->group($path, function (Router $r) use($val) {
             (new ConfigRouter($r))->routeByConfig($val);
         });
     }
 }
예제 #5
0
 /**
  */
 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());
 }
예제 #7
0
 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());
 }
예제 #8
0
 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());
 }
예제 #9
0
 /**
  * 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);
 }
예제 #10
0
 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());
 }
예제 #11
0
파일: Route.php 프로젝트: ranyuen/little
 /**
  * Name this route.
  *
  * @param string $name Name.
  *
  * @return this
  */
 public function name($name)
 {
     $this->router->addRoute($this, $name);
     return $this;
 }
예제 #12
0
 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());
 }
예제 #13
0
 /** @Route('/show/:id') */
 public function show(Router $r, Request $req)
 {
     return $r->error(403, $req);
 }
예제 #14
0
 /**
  * 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));
 }
예제 #15
0
 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());
 }