Skip to content

Commit 75b55fe

Browse files
authored
Merge pull request #11 from lcobucci/simplify-test
Extract supported formats in middleware test
2 parents af87ff9 + 49eefd7 commit 75b55fe

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

tests/ContentTypeMiddlewareTest.php

+30-17
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,29 @@
1717
use Zend\Diactoros\Response;
1818
use Zend\Diactoros\Response\EmptyResponse;
1919
use Zend\Diactoros\ServerRequest;
20+
use function array_map;
2021
use function ini_set;
2122

2223
/**
2324
* @coversDefaultClass \Lcobucci\ContentNegotiation\ContentTypeMiddleware
2425
*/
2526
final class ContentTypeMiddlewareTest extends TestCase
2627
{
28+
private const SUPPORTED_FORMATS = [
29+
'json' => [
30+
'extension' => ['json'],
31+
'mime-type' => ['application/json', 'text/json', 'application/x-json'],
32+
],
33+
'txt' => [
34+
'extension' => ['txt'],
35+
'mime-type' => ['text/plain'],
36+
],
37+
'html' => [
38+
'extension' => ['html', 'htm'],
39+
'mime-type' => ['text/html', 'application/xhtml+xml'],
40+
],
41+
];
42+
2743
/**
2844
* @test
2945
*
@@ -248,28 +264,25 @@ public function handle(ServerRequestInterface $request): ResponseInterface
248264
private function createMiddleware(bool $forceCharset = true, ?callable $streamFactory = null): ContentTypeMiddleware
249265
{
250266
return ContentTypeMiddleware::fromRecommendedSettings(
251-
[
252-
'json' => [
253-
'extension' => ['json'],
254-
'mime-type' => ['application/json', 'text/json', 'application/x-json'],
255-
'charset' => $forceCharset,
256-
],
257-
'txt' => [
258-
'extension' => ['txt'],
259-
'mime-type' => ['text/plain'],
260-
'charset' => $forceCharset,
261-
],
262-
'html' => [
263-
'extension' => ['html', 'htm'],
264-
'mime-type' => ['text/html', 'application/xhtml+xml'],
265-
'charset' => $forceCharset,
266-
],
267-
],
267+
$this->configureCharset($forceCharset),
268268
[
269269
'application/json' => new Formatter\Json(),
270270
'text/html' => new NaiveTemplateEngine(),
271271
],
272272
$streamFactory
273273
);
274274
}
275+
276+
/**
277+
* @return mixed[]
278+
*/
279+
private function configureCharset(bool $forceCharset = true): array
280+
{
281+
return array_map(
282+
function (array $config) use ($forceCharset): array {
283+
return ['charset' => $forceCharset] + $config;
284+
},
285+
self::SUPPORTED_FORMATS
286+
);
287+
}
275288
}

0 commit comments

Comments
 (0)