From ee2233c6dd61cb630bffb5df3c091ede9e44d69d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 26 Mar 2018 17:36:58 +0200 Subject: [PATCH] Updated PathVersionMiddleware to single-pass middleware --- .../src/Middleware/PathVersionMiddleware.php | 43 ++++++------------- .../Middleware/PathVersionMiddlewareTest.php | 29 +++++++++---- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/module/Rest/src/Middleware/PathVersionMiddleware.php b/module/Rest/src/Middleware/PathVersionMiddleware.php index f3ccdd01..928d2b84 100644 --- a/module/Rest/src/Middleware/PathVersionMiddleware.php +++ b/module/Rest/src/Middleware/PathVersionMiddleware.php @@ -3,53 +3,38 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Middleware; +use Interop\Http\ServerMiddleware\DelegateInterface; +use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -use Zend\Stratigility\MiddlewareInterface; class PathVersionMiddleware implements MiddlewareInterface { /** - * Process an incoming request and/or response. - * - * Accepts a server-side request and a response instance, and does - * something with them. - * - * If the response is not complete and/or further processing would not - * interfere with the work done in the middleware, or if the middleware - * wants to delegate to another process, it can use the `$out` callable - * if present. - * - * If the middleware does not return a value, execution of the current - * request is considered complete, and the response instance provided will - * be considered the response to return. - * - * Alternately, the middleware may return a response instance. - * - * Often, middleware will `return $out();`, with the assumption that a - * later middleware will return a response. + * Process an incoming server request and return a response, optionally delegating + * to the next middleware component to create the response. * * @param Request $request - * @param Response $response - * @param null|callable $out - * @return null|Response + * @param DelegateInterface $delegate + * + * @return Response */ - public function __invoke(Request $request, Response $response, callable $out = null) + public function process(Request $request, DelegateInterface $delegate) { $uri = $request->getUri(); $path = $uri->getPath(); // If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes - if (strpos($path, '/v') !== 0) { - $parts = explode('/', $path); + if (\strpos($path, '/v') !== 0) { + $parts = \explode('/', $path); // Remove the first empty part and the - array_shift($parts); + \array_shift($parts); // Prepend the version prefix - array_unshift($parts, '/v1'); + \array_unshift($parts, '/v1'); - $request = $request->withUri($uri->withPath(implode('/', $parts))); + $request = $request->withUri($uri->withPath(\implode('/', $parts))); } - return $out($request, $response); + return $delegate->process($request); } } diff --git a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php index 688d807d..1e2a7a69 100644 --- a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php +++ b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php @@ -3,7 +3,10 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Rest\Middleware; +use Interop\Http\ServerMiddleware\DelegateInterface; +use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Psr\Http\Message\ServerRequestInterface as Request; use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware; use Zend\Diactoros\Response; @@ -28,10 +31,13 @@ class PathVersionMiddlewareTest extends TestCase public function whenVersionIsProvidedRequestRemainsUnchanged() { $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo')); - $test = $this; - $this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) { - $test->assertSame($request, $req); - }); + + $delegate = $this->prophesize(DelegateInterface::class); + $process = $delegate->process($request)->willReturn(new Response()); + + $this->middleware->process($request, $delegate->reveal()); + + $process->shouldHaveBeenCalled(); } /** @@ -40,10 +46,17 @@ class PathVersionMiddlewareTest extends TestCase public function versionOneIsPrependedWhenNoVersionIsDefined() { $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz')); - $test = $this; - $this->middleware->__invoke($request, new Response(), function (Request $req) use ($request, $test) { - $test->assertNotSame($request, $req); - $this->assertEquals('/v1/bar/baz', $req->getUri()->getPath()); + + $delegate = $this->prophesize(DelegateInterface::class); + $delegate->process(Argument::type(Request::class))->will(function (array $args) use ($request) { + $req = \array_shift($args); + + Assert::assertNotSame($request, $req); + Assert::assertEquals('/v1/bar/baz', $req->getUri()->getPath()); + return new Response(); }); + + + $this->middleware->process($request, $delegate->reveal()); } }