|
17 | 17 | use Zend\Diactoros\Response;
|
18 | 18 | use Zend\Diactoros\Response\EmptyResponse;
|
19 | 19 | use Zend\Diactoros\ServerRequest;
|
| 20 | +use function array_map; |
20 | 21 | use function ini_set;
|
21 | 22 |
|
22 | 23 | /**
|
23 | 24 | * @coversDefaultClass \Lcobucci\ContentNegotiation\ContentTypeMiddleware
|
24 | 25 | */
|
25 | 26 | final class ContentTypeMiddlewareTest extends TestCase
|
26 | 27 | {
|
| 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 | + |
27 | 43 | /**
|
28 | 44 | * @test
|
29 | 45 | *
|
@@ -248,28 +264,25 @@ public function handle(ServerRequestInterface $request): ResponseInterface
|
248 | 264 | private function createMiddleware(bool $forceCharset = true, ?callable $streamFactory = null): ContentTypeMiddleware
|
249 | 265 | {
|
250 | 266 | 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), |
268 | 268 | [
|
269 | 269 | 'application/json' => new Formatter\Json(),
|
270 | 270 | 'text/html' => new NaiveTemplateEngine(),
|
271 | 271 | ],
|
272 | 272 | $streamFactory
|
273 | 273 | );
|
274 | 274 | }
|
| 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 | + } |
275 | 288 | }
|
0 commit comments