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

Commit

Permalink
Merge branch 'hotfix/robertbasic-callable-interop-middleware'
Browse files Browse the repository at this point in the history
Close #467
  • Loading branch information
weierophinney committed Mar 13, 2017
2 parents 7274961 + ae95b6d commit 4b2d778
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## 2.0.2 - TBD
## 2.0.2 - 2017-03-13

### Added

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

### Fixed

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

## 2.0.1 - 2017-03-09

Expand Down
4 changes: 4 additions & 0 deletions src/MarshalMiddlewareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ private function marshalInvokableMiddleware($middleware, ResponseInterface $resp
return $instance;
}

if ($this->isCallableInteropMiddleware($instance)) {
return new CallableInteropMiddlewareWrapper($instance);
}

if (! is_callable($instance)) {
throw new Exception\InvalidMiddlewareException(sprintf(
'Middleware of class "%s" is invalid; neither invokable nor %s',
Expand Down
2 changes: 1 addition & 1 deletion test/Application/MarshalMiddlewareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public function testPreparingInvokableInteropMiddlewareReturnsDecoratedInteropMi

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

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

Expand Down
37 changes: 37 additions & 0 deletions test/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
namespace ZendTest\Expressive;

use Fig\Http\Message\StatusCodeInterface as StatusCode;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\EmitterInterface;
Expand Down Expand Up @@ -76,4 +80,37 @@ public function testInjectedFinalHandlerCanEmitA404WhenNoMiddlewareMatches()
$this->assertInstanceOf(ResponseInterface::class, $this->response);
$this->assertEquals(StatusCode::STATUS_NOT_FOUND, $this->response->getStatusCode());
}

public function testCallableClassInteropMiddlewareNotRegisteredWithContainerCanBeComposedSuccessfully()
{
$response = new Response();
$routedMiddleware = $this->prophesize(MiddlewareInterface::class);
$routedMiddleware
->process(
Argument::type(ServerRequestInterface::class),
Argument::type(DelegateInterface::class)
)
->willReturn($response);

$container = $this->prophesize(ContainerInterface::class);
$container->has('RoutedMiddleware')->willReturn(true);
$container->get('RoutedMiddleware')->will([$routedMiddleware, 'reveal']);
$container->has(TestAsset\CallableInteropMiddleware::class)->willReturn(false);

$delegate = new NotFoundDelegate($response);
$app = new Application(new FastRouteRouter(), $container->reveal(), $delegate, $this->getEmitter());

$app->pipe(TestAsset\CallableInteropMiddleware::class);
$app->get('/', 'RoutedMiddleware');

$request = new ServerRequest([], [], 'https://example.com/foo', 'GET');
$app->run($request, new Response());

$this->assertInstanceOf(ResponseInterface::class, $this->response);
$this->assertTrue($this->response->hasHeader('X-Callable-Interop-Middleware'));
$this->assertEquals(
TestAsset\CallableInteropMiddleware::class,
$this->response->getHeaderLine('X-Callable-Interop-Middleware')
);
}
}
20 changes: 20 additions & 0 deletions test/TestAsset/CallableInteropMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Expressive\TestAsset;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ServerRequestInterface;

class CallableInteropMiddleware
{
public function __invoke(ServerRequestInterface $request, DelegateInterface $delegate)
{
$response = $delegate->process($request);
return $response->withHeader('X-Callable-Interop-Middleware', __CLASS__);
}
}

0 comments on commit 4b2d778

Please sign in to comment.