Skip to content

Commit

Permalink
Merge pull request Sylius#8309 from gruberro/advance-request-configur…
Browse files Browse the repository at this point in the history
…ation

Allow multiple Accept headers in request configuration
  • Loading branch information
lchrusciel committed Jul 26, 2017
2 parents 4c976d5 + b255f55 commit c3947b3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 5 deletions.
Expand Up @@ -76,12 +76,18 @@ private function parseApiParameters(Request $request)
{
$parameters = [];

if (preg_match(self::API_VERSION_REGEXP, (string) $request->headers->get(self::API_VERSION_HEADER), $matches)) {
$parameters['serialization_version'] = $matches['version'];
$apiVersionHeaders = $request->headers->get(self::API_VERSION_HEADER, null, false);
foreach ($apiVersionHeaders as $apiVersionHeader) {
if (preg_match(self::API_VERSION_REGEXP, $apiVersionHeader, $matches)) {
$parameters['serialization_version'] = $matches['version'];
}
}

if (preg_match(self::API_GROUPS_REGEXP, (string) $request->headers->get(self::API_GROUPS_HEADER), $matches)) {
$parameters['serialization_groups'] = array_map('trim', explode(',', $matches['groups']));
$apiGroupsHeaders = $request->headers->get(self::API_GROUPS_HEADER, null, false);
foreach ($apiGroupsHeaders as $apiGroupsHeader) {
if (preg_match(self::API_GROUPS_REGEXP, $apiGroupsHeader, $matches)) {
$parameters['serialization_groups'] = array_map('trim', explode(',', $matches['groups']));
}
}

return array_merge($request->attributes->get('_sylius', []), $parameters);
Expand Down
Expand Up @@ -53,7 +53,7 @@ function it_creates_configuration_from_resource_metadata_and_request(
$request->headers = $headersBag;
$request->attributes = $attributesBag;

$headersBag->get('Accept')->willReturn(null);
$headersBag->get('Accept', null, false)->willReturn([]);

$attributesBag->get('_sylius', [])->willReturn(['template' => ':Product:show.html.twig']);
$parametersParser
Expand All @@ -74,6 +74,8 @@ function it_creates_configuration_without_default_settings(
$request->headers = $headersBag;
$request->attributes = $attributesBag;

$headersBag->get('Accept', null, false)->willReturn([]);

$attributesBag->get('_sylius', [])->willReturn(['template' => ':Product:list.html.twig']);
$parametersParser
->parseRequestValues(['template' => ':Product:list.html.twig'], $request)
Expand All @@ -83,6 +85,92 @@ function it_creates_configuration_without_default_settings(
$this->create($metadata, $request)->isSortable()->shouldReturn(false);
}

function it_creates_configuration_for_serialization_group_from_single_header(
ParametersParserInterface $parametersParser,
MetadataInterface $metadata,
Request $request,
ParameterBag $headersBag,
ParameterBag $attributesBag
) {
$request->headers = $headersBag;
$request->attributes = $attributesBag;

$headersBag->get('Accept', null, false)->willReturn(['groups=Default,Detailed']);

$attributesBag->get('_sylius', [])->willReturn([]);
$parametersParser
->parseRequestValues(['serialization_groups' => ['Default', 'Detailed']], $request)
->willReturn(['template' => ':Product:list.html.twig'])
;

$this->create($metadata, $request)->isSortable()->shouldReturn(false);
}

function it_creates_configuration_for_serialization_group_from_multiple_headers(
ParametersParserInterface $parametersParser,
MetadataInterface $metadata,
Request $request,
ParameterBag $headersBag,
ParameterBag $attributesBag
) {
$request->headers = $headersBag;
$request->attributes = $attributesBag;

$headersBag->get('Accept', null, false)->willReturn(['application/json', 'groups=Default,Detailed']);

$attributesBag->get('_sylius', [])->willReturn([]);
$parametersParser
->parseRequestValues(['serialization_groups' => ['Default', 'Detailed']], $request)
->willReturn(['template' => ':Product:list.html.twig'])
;

$this->create($metadata, $request)->isSortable()->shouldReturn(false);
}

function it_creates_configuration_for_serialization_version_from_single_header(
ParametersParserInterface $parametersParser,
MetadataInterface $metadata,
Request $request,
ParameterBag $headersBag,
ParameterBag $attributesBag
) {
$request->headers = $headersBag;
$request->attributes = $attributesBag;

$headersBag->get('Accept', null, false)->willReturn(['version=1.0.0']);

$attributesBag->get('_sylius', [])->willReturn([]);

$parametersParser
->parseRequestValues(['serialization_version' => '1.0.0'], $request)
->willReturn(['template' => ':Product:list.html.twig'])
;

$this->create($metadata, $request)->isSortable()->shouldReturn(false);
}

function it_creates_configuration_for_serialization_version_from_multiple_headers(
ParametersParserInterface $parametersParser,
MetadataInterface $metadata,
Request $request,
ParameterBag $headersBag,
ParameterBag $attributesBag
) {
$request->headers = $headersBag;
$request->attributes = $attributesBag;

$headersBag->get('Accept', null, false)->willReturn(['application/xml', 'version=1.0.0']);

$attributesBag->get('_sylius', [])->willReturn([]);

$parametersParser
->parseRequestValues(['serialization_version' => '1.0.0'], $request)
->willReturn(['template' => ':Product:list.html.twig'])
;

$this->create($metadata, $request)->isSortable()->shouldReturn(false);
}

function it_creates_configuration_with_default_settings(
ParametersParserInterface $parametersParser,
MetadataInterface $metadata,
Expand All @@ -95,6 +183,8 @@ function it_creates_configuration_with_default_settings(
$request->headers = $headersBag;
$request->attributes = $attributesBag;

$headersBag->get('Accept', null, false)->willReturn([]);

$attributesBag->get('_sylius', [])->willReturn(['template' => ':Product:list.html.twig']);

$parametersParser
Expand Down

0 comments on commit c3947b3

Please sign in to comment.