Skip to content

Commit

Permalink
Merge branch 'feature/psr-15-compat' into feature/http-interop-v2
Browse files Browse the repository at this point in the history
Forward port updates from zendframework#75

Conflicts:
	src/Dispatch.php
	src/Route.php
  • Loading branch information
weierophinney committed Nov 3, 2016
2 parents 6a94376 + e831a70 commit 9ddaa53
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/book/migration/to-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ prototype will be passed to the callable for the response argument.

Starting in version 2.0.0, `MiddlewarePipe` *will no longer implement
`Zend\Stratigility\MiddlewareInterface`, and only implement the http-interop
`ServerMiddlewareInterface`*. This has several repurcussions.
`ServerMiddlewareInterface`*. This has several repercussions.

### Callable middleware in version 1.3.0

Expand Down
2 changes: 1 addition & 1 deletion src/Delegate/CallableDelegateDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(callable $delegate, ResponseInterface $response)
/**
* Proxies to the underlying callable delegate to process a request.
*
* {@inheritDocs}
* {@inheritDoc}
*/
public function process(RequestInterface $request)
{
Expand Down
1 change: 1 addition & 0 deletions src/Middleware/CallableInteropMiddlewareWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(callable $middleware)
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$middleware = $this->middleware;
return $middleware($request, $delegate);
}
}
3 changes: 0 additions & 3 deletions src/Middleware/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use Zend\Escaper\Escaper;
use Zend\Stratigility\Delegate\CallableDelegateDecorator;
use Zend\Stratigility\Exception\MissingDelegateException;
use Zend\Stratigility\Exception\MissingResponseException;
use Zend\Stratigility\Utils;

/**
* Error handler middleware.
Expand Down
2 changes: 2 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Route
/**
* @param string $path
* @param InteropMiddlewareInterface|ServerMiddlewareInterface $handler
* @throws Exception\InvalidArgumentException if the $handler provided is
* not an http-interop middleare type.
*/
public function __construct($path, $handler)
{
Expand Down
32 changes: 32 additions & 0 deletions test/Middleware/CallableInteropMiddlewareWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @link http://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stratigility\Middleware;

use Interop\Http\Middleware\DelegateInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Stratigility\Middleware\CallableInteropMiddlewareWrapper;

class CallableInteropMiddlewareWrapperTest extends TestCase
{
public function testWrapperDecoratesAndProxiesToCallableInteropMiddleware()
{
$request = $this->prophesize(ServerRequestInterface::class)->reveal();
$delegate = $this->prophesize(DelegateInterface::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();

$decorator = new CallableInteropMiddlewareWrapper(
function ($request, DelegateInterface $delegate) use ($response) {
return $response;
}
);

$this->assertSame($response, $decorator->process($request, $delegate));
}
}
89 changes: 89 additions & 0 deletions test/Middleware/CallableMiddlewareWrapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* @link http://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Stratigility\Middleware;

use Closure;
use Interop\Http\Middleware\DelegateInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Stratigility\Middleware\CallableMiddlewareWrapper;
use Zend\Stratigility\Next;

class CallableMiddlewareWrapperTest extends TestCase
{
public function testWrapperDecoratesAndProxiesToCallableMiddleware()
{
$request = $this->prophesize(ServerRequestInterface::class)->reveal();
$delegate = $this->prophesize(DelegateInterface::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();

$decorator = new CallableMiddlewareWrapper(
function ($request, $response, $delegate) {
return $response;
},
$response
);

$this->assertSame($response, $decorator->process($request, $delegate));
}

public function testWrapperDoesNotDecorateNextInstancesWhenProxying()
{
$request = $this->prophesize(ServerRequestInterface::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();

$delegate = $this->prophesize(Next::class)->reveal();
$decorator = new CallableMiddlewareWrapper(
function ($request, $response, $next) use ($delegate) {
$this->assertSame($delegate, $next);
return $response;
},
$response
);

$this->assertSame($response, $decorator->process($request, $delegate));
}

public function testWrapperDecoratesDelegatesNotExtendingNext()
{
$request = $this->prophesize(ServerRequestInterface::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();

$delegate = $this->prophesize(DelegateInterface::class)->reveal();
$decorator = new CallableMiddlewareWrapper(
function ($request, $response, $next) use ($delegate) {
$this->assertNotSame($delegate, $next);
$this->assertInstanceOf(Closure::class, $next);
return $response;
},
$response
);

$this->assertSame($response, $decorator->process($request, $delegate));
}

public function testDecoratedDelegateWillBeInvokedWithOnlyRequest()
{
$request = $this->prophesize(ServerRequestInterface::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();
$expected = $this->prophesize(ResponseInterface::class)->reveal();

$delegate = $this->prophesize(DelegateInterface::class);
$delegate->process($request)->willReturn($expected);

$decorator = new CallableMiddlewareWrapper(
function ($request, $response, $next) {
return $next($request, $response);
},
$response
);

$this->assertSame($expected, $decorator->process($request, $delegate->reveal()));
}
}

0 comments on commit 9ddaa53

Please sign in to comment.