From 12a5a1ed3ea07439d4d20d9450d63f70a2deae62 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Sun, 2 Jun 2024 22:49:23 +0200 Subject: [PATCH 1/9] IBX-8019: Replaced `LocationService::loadLocationChildren` usages with `SearchService::findLocations` --- .../AbstractRepositoryEventSubscriber.php | 43 ++++++++++++++++--- .../Subscriber/LocationEventSubscriber.php | 12 ++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php b/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php index eab48c9d..23b6163e 100644 --- a/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php +++ b/src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php @@ -10,7 +10,9 @@ use eZ\Publish\API\Repository\ContentService as ContentServiceInterface; use eZ\Publish\API\Repository\LocationService as LocationServiceInterface; +use eZ\Publish\API\Repository\SearchService; use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\Core\Query\QueryFactoryInterface; use EzSystems\EzRecommendationClient\Helper\ContentHelper; use EzSystems\EzRecommendationClient\Helper\LocationHelper; use EzSystems\EzRecommendationClient\Service\NotificationService; @@ -29,33 +31,64 @@ abstract class AbstractRepositoryEventSubscriber extends AbstractCoreEventSubscr /** @var \EzSystems\EzRecommendationClient\Helper\ContentHelper */ protected $contentHelper; + /** @var \eZ\Publish\Core\Query\QueryFactoryInterface */ + private $queryFactory; + + /** @var \eZ\Publish\API\Repository\SearchService */ + private $searchService; + public function __construct( NotificationService $notificationService, ContentServiceInterface $contentService, LocationServiceInterface $locationService, LocationHelper $locationHelper, - ContentHelper $contentHelper + ContentHelper $contentHelper, + QueryFactoryInterface $queryFactory, + SearchService $searchService ) { parent::__construct($notificationService); $this->contentService = $contentService; $this->locationService = $locationService; $this->locationHelper = $locationHelper; $this->contentHelper = $contentHelper; + $this->queryFactory = $queryFactory; + $this->searchService = $searchService; } /** + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ protected function updateLocationSubtree(Location $location, string $method, string $action): void { - $subtree = $this->locationService->loadLocationChildren($location); + $locationChildren = $this->loadLocationChildren($location); - /** @var \eZ\Publish\API\Repository\Values\Content\Location $content */ - foreach ($subtree as $content) { + foreach ($locationChildren as $locationChild) { $this->notificationService->sendNotification( - $method, $action, $content->getContentInfo() + $method, $action, $locationChild->getContentInfo() ); } } + + /** + * @return array<\eZ\Publish\API\Repository\Values\Content\Location> + * + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException + */ + protected function loadLocationChildren(Location $location): array + { + /** @var \eZ\Publish\API\Repository\Values\Content\LocationQuery $locationChildrenQuery */ + $locationChildrenQuery = $this->queryFactory->create('Children', ['location' => $location]); + $searchResult = $this->searchService->findLocations($locationChildrenQuery); + + $locations = []; + foreach ($searchResult->searchHits as $searchHit) { + /** @var \eZ\Publish\API\Repository\Values\Content\Location $locationChild */ + $locationChild = $searchHit->valueObject; + $locations[] = $locationChild; + } + + return $locations; + } } diff --git a/src/lib/Event/Subscriber/LocationEventSubscriber.php b/src/lib/Event/Subscriber/LocationEventSubscriber.php index 30fb4135..859c316b 100644 --- a/src/lib/Event/Subscriber/LocationEventSubscriber.php +++ b/src/lib/Event/Subscriber/LocationEventSubscriber.php @@ -128,15 +128,13 @@ public function onUpdateLocation(UpdateLocationEvent $event): void /** * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ private function hideLocation(Location $location, bool $isChild = false): void { - $children = $this->locationService->loadLocationChildren($location)->locations; - - /** @var \eZ\Publish\API\Repository\Values\Content\Location $child */ - foreach ($children as $child) { + foreach ($this->loadLocationChildren($location) as $child) { $this->hideLocation($child, true); } @@ -158,15 +156,13 @@ private function hideLocation(Location $location, bool $isChild = false): void } /** + * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ private function updateLocationWithChildren(Location $location, string $method, string $action): void { - $children = $this->locationService->loadLocationChildren($location)->locations; - - /** @var \eZ\Publish\API\Repository\Values\Content\Location $child */ - foreach ($children as $child) { + foreach ($this->loadLocationChildren($location) as $child) { $this->updateLocationWithChildren($child, $method, $action); } From 9b0ae0e1fc1898fc52f420ffee5d22571172c2d3 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Sun, 2 Jun 2024 22:50:30 +0200 Subject: [PATCH 2/9] IBX-8019: Fixed LocationEventSubscriberTest to actually check NotificationService::sendNotification calls --- .../AbstractCoreEventSubscriberTest.php | 3 - .../AbstractRepositoryEventSubscriberTest.php | 23 +- .../LocationEventSubscriberTest.php | 348 +++++++++++++----- 3 files changed, 268 insertions(+), 106 deletions(-) diff --git a/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php b/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php index 7304500e..179cd148 100644 --- a/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php @@ -18,8 +18,6 @@ abstract class AbstractCoreEventSubscriberTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject|\EzSystems\EzRecommendationClient\Service\EventNotificationService */ - protected $notificationServiceMock; /** @var \eZ\Publish\API\Repository\Values\Content\ContentInfo */ protected $contentInfo; @@ -32,7 +30,6 @@ abstract class AbstractCoreEventSubscriberTest extends TestCase public function setUp(): void { - $this->notificationServiceMock = $this->createMock(EventNotificationService::class); $this->contentInfo = new ContentInfo([ 'id' => 1, 'contentTypeId' => 2, diff --git a/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php b/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php index e734d1e6..00aac7dc 100644 --- a/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php @@ -10,23 +10,35 @@ use eZ\Publish\API\Repository\ContentService; use eZ\Publish\API\Repository\LocationService; +use eZ\Publish\API\Repository\SearchService; +use eZ\Publish\Core\Query\QueryFactoryInterface; use EzSystems\EzRecommendationClient\Helper\ContentHelper; use EzSystems\EzRecommendationClient\Helper\LocationHelper; +use EzSystems\EzRecommendationClient\Service\EventNotificationService; abstract class AbstractRepositoryEventSubscriberTest extends AbstractCoreEventSubscriberTest { - /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\ContentService */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\ContentService */ protected $contentServiceMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\LocationService */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\LocationService */ protected $locationServiceMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\EzSystems\EzRecommendationClient\Helper\LocationHelper */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Helper\LocationHelper */ protected $locationHelperMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\EzSystems\EzRecommendationClient\Helper\ContentHelper */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Helper\ContentHelper */ protected $contentHelperMock; + /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\Core\Query\QueryFactoryInterface */ + protected $queryFactoryMock; + + /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\SearchService */ + protected $searchServiceMock; + + /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Service\EventNotificationService */ + protected $notificationServiceMock; + public function setUp(): void { parent::setUp(); @@ -35,5 +47,8 @@ public function setUp(): void $this->locationServiceMock = $this->createMock(LocationService::class); $this->locationHelperMock = $this->createMock(LocationHelper::class); $this->contentHelperMock = $this->createMock(ContentHelper::class); + $this->queryFactoryMock = $this->createMock(QueryFactoryInterface::class); + $this->searchServiceMock = $this->createMock(SearchService::class); + $this->notificationServiceMock = $this->createMock(EventNotificationService::class); } } diff --git a/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php b/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php index 5d74b9d8..3c1a26ba 100644 --- a/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php @@ -17,6 +17,9 @@ use eZ\Publish\API\Repository\Events\Location\UpdateLocationEvent; 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\Content; use eZ\Publish\Core\Repository\Values\Content\Location; use eZ\Publish\Core\Repository\Values\Content\VersionInfo; @@ -38,12 +41,6 @@ class LocationEventSubscriberTest extends AbstractRepositoryEventSubscriberTest /** @var \eZ\Publish\Core\Repository\Values\Content\Location */ private $location2; - /** @var \eZ\Publish\API\Repository\Values\Content\LocationList */ - private $emptyLocationChildren; - - /** @var \eZ\Publish\API\Repository\Values\Content\LocationList */ - private $locationChildren; - public function setUp(): void { parent::setUp(); @@ -53,7 +50,9 @@ public function setUp(): void $this->contentServiceMock, $this->locationServiceMock, $this->locationHelperMock, - $this->contentHelperMock + $this->contentHelperMock, + $this->queryFactoryMock, + $this->searchServiceMock ); $this->location1 = new Location([ 'id' => 20, @@ -65,17 +64,6 @@ public function setUp(): void 'path' => ['1', '5', '30'], 'contentInfo' => new ContentInfo(['id' => self::CONTENT_ID + 2]), ]); - $this->emptyLocationChildren = new LocationList([ - 'totalCount' => 0, - 'locations' => [], - ]); - $this->locationChildren = new LocationList([ - 'totalCount' => 2, - 'locations' => [ - $this->location1, - $this->location2, - ], - ]); } public function testCreateInstanceOfLocationEventSubscriber() @@ -109,11 +97,34 @@ public function testCallOnCopySubtreeMethod() ->method('getLocation') ->willReturn($this->location); - $this->locationServiceMock - ->expects($this->atLeastOnce()) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location)) - ->willReturn($this->locationChildren); + $this->queryFactoryMock + ->method('create') + ->willReturn(new LocationQuery()); + + $this->searchServiceMock + ->method('findLocations') + ->willReturn(new SearchResult([ + 'searchHits' => [ + new SearchHit(['valueObject' => $this->location1]), + new SearchHit(['valueObject' => $this->location2]), + ], + ])); + + $this->notificationServiceMock + ->expects($this->exactly(2)) + ->method('sendNotification') + ->withConsecutive( + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onCopySubtree', + 'UPDATE', + $this->location1->getContentInfo(), + ], + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onCopySubtree', + 'UPDATE', + $this->location2->getContentInfo(), + ], + ); $this->locationEventSubscriber->onCopySubtree($event); } @@ -126,6 +137,15 @@ public function testCallOnCreateLocationMethod() ->method('getContentInfo') ->willReturn($this->contentInfo); + $this->notificationServiceMock + ->expects($this->once()) + ->method('sendNotification') + ->with( + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onCreateLocation', + 'UPDATE', + $this->contentInfo + ); + $this->locationEventSubscriber->onCreateLocation($event); } @@ -133,26 +153,36 @@ public function testCallOnHideLocationMethod() { $event = $this->createMock(HideLocationEvent::class); $event - ->expects($this->once()) ->method('getLocation') ->willReturn($this->location); $this->locationServiceMock - ->expects($this->atLeastOnce()) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location)) - ->willReturn($this->emptyLocationChildren); - - $this->locationServiceMock - ->expects($this->atLeastOnce()) ->method('loadLocation') - ->willReturn($this->location); + ->wilLReturn($this->location); $this->contentHelperMock - ->expects($this->atLeastOnce()) ->method('getIncludedContent') ->willReturn($this->content); + $this->queryFactoryMock + ->method('create') + ->willReturn(new LocationQuery()); + + $this->searchServiceMock + ->method('findLocations') + ->willReturn(new SearchResult([])); + + $this->notificationServiceMock + ->expects($this->once()) + ->method('sendNotification') + ->withConsecutive( + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::hideLocation', + 'DELETE', + $this->location->getContentInfo(), + ], + ); + $this->locationEventSubscriber->onHideLocation($event); } @@ -164,21 +194,53 @@ public function testCallOnMoveSubtreeMethod() ->method('getLocation') ->willReturn($this->location); - $this->locationServiceMock - ->expects($this->atLeastOnce()) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location)) - ->willReturn($this->locationChildren); + $this->queryFactoryMock + ->method('create') + ->willReturn(new LocationQuery()); + + $this->searchServiceMock + ->method('findLocations') + ->willReturn(new SearchResult([ + 'searchHits' => [ + new SearchHit(['valueObject' => $this->location1]), + new SearchHit(['valueObject' => $this->location2]), + ], + ])); + + $this->notificationServiceMock + ->expects($this->exactly(2)) + ->method('sendNotification') + ->withConsecutive( + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onMoveSubtree', + 'UPDATE', + $this->location1->getContentInfo(), + ], + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onMoveSubtree', + 'UPDATE', + $this->location2->getContentInfo(), + ], + ); $this->locationEventSubscriber->onMoveSubtree($event); } public function testCallOnSwapLocationMethod() { + $swappedLocationContentInfo = new ContentInfo(['id' => self::CONTENT_ID + 120]); $swappedLocation = new Location([ 'id' => 120, 'path' => ['1', '5', '120'], - 'contentInfo' => new ContentInfo(['id' => self::CONTENT_ID + 120]), + 'contentInfo' => $swappedLocationContentInfo, + ]); + $swappedLocationContent = new Content([ + 'versionInfo' => new VersionInfo([ + 'contentInfo' => new ContentInfo([ + 'id' => self::CONTENT_ID + 120, + ]), + ]), + 'internalFields' => [], ]); $event = $this->createMock(SwapLocationEvent::class); @@ -191,28 +253,44 @@ public function testCallOnSwapLocationMethod() ->method('getLocation2') ->willReturn($swappedLocation); - $this->locationServiceMock - ->expects($this->at(0)) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location)) - ->willReturn($this->emptyLocationChildren); + $this->queryFactoryMock + ->method('create') + ->willReturn(new LocationQuery()); + + $this->searchServiceMock + ->method('findLocations') + ->willReturn(new SearchResult([])); $this->locationServiceMock - ->expects($this->at(1)) + ->expects($this->at(0)) ->method('loadLocation') ->willReturn($this->location); $this->locationServiceMock - ->expects($this->at(2)) - ->method('loadLocationChildren') - ->with($this->equalTo($swappedLocation)) - ->willReturn($this->emptyLocationChildren); - - $this->locationServiceMock - ->expects($this->at(3)) + ->expects($this->at(1)) ->method('loadLocation') ->willReturn($swappedLocation); + $this->contentHelperMock + ->method('getIncludedContent') + ->willReturnOnConsecutiveCalls($this->content, $swappedLocationContent); + + $this->notificationServiceMock + ->expects($this->exactly(2)) + ->method('sendNotification') + ->withConsecutive( + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::swapLocation', + 'UPDATE', + $this->location->getContentInfo(), + ], + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::swapLocation', + 'UPDATE', + $swappedLocationContentInfo, + ], + ); + $this->locationEventSubscriber->onSwapLocation($event); } @@ -224,17 +302,34 @@ public function testCallOnUnhideLocationMethod() ->method('getLocation') ->willReturn($this->location); - $this->locationServiceMock - ->expects($this->atLeastOnce()) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location)) - ->willReturn($this->emptyLocationChildren); + $this->queryFactoryMock + ->method('create') + ->willReturn(new LocationQuery()); + + $this->searchServiceMock + ->method('findLocations') + ->willReturn(new SearchResult([])); $this->locationServiceMock ->expects($this->atLeastOnce()) ->method('loadLocation') ->willReturn($this->location); + $this->contentHelperMock + ->method('getIncludedContent') + ->willReturn($this->content); + + $this->notificationServiceMock + ->expects($this->once()) + ->method('sendNotification') + ->withConsecutive( + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onUnhideLocation', + 'UPDATE', + $this->location->getContentInfo(), + ], + ); + $this->locationEventSubscriber->onUnhideLocation($event); } @@ -246,83 +341,99 @@ public function testCallOnUpdateLocationMethod() ->method('getLocation') ->willReturn($this->location); + $this->notificationServiceMock + ->expects($this->once()) + ->method('sendNotification') + ->withConsecutive( + [ + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onUpdateLocation', + 'UPDATE', + $this->location->getContentInfo(), + ], + ); + $this->locationEventSubscriber->onUpdateLocation($event); } /** * @dataProvider updateLocationWithChildrenDataProvider */ - public function testUpdateSingleLocationWithChildren(string $event, string $method) - { + public function testUpdateSingleLocationWithChildren( + string $event, + string $method, + string $expectedNotificationMethod, + string $expectedNotificationType + ) { $eventMock = $this->createMock($event); $eventMock ->expects($this->once()) ->method('getLocation') ->willReturn($this->location); - $this->locationServiceMock - ->expects($this->at(0)) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location)) - ->willReturn($this->locationChildren); - - $this->locationServiceMock - ->expects($this->at(1)) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location1)) - ->willReturn($this->emptyLocationChildren); + $this->queryFactoryMock + ->method('create') + ->willReturn(new LocationQuery()); + + $this->searchServiceMock + ->method('findLocations') + ->willReturnOnConsecutiveCalls( + new SearchResult([ + 'searchHits' => [ + new SearchHit(['valueObject' => $this->location1]), + new SearchHit(['valueObject' => $this->location2]), + ], + ]), + new SearchResult([]), + new SearchResult([]) + ); $this->locationServiceMock - ->expects($this->at(2)) + ->expects($this->at(0)) ->method('loadLocation') ->with($this->equalTo($this->location1->id)) ->willReturn($this->location1); $this->locationServiceMock - ->expects($this->at(3)) - ->method('loadLocationChildren') - ->with($this->equalTo($this->location2)) - ->willReturn($this->emptyLocationChildren); - - $this->locationServiceMock - ->expects($this->at(4)) + ->expects($this->at(1)) ->method('loadLocation') ->with($this->equalTo($this->location2->id)) ->willReturn($this->location2); $this->locationServiceMock - ->expects($this->at(5)) + ->expects($this->at(2)) ->method('loadLocation') ->with($this->equalTo($this->location->id)) ->willReturn($this->location); + $content1 = new Content([ + 'versionInfo' => new VersionInfo([ + 'contentInfo' => new ContentInfo([ + 'id' => self::CONTENT_ID + 1, + 'contentTypeId' => self::CONTENT_TYPE_ID, + ]), + ]), + 'internalFields' => [], + ]); $this->contentHelperMock ->expects($this->at(0)) ->method('getIncludedContent') ->with($this->equalTo(self::CONTENT_ID + 1)) - ->willReturn(new Content([ - 'versionInfo' => new VersionInfo([ - 'contentInfo' => new ContentInfo([ - 'id' => self::CONTENT_ID + 1, - 'contentTypeId' => self::CONTENT_TYPE_ID, - ]), - ]), - 'internalFields' => [], - ])); + ->willReturn($content1); + $content2 = new Content([ + 'versionInfo' => new VersionInfo([ + 'contentInfo' => new ContentInfo([ + 'id' => self::CONTENT_ID + 2, + 'contentTypeId' => self::CONTENT_TYPE_ID, + ]), + ]), + 'internalFields' => [], + ]); $this->contentHelperMock ->expects($this->at(1)) ->method('getIncludedContent') ->with($this->equalTo(3)) - ->willReturn(new Content([ - 'versionInfo' => new VersionInfo([ - 'contentInfo' => new ContentInfo([ - 'id' => self::CONTENT_ID + 2, - 'contentTypeId' => self::CONTENT_TYPE_ID, - ]), - ]), - 'internalFields' => [], - ])); + ->willReturn($content2); $this->contentHelperMock ->expects($this->at(2)) @@ -330,14 +441,53 @@ public function testUpdateSingleLocationWithChildren(string $event, string $meth ->with($this->equalTo(self::CONTENT_ID)) ->willReturn($this->content); + $this->notificationServiceMock + ->expects($this->exactly(3)) + ->method('sendNotification') + ->withConsecutive( + [ + $expectedNotificationMethod, + $expectedNotificationType, + $content1->contentInfo, + ], + [ + $expectedNotificationMethod, + $expectedNotificationType, + $content2->contentInfo, + ], + [ + $expectedNotificationMethod, + $expectedNotificationType, + $this->content->contentInfo, + ] + ); + $this->locationEventSubscriber->$method($eventMock); } + /** + * @return array + */ public function updateLocationWithChildrenDataProvider(): array { return [ - [HideLocationEvent::class, 'onHideLocation'], - [UnhideLocationEvent::class, 'onUnhideLocation'], + [ + HideLocationEvent::class, + 'onHideLocation', + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::hideLocation', + 'DELETE', + ], + [ + UnhideLocationEvent::class, + 'onUnhideLocation', + 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onUnhideLocation', + 'UPDATE', + ], ]; } } From d9cf94a71e176f83bedb47400779ea3f66bdc7ed Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Sun, 2 Jun 2024 22:56:16 +0200 Subject: [PATCH 3/9] IBX-8019: Fixed codestyle --- tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php | 2 -- tests/lib/Event/Subscriber/LocationEventSubscriberTest.php | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php b/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php index 179cd148..d79cb510 100644 --- a/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php @@ -12,13 +12,11 @@ use eZ\Publish\Core\Repository\Values\Content\Content; use eZ\Publish\Core\Repository\Values\Content\Location; use eZ\Publish\Core\Repository\Values\Content\VersionInfo; -use EzSystems\EzRecommendationClient\Service\EventNotificationService; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventSubscriberInterface; abstract class AbstractCoreEventSubscriberTest extends TestCase { - /** @var \eZ\Publish\API\Repository\Values\Content\ContentInfo */ protected $contentInfo; diff --git a/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php b/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php index 3c1a26ba..dfd43dc7 100644 --- a/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php @@ -16,7 +16,6 @@ use eZ\Publish\API\Repository\Events\Location\UnhideLocationEvent; use eZ\Publish\API\Repository\Events\Location\UpdateLocationEvent; 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; From e0c8052a0f13bb571e8f7caba22e4aa74d88f122 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Mon, 3 Jun 2024 00:26:03 +0200 Subject: [PATCH 4/9] IBX-8019: Added missing dependencies to TrashEventSubscriber --- src/lib/Event/Subscriber/TrashEventSubscriber.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/Event/Subscriber/TrashEventSubscriber.php b/src/lib/Event/Subscriber/TrashEventSubscriber.php index a5ef59e4..e82e9e0c 100644 --- a/src/lib/Event/Subscriber/TrashEventSubscriber.php +++ b/src/lib/Event/Subscriber/TrashEventSubscriber.php @@ -13,7 +13,9 @@ use eZ\Publish\API\Repository\Events\Trash\TrashEvent; use eZ\Publish\API\Repository\LocationService as LocationServiceInterface; use eZ\Publish\API\Repository\Repository; +use eZ\Publish\API\Repository\SearchService; use eZ\Publish\API\Repository\Values\Content\ContentInfo; +use eZ\Publish\Core\Query\QueryFactoryInterface; use EzSystems\EzRecommendationClient\Helper\ContentHelper; use EzSystems\EzRecommendationClient\Helper\LocationHelper; use EzSystems\EzRecommendationClient\Service\NotificationService; @@ -31,9 +33,19 @@ public function __construct( LocationServiceInterface $locationService, LocationHelper $locationHelper, ContentHelper $contentHelper, + QueryFactoryInterface $queryFactory, + SearchService $searchService, Repository $repository ) { - parent::__construct($notificationService, $contentService, $locationService, $locationHelper, $contentHelper); + parent::__construct( + $notificationService, + $contentService, + $locationService, + $locationHelper, + $contentHelper, + $queryFactory, + $searchService + ); $this->repository = $repository; } From b0e13ec043a727b793c86829864ff09d545cf64b Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Mon, 3 Jun 2024 00:27:31 +0200 Subject: [PATCH 5/9] 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]), - ]), + ])]), ], ]); } From f19e85855f1cb5978f0aa08040309a3186782b28 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Mon, 3 Jun 2024 00:27:56 +0200 Subject: [PATCH 6/9] IBX-8019: Aligned unit tests and PHPStan --- phpstan-baseline.neon | 47 +++++-------------- .../AbstractCoreEventSubscriberTest.php | 5 ++ .../AbstractRepositoryEventSubscriberTest.php | 17 +++---- 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 211af313..976e860e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -85,11 +85,6 @@ parameters: count: 1 path: src/bundle/DependencyInjection/ConfigurationMapper.php - - - message: "#^Parameter \\#1 \\$configuration of method Symfony\\\\Component\\\\DependencyInjection\\\\Extension\\\\Extension\\:\\:processConfiguration\\(\\) expects Symfony\\\\Component\\\\Config\\\\Definition\\\\ConfigurationInterface, Symfony\\\\Component\\\\Config\\\\Definition\\\\ConfigurationInterface\\|null given\\.$#" - count: 1 - path: src/bundle/DependencyInjection/EzRecommendationClientExtension.php - - message: "#^Method EzSystems\\\\EzRecommendationClientBundle\\\\EzRecommendationClientBundle\\:\\:build\\(\\) has no return type specified\\.$#" count: 1 @@ -1235,6 +1230,11 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php + - + message: "#^PHPDoc tag @var for property EzSystems\\\\EzRecommendationClient\\\\Tests\\\\Event\\\\Subscriber\\\\AbstractCoreEventSubscriberTest\\:\\:\\$notificationServiceMock contains unresolvable type\\.$#" + count: 1 + path: tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php + - message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#" count: 1 @@ -1320,11 +1320,6 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/ContentEventSubscriberTest.php - - - message: "#^Parameter \\#1 \\$notificationService of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\ContentEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Service\\\\NotificationService, EzSystems\\\\EzRecommendationClient\\\\Service\\\\EventNotificationService\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/ContentEventSubscriberTest.php - - message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#" count: 6 @@ -1332,7 +1327,12 @@ parameters: - message: "#^Call to an undefined method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\:\\:expects\\(\\)\\.$#" - count: 4 + count: 3 + path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php + + - + message: "#^Call to an undefined method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\:\\:method\\(\\)\\.$#" + count: 3 path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - @@ -1385,16 +1385,6 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - - message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Tests\\\\Event\\\\Subscriber\\\\LocationEventSubscriberTest\\:\\:updateLocationWithChildrenDataProvider\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - - - message: "#^Parameter \\#1 \\$notificationService of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\LocationEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Service\\\\NotificationService, EzSystems\\\\EzRecommendationClient\\\\Service\\\\EventNotificationService\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - message: "#^Parameter \\#1 \\$originalClassName of method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) expects class\\-string\\, string given\\.$#" count: 1 @@ -1435,21 +1425,11 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/ObjectStateEventSubscriberTest.php - - - message: "#^Parameter \\#1 \\$notificationService of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\ObjectStateEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Service\\\\NotificationService, EzSystems\\\\EzRecommendationClient\\\\Service\\\\EventNotificationService\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/ObjectStateEventSubscriberTest.php - - message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#" count: 1 path: tests/lib/Event/Subscriber/ObjectStateEventSubscriberTest.php - - - message: "#^Anonymous function has an unused use \\$contentInfo\\.$#" - count: 2 - path: tests/lib/Event/Subscriber/TrashEventSubscriberTest.php - - message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Tests\\\\Event\\\\Subscriber\\\\TrashEventSubscriberTest\\:\\:getRelationList\\(\\) has no return type specified\\.$#" count: 1 @@ -1480,11 +1460,6 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/TrashEventSubscriberTest.php - - - message: "#^Parameter \\#1 \\$notificationService of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\TrashEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Service\\\\NotificationService, EzSystems\\\\EzRecommendationClient\\\\Service\\\\EventNotificationService\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/TrashEventSubscriberTest.php - - message: "#^Parameter \\#4 \\$locationHelper of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\TrashEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Helper\\\\LocationHelper, EzSystems\\\\EzRecommendationClient\\\\Helper\\\\LocationHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" count: 1 diff --git a/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php b/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php index d79cb510..4decc705 100644 --- a/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php @@ -12,6 +12,7 @@ use eZ\Publish\Core\Repository\Values\Content\Content; use eZ\Publish\Core\Repository\Values\Content\Location; use eZ\Publish\Core\Repository\Values\Content\VersionInfo; +use EzSystems\EzRecommendationClient\Service\EventNotificationService; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -26,6 +27,9 @@ abstract class AbstractCoreEventSubscriberTest extends TestCase /** @var \eZ\Publish\Core\Repository\Values\Content\Content */ protected $content; + /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Service\EventNotificationService */ + protected $notificationServiceMock; + public function setUp(): void { $this->contentInfo = new ContentInfo([ @@ -42,6 +46,7 @@ public function setUp(): void 'contentInfo' => $this->contentInfo, ]), ]); + $this->notificationServiceMock = $this->createMock(EventNotificationService::class); } /** diff --git a/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php b/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php index 00aac7dc..aea9744b 100644 --- a/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php @@ -14,31 +14,27 @@ use eZ\Publish\Core\Query\QueryFactoryInterface; use EzSystems\EzRecommendationClient\Helper\ContentHelper; use EzSystems\EzRecommendationClient\Helper\LocationHelper; -use EzSystems\EzRecommendationClient\Service\EventNotificationService; abstract class AbstractRepositoryEventSubscriberTest extends AbstractCoreEventSubscriberTest { - /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\ContentService */ + /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\ContentService */ protected $contentServiceMock; - /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\LocationService */ + /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\LocationService */ protected $locationServiceMock; - /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Helper\LocationHelper */ + /** @var \PHPUnit\Framework\MockObject\MockObject|\EzSystems\EzRecommendationClient\Helper\LocationHelper */ protected $locationHelperMock; - /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Helper\ContentHelper */ + /** @var \PHPUnit\Framework\MockObject\MockObject|\EzSystems\EzRecommendationClient\Helper\ContentHelper */ protected $contentHelperMock; - /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\Core\Query\QueryFactoryInterface */ + /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Query\QueryFactoryInterface */ protected $queryFactoryMock; - /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\SearchService */ + /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\SearchService */ protected $searchServiceMock; - /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Service\EventNotificationService */ - protected $notificationServiceMock; - public function setUp(): void { parent::setUp(); @@ -49,6 +45,5 @@ public function setUp(): void $this->contentHelperMock = $this->createMock(ContentHelper::class); $this->queryFactoryMock = $this->createMock(QueryFactoryInterface::class); $this->searchServiceMock = $this->createMock(SearchService::class); - $this->notificationServiceMock = $this->createMock(EventNotificationService::class); } } From cc8e8ef5eba39f93d8e2b6249532ce4c1f55f972 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Mon, 3 Jun 2024 08:43:37 +0200 Subject: [PATCH 7/9] IBX-8019: Used static PHPUnit invocations --- .../LocationEventSubscriberTest.php | 50 +++++++++---------- .../Subscriber/TrashEventSubscriberTest.php | 12 ++--- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php b/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php index dfd43dc7..f4546c98 100644 --- a/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/LocationEventSubscriberTest.php @@ -92,7 +92,7 @@ public function testCallOnCopySubtreeMethod() { $event = $this->createMock(CopySubtreeEvent::class); $event - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation') ->willReturn($this->location); @@ -110,7 +110,7 @@ public function testCallOnCopySubtreeMethod() ])); $this->notificationServiceMock - ->expects($this->exactly(2)) + ->expects(self::exactly(2)) ->method('sendNotification') ->withConsecutive( [ @@ -132,12 +132,12 @@ public function testCallOnCreateLocationMethod() { $event = $this->createMock(CreateLocationEvent::class); $event - ->expects($this->once()) + ->expects(self::once()) ->method('getContentInfo') ->willReturn($this->contentInfo); $this->notificationServiceMock - ->expects($this->once()) + ->expects(self::once()) ->method('sendNotification') ->with( 'EzSystems\EzRecommendationClient\Event\Subscriber\LocationEventSubscriber::onCreateLocation', @@ -172,7 +172,7 @@ public function testCallOnHideLocationMethod() ->willReturn(new SearchResult([])); $this->notificationServiceMock - ->expects($this->once()) + ->expects(self::once()) ->method('sendNotification') ->withConsecutive( [ @@ -189,7 +189,7 @@ public function testCallOnMoveSubtreeMethod() { $event = $this->createMock(MoveSubtreeEvent::class); $event - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation') ->willReturn($this->location); @@ -207,7 +207,7 @@ public function testCallOnMoveSubtreeMethod() ])); $this->notificationServiceMock - ->expects($this->exactly(2)) + ->expects(self::exactly(2)) ->method('sendNotification') ->withConsecutive( [ @@ -244,11 +244,11 @@ public function testCallOnSwapLocationMethod() $event = $this->createMock(SwapLocationEvent::class); $event - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation1') ->willReturn($this->location); $event - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation2') ->willReturn($swappedLocation); @@ -261,12 +261,12 @@ public function testCallOnSwapLocationMethod() ->willReturn(new SearchResult([])); $this->locationServiceMock - ->expects($this->at(0)) + ->expects(self::at(0)) ->method('loadLocation') ->willReturn($this->location); $this->locationServiceMock - ->expects($this->at(1)) + ->expects(self::at(1)) ->method('loadLocation') ->willReturn($swappedLocation); @@ -275,7 +275,7 @@ public function testCallOnSwapLocationMethod() ->willReturnOnConsecutiveCalls($this->content, $swappedLocationContent); $this->notificationServiceMock - ->expects($this->exactly(2)) + ->expects(self::exactly(2)) ->method('sendNotification') ->withConsecutive( [ @@ -297,7 +297,7 @@ public function testCallOnUnhideLocationMethod() { $event = $this->createMock(UnhideLocationEvent::class); $event - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation') ->willReturn($this->location); @@ -310,7 +310,7 @@ public function testCallOnUnhideLocationMethod() ->willReturn(new SearchResult([])); $this->locationServiceMock - ->expects($this->atLeastOnce()) + ->expects(self::atLeastOnce()) ->method('loadLocation') ->willReturn($this->location); @@ -319,7 +319,7 @@ public function testCallOnUnhideLocationMethod() ->willReturn($this->content); $this->notificationServiceMock - ->expects($this->once()) + ->expects(self::once()) ->method('sendNotification') ->withConsecutive( [ @@ -336,12 +336,12 @@ public function testCallOnUpdateLocationMethod() { $event = $this->createMock(UpdateLocationEvent::class); $event - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation') ->willReturn($this->location); $this->notificationServiceMock - ->expects($this->once()) + ->expects(self::once()) ->method('sendNotification') ->withConsecutive( [ @@ -365,7 +365,7 @@ public function testUpdateSingleLocationWithChildren( ) { $eventMock = $this->createMock($event); $eventMock - ->expects($this->once()) + ->expects(self::once()) ->method('getLocation') ->willReturn($this->location); @@ -387,19 +387,19 @@ public function testUpdateSingleLocationWithChildren( ); $this->locationServiceMock - ->expects($this->at(0)) + ->expects(self::at(0)) ->method('loadLocation') ->with($this->equalTo($this->location1->id)) ->willReturn($this->location1); $this->locationServiceMock - ->expects($this->at(1)) + ->expects(self::at(1)) ->method('loadLocation') ->with($this->equalTo($this->location2->id)) ->willReturn($this->location2); $this->locationServiceMock - ->expects($this->at(2)) + ->expects(self::at(2)) ->method('loadLocation') ->with($this->equalTo($this->location->id)) ->willReturn($this->location); @@ -414,7 +414,7 @@ public function testUpdateSingleLocationWithChildren( 'internalFields' => [], ]); $this->contentHelperMock - ->expects($this->at(0)) + ->expects(self::at(0)) ->method('getIncludedContent') ->with($this->equalTo(self::CONTENT_ID + 1)) ->willReturn($content1); @@ -429,19 +429,19 @@ public function testUpdateSingleLocationWithChildren( 'internalFields' => [], ]); $this->contentHelperMock - ->expects($this->at(1)) + ->expects(self::at(1)) ->method('getIncludedContent') ->with($this->equalTo(3)) ->willReturn($content2); $this->contentHelperMock - ->expects($this->at(2)) + ->expects(self::at(2)) ->method('getIncludedContent') ->with($this->equalTo(self::CONTENT_ID)) ->willReturn($this->content); $this->notificationServiceMock - ->expects($this->exactly(3)) + ->expects(self::exactly(3)) ->method('sendNotification') ->withConsecutive( [ diff --git a/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php b/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php index 1f0d481b..1b627187 100644 --- a/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/TrashEventSubscriberTest.php @@ -73,7 +73,7 @@ public function testCallOnTrashMethod() $event = $this->createMock(TrashEvent::class); $event - ->expects($this->atLeastOnce()) + ->expects(self::atLeastOnce()) ->method('getLocation') ->willReturn($this->location); @@ -82,7 +82,7 @@ public function testCallOnTrashMethod() ->willReturn($this->getRelationList()); $this->contentServiceMock - ->expects($this->atLeastOnce()) + ->expects(self::atLeastOnce()) ->method('loadContentInfo') ->willReturnOnConsecutiveCalls( $relationContentInfo1, @@ -91,7 +91,7 @@ public function testCallOnTrashMethod() ); $this->notificationServiceMock - ->expects($this->exactly(4)) + ->expects(self::exactly(4)) ->method('sendNotification') ->withConsecutive( [ @@ -130,7 +130,7 @@ public function testCallOnRecoverMethod() $event = $this->createMock(RecoverEvent::class); $event - ->expects($this->atLeastOnce()) + ->expects(self::atLeastOnce()) ->method('getLocation') ->willReturn($this->location); @@ -143,7 +143,7 @@ public function testCallOnRecoverMethod() ->willReturn($this->getLocationChildrenSearchResult()); $this->contentServiceMock - ->expects($this->atLeastOnce()) + ->expects(self::atLeastOnce()) ->method('loadContentInfo') ->willReturnOnConsecutiveCalls( $relationContentInfo1, @@ -156,7 +156,7 @@ public function testCallOnRecoverMethod() ->willReturn($this->getRelationList()); $this->notificationServiceMock - ->expects($this->exactly(5)) + ->expects(self::exactly(5)) ->method('sendNotification') ->withConsecutive( [ From 91e663dc4ec6c8fd82eae32fb4a4b3fb75fb67ca Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Mon, 3 Jun 2024 08:44:02 +0200 Subject: [PATCH 8/9] IBX-8019: Used union instead of intersection types on MockObjects --- .../Subscriber/AbstractRepositoryEventSubscriberTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php b/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php index aea9744b..c96265bc 100644 --- a/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php +++ b/tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php @@ -23,16 +23,16 @@ abstract class AbstractRepositoryEventSubscriberTest extends AbstractCoreEventSu /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\LocationService */ protected $locationServiceMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\EzSystems\EzRecommendationClient\Helper\LocationHelper */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Helper\LocationHelper */ protected $locationHelperMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\EzSystems\EzRecommendationClient\Helper\ContentHelper */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\EzSystems\EzRecommendationClient\Helper\ContentHelper */ protected $contentHelperMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Query\QueryFactoryInterface */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\Core\Query\QueryFactoryInterface */ protected $queryFactoryMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\SearchService */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\SearchService */ protected $searchServiceMock; public function setUp(): void From cba0a5917d53e56775c8062ea7728f7adfa8c503 Mon Sep 17 00:00:00 2001 From: Maciej Kobus Date: Mon, 3 Jun 2024 08:47:31 +0200 Subject: [PATCH 9/9] IBX-8019: Regenerated PHPStan baseline --- phpstan-baseline.neon | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 976e860e..1de0fbed 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1240,6 +1240,16 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/AbstractCoreEventSubscriberTest.php + - + message: "#^PHPDoc tag @var for property EzSystems\\\\EzRecommendationClient\\\\Tests\\\\Event\\\\Subscriber\\\\AbstractRepositoryEventSubscriberTest\\:\\:\\$contentHelperMock contains unresolvable type\\.$#" + count: 1 + path: tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php + + - + message: "#^PHPDoc tag @var for property EzSystems\\\\EzRecommendationClient\\\\Tests\\\\Event\\\\Subscriber\\\\AbstractRepositoryEventSubscriberTest\\:\\:\\$locationHelperMock contains unresolvable type\\.$#" + count: 1 + path: tests/lib/Event/Subscriber/AbstractRepositoryEventSubscriberTest.php + - message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#" count: 2 @@ -1325,16 +1335,6 @@ parameters: count: 6 path: tests/lib/Event/Subscriber/ContentEventSubscriberTest.php - - - message: "#^Call to an undefined method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\:\\:expects\\(\\)\\.$#" - count: 3 - path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - - - message: "#^Call to an undefined method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\:\\:method\\(\\)\\.$#" - count: 3 - path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Tests\\\\Event\\\\Subscriber\\\\LocationEventSubscriberTest\\:\\:subscribedEventsDataProvider\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -1390,16 +1390,6 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - - message: "#^Parameter \\#4 \\$locationHelper of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\LocationEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Helper\\\\LocationHelper, EzSystems\\\\EzRecommendationClient\\\\Helper\\\\LocationHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - - - message: "#^Parameter \\#5 \\$contentHelper of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\LocationEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper, EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/LocationEventSubscriberTest.php - - message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#" count: 7 @@ -1460,16 +1450,6 @@ parameters: count: 1 path: tests/lib/Event/Subscriber/TrashEventSubscriberTest.php - - - message: "#^Parameter \\#4 \\$locationHelper of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\TrashEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Helper\\\\LocationHelper, EzSystems\\\\EzRecommendationClient\\\\Helper\\\\LocationHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/TrashEventSubscriberTest.php - - - - message: "#^Parameter \\#5 \\$contentHelper of class EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\TrashEventSubscriber constructor expects EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper, EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" - count: 1 - path: tests/lib/Event/Subscriber/TrashEventSubscriberTest.php - - message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#" count: 2