|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Metadata\Tests\Resource\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\ApiResource; |
| 17 | +use ApiPlatform\Metadata\Exception\RuntimeException; |
| 18 | +use ApiPlatform\Metadata\GraphQl\Query; |
| 19 | +use ApiPlatform\Metadata\GraphQl\QueryCollection; |
| 20 | +use ApiPlatform\Metadata\Resource\Factory\PolymorphicGraphQlResourceMetadataCollectionFactory; |
| 21 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 22 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; |
| 25 | +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; |
| 26 | + |
| 27 | +final class PolymorphicGraphQlResourceMetadataCollectionFactoryTest extends TestCase |
| 28 | +{ |
| 29 | + public function testDefaultOperationsAreReplacedByNestedOnes(): void |
| 30 | + { |
| 31 | + $factory = $this->createFactory([ |
| 32 | + 'item_query' => (new Query(name: 'item_query'))->withExtraProperties(['graphql_default_operation' => true]), |
| 33 | + 'collection_query' => (new QueryCollection(name: 'collection_query'))->withExtraProperties(['graphql_default_operation' => true]), |
| 34 | + ], true); |
| 35 | + |
| 36 | + $operations = $factory->create(\stdClass::class)[0]->getGraphQlOperations(); |
| 37 | + |
| 38 | + $this->assertSame(['item_query', 'collection_query'], array_keys($operations)); |
| 39 | + $this->assertInstanceOf(QueryCollection::class, $operations['collection_query']); |
| 40 | + $this->assertInstanceOf(Query::class, $operations['item_query']); |
| 41 | + $this->assertTrue($operations['item_query']->getNested()); |
| 42 | + $this->assertTrue($operations['collection_query']->getNested()); |
| 43 | + $this->assertArrayNotHasKey('graphql_default_operation', $operations['item_query']->getExtraProperties()); |
| 44 | + } |
| 45 | + |
| 46 | + public function testNestedOperationsReceiveTheGlobalDefaults(): void |
| 47 | + { |
| 48 | + $factory = $this->createFactory( |
| 49 | + ['item_query' => (new Query(name: 'item_query'))->withExtraProperties(['graphql_default_operation' => true])], |
| 50 | + true, |
| 51 | + ['normalization_context' => ['skip_null_values' => true]] |
| 52 | + ); |
| 53 | + |
| 54 | + $operations = $factory->create(\stdClass::class)[0]->getGraphQlOperations(); |
| 55 | + |
| 56 | + $this->assertSame(['skip_null_values' => true], $operations['collection_query']->getNormalizationContext()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testExplicitEmptyOperationsAreUntouched(): void |
| 60 | + { |
| 61 | + $operations = ['item_query' => (new Query(name: 'item_query'))->withNested(true)]; |
| 62 | + $factory = $this->createFactory($operations, true); |
| 63 | + |
| 64 | + $this->assertSame($operations, $factory->create(\stdClass::class)[0]->getGraphQlOperations()); |
| 65 | + } |
| 66 | + |
| 67 | + public function testSubtypeResourcesAreDetectedThroughTheirParentMap(): void |
| 68 | + { |
| 69 | + $decorated = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 70 | + $decorated->method('create')->willReturn(new ResourceMetadataCollection(\UnexpectedValueException::class, [(new ApiResource(class: \UnexpectedValueException::class))->withGraphQlOperations(['item_query' => new Query(name: 'item_query')])])); |
| 71 | + |
| 72 | + $resolver = $this->createStub(ClassDiscriminatorResolverInterface::class); |
| 73 | + $resolver->method('getMappingForClass')->willReturnCallback(static fn (string $class): ?ClassDiscriminatorMapping => \RuntimeException::class === $class ? new ClassDiscriminatorMapping('kind', ['iterator' => \UnexpectedValueException::class]) : null); |
| 74 | + |
| 75 | + $this->expectException(RuntimeException::class); |
| 76 | + |
| 77 | + (new PolymorphicGraphQlResourceMetadataCollectionFactory($resolver, $decorated))->create(\UnexpectedValueException::class); |
| 78 | + } |
| 79 | + |
| 80 | + public function testExplicitOperationsThrow(): void |
| 81 | + { |
| 82 | + $factory = $this->createFactory(['item_query' => new Query(name: 'item_query')], true); |
| 83 | + |
| 84 | + $this->expectException(RuntimeException::class); |
| 85 | + $this->expectExceptionMessage(\sprintf('GraphQL is not supported yet for "%s", a resource with a discriminator map', \stdClass::class)); |
| 86 | + |
| 87 | + $factory->create(\stdClass::class); |
| 88 | + } |
| 89 | + |
| 90 | + public function testNonPolymorphicResourcesAreUntouched(): void |
| 91 | + { |
| 92 | + $operations = ['item_query' => new Query(name: 'item_query')]; |
| 93 | + $factory = $this->createFactory($operations, false); |
| 94 | + |
| 95 | + $this->assertSame($operations, $factory->create(\stdClass::class)[0]->getGraphQlOperations()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param array<string, Query> $graphQlOperations |
| 100 | + */ |
| 101 | + private function createFactory(array $graphQlOperations, bool $hasMapping, array $defaults = []): PolymorphicGraphQlResourceMetadataCollectionFactory |
| 102 | + { |
| 103 | + $decorated = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 104 | + $decorated->method('create')->willReturn(new ResourceMetadataCollection(\stdClass::class, [(new ApiResource(class: \stdClass::class))->withGraphQlOperations($graphQlOperations)])); |
| 105 | + |
| 106 | + $resolver = $this->createStub(ClassDiscriminatorResolverInterface::class); |
| 107 | + $resolver->method('getMappingForClass')->willReturn($hasMapping ? new ClassDiscriminatorMapping('kind', ['standard' => \stdClass::class]) : null); |
| 108 | + |
| 109 | + return new PolymorphicGraphQlResourceMetadataCollectionFactory($resolver, $decorated, $defaults); |
| 110 | + } |
| 111 | +} |
0 commit comments