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

Commit eab9ab2

Browse files
committed
Test case for callable class-based interop middleware
Based on an issue reported by @robertbasic. He reported that when you compose invokable class-based middleware that duck-types the `MiddlewareInterface`, Expressive raises an error; further, this only happens if the middleware is not composed in the container. This patch proposes a test case for that scenario.
1 parent 7274961 commit eab9ab2

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

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)