-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IBX-8534: Improved iteration over relation list #444
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13bd157
IBX-8534: Added RelationListIteratorAdapter
ViniTou b9f0825
IBX-8534: Added relationList helper
ViniTou 634b972
Move from helper to Repository namespace + unit tests
ViniTou b16abf8
Move from helper to Repository namespace + unit tests
ViniTou 95a8a93
Included code review remakrs
ViniTou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/contracts/Repository/ContentService/RelationListFacadeInterface.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,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 RelationListFacadeInterface | ||
{ | ||
/** | ||
* @return iterable<\Ibexa\Contracts\Core\Repository\Values\Content\Relation> | ||
*/ | ||
public function getRelations(VersionInfo $versionInfo): iterable; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/contracts/Repository/Iterator/BatchIteratorAdapter/RelationListIteratorAdapter.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,42 @@ | ||
<?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\Iterator\BatchIteratorAdapter; | ||
|
||
use Ibexa\Contracts\Core\Repository\ContentService; | ||
use Ibexa\Contracts\Core\Repository\Iterator\BatchIteratorAdapter; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\RelationType; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; | ||
use Iterator; | ||
use IteratorIterator; | ||
|
||
final class RelationListIteratorAdapter implements BatchIteratorAdapter | ||
{ | ||
public function __construct( | ||
readonly private ContentService $contentService, | ||
readonly private VersionInfo $versionInfo, | ||
readonly private ?RelationType $relationType = null, | ||
) { | ||
} | ||
|
||
public function fetch(int $offset, int $limit): Iterator | ||
{ | ||
$iterator = $this->contentService->loadRelationList( | ||
$this->versionInfo, | ||
$offset, | ||
$limit, | ||
$this->relationType | ||
)->getIterator(); | ||
|
||
if ($iterator instanceof Iterator) { | ||
return $iterator; | ||
} | ||
|
||
return new IteratorIterator($iterator); | ||
} | ||
} |
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
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,39 @@ | ||
<?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\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 RelationListFacade implements ContentService\RelationListFacadeInterface | ||
{ | ||
public function __construct( | ||
private readonly ContentService $contentService | ||
) { | ||
} | ||
|
||
public function getRelations(VersionInfo $versionInfo): iterable | ||
{ | ||
$relationListIterator = new BatchIterator( | ||
new RelationListIteratorAdapter( | ||
$this->contentService, | ||
$versionInfo | ||
) | ||
); | ||
|
||
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\RelationList\RelationListItemInterface $relationListItem */ | ||
foreach ($relationListIterator as $relationListItem) { | ||
if ($relationListItem->hasRelation()) { | ||
yield $relationListItem->getRelation(); | ||
} | ||
} | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
tests/lib/Repository/ContentService/RelationListFacadeTest.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,101 @@ | ||
<?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\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RelationListFacadeTest extends TestCase | ||
{ | ||
private ContentService&MockObject $contentService; | ||
|
||
private RelationListFacade $relationListFacade; | ||
|
||
private VersionInfo&MockObject $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()) | ||
->method('loadRelationList') | ||
->with($this->versionInfo) | ||
ViniTou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->willReturn($relationList); | ||
|
||
$result = iterator_to_array($this->relationListFacade->getRelations($this->versionInfo)); | ||
|
||
self::assertEmpty($result); | ||
} | ||
|
||
public function testGetRelationsIgnoresItemsWithoutRelations(): void | ||
{ | ||
$relationListItem = $this->createMock(RelationListItemInterface::class); | ||
$relationListItem | ||
->method('hasRelation') | ||
->willReturn(false); | ||
|
||
$relationList = $this->createMock(RelationList::class); | ||
$relationList->method('getIterator') | ||
->willReturn(new \ArrayIterator([$relationListItem])); | ||
|
||
$this->contentService | ||
->expects(self::once()) | ||
->method('loadRelationList') | ||
->with(self::identicalTo($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 | ||
->method('hasRelation') | ||
->willReturn(true); | ||
$relationListItem | ||
->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]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we've probably made a mistake by not marking this
BatchIteratorAdapter::fetch
as returningTraversable
(eitherIterator
andIteratorAgregate
)@adamwojs & everyone - can we consider expanding return type in 5.x?