Skip to content

Commit

Permalink
test: OpenApi (api-platform#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deuchnord authored and alanpoulain committed May 30, 2022
1 parent 817da50 commit 628ffb9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
14 changes: 5 additions & 9 deletions src/JsonApi/Serializer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace ApiPlatform\JsonApi\Serializer;

use ApiPlatform\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Serializer\AbstractCollectionNormalizer;
use ApiPlatform\Util\IriHelper;
Expand All @@ -31,7 +31,7 @@ final class CollectionNormalizer extends AbstractCollectionNormalizer
{
public const FORMAT = 'jsonapi';

public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, $resourceMetadataFactory)
public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName, ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
{
parent::__construct($resourceClassResolver, $pageParameterName, $resourceMetadataFactory);
}
Expand All @@ -44,14 +44,10 @@ protected function getPaginationData($object, array $context = []): array
[$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems] = $this->getPaginationConfig($object, $context);
$parsed = IriHelper::parseIri($context['uri'] ?? '/', $this->pageParameterName);

/** @var ResourceMetadata|ResourceMetadataCollection */
/** @var ResourceMetadataCollection */
$metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? '');
if ($metadata instanceof ResourceMetadataCollection) {
$operation = $metadata->getOperation($context['operation_name'] ?? null);
$urlGenerationStrategy = $operation->getUrlGenerationStrategy();
} else {
$urlGenerationStrategy = $metadata->getAttribute('url_generation_strategy');
}
$operation = $metadata->getOperation($context['operation_name'] ?? null);
$urlGenerationStrategy = $operation->getUrlGenerationStrategy();

$data = [
'links' => [
Expand Down
59 changes: 46 additions & 13 deletions tests/JsonApi/Serializer/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
namespace ApiPlatform\Tests\JsonApi\Serializer;

use ApiPlatform\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\JsonApi\Serializer\CollectionNormalizer;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\State\Pagination\PaginatorInterface;
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
use ApiPlatform\Tests\ProphecyTrait;
Expand All @@ -35,7 +38,7 @@ class CollectionNormalizerTest extends TestCase
public function testSupportsNormalize()
{
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);

$normalizer = new CollectionNormalizer($resourceClassResolverProphecy->reveal(), 'page', $resourceMetadataFactoryProphecy->reveal());

Expand Down Expand Up @@ -63,12 +66,17 @@ public function testNormalizePaginator()
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($paginator, 'Foo')->willReturn('Foo');

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata());
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadataCollection('Foo', [
(new ApiResource())
->withShortName('Foo')
->withOperations(new Operations(['get' => (new GetCollection())]))
]));

$itemNormalizer = $this->prophesize(NormalizerInterface::class);
$itemNormalizer->normalize('foo', CollectionNormalizer::FORMAT, [
'request_uri' => '/foos?page=3',
'operation_name' => 'get',
'uri' => 'http://example.com/foos?page=3',
'api_sub_level' => true,
'resource_class' => 'Foo',
Expand Down Expand Up @@ -113,6 +121,7 @@ public function testNormalizePaginator()

$this->assertEquals($expected, $normalizer->normalize($paginator, CollectionNormalizer::FORMAT, [
'request_uri' => '/foos?page=3',
'operation_name' => 'get',
'uri' => 'http://example.com/foos?page=3',
'resource_class' => 'Foo',
]));
Expand All @@ -134,12 +143,17 @@ public function testNormalizePartialPaginator()
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($paginator, 'Foo')->willReturn('Foo');

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata());
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadataCollection('Foo', [
(new ApiResource())
->withShortName('Foo')
->withOperations(new Operations(['get' => (new GetCollection())]))
]));

$itemNormalizer = $this->prophesize(NormalizerInterface::class);
$itemNormalizer->normalize('foo', CollectionNormalizer::FORMAT, [
'request_uri' => '/foos?page=3',
'operation_name' => 'get',
'uri' => 'http://example.com/foos?page=3',
'api_sub_level' => true,
'resource_class' => 'Foo',
Expand Down Expand Up @@ -181,6 +195,7 @@ public function testNormalizePartialPaginator()

$this->assertEquals($expected, $normalizer->normalize($paginator, CollectionNormalizer::FORMAT, [
'request_uri' => '/foos?page=3',
'operation_name' => 'get',
'uri' => 'http://example.com/foos?page=3',
'resource_class' => 'Foo',
]));
Expand All @@ -192,11 +207,16 @@ public function testNormalizeArray()

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($data, 'Foo')->willReturn('Foo');
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata());
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadataCollection('Foo', [
(new ApiResource())
->withShortName('Foo')
->withOperations(new Operations(['get' => (new GetCollection())]))
]));
$itemNormalizer = $this->prophesize(NormalizerInterface::class);
$itemNormalizer->normalize('foo', CollectionNormalizer::FORMAT, [
'request_uri' => '/foos',
'operation_name' => 'get',
'uri' => 'http://example.com/foos',
'api_sub_level' => true,
'resource_class' => 'Foo',
Expand Down Expand Up @@ -231,6 +251,7 @@ public function testNormalizeArray()

$this->assertEquals($expected, $normalizer->normalize($data, CollectionNormalizer::FORMAT, [
'request_uri' => '/foos',
'operation_name' => 'get',
'uri' => 'http://example.com/foos',
'resource_class' => 'Foo',
]));
Expand All @@ -243,12 +264,17 @@ public function testNormalizeIncludedData()
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($data, 'Foo')->willReturn('Foo');

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata());
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadataCollection('Foo', [
(new ApiResource())
->withShortName('Foo')
->withOperations(new Operations(['get' => (new GetCollection())]))
]));

$itemNormalizer = $this->prophesize(NormalizerInterface::class);
$itemNormalizer->normalize('foo', CollectionNormalizer::FORMAT, [
'request_uri' => '/foos',
'operation_name' => 'get',
'uri' => 'http://example.com/foos',
'api_sub_level' => true,
'resource_class' => 'Foo',
Expand Down Expand Up @@ -303,6 +329,7 @@ public function testNormalizeIncludedData()

$this->assertEquals($expected, $normalizer->normalize($data, CollectionNormalizer::FORMAT, [
'request_uri' => '/foos',
'operation_name' => 'get',
'uri' => 'http://example.com/foos',
'resource_class' => 'Foo',
]));
Expand All @@ -318,12 +345,17 @@ public function testNormalizeWithoutDataKey()
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($data, 'Foo')->willReturn('Foo');

$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadata());
$resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataFactoryProphecy->create('Foo')->willReturn(new ResourceMetadataCollection('Foo', [
(new ApiResource())
->withShortName('Foo')
->withOperations(new Operations(['get' => (new GetCollection())]))
]));

$itemNormalizer = $this->prophesize(NormalizerInterface::class);
$itemNormalizer->normalize('foo', CollectionNormalizer::FORMAT, [
'request_uri' => '/foos',
'operation_name' => 'get',
'uri' => 'http://example.com/foos',
'api_sub_level' => true,
'resource_class' => 'Foo',
Expand All @@ -334,6 +366,7 @@ public function testNormalizeWithoutDataKey()

$normalizer->normalize($data, CollectionNormalizer::FORMAT, [
'request_uri' => '/foos',
'operation_name' => 'get',
'uri' => 'http://example.com/foos',
'resource_class' => 'Foo',
]);
Expand Down

0 comments on commit 628ffb9

Please sign in to comment.