/**
  * @param Application $app
  * @param Closure[] $routes
  */
 public static function apply(Application $app, array $routes)
 {
     foreach ($routes as $pattern => $handler) {
         list($method, $pattern) = array_values(array_filter(explode(' ', $pattern)));
         $pattern = preg_replace('/@([\\w]+)/', '{$1}', $pattern);
         switch ($method) {
             case 'GET':
                 $app->get($pattern, $handler);
                 break;
             case 'POST':
                 $app->post($pattern, $handler);
                 break;
             case 'PUT':
                 $app->put($pattern, $handler);
                 break;
             case 'DELETE':
                 $app->delete($pattern, $handler);
                 break;
             case 'PATCH':
                 $app->patch($pattern, $handler);
                 break;
         }
     }
 }
 public function testMiddlewareReceiveResponsesEvenWhenStringReturned()
 {
     unset($_SERVER['__middleware.response']);
     $app = new Application();
     $app->routeMiddleware(['foo' => 'LumenTestPlainMiddleware']);
     $app->get('/', ['middleware' => 'foo', function () {
         return 'Hello World';
     }]);
     $response = $app->handle(Request::create('/', 'GET'));
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('Hello World', $response->getContent());
     $this->assertEquals(true, $_SERVER['__middleware.response']);
 }
 public function testRedirectToNamedRoute()
 {
     $app = new Application();
     $app->get('login', ['as' => 'login', function (Illuminate\Http\Request $request) {
         return 'login';
     }]);
     $app->get('/', function (Illuminate\Http\Request $request) {
         return redirect()->route('login');
     });
     $response = $app->handle(Request::create('/', 'GET'));
     $this->assertEquals(302, $response->getStatusCode());
 }
 protected function get($method, $pathRoot, $controller, $routeNamePrefix)
 {
     $path = $pathRoot . $this->methodPathBind[$method];
     $this->app->get($path, $this->getAction($routeNamePrefix, $controller, $method));
 }
 public function testGeneratingUrlsForRegexParameters()
 {
     $app = new Application();
     $app->instance('request', Request::create('http://lumen.laravel.com', 'GET'));
     unset($app->availableBindings['request']);
     $app->get('/foo-bar', ['as' => 'foo', function () {
         //
     }]);
     $app->get('/foo-bar/{baz:[0-9]+}/{boom}', ['as' => 'bar', function () {
         //
     }]);
     $app->get('/foo-bar/{baz:[0-9]+}/{boom:[0-9]+}', ['as' => 'baz', function () {
         //
     }]);
     $this->assertEquals('http://lumen.laravel.com/something', url('something'));
     $this->assertEquals('http://lumen.laravel.com/foo-bar', route('foo'));
     $this->assertEquals('http://lumen.laravel.com/foo-bar/1/2', route('bar', ['baz' => 1, 'boom' => 2]));
     $this->assertEquals('http://lumen.laravel.com/foo-bar/1/2', route('baz', ['baz' => 1, 'boom' => 2]));
     $this->assertEquals('http://lumen.laravel.com/foo-bar/{baz:[0-9]+}/{boom:[0-9]+}?ba=1&bo=2', route('baz', ['ba' => 1, 'bo' => 2]));
 }
 public function testBasicControllerDispatchingWithMiddlewareIntercept()
 {
     $app = new Application();
     $app->routeMiddleware(['test' => LumenTestMiddleware::class]);
     $app->get('/show/{id}', 'LumenTestControllerWithMiddleware@show');
     $response = $app->handle(Request::create('/show/25', 'GET'));
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals('Middleware', $response->getContent());
 }
 /**
  * Register a route with the application.
  *
  * @param string $uri
  * @param mixed $action
  * @return $this 
  * @static 
  */
 public static function get($uri, $action)
 {
     return \Laravel\Lumen\Application::get($uri, $action);
 }
 public function testRequestUser()
 {
     $app = new Application();
     $app['auth']->viaRequest('api', function ($request) {
         return new \Illuminate\Auth\GenericUser(['id' => 1234]);
     });
     $app->get('/', function (Illuminate\Http\Request $request) {
         return $request->user()->getAuthIdentifier();
     });
     $response = $app->handle(Request::create('/', 'GET'));
     $this->assertSame('1234', $response->getContent());
 }
 public function testValidationHelpers()
 {
     $app = new Application();
     $app->get('/', function (Illuminate\Http\Request $request) {
         $this->validate($request, ['name' => 'required']);
     });
     $response = $app->handle(Request::create('/', 'GET'));
     $this->assertEquals(422, $response->getStatusCode());
 }