Skip to content

Commit

Permalink
Merge pull request #11 from lcobucci/simplify-test
Browse files Browse the repository at this point in the history
Extract supported formats in middleware test
  • Loading branch information
lcobucci authored Apr 22, 2018
2 parents af87ff9 + 49eefd7 commit 75b55fe
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions tests/ContentTypeMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Diactoros\ServerRequest;
use function array_map;
use function ini_set;

/**
* @coversDefaultClass \Lcobucci\ContentNegotiation\ContentTypeMiddleware
*/
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
*
Expand Down Expand Up @@ -248,28 +264,25 @@ 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(),
],
$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
);
}
}

0 comments on commit 75b55fe

Please sign in to comment.