|
| 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\JsonApi\Tests\JsonSchema; |
| 15 | + |
| 16 | +use ApiPlatform\JsonApi\JsonSchema\SchemaFactory; |
| 17 | +use ApiPlatform\JsonApi\Tests\Fixtures\Order; |
| 18 | +use ApiPlatform\JsonApi\Tests\Fixtures\OrderInput; |
| 19 | +use ApiPlatform\JsonApi\Tests\Fixtures\OrderLineInput; |
| 20 | +use ApiPlatform\JsonApi\Tests\Fixtures\RelatedDummy; |
| 21 | +use ApiPlatform\JsonSchema\DefinitionNameFactory; |
| 22 | +use ApiPlatform\JsonSchema\Schema; |
| 23 | +use ApiPlatform\JsonSchema\SchemaFactory as BaseSchemaFactory; |
| 24 | +use ApiPlatform\Metadata\ApiProperty; |
| 25 | +use ApiPlatform\Metadata\ApiResource; |
| 26 | +use ApiPlatform\Metadata\Get; |
| 27 | +use ApiPlatform\Metadata\Operations; |
| 28 | +use ApiPlatform\Metadata\Post; |
| 29 | +use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
| 30 | +use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
| 31 | +use ApiPlatform\Metadata\Property\PropertyNameCollection; |
| 32 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 33 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 34 | +use ApiPlatform\Metadata\ResourceClassResolverInterface; |
| 35 | +use PHPUnit\Framework\TestCase; |
| 36 | +use Symfony\Component\TypeInfo\Type; |
| 37 | + |
| 38 | +class InputDtoSchemaFactoryTest extends TestCase |
| 39 | +{ |
| 40 | + public function testInputSchemaDocumentsEmbeddedDtoCollectionAsAttribute(): void |
| 41 | + { |
| 42 | + $operation = (new Post(input: ['class' => OrderInput::class]))->withName('post')->withShortName('Order'); |
| 43 | + $schemaFactory = $this->buildSchemaFactory($operation); |
| 44 | + |
| 45 | + $schema = $schemaFactory->buildSchema(Order::class, 'jsonapi', Schema::TYPE_INPUT, $operation); |
| 46 | + $data = $schema->getDefinitions()[$schema->getRootDefinitionKey()]['properties']['data']; |
| 47 | + |
| 48 | + $this->assertArrayNotHasKey('relationships', $data['properties']); |
| 49 | + $this->assertEqualsCanonicalizing(['reference', 'lines'], array_keys($data['properties']['attributes']['properties'])); |
| 50 | + } |
| 51 | + |
| 52 | + public function testOutputSchemaKeepsResourceCollectionAsRelationship(): void |
| 53 | + { |
| 54 | + $operation = (new Get())->withName('get')->withShortName('Order'); |
| 55 | + $schemaFactory = $this->buildSchemaFactory($operation); |
| 56 | + |
| 57 | + $schema = $schemaFactory->buildSchema(Order::class, 'jsonapi', Schema::TYPE_OUTPUT, $operation); |
| 58 | + $data = $schema->getDefinitions()[$schema->getRootDefinitionKey()]['properties']['data']; |
| 59 | + |
| 60 | + $this->assertArrayHasKey('lines', $data['properties']['relationships']['properties']); |
| 61 | + $this->assertArrayNotHasKey('lines', $data['properties']['attributes']['properties']); |
| 62 | + } |
| 63 | + |
| 64 | + private function buildSchemaFactory(Get|Post $operation): SchemaFactory |
| 65 | + { |
| 66 | + $resourceMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 67 | + $resourceMetadataFactory->method('create')->willReturnCallback(static fn (string $class): ResourceMetadataCollection => new ResourceMetadataCollection($class, [ |
| 68 | + (new ApiResource())->withOperations(new Operations(Order::class === $class |
| 69 | + ? [$operation->getName() => $operation] |
| 70 | + : ['get' => (new Get())->withName('get')->withShortName('RelatedDummy')] |
| 71 | + )), |
| 72 | + ])); |
| 73 | + |
| 74 | + $propertyNameCollectionFactory = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 75 | + $propertyNameCollectionFactory->method('create')->willReturnCallback(static fn (string $class): PropertyNameCollection => new PropertyNameCollection(match ($class) { |
| 76 | + Order::class, OrderInput::class => ['reference', 'lines'], |
| 77 | + OrderLineInput::class => ['sku'], |
| 78 | + default => [], |
| 79 | + })); |
| 80 | + |
| 81 | + $types = [ |
| 82 | + Order::class.'::reference' => Type::string(), |
| 83 | + Order::class.'::lines' => Type::list(Type::object(RelatedDummy::class)), |
| 84 | + OrderInput::class.'::reference' => Type::string(), |
| 85 | + OrderInput::class.'::lines' => Type::list(Type::object(OrderLineInput::class)), |
| 86 | + OrderLineInput::class.'::sku' => Type::string(), |
| 87 | + ]; |
| 88 | + $propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 89 | + $propertyMetadataFactory->method('create')->willReturnCallback(static fn (string $class, string $property): ApiProperty => (new ApiProperty())->withNativeType($types[$class.'::'.$property])->withReadable(true)->withWritable(true)); |
| 90 | + |
| 91 | + $resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class); |
| 92 | + $resourceClassResolver->method('isResourceClass')->willReturnCallback(static fn (string $class): bool => \in_array($class, [Order::class, RelatedDummy::class], true)); |
| 93 | + |
| 94 | + $definitionNameFactory = new DefinitionNameFactory(null); |
| 95 | + |
| 96 | + $baseSchemaFactory = new BaseSchemaFactory( |
| 97 | + resourceMetadataFactory: $resourceMetadataFactory, |
| 98 | + propertyNameCollectionFactory: $propertyNameCollectionFactory, |
| 99 | + propertyMetadataFactory: $propertyMetadataFactory, |
| 100 | + resourceClassResolver: $resourceClassResolver, |
| 101 | + definitionNameFactory: $definitionNameFactory, |
| 102 | + ); |
| 103 | + |
| 104 | + return new SchemaFactory( |
| 105 | + schemaFactory: $baseSchemaFactory, |
| 106 | + propertyMetadataFactory: $propertyMetadataFactory, |
| 107 | + resourceClassResolver: $resourceClassResolver, |
| 108 | + resourceMetadataFactory: $resourceMetadataFactory, |
| 109 | + definitionNameFactory: $definitionNameFactory, |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments