From 634b9721095104e45006fafe37fafe0476c71408 Mon Sep 17 00:00:00 2001 From: Dawid Parafinski Date: Thu, 31 Oct 2024 12:18:32 +0100 Subject: [PATCH] Move from helper to Repository namespace + unit tests --- src/bundle/Core/Resources/config/helpers.yml | 4 - .../ContentService/RelationListFacade.php | 22 +++++ .../ContentService/RelationListFacade.php} | 16 ++- .../Resources/settings/repository/inner.yml | 6 ++ .../ContentService/RelationListFacadeTest.php | 98 +++++++++++++++++++ 5 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 src/contracts/Repository/ContentService/RelationListFacade.php rename src/lib/{Helper/RelationListHelper.php => Repository/ContentService/RelationListFacade.php} (71%) create mode 100644 tests/lib/Repository/ContentService/RelationListFacadeTest.php diff --git a/src/bundle/Core/Resources/config/helpers.yml b/src/bundle/Core/Resources/config/helpers.yml index 9f7d8ce313..1ea501e559 100644 --- a/src/bundle/Core/Resources/config/helpers.yml +++ b/src/bundle/Core/Resources/config/helpers.yml @@ -65,7 +65,3 @@ services: Ibexa\Bundle\Core\SiteAccess\Config\IOConfigResolver: arguments: $complexConfigProcessor: '@Ibexa\Bundle\Core\SiteAccess\Config\ComplexConfigProcessor' - - Ibexa\Core\Helper\RelationListHelper: - arguments: - $contentService: '@ibexa.api.service.content' diff --git a/src/contracts/Repository/ContentService/RelationListFacade.php b/src/contracts/Repository/ContentService/RelationListFacade.php new file mode 100644 index 0000000000..16d8ebda16 --- /dev/null +++ b/src/contracts/Repository/ContentService/RelationListFacade.php @@ -0,0 +1,22 @@ +hasRelation()) { - /** @var \Ibexa\Core\Repository\Values\Content\Relation $relation */ - $relation = $relationListItem->getRelation(); - $relations[] = $relation; + /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Relation $relation */ + yield $relationListItem->getRelation(); } } - - return $relations; } } diff --git a/src/lib/Resources/settings/repository/inner.yml b/src/lib/Resources/settings/repository/inner.yml index d5098b4811..7af5069fee 100644 --- a/src/lib/Resources/settings/repository/inner.yml +++ b/src/lib/Resources/settings/repository/inner.yml @@ -272,3 +272,9 @@ services: Ibexa\Core\Repository\Validator\TargetContentValidatorInterface: alias: Ibexa\Core\Repository\Validator\TargetContentValidator + + Ibexa\Contracts\Core\Repository\ContentService\RelationListFacade: '@Ibexa\Core\Repository\ContentService\RelationListFacade' + + Ibexa\Core\Repository\ContentService\RelationListFacade: + arguments: + $contentService: '@ibexa.api.service.content' diff --git a/tests/lib/Repository/ContentService/RelationListFacadeTest.php b/tests/lib/Repository/ContentService/RelationListFacadeTest.php new file mode 100644 index 0000000000..af8c7400e2 --- /dev/null +++ b/tests/lib/Repository/ContentService/RelationListFacadeTest.php @@ -0,0 +1,98 @@ +contentService = $this->createMock(ContentService::class); + $this->versionInfo = $this->createMock(VersionInfo::class); + $this->relationListFacade = new RelationListFacade($this->contentService); + } + + public function testGetRelationsReturnsEmptyIteratorWhenNoRelations(): void + { + $relationList = $this->createMock(RelationList::class); + $relationList->method('getIterator') + ->willReturn(new \ArrayIterator([])); + + $this->contentService->expects(self::once()) + ->method('loadRelationList') + ->with($this->versionInfo) + ->willReturn($relationList); + + $result = iterator_to_array($this->relationListFacade->getRelations($this->versionInfo)); + + self::assertEmpty($result); + } + + public function testGetRelationsIgnoresItemsWithoutRelations(): void + { + $relationListItem = $this->createMock(RelationListItemInterface::class); + $relationListItem + ->expects(self::any()) + ->method('hasRelation') + ->willReturn(false); + + $relationList = $this->createMock(RelationList::class); + $relationList->method('getIterator') + ->willReturn(new \ArrayIterator([$relationListItem])); + + $this->contentService->expects(self::once()) + ->method('loadRelationList') + ->with($this->versionInfo) + ->willReturn($relationList); + + $result = iterator_to_array($this->relationListFacade->getRelations($this->versionInfo)); + + self::assertEmpty($result); + } + + public function testGetRelationsYieldsRelationsWhenPresent(): void + { + $relation = $this->createMock(Relation::class); + + $relationListItem = $this->createMock(RelationListItemInterface::class); + $relationListItem->expects(self::any()) + ->method('hasRelation') + ->willReturn(true); + $relationListItem->expects(self::any()) + ->method('getRelation') + ->willReturn($relation); + + $relationList = $this->createMock(RelationList::class); + $relationList->method('getIterator') + ->willReturn(new \ArrayIterator([$relationListItem])); + + $this->contentService->expects(self::once()) + ->method('loadRelationList') + ->with($this->versionInfo) + ->willReturn($relationList); + + $result = iterator_to_array($this->relationListFacade->getRelations($this->versionInfo)); + + self::assertCount(1, $result); + self::assertSame($relation, $result[0]); + } +}