From 535559fbbe9d3d2c4c63d52e0739873e64226ca1 Mon Sep 17 00:00:00 2001 From: Yosh de Vos Date: Mon, 1 Feb 2021 15:16:52 +0100 Subject: [PATCH 1/2] Allow to set openapi_context on subresourceOperations --- .../Factory/SubresourceOperationFactory.php | 4 ++ .../SubresourceOperationFactoryTest.php | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/Operation/Factory/SubresourceOperationFactory.php b/src/Operation/Factory/SubresourceOperationFactory.php index e5a19bedeaf..73e14471d22 100644 --- a/src/Operation/Factory/SubresourceOperationFactory.php +++ b/src/Operation/Factory/SubresourceOperationFactory.php @@ -184,6 +184,10 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre } } + if (isset($subresourceOperation['openapi_context'])) { + $operation['openapi_context'] = $subresourceOperation['openapi_context']; + } + foreach (self::ROUTE_OPTIONS as $routeOption => $defaultValue) { $operation[$routeOption] = $subresourceOperation[$routeOption] ?? $defaultValue; } diff --git a/tests/Operation/Factory/SubresourceOperationFactoryTest.php b/tests/Operation/Factory/SubresourceOperationFactoryTest.php index 59e644a14d1..89a0bee4960 100644 --- a/tests/Operation/Factory/SubresourceOperationFactoryTest.php +++ b/tests/Operation/Factory/SubresourceOperationFactoryTest.php @@ -837,4 +837,61 @@ public function testCreateSelfReferencingSubresourcesWithSubresources() ] + SubresourceOperationFactory::ROUTE_OPTIONS, ], $subresourceOperationFactory->create(DummyEntity::class)); } + + public function testCreateWithOpenapiContext() + { + $resourceMetadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); + $resourceMetadataFactoryProphecy->create(RelatedDummyEntity::class)->shouldBeCalled()->willReturn(new ResourceMetadata('relatedDummyEntity')); + $resourceMetadataFactoryProphecy->create(DummyEntity::class)->shouldBeCalled()->willReturn((new ResourceMetadata('dummyEntity'))->withSubresourceOperations([ + 'subresource_get_subresource' => [ + 'openapi_context' => [ + 'summary' => 'Get related dummy entities', + 'tags' => ['Dummy'], + ] + ] + ])); + + $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); + $propertyNameCollectionFactoryProphecy->create(DummyEntity::class)->shouldBeCalled()->willReturn(new PropertyNameCollection(['subresource'])); + $propertyNameCollectionFactoryProphecy->create(RelatedDummyEntity::class)->shouldBeCalled()->willReturn(new PropertyNameCollection([])); + + $subresourceMetadata = (new PropertyMetadata())->withSubresource(new SubresourceMetadata(RelatedDummyEntity::class, false)); + + $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class); + $propertyMetadataFactoryProphecy->create(DummyEntity::class, 'subresource')->shouldBeCalled()->willReturn($subresourceMetadata); + + $pathSegmentNameGeneratorProphecy = $this->prophesize(PathSegmentNameGeneratorInterface::class); + $pathSegmentNameGeneratorProphecy->getSegmentName('dummyEntity')->shouldBeCalled()->willReturn('dummy_entities'); + $pathSegmentNameGeneratorProphecy->getSegmentName('subresource', false)->shouldBeCalled()->willReturn('subresources'); + + $identifiersExtractorProphecy = $this->prophesize(IdentifiersExtractorInterface::class); + $identifiersExtractorProphecy->getIdentifiersFromResourceClass(Argument::type('string'))->willReturn(['id']); + + $subresourceOperationFactory = new SubresourceOperationFactory( + $resourceMetadataFactoryProphecy->reveal(), + $propertyNameCollectionFactoryProphecy->reveal(), + $propertyMetadataFactoryProphecy->reveal(), + $pathSegmentNameGeneratorProphecy->reveal(), + $identifiersExtractorProphecy->reveal() + ); + + $this->assertEquals([ + 'api_dummy_entities_subresource_get_subresource' => [ + 'property' => 'subresource', + 'collection' => false, + 'resource_class' => RelatedDummyEntity::class, + 'shortNames' => ['relatedDummyEntity', 'dummyEntity'], + 'identifiers' => [ + 'id' => [DummyEntity::class, 'id', true], + ], + 'route_name' => 'api_dummy_entities_subresource_get_subresource', + 'path' => '/dummy_entities/{id}/subresources.{_format}', + 'operation_name' => 'subresource_get_subresource', + 'openapi_context' => [ + 'summary' => 'Get related dummy entities', + 'tags' => ['Dummy'], + ] + ] + SubresourceOperationFactory::ROUTE_OPTIONS, + ], $subresourceOperationFactory->create(DummyEntity::class)); + } } From 6ba1c1df0e83c12b2b6e7edccbf90a8780541a0e Mon Sep 17 00:00:00 2001 From: Yosh de Vos Date: Mon, 1 Feb 2021 19:20:11 +0100 Subject: [PATCH 2/2] phpcs fixes --- tests/Operation/Factory/SubresourceOperationFactoryTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Operation/Factory/SubresourceOperationFactoryTest.php b/tests/Operation/Factory/SubresourceOperationFactoryTest.php index 89a0bee4960..c9ea4b2dc84 100644 --- a/tests/Operation/Factory/SubresourceOperationFactoryTest.php +++ b/tests/Operation/Factory/SubresourceOperationFactoryTest.php @@ -847,8 +847,8 @@ public function testCreateWithOpenapiContext() 'openapi_context' => [ 'summary' => 'Get related dummy entities', 'tags' => ['Dummy'], - ] - ] + ], + ], ])); $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class); @@ -890,7 +890,7 @@ public function testCreateWithOpenapiContext() 'openapi_context' => [ 'summary' => 'Get related dummy entities', 'tags' => ['Dummy'], - ] + ], ] + SubresourceOperationFactory::ROUTE_OPTIONS, ], $subresourceOperationFactory->create(DummyEntity::class)); }