From fb6aea3d64383c3094fa83e3a9a6aac03ace4b31 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Thu, 20 Jun 2024 18:58:32 +0200 Subject: [PATCH] [Tests] Fixed code duplication in Content & Location handler tests --- .../Legacy/Content/AbstractTestCase.php | 55 +++++++++++++++++++ .../Legacy/Content/HandlerContentSortTest.php | 43 --------------- .../Legacy/Content/HandlerContentTest.php | 43 --------------- .../Content/HandlerLocationSortTest.php | 36 ------------ .../Legacy/Content/HandlerLocationTest.php | 36 ------------ 5 files changed, 55 insertions(+), 158 deletions(-) diff --git a/tests/lib/Search/Legacy/Content/AbstractTestCase.php b/tests/lib/Search/Legacy/Content/AbstractTestCase.php index a53b70569a..570a62bc5c 100644 --- a/tests/lib/Search/Legacy/Content/AbstractTestCase.php +++ b/tests/lib/Search/Legacy/Content/AbstractTestCase.php @@ -7,10 +7,14 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; +use Ibexa\Contracts\Core\Persistence\Content\ContentInfo; +use Ibexa\Contracts\Core\Persistence\Content\Location as SPILocation; use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as SPIContentTypeHandler; use Ibexa\Core\Persistence\Legacy\Content\FieldValue\Converter; use Ibexa\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry; use Ibexa\Core\Persistence\Legacy\Content\Gateway; +use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; +use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Persistence\Legacy\Content\Mapper\ResolveVirtualFieldSubscriber; use Ibexa\Core\Persistence\Legacy\Content\StorageRegistry; use Ibexa\Core\Persistence\Legacy\Content\Type\Gateway\DoctrineDatabase as ContentTypeGateway; @@ -19,6 +23,7 @@ use Ibexa\Core\Persistence\Legacy\Content\Type\StorageDispatcherInterface; use Ibexa\Core\Persistence\Legacy\Content\Type\Update\Handler as ContentTypeUpdateHandler; use Ibexa\Tests\Core\Persistence\Legacy\Content\LanguageAwareTestCase; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -139,4 +144,54 @@ protected function getEventDispatcher(): EventDispatcherInterface return $eventDispatcher; } + + protected function getContentMapperMock(): ContentMapper & MockObject + { + $mapperMock = $this->createMock(ContentMapper::class); + $mapperMock + ->method('extractContentInfoFromRows') + ->with(self::isType('array')) + ->willReturnCallback( + static function (array $rows): array { + $contentInfoObjs = []; + foreach ($rows as $row) { + $contentId = (int)$row['id']; + if (!isset($contentInfoObjs[$contentId])) { + $contentInfoObjs[$contentId] = new ContentInfo(); + $contentInfoObjs[$contentId]->id = $contentId; + } + } + + return array_values($contentInfoObjs); + } + ) + ; + + return $mapperMock; + } + + protected function getLocationMapperMock(): LocationMapper & MockObject + { + $mapperMock = $this->createMock(LocationMapper::class); + + $mapperMock + ->method('createLocationsFromRows') + ->with(self::isType('array')) + ->willReturnCallback( + static function ($rows): array { + $locations = []; + foreach ($rows as $row) { + $locationId = (int)$row['node_id']; + if (!isset($locations[$locationId])) { + $locations[$locationId] = new SPILocation(); + $locations[$locationId]->id = $locationId; + } + } + + return array_values($locations); + } + ); + + return $mapperMock; + } } diff --git a/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php b/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php index 75c42f537f..35fda09dca 100644 --- a/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php @@ -7,14 +7,12 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\ContentInfo; use Ibexa\Contracts\Core\Repository\Values\Content\Query; use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; use Ibexa\Core\Persistence\Legacy\Content\FieldHandler; use Ibexa\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry; use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; -use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Location\Gateway as LocationGateway; @@ -90,47 +88,6 @@ protected function getContentSearchHandler(array $fullTextSearchConfiguration = ); } - /** - * Returns a content mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Mapper - */ - protected function getContentMapperMock() - { - $mapperMock = $this->getMockBuilder(ContentMapper::class) - ->setConstructorArgs( - [ - $this->getFieldRegistry(), - $this->getLanguageHandler(), - $this->getContentTypeHandler(), - $this->getEventDispatcher(), - ] - ) - ->setMethods(['extractContentInfoFromRows']) - ->getMock(); - $mapperMock->expects(self::any()) - ->method('extractContentInfoFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows): array { - $contentInfoObjs = []; - foreach ($rows as $row) { - $contentId = (int)$row['id']; - if (!isset($contentInfoObjs[$contentId])) { - $contentInfoObjs[$contentId] = new ContentInfo(); - $contentInfoObjs[$contentId]->id = $contentId; - } - } - - return array_values($contentInfoObjs); - } - ) - ); - - return $mapperMock; - } - /** * Returns a field registry mock object. * diff --git a/tests/lib/Search/Legacy/Content/HandlerContentTest.php b/tests/lib/Search/Legacy/Content/HandlerContentTest.php index 26befbcdcf..fed2b42f98 100644 --- a/tests/lib/Search/Legacy/Content/HandlerContentTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerContentTest.php @@ -7,7 +7,6 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\ContentInfo; use Ibexa\Contracts\Core\Persistence\Content\Type; use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException; use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; @@ -17,7 +16,6 @@ use Ibexa\Core\Persistence; use Ibexa\Core\Persistence\Legacy\Content\FieldHandler; use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; -use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Location\Gateway as LocationGateway; @@ -189,47 +187,6 @@ protected function getContentSearchHandler(array $fullTextSearchConfiguration = ); } - /** - * Returns a content mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Mapper - */ - protected function getContentMapperMock() - { - $mapperMock = $this->getMockBuilder(ContentMapper::class) - ->setConstructorArgs( - [ - $this->getConverterRegistry(), - $this->getLanguageHandler(), - $this->getContentTypeHandler(), - $this->getEventDispatcher(), - ] - ) - ->setMethods(['extractContentInfoFromRows']) - ->getMock(); - $mapperMock->expects(self::any()) - ->method('extractContentInfoFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows): array { - $contentInfoObjs = []; - foreach ($rows as $row) { - $contentId = (int)$row['id']; - if (!isset($contentInfoObjs[$contentId])) { - $contentInfoObjs[$contentId] = new ContentInfo(); - $contentInfoObjs[$contentId]->id = $contentId; - } - } - - return array_values($contentInfoObjs); - } - ) - ); - - return $mapperMock; - } - /** * Returns a content field handler mock. * diff --git a/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php b/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php index 9c9865ea75..55a521986c 100644 --- a/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php @@ -7,11 +7,9 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\Location as SPILocation; use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery; use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; -use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter; @@ -105,40 +103,6 @@ protected function getContentSearchHandler() ); } - /** - * Returns a location mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Location\Mapper - */ - protected function getLocationMapperMock() - { - $mapperMock = $this->getMockBuilder(LocationMapper::class) - ->setMethods(['createLocationsFromRows']) - ->getMock(); - $mapperMock - ->expects(self::any()) - ->method('createLocationsFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows): array { - $locations = []; - foreach ($rows as $row) { - $locationId = (int)$row['node_id']; - if (!isset($locations[$locationId])) { - $locations[$locationId] = new SPILocation(); - $locations[$locationId]->id = $locationId; - } - } - - return array_values($locations); - } - ) - ); - - return $mapperMock; - } - public function testNoSorting() { $handler = $this->getContentSearchHandler(); diff --git a/tests/lib/Search/Legacy/Content/HandlerLocationTest.php b/tests/lib/Search/Legacy/Content/HandlerLocationTest.php index 3d6ef6da82..e592ba3ce8 100644 --- a/tests/lib/Search/Legacy/Content/HandlerLocationTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerLocationTest.php @@ -7,13 +7,11 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\Location as SPILocation; use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException; use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery; use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; use Ibexa\Core\Persistence; -use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter; @@ -166,40 +164,6 @@ protected function getContentSearchHandler(array $fullTextSearchConfiguration = ); } - /** - * Returns a location mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Location\Mapper - */ - protected function getLocationMapperMock() - { - $mapperMock = $this->getMockBuilder(LocationMapper::class) - ->setMethods(['createLocationsFromRows']) - ->getMock(); - $mapperMock - ->expects(self::any()) - ->method('createLocationsFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows): array { - $locations = []; - foreach ($rows as $row) { - $locationId = (int)$row['node_id']; - if (!isset($locations[$locationId])) { - $locations[$locationId] = new SPILocation(); - $locations[$locationId]->id = $locationId; - } - } - - return array_values($locations); - } - ) - ); - - return $mapperMock; - } - public function testFindWithoutOffsetLimit() { $handler = $this->getContentSearchHandler();