Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Fix deprecation notice with zend-expressive-router 2.4 for config driven apps #583

Merged
merged 2 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/ApplicationConfigInjectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Zend\Expressive;

use SplPriorityQueue;
use Zend\Expressive\Router\Route;

trait ApplicationConfigInjectionTrait
{
Expand Down Expand Up @@ -152,7 +151,7 @@ public function injectRoutesFromConfig(array $config = null)
continue;
}

$methods = Route::HTTP_METHOD_ANY;
$methods = null;
if (isset($spec['allowed_methods'])) {
$methods = $spec['allowed_methods'];
if (! is_array($methods)) {
Expand All @@ -164,7 +163,14 @@ public function injectRoutesFromConfig(array $config = null)
}

$name = isset($spec['name']) ? $spec['name'] : null;
$route = new Route($spec['path'], $spec['middleware'], $methods, $name);
$middleware = $this->prepareMiddleware(
$spec['middleware'],
$this->router,
$this->responsePrototype,
$this->container
);

$route = $this->route($spec['path'], $middleware, $methods, $name);

if (isset($spec['options'])) {
$options = $spec['options'];
Expand All @@ -177,8 +183,6 @@ public function injectRoutesFromConfig(array $config = null)

$route->setOptions($options);
}

$this->route($route);
}
}

Expand Down
8 changes: 5 additions & 3 deletions test/Application/ConfigInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ZendTest\Expressive\Application;

use Interop\Http\ServerMiddleware\MiddlewareInterface;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down Expand Up @@ -58,7 +59,7 @@ public static function assertRoute($spec, array $routes)
return false;
}

