Skip to content

Commit

Permalink
IBX-8019: Replaced LocationService::loadLocationChildren usages with …
Browse files Browse the repository at this point in the history
…`SearchService::findLocations` (#114)

* IBX-8019: Replaced `LocationService::loadLocationChildren` usages with `SearchService::findLocations`

* IBX-8019: Fixed LocationEventSubscriberTest to actually check NotificationService::sendNotification calls

* IBX-8019: Fixed codestyle

* IBX-8019: Added missing dependencies to TrashEventSubscriber

* IBX-8019: Fixed TrashEventSubscriberTest to actually test EventNotificationService::sendNotification calls

* IBX-8019: Aligned unit tests and PHPStan

* IBX-8019: Used static PHPUnit invocations

* IBX-8019: Used union instead of intersection types on MockObjects

* IBX-8019: Regenerated PHPStan baseline
  • Loading branch information
webhdx authored Jun 7, 2024
1 parent 9637aed commit 2516953
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 215 deletions.
70 changes: 15 additions & 55 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1230,11 +1230,26 @@ 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
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
Expand Down Expand Up @@ -1315,21 +1330,11 @@ 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
path: tests/lib/Event/Subscriber/ContentEventSubscriberTest.php

-
message: "#^Call to an undefined method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ContentHelper\\|PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\:\\:expects\\(\\)\\.$#"
count: 4
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
Expand Down Expand Up @@ -1380,31 +1385,11 @@ 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\\<mixed\\>, string given\\.$#"
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
Expand All @@ -1430,21 +1415,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
Expand Down Expand Up @@ -1475,21 +1450,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
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
Expand Down
43 changes: 38 additions & 5 deletions src/lib/Event/Subscriber/AbstractRepositoryEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
12 changes: 4 additions & 8 deletions src/lib/Event/Subscriber/LocationEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
14 changes: 13 additions & 1 deletion src/lib/Event/Subscriber/TrashEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +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;

Expand All @@ -30,9 +27,11 @@ 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->notificationServiceMock = $this->createMock(EventNotificationService::class);
$this->contentInfo = new ContentInfo([
'id' => 1,
'contentTypeId' => 2,
Expand All @@ -47,6 +46,7 @@ public function setUp(): void
'contentInfo' => $this->contentInfo,
]),
]);
$this->notificationServiceMock = $this->createMock(EventNotificationService::class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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;

Expand All @@ -21,12 +23,18 @@ 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 */
protected $queryFactoryMock;

/** @var \PHPUnit\Framework\MockObject\MockObject&\eZ\Publish\API\Repository\SearchService */
protected $searchServiceMock;

public function setUp(): void
{
parent::setUp();
Expand All @@ -35,5 +43,7 @@ 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);
}
}
Loading

0 comments on commit 2516953

Please sign in to comment.