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

Commit f153fbf

Browse files
committed
Merge branch 'hotfix/583-router-2.4-test-fixes' into develop
Forward port #583
2 parents 1f5d5b5 + db8d510 commit f153fbf

File tree

5 files changed

+70
-26
lines changed

5 files changed

+70
-26
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 2.1.1 - 2018-03-09
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- Nothing.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- [#583](https://github.com/zendframework/zend-expressive/pull/583) provides a
26+
number of minor fixes and test changes to ensure the component works with the
27+
zend-expressive-router 2.4 version. In particular, configuration-driven routes
28+
will now work properly across all versions, without deprecation notices.
29+
530
## 2.1.0 - 2017-12-11
631

732
### Added

src/ApplicationConfigInjectionTrait.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Zend\Expressive;
99

1010
use SplPriorityQueue;
11-
use Zend\Expressive\Router\Route;
1211

1312
trait ApplicationConfigInjectionTrait
1413
{
@@ -152,7 +151,7 @@ public function injectRoutesFromConfig(array $config = null)
152151
continue;
153152
}
154153

155-
$methods = Route::HTTP_METHOD_ANY;
154+
$methods = null;
156155
if (isset($spec['allowed_methods'])) {
157156
$methods = $spec['allowed_methods'];
158157
if (! is_array($methods)) {
@@ -164,7 +163,14 @@ public function injectRoutesFromConfig(array $config = null)
164163
}
165164

166165
$name = isset($spec['name']) ? $spec['name'] : null;
167-
$route = new Route($spec['path'], $spec['middleware'], $methods, $name);
166+
$middleware = $this->prepareMiddleware(
167+
$spec['middleware'],
168+
$this->router,
169+
$this->responsePrototype,
170+
$this->container
171+
);
172+
173+
$route = $this->route($spec['path'], $middleware, $methods, $name);
168174

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

178184
$route->setOptions($options);
179185
}
180-
181-
$this->route($route);
182186
}
183187
}
184188

test/Application/ConfigInjectionTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace ZendTest\Expressive\Application;
99

10+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
1011
use PHPUnit\Framework\Assert;
1112
use PHPUnit\Framework\TestCase;
1213
use Prophecy\Prophecy\ObjectProphecy;
@@ -58,7 +59,7 @@ public static function assertRoute($spec, array $routes)
5859
return false;
5960
}
6061

