From b0e13ec043a727b793c86829864ff09d545cf64b Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Mon, 3 Jun 2024 00:27:31 +0200 Subject: [PATCH] IBX-8019: Fixed TrashEventSubscriberTest to actually test EventNotificationService::sendNotification calls --- .../Subscriber/TrashEventSubscriberTest.php | 120 ++++++++++++++---- 1 file changed, 96 insertions(+), 24 deletions(-) diff --git a/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php b/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php index 356d0b56..1f0d481b 100644 --- a/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php @@ -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; @@ -39,6 +41,8 @@ public function setUp(): void $this->locationServiceMock, $this->locationHelperMock, $this->contentHelperMock, + $this->queryFactoryMock, + $this->searchServiceMock, $this->repositoryMock ); } @@ -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); } @@ -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]), - ]), + ])]), ], ]); }