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

Commit 4b2d778

Browse files
committed
Merge branch 'hotfix/robertbasic-callable-interop-middleware'
Close #467
2 parents 7274961 + ae95b6d commit 4b2d778

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
## 2.0.2 - TBD
5+
## 2.0.2 - 2017-03-13
66

77
### Added
88

@@ -18,7 +18,12 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#467](https://github.com/zendframework/zend-expressive/pull/467) fixes an
22+
issue when passing invokable, duck-typed, interop middleware to the
23+
application without registering it with the container. Prior to the patch, it
24+
was incorrectly being decorated by
25+
`Zend\Stratigility\Middleware\CallableMiddlewareWrapper` instead of
26+
`Zend\Stratigility\Middleware\CallableInteropMiddlewareWrapper`.
2227

2328
## 2.0.1 - 2017-03-09
2429

src/MarshalMiddlewareTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ private function marshalInvokableMiddleware($middleware, ResponseInterface $resp
148148
return $instance;
149149
}
150150

151+
if ($this->isCallableInteropMiddleware($instance)) {
152+
return new CallableInteropMiddlewareWrapper($instance);
153+
}
154+
151155
if (! is_callable($instance)) {
152156
throw new Exception\InvalidMiddlewareException(sprintf(
153157
'Middleware of class "%s" is invalid; neither invokable nor %s',

test/Application/MarshalMiddlewareTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public function testPreparingInvokableInteropMiddlewareReturnsDecoratedInteropMi
256256

257257
$middleware = $this->prepareMiddleware($base);
258258

259-
$this->assertInstanceOf(CallableMiddlewareWrapper::class, $middleware);
259+
$this->assertInstanceOf(CallableInteropMiddlewareWrapper::class, $middleware);
260260
$this->assertAttributeInstanceOf(TestAsset\CallableInteropMiddleware::class, 'middleware', $middleware);
261261
}
262262

test/IntegrationTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
namespace ZendTest\Expressive;
99

1010
use Fig\Http\Message\StatusCodeInterface as StatusCode;
11+
use Interop\Http\ServerMiddleware\DelegateInterface;
12+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
1113
use PHPUnit\Framework\TestCase;
1214
use Prophecy\Argument;
15+
use Psr\Container\ContainerInterface;
16+
use Psr\Http\Message\ServerRequestInterface;
1317
use Psr\Http\Message\ResponseInterface;
1418
use Zend\Diactoros\Response;
1519
use Zend\Diactoros\Response\EmitterInterface;
@@ -76,4 +80,37 @@ public function testInjectedFinalHandlerCanEmitA404WhenNoMiddlewareMatches()
7680
$this->assertInstanceOf(ResponseInterface::class, $this->response);
7781
$this->assertEquals(StatusCode::STATUS_NOT_FOUND, $this->response->getStatusCode());
7882
}
83+
84+
public function testCallableClassInteropMiddlewareNotRegisteredWithContainerCanBeComposedSuccessfully()
85+
{
86+
$response = new Response();
87+
$routedMiddleware = $this->prophesize(MiddlewareInterface::class);
88+
$routedMiddleware
89+
->process(
90+
Argument::type(ServerRequestInterface::class),
91+
Argument::type(DelegateInterface::class)
92+
)
93+
->willReturn($response);
94+
95+
$container = $this->prophesize(ContainerInterface::class);
96+
$container->has('RoutedMiddleware')->willReturn(true);
97+
$container->get('RoutedMiddleware')->will([$routedMiddleware, 'reveal']);
98+
$container->has(TestAsset\CallableInteropMiddleware::class)->willReturn(false);
99+
100+
$delegate = new NotFoundDelegate($response);
101+
$app = new Application(new FastRouteRouter(), $container->reveal(), $delegate, $this->getEmitter());
102+
103+
$app->pipe(TestAsset\CallableInteropMiddleware::class);
104+
$app->get('/', 'RoutedMiddleware');
105+
106+
$request = new ServerRequest([], [], 'https://example.com/foo', 'GET');
107+
$app->run($request, new Response());
108+
109+
$this->assertInstanceOf(ResponseInterface::class, $this->response);
110+
$this->assertTrue($this->response->hasHeader('X-Callable-Interop-Middleware'));
111+
$this->assertEquals(
112+
TestAsset\CallableInteropMiddleware::class,
113+
$this->response->getHeaderLine('X-Callable-Interop-Middleware')
114+
);
115+
}
79116
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
4+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\Expressive\TestAsset;
9+
10+
use Interop\Http\ServerMiddleware\DelegateInterface;
11+
use Psr\Http\Message\ServerRequestInterface;
12+
13+
class CallableInteropMiddleware
14+
{
15+
public function __invoke(ServerRequestInterface $request, DelegateInterface $delegate)
16+
{
17+
$response = $delegate->process($request);
18+
return $response->withHeader('X-Callable-Interop-Middleware', __CLASS__);
19+
}
20+
}

0 commit comments

Comments
 (0)