From 49eefd71e605d6b5e6b60b0be04ad8809a663b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 22 Apr 2018 12:26:12 +0200 Subject: [PATCH] Extract supported formats in middleware test To make things a bit more understandable. --- tests/ContentTypeMiddlewareTest.php | 47 ++++++++++++++++++----------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/tests/ContentTypeMiddlewareTest.php b/tests/ContentTypeMiddlewareTest.php index 0ade5445..3b269951 100644 --- a/tests/ContentTypeMiddlewareTest.php +++ b/tests/ContentTypeMiddlewareTest.php @@ -17,6 +17,7 @@ use Zend\Diactoros\Response; use Zend\Diactoros\Response\EmptyResponse; use Zend\Diactoros\ServerRequest; +use function array_map; use function ini_set; /** @@ -24,6 +25,21 @@ */ final class ContentTypeMiddlewareTest extends TestCase { + private const SUPPORTED_FORMATS = [ + 'json' => [ + 'extension' => ['json'], + 'mime-type' => ['application/json', 'text/json', 'application/x-json'], + ], + 'txt' => [ + 'extension' => ['txt'], + 'mime-type' => ['text/plain'], + ], + 'html' => [ + 'extension' => ['html', 'htm'], + 'mime-type' => ['text/html', 'application/xhtml+xml'], + ], + ]; + /** * @test * @@ -248,23 +264,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface private function createMiddleware(bool $forceCharset = true, ?callable $streamFactory = null): ContentTypeMiddleware { return ContentTypeMiddleware::fromRecommendedSettings( - [ - 'json' => [ - 'extension' => ['json'], - 'mime-type' => ['application/json', 'text/json', 'application/x-json'], - 'charset' => $forceCharset, - ], - 'txt' => [ - 'extension' => ['txt'], - 'mime-type' => ['text/plain'], - 'charset' => $forceCharset, - ], - 'html' => [ - 'extension' => ['html', 'htm'], - 'mime-type' => ['text/html', 'application/xhtml+xml'], - 'charset' => $forceCharset, - ], - ], + $this->configureCharset($forceCharset), [ 'application/json' => new Formatter\Json(), 'text/html' => new NaiveTemplateEngine(), @@ -272,4 +272,17 @@ private function createMiddleware(bool $forceCharset = true, ?callable $streamFa $streamFactory ); } + + /** + * @return mixed[] + */ + private function configureCharset(bool $forceCharset = true): array + { + return array_map( + function (array $config) use ($forceCharset): array { + return ['charset' => $forceCharset] + $config; + }, + self::SUPPORTED_FORMATS + ); + } }