Skip to content

Commit 8e28a58

Browse files
authored
Merge pull request #3114 from mRoca/entrypoints-sorting
Add entrypoints sorting
2 parents 9799193 + 854bcc9 commit 8e28a58

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

features/bootstrap/JsonApiContext.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ public function theJsonNodeShouldNotBeAnEmptyString($node)
107107
}
108108
}
109109

110+
/**
111+
* @Then the JSON node :node should be sorted
112+
* @Then the JSON should be sorted
113+
*/
114+
public function theJsonNodeShouldBeSorted($node = '')
115+
{
116+
$actual = (array) $this->getValueOfNode($node);
117+
118+
if (!is_array($actual)) {
119+
throw new \Exception(sprintf('The "%s" node value is not an array', $node));
120+
}
121+
122+
$expected = $actual;
123+
ksort($expected);
124+
125+
if ($actual !== $expected) {
126+
throw new ExpectationFailedException(sprintf('The json node "%s" is not sorted by keys', $node));
127+
}
128+
}
129+
110130
/**
111131
* @Given there is a RelatedDummy
112132
*/

features/hydra/entrypoint.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Feature: Entrypoint support
88
Then the response status code should be 200
99
And the response should be in JSON
1010
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
11+
And the JSON should be sorted
1112
And the JSON node "@context" should be equal to "/contexts/Entrypoint"
1213
And the JSON node "@id" should be equal to "/"
1314
And the JSON node "@type" should be equal to "Entrypoint"

src/Hydra/Serializer/EntrypointNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public function normalize($object, $format = null, array $context = [])
6565
}
6666
}
6767

68+
ksort($entrypoint);
69+
6870
return $entrypoint;
6971
}
7072

tests/Hydra/Serializer/EntrypointNormalizerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2222
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
2323
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
24+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FooDummy;
2425
use PHPUnit\Framework\TestCase;
2526

2627
/**
@@ -47,14 +48,16 @@ public function testSupportNormalization()
4748

4849
public function testNormalize()
4950
{
50-
$collection = new ResourceNameCollection([Dummy::class]);
51+
$collection = new ResourceNameCollection([FooDummy::class, Dummy::class]);
5152
$entrypoint = new Entrypoint($collection);
5253

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

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

5962
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
6063
$urlGeneratorProphecy->generate('api_entrypoint')->willReturn('/api')->shouldBeCalled();
@@ -67,6 +70,7 @@ public function testNormalize()
6770
'@id' => '/api',
6871
'@type' => 'Entrypoint',
6972
'dummy' => '/api/dummies',
73+
'fooDummy' => '/api/foo_dummies',
7074
];
7175
$this->assertEquals($expected, $normalizer->normalize($entrypoint, EntrypointNormalizer::FORMAT));
7276
}

0 commit comments

Comments
 (0)