Skip to content

Commit

Permalink
[Tests] Fixed code duplication in Content & Location handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jun 20, 2024
1 parent bdccfd8 commit fb6aea3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 158 deletions.
55 changes: 55 additions & 0 deletions tests/lib/Search/Legacy/Content/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
43 changes: 0 additions & 43 deletions tests/lib/Search/Legacy/Content/HandlerContentSortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
43 changes: 0 additions & 43 deletions tests/lib/Search/Legacy/Content/HandlerContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
36 changes: 0 additions & 36 deletions tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 0 additions & 36 deletions tests/lib/Search/Legacy/Content/HandlerLocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit fb6aea3

Please sign in to comment.