diff --git a/module/Common/src/Middleware/LocaleMiddleware.php b/module/Common/src/Middleware/LocaleMiddleware.php index 20f796ff..0cb81502 100644 --- a/module/Common/src/Middleware/LocaleMiddleware.php +++ b/module/Common/src/Middleware/LocaleMiddleware.php @@ -2,10 +2,11 @@ namespace Shlinkio\Shlink\Common\Middleware; 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 Zend\I18n\Translator\Translator; -use Zend\Stratigility\MiddlewareInterface; class LocaleMiddleware implements MiddlewareInterface { @@ -25,40 +26,26 @@ class LocaleMiddleware implements MiddlewareInterface $this->translator = $translator; } + + /** - * 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) { if (! $request->hasHeader('Accept-Language')) { - return $out($request, $response); + return $delegate->process($request); } $locale = $request->getHeaderLine('Accept-Language'); $this->translator->setLocale($this->normalizeLocale($locale)); - return $out($request, $response); + return $delegate->process($request); } /** diff --git a/module/Common/test/Middleware/LocaleMiddlewareTest.php b/module/Common/test/Middleware/LocaleMiddlewareTest.php index 4d9e81ce..762dc31f 100644 --- a/module/Common/test/Middleware/LocaleMiddlewareTest.php +++ b/module/Common/test/Middleware/LocaleMiddlewareTest.php @@ -3,7 +3,7 @@ namespace ShlinkioTest\Shlink\Common\Middleware; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware; -use Zend\Diactoros\Response; +use ShlinkioTest\Shlink\Common\Util\TestUtils; use Zend\Diactoros\ServerRequestFactory; use Zend\I18n\Translator\Translator; @@ -30,9 +30,7 @@ class LocaleMiddlewareTest extends TestCase public function whenNoHeaderIsPresentLocaleIsNotChanged() { $this->assertEquals('ru', $this->translator->getLocale()); - $this->middleware->__invoke(ServerRequestFactory::fromGlobals(), new Response(), function ($req, $resp) { - return $resp; - }); + $this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createDelegateMock()->reveal()); $this->assertEquals('ru', $this->translator->getLocale()); } @@ -43,9 +41,7 @@ class LocaleMiddlewareTest extends TestCase { $this->assertEquals('ru', $this->translator->getLocale()); $request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es'); - $this->middleware->__invoke($request, new Response(), function ($req, $resp) { - return $resp; - }); + $this->middleware->process($request, TestUtils::createDelegateMock()->reveal()); $this->assertEquals('es', $this->translator->getLocale()); } @@ -54,18 +50,16 @@ class LocaleMiddlewareTest extends TestCase */ public function localeGetsNormalized() { + $delegate = TestUtils::createDelegateMock(); + $this->assertEquals('ru', $this->translator->getLocale()); $request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es_ES'); - $this->middleware->__invoke($request, new Response(), function ($req, $resp) { - return $resp; - }); + $this->middleware->process($request, $delegate->reveal()); $this->assertEquals('es', $this->translator->getLocale()); $request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'en-US'); - $this->middleware->__invoke($request, new Response(), function ($req, $resp) { - return $resp; - }); + $this->middleware->process($request, $delegate->reveal()); $this->assertEquals('en', $this->translator->getLocale()); } } diff --git a/module/Common/test/Util/TestUtils.php b/module/Common/test/Util/TestUtils.php new file mode 100644 index 00000000..8bd4969d --- /dev/null +++ b/module/Common/test/Util/TestUtils.php @@ -0,0 +1,35 @@ +prophesize(DelegateInterface::class); + $delegate->process($argument)->willReturn($response ?: new Response()); + + return $delegate; + } + + /** + * @return Prophet + */ + private static function getProphet() + { + if (static::$prophet === null) { + static::$prophet = new Prophet(); + } + + return static::$prophet; + } +}