Skip to content

Commit

Permalink
Move from helper to Repository namespace + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniTou committed Oct 31, 2024
1 parent b9f0825 commit 634b972
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 14 deletions.
4 changes: 0 additions & 4 deletions src/bundle/Core/Resources/config/helpers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
22 changes: 22 additions & 0 deletions src/contracts/Repository/ContentService/RelationListFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\ContentService;

use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;

/**
* @internal
*/
interface RelationListFacade
{
/**
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Relation[]
*/
public function getRelations(VersionInfo $versionInfo): iterable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
*/
declare(strict_types=1);

namespace Ibexa\Core\Helper;
namespace Ibexa\Core\Repository\ContentService;

use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Iterator\BatchIterator;
use Ibexa\Contracts\Core\Repository\Iterator\BatchIteratorAdapter\RelationListIteratorAdapter;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;

final class RelationListHelper
final class RelationListFacade implements ContentService\RelationListFacade
{
public function __construct(
private readonly ContentService $contentService
) {
}

/**
* @return \Ibexa\Core\Repository\Values\Content\Relation[]
* @return \Ibexa\Contracts\Core\Repository\Values\Content\Relation[]
*/
public function getRelations(VersionInfo $versionInfo): array
public function getRelations(VersionInfo $versionInfo): iterable
{
$relationListIterator = new BatchIterator(
new RelationListIteratorAdapter(
Expand All @@ -32,16 +32,12 @@ public function getRelations(VersionInfo $versionInfo): array
)
);

$relations = [];
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\RelationList\RelationListItemInterface $relationListItem */
foreach ($relationListIterator as $relationListItem) {
if ($relationListItem->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();

Check failure on line 39 in src/lib/Repository/ContentService/RelationListFacade.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.3)

Generator expects value type Ibexa\Contracts\Core\Repository\Values\Content\Relation, Ibexa\Contracts\Core\Repository\Values\Content\Relation|null given.

Check failure on line 39 in src/lib/Repository/ContentService/RelationListFacade.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.3)

Variable $relation in PHPDoc tag @var does not exist.
}
}

return $relations;
}
}
6 changes: 6 additions & 0 deletions src/lib/Resources/settings/repository/inner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
98 changes: 98 additions & 0 deletions tests/lib/Repository/ContentService/RelationListFacadeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\Repository\ContentService;

use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Values\Content\Relation;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList;
use Ibexa\Contracts\Core\Repository\Values\Content\RelationList\RelationListItemInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
use Ibexa\Core\Repository\ContentService\RelationListFacade;
use PHPUnit\Framework\TestCase;

final class RelationListFacadeTest extends TestCase
{
private ContentService $contentService;

private RelationListFacade $relationListFacade;

private VersionInfo $versionInfo;

protected function setUp(): void
{
$this->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())

Check failure on line 40 in tests/lib/Repository/ContentService/RelationListFacadeTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.3)

Call to an undefined method Ibexa\Contracts\Core\Repository\ContentService::expects().
->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())

Check failure on line 62 in tests/lib/Repository/ContentService/RelationListFacadeTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.3)

Call to an undefined method Ibexa\Contracts\Core\Repository\ContentService::expects().
->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())

Check failure on line 88 in tests/lib/Repository/ContentService/RelationListFacadeTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.3)

Call to an undefined method Ibexa\Contracts\Core\Repository\ContentService::expects().
->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]);
}
}

0 comments on commit 634b972

Please sign in to comment.