Skip to content

Commit e12c44e

Browse files
vincentchalamonsoyuka
authored andcommitted
test: fix Hal/Serializer tests (#38)
* test: fix Hal/Serializer tests * Update src/Hal/Serializer/EntrypointNormalizer.php Co-authored-by: Antoine Bluchet <[email protected]> * Apply suggestions from code review c * fix: review Co-authored-by: Antoine Bluchet <[email protected]>
1 parent 8ba9038 commit e12c44e

File tree

8 files changed

+54
-106
lines changed

8 files changed

+54
-106
lines changed

src/Hal/Serializer/CollectionNormalizer.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
namespace ApiPlatform\Hal\Serializer;
1515

1616
use ApiPlatform\Api\ResourceClassResolverInterface;
17-
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
18-
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
1917
use ApiPlatform\Serializer\AbstractCollectionNormalizer;
2018
use ApiPlatform\Util\IriHelper;
2119
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
@@ -43,14 +41,9 @@ protected function getPaginationData($object, array $context = []): array
4341
[$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems] = $this->getPaginationConfig($object, $context);
4442
$parsed = IriHelper::parseIri($context['uri'] ?? '/', $this->pageParameterName);
4543

46-
/** @var ResourceMetadata|ResourceMetadataCollection */
4744
$metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? '');
48-
if ($metadata instanceof ResourceMetadataCollection) {
49-
$operation = $metadata->getOperation($context['operation_name'] ?? null);
50-
$urlGenerationStrategy = $operation->getUrlGenerationStrategy();
51-
} else {
52-
$urlGenerationStrategy = $metadata->getAttribute('url_generation_strategy');
53-
}
45+
$operation = $metadata->getOperation($context['operation_name'] ?? null);
46+
$urlGenerationStrategy = $operation->getUrlGenerationStrategy();
5447

