Skip to content

Commit 7479f51

Browse files
vincentchalamonalanpoulain
authored andcommitted
test: fix Hydra/Serializer tests (#41)
1 parent de47d8c commit 7479f51

11 files changed

+185
-499
lines changed

src/Hydra/Serializer/CollectionFiltersNormalizer.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use ApiPlatform\Api\FilterInterface;
1717
use ApiPlatform\Api\FilterLocatorTrait;
1818
use ApiPlatform\Api\ResourceClassResolverInterface;
19-
use ApiPlatform\Core\Api\FilterCollection;
20-
use ApiPlatform\Core\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
21-
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2219
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2320
use Psr\Container\ContainerInterface;
2421
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
@@ -39,19 +36,15 @@ final class CollectionFiltersNormalizer implements NormalizerInterface, Normaliz
3936
private $resourceClassResolver;
4037

4138
/**
42-
* @param ContainerInterface|FilterCollection $filterLocator The new filter locator or the deprecated filter collection
43-
* @param mixed $resourceMetadataFactory
44-
* @param ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver
39+
* @param ContainerInterface $filterLocator The new filter locator or the deprecated filter collection
40+
* @param mixed $resourceMetadataFactory
4541
*/
46-
public function __construct(NormalizerInterface $collectionNormalizer, $resourceMetadataFactory, $resourceClassResolver, $filterLocator)
42+
public function __construct(NormalizerInterface $collectionNormalizer, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, ContainerInterface $filterLocator)
4743
{
4844
$this->setFilterLocator($filterLocator);
4945
$this->collectionNormalizer = $collectionNormalizer;
5046
$this->resourceMetadataFactory = $resourceMetadataFactory;
5147
$this->resourceClassResolver = $resourceClassResolver;
52-
if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
53-
trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
54-
}
5548
}
5649

5750
/**
@@ -85,19 +78,8 @@ public function normalize($object, $format = null, array $context = [])
8578
return $data;
8679
}
8780
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
88-
$resourceFilters = null;
89-
if ($this->resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
90-
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
91-
$operationName = $context['collection_operation_name'] ?? null;
92-
if (null === $operationName) {
93-
$resourceFilters = $resourceMetadata->getAttribute('filters', []);
94-
} else {
95-
$resourceFilters = $resourceMetadata->getCollectionOperationAttribute($operationName, 'filters', [], true);
96-
}
97-
} elseif ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
98-
$operation = $context['operation'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
99-
$resourceFilters = $operation->getFilters();
100-
}
81+
$operation = $context['operation'] ?? $this->resourceMetadataFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
82+
$resourceFilters = $operation->getFilters();
10183
if (!$resourceFilters) {
10284
return $data;
10385
}

src/Hydra/Serializer/CollectionNormalizer.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use ApiPlatform\Api\IriConverterInterface;
1717
use ApiPlatform\Api\ResourceClassResolverInterface;
1818
use ApiPlatform\Api\UrlGeneratorInterface;
19-
use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
20-
use ApiPlatform\Core\Api\OperationType;
2119
use ApiPlatform\JsonLd\ContextBuilderInterface;
2220
use ApiPlatform\JsonLd\Serializer\JsonLdContextTrait;
2321
use ApiPlatform\Serializer\ContextTrait;
@@ -50,14 +48,10 @@ final class CollectionNormalizer implements NormalizerInterface, NormalizerAware
5048
self::IRI_ONLY => false,
5149
];
5250

53-
public function __construct(ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, $iriConverter, array $defaultContext = [])
51+
public function __construct(ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, IriConverterInterface $iriConverter, array $defaultContext = [])
5452
{
5553
$this->contextBuilder = $contextBuilder;
5654
$this->resourceClassResolver = $resourceClassResolver;
57-
58-
if ($iriConverter instanceof LegacyIriConverterInterface) {
59-
trigger_deprecation('api-platform/core', '2.7', sprintf('Use an implementation of "%s" instead of "%s".', IriConverterInterface::class, LegacyIriConverterInterface::class));
60-
}
6155
$this->iriConverter = $iriConverter;
6256
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
6357
}
@@ -84,22 +78,15 @@ public function normalize($object, $format = null, array $context = []): array
8478
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
8579
$context = $this->initContext($resourceClass, $context);
8680
$data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
87-
88-
if ($this->iriConverter instanceof LegacyIriConverterInterface) {
89-
// TODO: remove in 3.0
90-
$data['@id'] = isset($context['operation_type']) && OperationType::SUBRESOURCE === $context['operation_type'] ? $this->iriConverter->getSubresourceIriFromResourceClass($resourceClass, $context) : $this->iriConverter->getIriFromResourceClass($resourceClass);
91-
} else {
92-
$data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
93-
}
94-
81+
$data['@id'] = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context);
9582
$data['@type'] = 'hydra:Collection';
9683
$data['hydra:member'] = [];
9784
$iriOnly = $context[self::IRI_ONLY] ?? $this->defaultContext[self::IRI_ONLY];
9885
unset($context['operation'], $context['operation_name'], $context['uri_variables']);
9986

10087
foreach ($object as $obj) {
10188
if ($iriOnly) {
102-
$data['hydra:member'][] = $this->iriConverter instanceof LegacyIriConverterInterface ? $this->iriConverter->getIriFromItem($obj) : $this->iriConverter->getIriFromResource($obj);
89+
$data['hydra:member'][] = $this->iriConverter->getIriFromResource($obj);
10390
} else {
10491
$data['hydra:member'][] = $this->normalizer->normalize($obj, $format, $context);
10592
}
@@ -108,6 +95,7 @@ public function normalize($object, $format = null, array $context = []): array
10895
if ($object instanceof PaginatorInterface) {
10996
$data['hydra:totalItems'] = $object->getTotalItems();
11097
}
98+
11199
if (\is_array($object) || ($object instanceof \Countable && !$object instanceof PartialPaginatorInterface)) {
112100
$data['hydra:totalItems'] = \count($object);
113101
}

0 commit comments

Comments
 (0)