if ($route->getMiddleware() !== $spec['middleware']) {
if (! $route->getMiddleware()) {
return false;
}

Expand Down Expand Up @@ -99,7 +100,7 @@ public static function assertPipelineContainsInstanceOf($class, $pipeline, $mess
public function callableMiddlewares()
{
return [
['HelloWorld'],
[InvokableMiddleware::class],
[
function () {
},
Expand All @@ -115,6 +116,7 @@ function () {
*/
public function testInjectRoutesFromConfigSetsUpRoutesFromConfig($middleware)
{
$pingMiddleware = $this->prophesize(MiddlewareInterface::class)->reveal();
$config = [
'routes' => [
[
Expand All @@ -124,7 +126,7 @@ public function testInjectRoutesFromConfigSetsUpRoutesFromConfig($middleware)
],
[
'path' => '/ping',
'middleware' => 'Ping',
'middleware' => $pingMiddleware,
'allowed_methods' => ['GET'],
],
],
Expand Down
18 changes: 10 additions & 8 deletions test/Container/ApplicationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use ArrayObject;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static function assertRoute($spec, array $routes)
return false;
}

if ($route->getMiddleware() !== $spec['middleware']) {
if (! $route->getMiddleware()) {
return false;
}

Expand Down Expand Up @@ -123,8 +124,8 @@ public function testFactoryWillPullAllReplaceableDependenciesFromContainerWhenPr

public function callableMiddlewares()
{
$middleware = [
'service-name' => 'HelloWorld',
$middlewareTypes = [
'service-name' => InvokableMiddleware::class,
'closure' => function () {
},
'callable' => [InvokableMiddleware::class, 'staticallyCallableMiddleware'],
Expand All @@ -136,7 +137,7 @@ public function callableMiddlewares()
];

foreach ($configTypes as $configType => $config) {
foreach ($middleware as $middlewareType => $middleware) {
foreach ($middlewareTypes as $middlewareType => $middleware) {
$name = sprintf('%s-%s', $configType, $middlewareType);
yield $name => [$middleware, $config];
}
Expand All @@ -151,6 +152,7 @@ public function callableMiddlewares()
*/
public function testFactorySetsUpRoutesFromConfig($middleware, $configType)
{
$pingMiddleware = $this->prophesize(MiddlewareInterface::class)->reveal();
$config = [
'routes' => [
[
Expand All @@ -160,7 +162,7 @@ public function testFactorySetsUpRoutesFromConfig($middleware, $configType)
],
[
'path' => '/ping',
'middleware' => 'Ping',
'middleware' => $pingMiddleware,
'allowed_methods' => ['GET'],
],
],
Expand Down Expand Up @@ -238,7 +240,7 @@ public function testCanSpecifyRouteViaConfigurationWithNoMethods($configType)
'routes' => [
[
'path' => '/',
'middleware' => 'HelloWorld',
'middleware' => $this->prophesize(MiddlewareInterface::class)->reveal(),
],
],
];
Expand Down Expand Up @@ -275,7 +277,7 @@ public function testCanSpecifyRouteOptionsViaConfiguration($configType)
'routes' => [
[
'path' => '/',
'middleware' => 'HelloWorld',
'middleware' => $this->prophesize(MiddlewareInterface::class)->reveal(),
'name' => 'home',
'allowed_methods' => ['GET'],
'options' => $expected,
Expand Down Expand Up @@ -333,7 +335,7 @@ public function testExceptionIsRaisedInCaseOfInvalidRouteOptionsConfiguration($c
'routes' => [
[
'path' => '/',
'middleware' => 'HelloWorld',
'middleware' => $this->prophesize(MiddlewareInterface::class)->reveal(),
'options' => 'invalid',
],
],
Expand Down
31 changes: 21 additions & 10 deletions test/Middleware/DispatchMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Middleware\DispatchMiddleware;
use Zend\Expressive\Middleware\LazyLoadingMiddleware;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Router\RouterInterface;
Expand Down Expand Up @@ -88,11 +90,12 @@ public function testCanDispatchCallableDoublePassMiddleware()
$this->delegate->process()->shouldNotBeCalled();

$expected = $this->prophesize(ResponseInterface::class)->reveal();
$routedMiddleware = function ($request, $response, $next) use ($expected) {
return $expected;
};
$routedMiddleware = $this->prophesize(ServerMiddlewareInterface::class);
$routedMiddleware
->process(Argument::that([$this->request, 'reveal']), Argument::that([$this->delegate, 'reveal']))
->willReturn($expected);

$routeResult = RouteResult::fromRoute(new Route('/', $routedMiddleware));
$routeResult = RouteResult::fromRoute(new Route('/', $routedMiddleware->reveal()));

$this->request->getAttribute(RouteResult::class, false)->willReturn($routeResult);

Expand All @@ -104,19 +107,27 @@ public function testCanDispatchCallableDoublePassMiddleware()
/**
* @group 453
*/
public function testCanDispatchMiddlewareServices()
public function testCanDispatchLazyMiddleware()
{
$this->delegate->process()->shouldNotBeCalled();

$expected = $this->prophesize(ResponseInterface::class)->reveal();
$routedMiddleware = function ($request, $response, $next) use ($expected) {
return $expected;
};
$routedMiddleware = $this->prophesize(ServerMiddlewareInterface::class);
$routedMiddleware
->process(Argument::that([$this->request, 'reveal']), Argument::that([$this->delegate, 'reveal']))
->willReturn($expected);

$this->container->has('RoutedMiddleware')->willReturn(true);
$this->container->get('RoutedMiddleware')->willReturn($routedMiddleware);
$this->container->get('RoutedMiddleware')->willReturn($routedMiddleware->reveal());

// Since 2.0, we never have service names in routes, only lazy-loading middleware
$lazyMiddleware = new LazyLoadingMiddleware(
$this->container->reveal(),
$this->responsePrototype->reveal(),
'RoutedMiddleware'
);

$routeResult = RouteResult::fromRoute(new Route('/', 'RoutedMiddleware'));
$routeResult = RouteResult::fromRoute(new Route('/', $lazyMiddleware));

$this->request->getAttribute(RouteResult::class, false)->willReturn($routeResult);

Expand Down