From df5b2c75176f68284eaba3f7eff9fa97a179c9c3 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Mon, 22 Jul 2024 10:08:20 +0200 Subject: [PATCH] Decorate structure resolver to support AuthorInterface (#260) --- .../Structure/DecoratedStructureResolver.php | 51 ++++++ Resources/config/services.xml | 5 + .../DecoratedStructureResolverTest.php | 148 ++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 Content/Infrastructure/Sulu/Structure/DecoratedStructureResolver.php create mode 100644 Tests/Unit/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolverTest.php diff --git a/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolver.php b/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolver.php new file mode 100644 index 00000000..4849f1dd --- /dev/null +++ b/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolver.php @@ -0,0 +1,51 @@ +inner->resolve($structure, $loadExcerpt); + + if (!$structure instanceof ContentStructureBridge) { + return $data; + } + + /** @var ContentDocument $document */ + $document = $structure->getDocument(); + $content = $document->getContent(); + + if ($content instanceof AuthorInterface) { + $data['authored'] = $content->getAuthored(); + $data['author'] = $content->getAuthor()?->getId(); + } + + return $data; + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 44a8235e..024b2505 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -191,5 +191,10 @@ + + + + + diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolverTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolverTest.php new file mode 100644 index 00000000..b9ac69d4 --- /dev/null +++ b/Tests/Unit/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolverTest.php @@ -0,0 +1,148 @@ + + */ + private $innerResolver; + + /** + * @var DecoratedStructureResolver + */ + private $decoratedResolver; + + protected function setUp(): void + { + $this->innerResolver = $this->prophesize(StructureResolverInterface::class); + $this->decoratedResolver = new DecoratedStructureResolver($this->innerResolver->reveal()); + } + + public function testResolveWithNonContentStructureBridge(): void + { + $structure = $this->prophesize(StructureInterface::class); + $expectedData = ['key' => 'value']; + + $this->innerResolver->resolve($structure->reveal(), true) + ->willReturn($expectedData); + + $result = $this->decoratedResolver->resolve($structure->reveal()); + + $this->assertSame($expectedData, $result); + } + + public function testResolveWithContentStructureBridgeNonAuthorInterface(): void + { + $structure = $this->prophesize(ContentStructureBridge::class); + $document = $this->prophesize(ContentDocument::class); + $content = $this->prophesize(TemplateInterface::class); + + $structure->getDocument()->willReturn($document->reveal()); + $document->getContent()->willReturn($content); + + $expectedData = ['key' => 'value']; + $this->innerResolver->resolve($structure->reveal(), true) + ->willReturn($expectedData); + + $result = $this->decoratedResolver->resolve($structure->reveal()); + + $this->assertSame($expectedData, $result); + } + + public function testResolveWithContentStructureBridgeAndAuthorInterface(): void + { + $structure = $this->prophesize(ContentStructureBridge::class); + $document = $this->prophesize(ContentDocument::class); + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(AuthorInterface::class); + $author = $this->prophesize(ContactInterface::class); + + $structure->getDocument()->willReturn($document->reveal()); + $document->getContent()->willReturn($content->reveal()); + + $expectedData = ['key' => 'value']; + $this->innerResolver->resolve($structure->reveal(), true) + ->willReturn($expectedData); + + $authDate = new \DateTimeImmutable(); + $content->getAuthored()->willReturn($authDate); + $content->getAuthor()->willReturn($author->reveal()); + $author->getId()->willReturn(123); + + $result = $this->decoratedResolver->resolve($structure->reveal()); + + $expectedResult = \array_merge($expectedData, [ + 'authored' => $authDate, + 'author' => 123, + ]); + + $this->assertSame($expectedResult, $result); + } + + public function testResolveWithContentStructureBridgeAndAuthorInterfaceNoAuthor(): void + { + $structure = $this->prophesize(ContentStructureBridge::class); + $document = $this->prophesize(ContentDocument::class); + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(AuthorInterface::class); + + $structure->getDocument()->willReturn($document->reveal()); + $document->getContent()->willReturn($content->reveal()); + + $expectedData = ['key' => 'value']; + $this->innerResolver->resolve($structure->reveal(), true) + ->willReturn($expectedData); + + $content->getAuthored()->willReturn(null); + $content->getAuthor()->willReturn(null); + + $result = $this->decoratedResolver->resolve($structure->reveal()); + + $expectedResult = \array_merge($expectedData, [ + 'authored' => null, + 'author' => null, + ]); + + $this->assertSame($expectedResult, $result); + } + + public function testResolveWithExcerptFlag(): void + { + $structure = $this->prophesize(StructureInterface::class); + $expectedData = ['key' => 'value']; + + $this->innerResolver->resolve($structure->reveal(), false) + ->willReturn($expectedData); + + $result = $this->decoratedResolver->resolve($structure->reveal(), false); + + $this->assertSame($expectedData, $result); + } +}