Skip to content

Commit

Permalink
IBX-8019: Fixed TrashEventSubscriberTest to actually test EventNotifi…
Browse files Browse the repository at this point in the history
…cationService::sendNotification calls
  • Loading branch information
webhdx committed Jun 2, 2024
1 parent e0c8052 commit b0e13ec
Showing 1 changed file with 96 additions and 24 deletions.
120 changes: 96 additions & 24 deletions tests/lib/Event/Subscriber/TrashEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use eZ\Publish\API\Repository\Events\Trash\TrashEvent;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\LocationList;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\Core\Repository\Values\Content\Location;
use eZ\Publish\Core\Repository\Values\Content\Relation;
use EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber;
Expand All @@ -39,6 +41,8 @@ public function setUp(): void
$this->locationServiceMock,
$this->locationHelperMock,
$this->contentHelperMock,
$this->queryFactoryMock,
$this->searchServiceMock,
$this->repositoryMock
);
}
Expand All @@ -63,55 +67,124 @@ public function subscribedEventsDataProvider(): array

public function testCallOnTrashMethod()
{
$relationContentInfo1 = new ContentInfo(['id' => 1, 'contentTypeId' => 2]);
$relationContentInfo2 = new ContentInfo(['id' => 2, 'contentTypeId' => 2]);
$relationContentInfo3 = new ContentInfo(['id' => 3, 'contentTypeId' => 3]);

$event = $this->createMock(TrashEvent::class);
$event
->expects($this->atLeastOnce())
->method('getLocation')
->willReturn($this->location);

$contentInfo = $this->contentInfo;
$this->repositoryMock
->method('sudo')
->with(static function () use ($contentInfo) {})
->willReturn($this->getRelationList());

$this->contentServiceMock
->expects($this->atLeastOnce())
->method('loadContentInfo')
->willReturn($this->contentInfo);
->willReturnOnConsecutiveCalls(
$relationContentInfo1,
$relationContentInfo2,
$relationContentInfo3,
);

$this->notificationServiceMock
->expects($this->exactly(4))
->method('sendNotification')
->withConsecutive(
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::onTrash',
'DELETE',
$this->contentInfo,
],
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::updateRelations',
'UPDATE',
$relationContentInfo1,
],
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::updateRelations',
'UPDATE',
$relationContentInfo2,
],
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::updateRelations',
'UPDATE',
$relationContentInfo3,
],
);

$this->trashEventSubscriber->onTrash($event);
}

public function testCallOnRecoverMethod()
{
$relationContentInfo1 = new ContentInfo(['id' => 1, 'contentTypeId' => 2]);
$relationContentInfo2 = new ContentInfo(['id' => 2, 'contentTypeId' => 2]);
$relationContentInfo3 = new ContentInfo(['id' => 3, 'contentTypeId' => 3]);

$locationContentInfo1 = new ContentInfo(['id' => self::CONTENT_ID + 1]);
$locationContentInfo2 = new ContentInfo(['id' => self::CONTENT_ID + 2]);

$event = $this->createMock(RecoverEvent::class);
$event
->expects($this->atLeastOnce())
->method('getLocation')
->willReturn($this->location);

$this->locationServiceMock
->expects($this->atLeastOnce())
->method('loadLocationChildren')
->with($this->location)
->willReturn($this->getLocationChildren());
$this->queryFactoryMock
->method('create')
->willReturn(new LocationQuery());

$this->searchServiceMock
->method('findLocations')
->willReturn($this->getLocationChildrenSearchResult());

$this->contentServiceMock
->expects($this->atLeastOnce())
->method('loadContentInfo')
->willReturn($this->contentInfo);
->willReturnOnConsecutiveCalls(
$relationContentInfo1,
$relationContentInfo2,
$relationContentInfo3,
);

$contentInfo = $this->contentInfo;
$this->repositoryMock
->method('sudo')
->with(static function () use ($contentInfo) {})
->willReturn($this->getRelationList());

$this->contentServiceMock
->expects($this->atLeastOnce())
->method('loadContentInfo')
->willReturn($this->contentInfo);
$this->notificationServiceMock
->expects($this->exactly(5))
->method('sendNotification')
->withConsecutive(
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::onRecover',
'UPDATE',
$locationContentInfo1,
],
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::onRecover',
'UPDATE',
$locationContentInfo2,
],
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::updateRelations',
'UPDATE',
$relationContentInfo1,
],
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::updateRelations',
'UPDATE',
$relationContentInfo2,
],
[
'EzSystems\EzRecommendationClient\Event\Subscriber\TrashEventSubscriber::updateRelations',
'UPDATE',
$relationContentInfo3,
],
);

$this->trashEventSubscriber->onRecover($event);
}
Expand Down Expand Up @@ -143,21 +216,20 @@ private function getReverseRelations(): array
];
}

private function getLocationChildren(): LocationList
private function getLocationChildrenSearchResult(): SearchResult
{
return new LocationList([
'totalCount' => 2,
'locations' => [
new Location([
return new SearchResult([
'searchHits' => [
new SearchHit(['valueObject' => new Location([
'id' => 20,
'path' => ['1', '5', '20'],
'contentInfo' => new ContentInfo(['id' => self::CONTENT_ID + 1]),
]),
new Location([
])]),
new SearchHit(['valueObject' => new Location([
'id' => 30,
'path' => ['1', '5', '30'],
'contentInfo' => new ContentInfo(['id' => self::CONTENT_ID + 2]),
]),
])]),
],
]);
}
Expand Down

0 comments on commit b0e13ec

Please sign in to comment.