/**
  * Run the request/response through the Application and its middleware.
  *
  * This will invoke the following methods:
  *
  * - App->bootstrap() - Perform any bootstrapping logic for your application here.
  * - App->middleware() - Attach any application middleware here.
  * - Trigger the 'Server.buildMiddleware' event. You can use this to modify the
  *   from event listeners.
  * - Run the middleware stack including the application.
  *
  * @param \Psr\Http\Message\ServerRequestInterface $request  The request to use or null.
  * @param \Psr\Http\Message\ResponseInterface      $response The response to use or null.
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function run(ServerRequestInterface $request = null, ResponseInterface $response = null)
 {
     $this->app->bootstrap();
     $request = $request ?: ServerRequestFactory::fromGlobals();
     $response = $response ?: new Response();
     $middleware = $this->app->middleware(new MiddlewareStack());
     if (!$middleware instanceof MiddlewareStack) {
         throw new RuntimeException('The application `middleware` method did not return a middleware stack.');
     }
     $middleware->push($this->app);
     $this->dispatchEvent('Server.buildMiddleware', ['middleware' => $middleware]);
     $response = $this->runner->run($middleware, $request, $response);
     if (!$response instanceof ResponseInterface) {
         throw new RuntimeException(sprintf('Application did not create a response. Got "%s" instead.', is_object($response) ? get_class($response) : $response));
     }
     return $response;
 }
 public function testRunNextNotCalled()
 {
     $this->stack->push($this->noNext);
     $req = $this->getMock('Psr\\Http\\Message\\ServerRequestInterface');
     $res = $this->getMock('Psr\\Http\\Message\\ResponseInterface');
     $runner = new Runner();
     $result = $runner->run($this->stack, $req, $res);
     $this->assertNull($result);
 }