61-
if ($route->getMiddleware() !== $spec['middleware']) {
62+
if (! $route->getMiddleware()) {
6263
return false;
6364
}
6465

@@ -99,7 +100,7 @@ public static function assertPipelineContainsInstanceOf($class, $pipeline, $mess
99100
public function callableMiddlewares()
100101
{
101102
return [
102-
['HelloWorld'],
103+
[InvokableMiddleware::class],
103104
[
104105
function () {
105106
},
@@ -115,6 +116,7 @@ function () {
115116
*/
116117
public function testInjectRoutesFromConfigSetsUpRoutesFromConfig($middleware)
117118
{
119+
$pingMiddleware = $this->prophesize(MiddlewareInterface::class)->reveal();
118120
$config = [
119121
'routes' => [
120122
[
@@ -124,7 +126,7 @@ public function testInjectRoutesFromConfigSetsUpRoutesFromConfig($middleware)
124126
],
125127
[
126128
'path' => '/ping',
127-
'middleware' => 'Ping',
129+
'middleware' => $pingMiddleware,
128130
'allowed_methods' => ['GET'],
129131
],
130132
],

test/Container/ApplicationFactoryTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use ArrayObject;
1111
use Interop\Http\ServerMiddleware\DelegateInterface;
12+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
1213
use PHPUnit\Framework\Assert;
1314
use PHPUnit\Framework\TestCase;
1415
use Prophecy\Prophecy\ObjectProphecy;
@@ -80,7 +81,7 @@ public static function assertRoute($spec, array $routes)
8081
return false;
8182
}
8283

83-
if ($route->getMiddleware() !== $spec['middleware']) {
84+
if (! $route->getMiddleware()) {
8485
return false;
8586
}
8687

@@ -123,8 +124,8 @@ public function testFactoryWillPullAllReplaceableDependenciesFromContainerWhenPr
123124

124125
public function callableMiddlewares()
125126
{
126-
$middleware = [
127-
'service-name' => 'HelloWorld',
127+
$middlewareTypes = [
128+
'service-name' => InvokableMiddleware::class,
128129
'closure' => function () {
129130
},
130131
'callable' => [InvokableMiddleware::class, 'staticallyCallableMiddleware'],
@@ -136,7 +137,7 @@ public function callableMiddlewares()
136137
];
137138

138139
foreach ($configTypes as $configType => $config) {
139-
foreach ($middleware as $middlewareType => $middleware) {
140+
foreach ($middlewareTypes as $middlewareType => $middleware) {
140141
$name = sprintf('%s-%s', $configType, $middlewareType);
141142
yield $name => [$middleware, $config];
142143
}
@@ -151,6 +152,7 @@ public function callableMiddlewares()
151152
*/
152153
public function testFactorySetsUpRoutesFromConfig($middleware, $configType)
153154
{
155+
$pingMiddleware = $this->prophesize(MiddlewareInterface::class)->reveal();
154156
$config = [
155157
'routes' => [
156158
[
@@ -160,7 +162,7 @@ public function testFactorySetsUpRoutesFromConfig($middleware, $configType)
160162
],
161163
[
162164
'path' => '/ping',
163-
'middleware' => 'Ping',
165+
'middleware' => $pingMiddleware,
164166
'allowed_methods' => ['GET'],
165167
],
166168
],
@@ -238,7 +240,7 @@ public function testCanSpecifyRouteViaConfigurationWithNoMethods($configType)
238240
'routes' => [
239241
[
240242
'path' => '/',
241-
'middleware' => 'HelloWorld',
243+
'middleware' => $this->prophesize(MiddlewareInterface::class)->reveal(),
242244
],
243245
],
244246
];
@@ -275,7 +277,7 @@ public function testCanSpecifyRouteOptionsViaConfiguration($configType)
275277
'routes' => [
276278
[
277279
'path' => '/',
278-
'middleware' => 'HelloWorld',
280+
'middleware' => $this->prophesize(MiddlewareInterface::class)->reveal(),
279281
'name' => 'home',
280282
'allowed_methods' => ['GET'],
281283
'options' => $expected,
@@ -333,7 +335,7 @@ public function testExceptionIsRaisedInCaseOfInvalidRouteOptionsConfiguration($c
333335
'routes' => [
334336
[
335337
'path' => '/',
336-
'middleware' => 'HelloWorld',
338+
'middleware' => $this->prophesize(MiddlewareInterface::class)->reveal(),
337339
'options' => 'invalid',
338340
],
339341
],

test/Middleware/DispatchMiddlewareTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use Interop\Http\ServerMiddleware\DelegateInterface;
1111
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
1212
use PHPUnit\Framework\TestCase;
13+
use Prophecy\Argument;
1314
use Prophecy\Prophecy\ObjectProphecy;
1415
use Psr\Container\ContainerInterface;
1516
use Psr\Http\Message\ResponseInterface;
1617
use Psr\Http\Message\ServerRequestInterface;
1718
use Zend\Expressive\Middleware\DispatchMiddleware;
19+
use Zend\Expressive\Middleware\LazyLoadingMiddleware;
1820
use Zend\Expressive\Router\Route;
1921
use Zend\Expressive\Router\RouteResult;
2022
use Zend\Expressive\Router\RouterInterface;
@@ -88,11 +90,12 @@ public function testCanDispatchCallableDoublePassMiddleware()
8890
$this->delegate->process()->shouldNotBeCalled();
8991

9092
$expected = $this->prophesize(ResponseInterface::class)->reveal();
91-
$routedMiddleware = function ($request, $response, $next) use ($expected) {
92-
return $expected;
93-
};
93+
$routedMiddleware = $this->prophesize(ServerMiddlewareInterface::class);
94+
$routedMiddleware
95+
->process(Argument::that([$this->request, 'reveal']), Argument::that([$this->delegate, 'reveal']))
96+
->willReturn($expected);
9497

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

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

@@ -104,19 +107,27 @@ public function testCanDispatchCallableDoublePassMiddleware()
104107
/**
105108
* @group 453
106109
*/
107-
public function testCanDispatchMiddlewareServices()
110+
public function testCanDispatchLazyMiddleware()
108111
{
109112
$this->delegate->process()->shouldNotBeCalled();
110113

111114
$expected = $this->prophesize(ResponseInterface::class)->reveal();
112-
$routedMiddleware = function ($request, $response, $next) use ($expected) {
113-
return $expected;
114-
};
115+
$routedMiddleware = $this->prophesize(ServerMiddlewareInterface::class);
116+
$routedMiddleware
117+
->process(Argument::that([$this->request, 'reveal']), Argument::that([$this->delegate, 'reveal']))
118+
->willReturn($expected);
115119

116120
$this->container->has('RoutedMiddleware')->willReturn(true);
117-
$this->container->get('RoutedMiddleware')->willReturn($routedMiddleware);
121+
$this->container->get('RoutedMiddleware')->willReturn($routedMiddleware->reveal());
122+
123+
// Since 2.0, we never have service names in routes, only lazy-loading middleware
124+
$lazyMiddleware = new LazyLoadingMiddleware(
125+
$this->container->reveal(),
126+
$this->responsePrototype->reveal(),
127+
'RoutedMiddleware'
128+
);
118129

119-
$routeResult = RouteResult::fromRoute(new Route('/', 'RoutedMiddleware'));
130+
$routeResult = RouteResult::fromRoute(new Route('/', $lazyMiddleware));
120131

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

0 commit comments

Comments
 (0)