Skip to content

Add entrypoints sorting #3114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions features/bootstrap/JsonApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ public function theJsonNodeShouldNotBeAnEmptyString($node)
}
}

/**
* @Then the JSON node :node should be sorted
* @Then the JSON should be sorted
*/
public function theJsonNodeShouldBeSorted($node = '')
{
$actual = (array) $this->getValueOfNode($node);

if (!is_array($actual)) {
throw new \Exception(sprintf('The "%s" node value is not an array', $node));
}

$expected = $actual;
ksort($expected);

if ($actual !== $expected) {
throw new ExpectationFailedException(sprintf('The json node "%s" is not sorted by keys', $node));
}
}

/**
* @Given there is a RelatedDummy
*/
Expand Down
1 change: 1 addition & 0 deletions features/hydra/entrypoint.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Feature: Entrypoint support
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be sorted
And the JSON node "@context" should be equal to "/contexts/Entrypoint"
And the JSON node "@id" should be equal to "/"
And the JSON node "@type" should be equal to "Entrypoint"
Expand Down
2 changes: 2 additions & 0 deletions src/Hydra/Serializer/EntrypointNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function normalize($object, $format = null, array $context = [])
}
}

ksort($entrypoint);

return $entrypoint;
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Hydra/Serializer/EntrypointNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FooDummy;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -47,14 +48,16 @@ public function testSupportNormalization()

public function testNormalize()
{
$collection = new ResourceNameCollection([Dummy::class]);
$collection = new ResourceNameCollection([FooDummy::class, Dummy::class]);
$entrypoint = new Entrypoint($collection);

$factoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$factoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('Dummy', null, null, null, ['get']))->shouldBeCalled();
$factoryProphecy->create(FooDummy::class)->willReturn(new ResourceMetadata('FooDummy', null, null, null, ['get']))->shouldBeCalled();

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getIriFromResourceClass(Dummy::class)->willReturn('/api/dummies')->shouldBeCalled();
$iriConverterProphecy->getIriFromResourceClass(FooDummy::class)->willReturn('/api/foo_dummies')->shouldBeCalled();

$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
$urlGeneratorProphecy->generate('api_entrypoint')->willReturn('/api')->shouldBeCalled();
Expand All @@ -67,6 +70,7 @@ public function testNormalize()
'@id' => '/api',
'@type' => 'Entrypoint',
'dummy' => '/api/dummies',
'fooDummy' => '/api/foo_dummies',
];
$this->assertEquals($expected, $normalizer->normalize($entrypoint, EntrypointNormalizer::FORMAT));
}
Expand Down