|
13 | 13 |
|
14 | 14 | namespace ApiPlatform\Core\Tests\JsonSchema;
|
15 | 15 |
|
| 16 | +use ApiPlatform\Core\Api\OperationType; |
16 | 17 | use ApiPlatform\Core\Api\ResourceClassResolverInterface;
|
| 18 | +use ApiPlatform\Core\JsonSchema\Schema; |
17 | 19 | use ApiPlatform\Core\JsonSchema\SchemaFactory;
|
18 | 20 | use ApiPlatform\Core\JsonSchema\TypeFactoryInterface;
|
19 | 21 | use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
|
20 | 22 | use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
|
21 | 23 | use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
|
22 | 24 | use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
|
23 | 25 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
|
| 26 | +use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
24 | 27 | use ApiPlatform\Core\Tests\Fixtures\NotAResource;
|
| 28 | +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\OverriddenOperationDummy; |
25 | 29 | use PHPUnit\Framework\TestCase;
|
26 | 30 | use Prophecy\Argument;
|
27 | 31 | use Symfony\Component\PropertyInfo\Type;
|
@@ -74,4 +78,67 @@ public function testBuildSchemaForNonResourceClass(): void
|
74 | 78 | $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['bar']);
|
75 | 79 | $this->assertSame('integer', $definitions[$rootDefinitionKey]['properties']['bar']['type']);
|
76 | 80 | }
|
| 81 | + |
| 82 | + public function testBuildSchemaForOperationWithOverriddenSerializerGroups(): void |
| 83 | + { |
| 84 | + $typeFactoryProphecy = $this->prophesize(TypeFactoryInterface::class); |
| 85 | + $typeFactoryProphecy->getType(Argument::allOf( |
| 86 | + Argument::type(Type::class), |
| 87 | + Argument::which('getBuiltinType', Type::BUILTIN_TYPE_STRING) |
| 88 | + ), Argument::cetera())->willReturn([ |
| 89 | + 'type' => 'string', |
| 90 | + ]); |
| 91 | + |
| 92 | + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); |
| 93 | + $resourceMetadataFactoryProphecy->create(OverriddenOperationDummy::class)->willReturn(new ResourceMetadata((new \ReflectionClass(OverriddenOperationDummy::class))->getShortName(), null, null, [ |
| 94 | + 'put' => [ |
| 95 | + 'normalization_context' => [ |
| 96 | + 'groups' => 'overridden_operation_dummy_put', |
| 97 | + ], |
| 98 | + ], |
| 99 | + ], [], [ |
| 100 | + 'normalization_context' => [ |
| 101 | + 'groups' => 'overridden_operation_dummy_read', |
| 102 | + ], |
| 103 | + ])); |
| 104 | + |
| 105 | + $serializerGroup = 'overridden_operation_dummy_put'; |
| 106 | + |
| 107 | + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); |
| 108 | + $propertyNameCollectionFactoryProphecy->create(OverriddenOperationDummy::class, Argument::allOf( |
| 109 | + Argument::type('array'), |
| 110 | + Argument::withEntry('serializer_groups', [$serializerGroup]) |
| 111 | + ))->willReturn(new PropertyNameCollection(['alias', 'description'])); |
| 112 | + |
| 113 | + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); |
| 114 | + $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'alias', Argument::allOf( |
| 115 | + Argument::type('array'), |
| 116 | + Argument::withEntry('serializer_groups', [$serializerGroup]) |
| 117 | + ))->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), null, true)); |
| 118 | + $propertyMetadataFactoryProphecy->create(OverriddenOperationDummy::class, 'description', Argument::allOf( |
| 119 | + Argument::type('array'), |
| 120 | + Argument::withEntry('serializer_groups', [$serializerGroup]) |
| 121 | + ))->willReturn(new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING), null, true)); |
| 122 | + |
| 123 | + $resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); |
| 124 | + $resourceClassResolverProphecy->isResourceClass(OverriddenOperationDummy::class)->willReturn(true); |
| 125 | + |
| 126 | + $schemaFactory = new SchemaFactory($typeFactoryProphecy->reveal(), $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal()); |
| 127 | + $resultSchema = $schemaFactory->buildSchema(OverriddenOperationDummy::class, 'json', Schema::TYPE_OUTPUT, OperationType::ITEM, 'put'); |
| 128 | + |
| 129 | + $rootDefinitionKey = $resultSchema->getRootDefinitionKey(); |
| 130 | + $definitions = $resultSchema->getDefinitions(); |
| 131 | + |
| 132 | + $this->assertSame((new \ReflectionClass(OverriddenOperationDummy::class))->getShortName().'-'.$serializerGroup, $rootDefinitionKey); |
| 133 | + $this->assertArrayHasKey($rootDefinitionKey, $definitions); |
| 134 | + $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]); |
| 135 | + $this->assertSame('object', $definitions[$rootDefinitionKey]['type']); |
| 136 | + $this->assertArrayHasKey('properties', $definitions[$rootDefinitionKey]); |
| 137 | + $this->assertArrayHasKey('alias', $definitions[$rootDefinitionKey]['properties']); |
| 138 | + $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['alias']); |
| 139 | + $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['alias']['type']); |
| 140 | + $this->assertArrayHasKey('description', $definitions[$rootDefinitionKey]['properties']); |
| 141 | + $this->assertArrayHasKey('type', $definitions[$rootDefinitionKey]['properties']['description']); |
| 142 | + $this->assertSame('string', $definitions[$rootDefinitionKey]['properties']['description']['type']); |
| 143 | + } |
77 | 144 | }
|
0 commit comments