From 65e54f0f7ae779d6d00a8ef9c743cf8c9ecc66d9 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 15 Sep 2020 09:34:22 +0200 Subject: [PATCH] Allow overwriting parent metadata resource informations Currently parent resource data is always being selected if set, while child is silently ignored, this changes the approach to allow extending resource change parent data. --- .../AnnotationResourceMetadataFactory.php | 9 +++++++-- .../AnnotationResourceMetadataFactoryTest.php | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Metadata/Resource/Factory/AnnotationResourceMetadataFactory.php b/src/Metadata/Resource/Factory/AnnotationResourceMetadataFactory.php index d941e0e4918..125f68b7602 100644 --- a/src/Metadata/Resource/Factory/AnnotationResourceMetadataFactory.php +++ b/src/Metadata/Resource/Factory/AnnotationResourceMetadataFactory.php @@ -107,8 +107,13 @@ private function createWith(ResourceMetadata $resourceMetadata, string $property $upperProperty = ucfirst($property); $getter = "get$upperProperty"; - if (null !== $resourceMetadata->{$getter}()) { - return $resourceMetadata; + $currentValue = $resourceMetadata->{$getter}(); + if (null !== $currentValue) { + if (null === $value) { + $value = $currentValue; + } elseif (is_array($currentValue)) { + $value = array_merge_recursive($currentValue, $value); + } } $wither = "with$upperProperty"; diff --git a/tests/Metadata/Resource/Factory/AnnotationResourceMetadataFactoryTest.php b/tests/Metadata/Resource/Factory/AnnotationResourceMetadataFactoryTest.php index 4a071077179..2bef85d45f1 100644 --- a/tests/Metadata/Resource/Factory/AnnotationResourceMetadataFactoryTest.php +++ b/tests/Metadata/Resource/Factory/AnnotationResourceMetadataFactoryTest.php @@ -34,7 +34,7 @@ class AnnotationResourceMetadataFactoryTest extends TestCase /** * @dataProvider getCreateDependencies */ - public function testCreate($reader, $decorated, string $expectedShortName, string $expectedDescription) + public function testCreate($reader, $decorated, string $expectedShortName, ?string $expectedDescription) { $factory = new AnnotationResourceMetadataFactory($reader->reveal(), $decorated ? $decorated->reveal() : null); $metadata = $factory->create(Dummy::class); @@ -62,7 +62,7 @@ public function testCreateWithoutAttributes() public function getCreateDependencies() { - $annotation = new ApiResource([ + $resourceData = [ 'shortName' => 'shortName', 'description' => 'description', 'iri' => 'http://example.com', @@ -71,10 +71,11 @@ public function getCreateDependencies() 'subresourceOperations' => ['sub' => ['bus' => false]], 'attributes' => ['a' => 1, 'route_prefix' => '/foobar'], 'graphql' => ['foo' => 'bar'], - ]); + ]; + $annotationFull = new ApiResource($resourceData); $reader = $this->prophesize(Reader::class); - $reader->getClassAnnotation(Argument::type(\ReflectionClass::class), ApiResource::class)->willReturn($annotation)->shouldBeCalled(); + $reader->getClassAnnotation(Argument::type(\ReflectionClass::class), ApiResource::class)->willReturn($annotationFull)->shouldBeCalled(); $decoratedThrow = $this->prophesize(ResourceMetadataFactoryInterface::class); $decoratedThrow->create(Dummy::class)->willThrow(ResourceClassNotFoundException::class); @@ -82,10 +83,20 @@ public function getCreateDependencies() $decoratedReturn = $this->prophesize(ResourceMetadataFactoryInterface::class); $decoratedReturn->create(Dummy::class)->willReturn(new ResourceMetadata('hello', 'blabla'))->shouldBeCalled(); + $resourceData['description'] = null; + $annotationWithNull = new ApiResource($resourceData); + + $decoratedReturnWithNull = $this->prophesize(ResourceMetadataFactoryInterface::class); + $decoratedReturnWithNull->create(Dummy::class)->willReturn(new ResourceMetadata('hello'))->shouldBeCalled(); + + $readerWithNull = $this->prophesize(Reader::class); + $readerWithNull->getClassAnnotation(Argument::type(\ReflectionClass::class), ApiResource::class)->willReturn($annotationWithNull)->shouldBeCalled(); + return [ [$reader, $decoratedThrow, 'shortName', 'description'], [$reader, null, 'shortName', 'description'], [$reader, $decoratedReturn, 'hello', 'blabla'], + [$readerWithNull, $decoratedReturnWithNull, 'hello', null], ]; } }