Skip to content

Commit

Permalink
Merge pull request #3114 from mRoca/entrypoints-sorting
Browse files Browse the repository at this point in the history
Add entrypoints sorting
  • Loading branch information
soyuka committed Sep 30, 2019
2 parents 9799193 + 854bcc9 commit 8e28a58
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
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

0 comments on commit 8e28a58

Please sign in to comment.