5548
$data = [
5649
'_links' => [

src/Hal/Serializer/EntrypointNormalizer.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616
use ApiPlatform\Api\Entrypoint;
1717
use ApiPlatform\Api\IriConverterInterface;
1818
use ApiPlatform\Api\UrlGeneratorInterface;
19-
use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
20-
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
21-
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2219
use ApiPlatform\Exception\InvalidArgumentException;
2320
use ApiPlatform\Metadata\CollectionOperationInterface;
2421
use ApiPlatform\Metadata\Operation;
2522
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
26-
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2723
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2824
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2925

@@ -36,27 +32,15 @@ final class EntrypointNormalizer implements NormalizerInterface, CacheableSuppor
3632
{
3733
public const FORMAT = 'jsonhal';
3834

39-
/**
40-
* @var ResourceMetadataFactoryInterface|ResourceMetadataCollectionFactoryInterface
41-
*/
4235
private $resourceMetadataFactory;
43-
/** @var LegacyIriConverterInterface|IriConverterInterface */
4436
private $iriConverter;
4537
private $urlGenerator;
4638

47-
public function __construct($resourceMetadataFactory, $iriConverter, UrlGeneratorInterface $urlGenerator)
39+
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, UrlGeneratorInterface $urlGenerator)
4840
{
4941
$this->resourceMetadataFactory = $resourceMetadataFactory;
5042
$this->iriConverter = $iriConverter;
5143
$this->urlGenerator = $urlGenerator;
52-
53-
if ($iriConverter instanceof LegacyIriConverterInterface) {
54-
trigger_deprecation('api-platform/core', '2.7', sprintf('Use an implementation of "%s" instead of "%s".', IriConverterInterface::class, LegacyIriConverterInterface::class));
55-
}
56-
57-
if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
58-
trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
59-
}
6044
}
6145

6246
/**
@@ -67,32 +51,19 @@ public function normalize($object, $format = null, array $context = []): array
6751
$entrypoint = ['_links' => ['self' => ['href' => $this->urlGenerator->generate('api_entrypoint')]]];
6852

6953
foreach ($object->getResourceNameCollection() as $resourceClass) {
70-
/** @var ResourceMetadata|ResourceMetadataCollection */
7154
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
7255

73-
if ($resourceMetadata instanceof ResourceMetadata) {
74-
if (!$resourceMetadata->getCollectionOperations()) {
75-
continue;
76-
}
77-
78-
try {
79-
$entrypoint['_links'][lcfirst($resourceMetadata->getShortName())]['href'] = $this->iriConverter->getIriFromResourceClass($resourceClass);
80-
} catch (InvalidArgumentException $ex) {
81-
// Ignore resources without GET operations
82-
}
83-
}
84-
8556
foreach ($resourceMetadata as $resource) {
86-
foreach ($resource->getOperations() as $operationName => $operation) {
57+
foreach ($resource->getOperations() as $operation) {
8758
/** @var Operation $operation */
8859
if (!$operation instanceof CollectionOperationInterface) {
8960
continue;
9061
}
9162

9263
try {
93-
$href = $this->iriConverter instanceof LegacyIriConverterInterface ? $this->iriConverter->getIriFromResourceClass($resourceClass) : $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
64+
$href = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
9465
$entrypoint['_links'][lcfirst($operation->getShortName())]['href'] = $href;
95-
} catch (InvalidArgumentException $ex) {
66+
} catch (InvalidArgumentException) {
9667
// Ignore resources without GET operations
9768
}
9869
}

src/Hal/Serializer/ObjectNormalizer.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace ApiPlatform\Hal\Serializer;
1515

1616
use ApiPlatform\Api\IriConverterInterface;
17-
use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
1817
use Symfony\Component\Serializer\Exception\LogicException;
1918
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2019
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
@@ -31,14 +30,10 @@ final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterfa
3130
private $decorated;
3231
private $iriConverter;
3332

34-
public function __construct(NormalizerInterface $decorated, $iriConverter)
33+
public function __construct(NormalizerInterface $decorated, IriConverterInterface $iriConverter)
3534
{
3635
$this->decorated = $decorated;
3736
$this->iriConverter = $iriConverter;
38-
39-
if ($iriConverter instanceof LegacyIriConverterInterface) {
40-
trigger_deprecation('api-platform/core', '2.7', sprintf('Use an implementation of "%s" instead of "%s".', IriConverterInterface::class, LegacyIriConverterInterface::class));
41-
}
4237
}
4338

4439
/**
@@ -81,7 +76,7 @@ public function normalize($object, $format = null, array $context = [])
8176
$metadata = [
8277
'_links' => [
8378
'self' => [
84-
'href' => $this->iriConverter instanceof LegacyIriConverterInterface ? $this->iriConverter->getIriFromItem($originalResource) : $this->iriConverter->getIriFromResource($originalResource),
79+
'href' => $this->iriConverter->getIriFromResource($originalResource),
8580
],
8681
],
8782
];

src/Serializer/AbstractCollectionNormalizer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace ApiPlatform\Serializer;
1515

1616
use ApiPlatform\Api\ResourceClassResolverInterface;
17-
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1817
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
1918
use ApiPlatform\State\Pagination\PaginatorInterface;
2019
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
@@ -43,12 +42,9 @@ abstract class AbstractCollectionNormalizer implements NormalizerInterface, Norm
4342
protected $resourceClassResolver;
4443
protected $pageParameterName;
4544

46-
/**
47-
* @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface
48-
*/
4945
protected $resourceMetadataFactory;
5046

51-
public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, $resourceMetadataFactory = null)
47+
public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null)
5248
{
5349
$this->resourceClassResolver = $resourceClassResolver;
5450
$this->pageParameterName = $pageParameterName;

src/Serializer/AbstractItemNormalizer.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,14 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer
5959
* @var PropertyNameCollectionFactoryInterface
6060
*/
6161
protected $propertyNameCollectionFactory;
62-
/**
63-
* @var LegacyPropertyMetadataFactoryInterface|PropertyMetadataFactoryInterface
64-
*/
6562
protected $propertyMetadataFactory;
66-
/**
67-
* @var LegacyIriConverterInterface|IriConverterInterface
68-
*/
6963
protected $iriConverter;
7064
protected $resourceClassResolver;
7165
protected $resourceAccessChecker;
7266
protected $propertyAccessor;
73-
protected $allowPlainIdentifiers;
74-
protected $dataTransformers = [];
7567
protected $localCache = [];
7668

77-
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null)
69+
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, ResourceAccessCheckerInterface $resourceAccessChecker = null)
7870
{
7971
if (!isset($defaultContext['circular_reference_handler'])) {
8072
$defaultContext['circular_reference_handler'] = function ($object) {

tests/Hal/Serializer/CollectionNormalizerTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
namespace ApiPlatform\Tests\Hal\Serializer;
1515

1616
use ApiPlatform\Api\ResourceClassResolverInterface;
17-
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18-
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1917
use ApiPlatform\Hal\Serializer\CollectionNormalizer;
18+
use ApiPlatform\Metadata\ApiResource;
19+
use ApiPlatform\Metadata\GetCollection;
20+
use ApiPlatform\Metadata\Operations;
21+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22+
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2023
use ApiPlatform\State\Pagination\PaginatorInterface;
2124
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
2225
use ApiPlatform\Tests\ProphecyTrait;
@@ -34,7 +37,7 @@ class CollectionNormalizerTest extends TestCase
3437
public function testSupportsNormalize()
3538
{
3639
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
37-
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
40+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
3841
$normalizer = new CollectionNormalizer($resourceClassResolverProphecy->reveal(), 'page', $resourceMetadataFactoryProphecy->reveal());
3942

4043
$this->assertTrue($normalizer->supportsNormalization([], CollectionNormalizer::FORMAT));
@@ -48,7 +51,7 @@ public function testNormalizeApiSubLevel()
4851
{
4952
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
5053
$resourceClassResolverProphecy->getResourceClass()->shouldNotBeCalled();
51-
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
54+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
5255

5356
$itemNormalizer = $this->prophesize(NormalizerInterface::class);
5457
$itemNormalizer->normalize('bar', null, ['api_sub_level' => true])->willReturn(22);
@@ -142,20 +145,26 @@ private function normalizePaginator($partial = false)
142145
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
143146
$resourceClassResolverProphecy->getResourceClass($paginatorProphecy, 'Foo')->willReturn('Foo');
144147

145-
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
146-
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata());
148+
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
149+
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadataCollection('Foo', [
150+
(new ApiResource())->withShortName('Foo')->withOperations(new Operations([
151+
'bar' => (new GetCollection())->withShortName('Foo'),
152+
])),
153+
]));
147154

148155
$itemNormalizer = $this->prophesize(NormalizerInterface::class);
149156
$itemNormalizer->normalize('foo', CollectionNormalizer::FORMAT, [
150-
'api_sub_level' => true,
151157
'resource_class' => 'Foo',
158+
'operation_name' => 'bar',
159+
'api_sub_level' => true,
152160
])->willReturn(['_links' => ['self' => '/me'], 'name' => 'Kévin']);
153161

154162
$normalizer = new CollectionNormalizer($resourceClassResolverProphecy->reveal(), 'page', $resourceMetadataFactoryProphecy->reveal());
155163
$normalizer->setNormalizer($itemNormalizer->reveal());
156164

157165
return $normalizer->normalize($paginatorProphecy->reveal(), CollectionNormalizer::FORMAT, [
158166
'resource_class' => 'Foo',
167+
'operation_name' => 'bar',
159168
]);
160169
}
161170
}

tests/Hal/Serializer/EntrypointNormalizerTest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
namespace ApiPlatform\Tests\Hal\Serializer;
1515

1616
use ApiPlatform\Api\Entrypoint;
17+
use ApiPlatform\Api\IriConverterInterface;
1718
use ApiPlatform\Api\UrlGeneratorInterface;
18-
use ApiPlatform\Core\Api\IriConverterInterface;
19-
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
20-
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2119
use ApiPlatform\Hal\Serializer\EntrypointNormalizer;
20+
use ApiPlatform\Metadata\ApiResource;
21+
use ApiPlatform\Metadata\GetCollection;
22+
use ApiPlatform\Metadata\Operations;
23+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24+
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2225
use ApiPlatform\Metadata\Resource\ResourceNameCollection;
2326
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
2427
use ApiPlatform\Tests\ProphecyTrait;
@@ -37,7 +40,7 @@ public function testSupportNormalization()
3740
$collection = new ResourceNameCollection();
3841
$entrypoint = new Entrypoint($collection);
3942

40-
$factoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
43+
$factoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
4144
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
4245
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
4346

@@ -53,11 +56,18 @@ public function testNormalize()
5356
{
5457
$collection = new ResourceNameCollection([Dummy::class]);
5558
$entrypoint = new Entrypoint($collection);
56-
$factoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
57-
$factoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata('Dummy', null, null, null, ['get']))->shouldBeCalled();
59+
$factoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
60+
$operation = (new GetCollection())->withShortName('Dummy')->withClass(Dummy::class);
61+
$factoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadataCollection('Dummy', [
62+
(new ApiResource('Dummy'))
63+
->withShortName('Dummy')
64+
->withOperations(new Operations([
65+
'get' => $operation,
66+
])),
67+
]))->shouldBeCalled();
5868

5969
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
60-
$iriConverterProphecy->getIriFromResourceClass(Dummy::class)->willReturn('/api/dummies')->shouldBeCalled();
70+
$iriConverterProphecy->getIriFromResource(Dummy::class, UrlGeneratorInterface::ABS_PATH, $operation)->willReturn('/api/dummies')->shouldBeCalled();
6171

6272
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
6373
$urlGeneratorProphecy->generate('api_entrypoint')->willReturn('/api')->shouldBeCalled();

0 commit comments

Comments
 (0)