Skip to content

Commit

Permalink
Merge pull request slimphp#2765 from adriansuter/patch-middleware-hel…
Browse files Browse the repository at this point in the history
…pers

[4.x] Simplifying queuing internal middleware
  • Loading branch information
l0gicgate authored Jul 29, 2019
2 parents 04be469 + 7246eff commit 9e7b1d2
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Slim/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\RouteCollectorInterface;
use Slim\Interfaces\RouteResolverInterface;
use Slim\Middleware\ErrorMiddleware;
use Slim\Middleware\RoutingMiddleware;
use Slim\Routing\RouteCollectorProxy;
use Slim\Routing\RouteResolver;
use Slim\Routing\RouteRunner;
Expand Down Expand Up @@ -101,6 +103,46 @@ public function addMiddleware(MiddlewareInterface $middleware): self
return $this;
}

/**
* Add the slim built-in routing middleware to the app middleware stack
*
* @return RoutingMiddleware
*/
public function addRoutingMiddleware(): RoutingMiddleware
{
$routingMiddleware = new RoutingMiddleware(
$this->getRouteResolver(),
$this->getRouteCollector()->getRouteParser()
);
$this->add($routingMiddleware);
return $routingMiddleware;
}

/**
* Add the slim built-in error middleware to the app middleware stack
*
* @param bool $displayErrorDetails
* @param bool $logErrors
* @param bool $logErrorDetails
*
* @return ErrorMiddleware
*/
public function addErrorMiddleware(
bool $displayErrorDetails,
bool $logErrors,
bool $logErrorDetails
): ErrorMiddleware {
$errorMiddleware = new ErrorMiddleware(
$this->getCallableResolver(),
$this->getResponseFactory(),
$displayErrorDetails,
$logErrors,
$logErrorDetails
);
$this->add($errorMiddleware);
return $errorMiddleware;
}

/**
* Run application
*
Expand Down
63 changes: 63 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use Slim\Interfaces\RouteCollectorInterface;
use Slim\Interfaces\RouteCollectorProxyInterface;
use Slim\Interfaces\RouteParserInterface;
use Slim\Middleware\ErrorMiddleware;
use Slim\Middleware\RoutingMiddleware;
use Slim\MiddlewareDispatcher;
use Slim\Routing\RouteCollector;
use Slim\Routing\RouteCollectorProxy;
use Slim\Tests\Mocks\MockAction;
Expand Down Expand Up @@ -651,6 +654,66 @@ public function testAddMiddlewareUsingDeferredResolution()
$this->assertSame('Hello World', (string) $response->getBody());
}

public function testAddRoutingMiddleware()
{
/** @var ResponseFactoryInterface $responseFactory */
$responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal();

// Create the app.
$app = new App($responseFactory);

// Add the routing middleware.
$routingMiddleware = $app->addRoutingMiddleware();

// Check that the routing middleware really has been added to the tip of the app middleware stack.
$middlewareDispatcherProperty = new \ReflectionProperty(App::class, 'middlewareDispatcher');
$middlewareDispatcherProperty->setAccessible(true);
/** @var MiddlewareDispatcher $middlewareDispatcher */
$middlewareDispatcher = $middlewareDispatcherProperty->getValue($app);

$tipProperty = new \ReflectionProperty(MiddlewareDispatcher::class, 'tip');
$tipProperty->setAccessible(true);
/** @var RequestHandlerInterface $tip */
$tip = $tipProperty->getValue($middlewareDispatcher);

$reflection = new \ReflectionClass($tip);
$middlewareProperty = $reflection->getProperty('middleware');
$middlewareProperty->setAccessible(true);

$this->assertSame($routingMiddleware, $middlewareProperty->getValue($tip));
$this->assertInstanceOf(RoutingMiddleware::class, $routingMiddleware);
}

public function testAddErrorMiddleware()
{
/** @var ResponseFactoryInterface $responseFactory */
$responseFactory = $this->prophesize(ResponseFactoryInterface::class)->reveal();

// Create the app.
$app = new App($responseFactory);

// Add the error middleware.
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

// Check that the error middleware really has been added to the tip of the app middleware stack.
$middlewareDispatcherProperty = new \ReflectionProperty(App::class, 'middlewareDispatcher');
$middlewareDispatcherProperty->setAccessible(true);
/** @var MiddlewareDispatcher $middlewareDispatcher */
$middlewareDispatcher = $middlewareDispatcherProperty->getValue($app);

$tipProperty = new \ReflectionProperty(MiddlewareDispatcher::class, 'tip');
$tipProperty->setAccessible(true);
/** @var RequestHandlerInterface $tip */
$tip = $tipProperty->getValue($middlewareDispatcher);

$reflection = new \ReflectionClass($tip);
$middlewareProperty = $reflection->getProperty('middleware');
$middlewareProperty->setAccessible(true);

$this->assertSame($errorMiddleware, $middlewareProperty->getValue($tip));
$this->assertInstanceOf(ErrorMiddleware::class, $errorMiddleware);
}

public function testAddMiddlewareOnRoute()
{
$output = '';
Expand Down

0 comments on commit 9e7b1d2

Please sign in to comment.