From 734dac9456f3ea2b703b650e0bc50b5211109d60 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 24 Mar 2017 23:24:11 +0100 Subject: [PATCH] Migrated RedirectAction to psr-15 middleware --- module/Core/src/Action/RedirectAction.php | 48 +++++-------------- .../Core/test/Action/RedirectActionTest.php | 45 +++++------------ 2 files changed, 23 insertions(+), 70 deletions(-) diff --git a/module/Core/src/Action/RedirectAction.php b/module/Core/src/Action/RedirectAction.php index a86a98c4..d8b4a04e 100644 --- a/module/Core/src/Action/RedirectAction.php +++ b/module/Core/src/Action/RedirectAction.php @@ -2,6 +2,8 @@ namespace Shlinkio\Shlink\Core\Action; use Acelaya\ZsmAnnotatedServices\Annotation\Inject; +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 Psr\Log\LoggerInterface; @@ -11,7 +13,6 @@ use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Core\Service\VisitsTracker; use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; use Zend\Diactoros\Response\RedirectResponse; -use Zend\Stratigility\MiddlewareInterface; class RedirectAction implements MiddlewareInterface { @@ -47,31 +48,15 @@ class RedirectAction 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) { $shortCode = $request->getAttribute('shortCode', ''); @@ -80,8 +65,8 @@ class RedirectAction implements MiddlewareInterface // If provided shortCode does not belong to a valid long URL, dispatch next middleware, which will trigger // a not-found error - if (! isset($longUrl)) { - return $this->notFoundResponse($request, $response, $out); + if ($longUrl === null) { + return $delegate->process($request); } // Track visit to this short code @@ -93,18 +78,7 @@ class RedirectAction implements MiddlewareInterface } catch (\Exception $e) { // In case of error, dispatch 404 error $this->logger->error('Error redirecting to long URL.' . PHP_EOL . $e); - return $this->notFoundResponse($request, $response, $out); + return $delegate->process($request); } } - - /** - * @param Request $request - * @param Response $response - * @param callable $out - * @return Response - */ - protected function notFoundResponse(Request $request, Response $response, callable $out) - { - return $out($request, $response->withStatus(404), 'Not Found'); - } } diff --git a/module/Core/test/Action/RedirectActionTest.php b/module/Core/test/Action/RedirectActionTest.php index 75726684..65b6250f 100644 --- a/module/Core/test/Action/RedirectActionTest.php +++ b/module/Core/test/Action/RedirectActionTest.php @@ -4,11 +4,10 @@ namespace ShlinkioTest\Shlink\Core\Action; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Core\Action\RedirectAction; use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Core\Service\VisitsTracker; +use ShlinkioTest\Shlink\Common\Util\TestUtils; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequestFactory; @@ -42,7 +41,7 @@ class RedirectActionTest extends TestCase ->shouldBeCalledTimes(1); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); - $response = $this->action->__invoke($request, new Response()); + $response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $this->assertInstanceOf(Response\RedirectResponse::class, $response); $this->assertEquals(302, $response->getStatusCode()); @@ -53,52 +52,32 @@ class RedirectActionTest extends TestCase /** * @test */ - public function nextErrorMiddlewareIsInvokedIfLongUrlIsNotFound() + public function nextMiddlewareIsInvokedIfLongUrlIsNotFound() { $shortCode = 'abc123'; $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null) ->shouldBeCalledTimes(1); + $delegate = TestUtils::createDelegateMock(); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); - $originalResponse = new Response(); - $test = $this; - $this->action->__invoke($request, $originalResponse, function ( - ServerRequestInterface $req, - ResponseInterface $resp, - $error - ) use ( - $test, - $request - ) { - $test->assertSame($request, $req); - $test->assertEquals(404, $resp->getStatusCode()); - $test->assertEquals('Not Found', $error); - }); + $this->action->process($request, $delegate->reveal()); + + $delegate->process($request)->shouldHaveBeenCalledTimes(1); } /** * @test */ - public function nextErrorMiddlewareIsInvokedIfAnExceptionIsThrown() + public function nextMiddlewareIsInvokedIfAnExceptionIsThrown() { $shortCode = 'abc123'; $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class) ->shouldBeCalledTimes(1); + $delegate = TestUtils::createDelegateMock(); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); - $originalResponse = new Response(); - $test = $this; - $this->action->__invoke($request, $originalResponse, function ( - ServerRequestInterface $req, - ResponseInterface $resp, - $error - ) use ( - $test, - $request - ) { - $test->assertSame($request, $req); - $test->assertEquals(404, $resp->getStatusCode()); - $test->assertEquals('Not Found', $error); - }); + $this->action->process($request, $delegate->reveal()); + + $delegate->process($request)->shouldHaveBeenCalledTimes(1); } }