-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decorate structure resolver to support AuthorInterface (#260)
- Loading branch information
Showing
3 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Content/Infrastructure/Sulu/Structure/DecoratedStructureResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; | ||
use Sulu\Bundle\WebsiteBundle\Resolver\StructureResolverInterface; | ||
use Sulu\Component\Content\Compat\StructureInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class DecoratedStructureResolver implements StructureResolverInterface | ||
{ | ||
public function __construct(private StructureResolverInterface $inner) | ||
{ | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function resolve(StructureInterface $structure, bool $loadExcerpt = true): array | ||
{ | ||
$data = $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
Tests/Unit/Content/Infrastructure/Sulu/Structure/DecoratedStructureResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Infrastructure\Sulu\Structure; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Sulu\Bundle\ContactBundle\Entity\ContactInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure\ContentDocument; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure\ContentStructureBridge; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure\DecoratedStructureResolver; | ||
use Sulu\Bundle\WebsiteBundle\Resolver\StructureResolverInterface; | ||
use Sulu\Component\Content\Compat\StructureInterface; | ||
|
||
class DecoratedStructureResolverTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
/** | ||
* @var ObjectProphecy<StructureResolverInterface> | ||
*/ | ||
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); | ||
} | ||
} |