Skip to content

Commit d515e75

Browse files
committed
IBX-10458: Fixed count
1 parent 4a271b8 commit d515e75

File tree

6 files changed

+83
-8
lines changed

6 files changed

+83
-8
lines changed

src/contracts/Persistence/Content/Type/CriterionHandlerInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
use Ibexa\Core\Persistence\Legacy\Content\Type\Gateway\CriterionVisitor\CriterionVisitor;
1414

1515
/**
16-
* @template T of \Ibexa\Contracts\Core\Repository\Values\ContentType\Query\CriterionInterface
16+
* @template TCriterion of \Ibexa\Contracts\Core\Repository\Values\ContentType\Query\CriterionInterface
1717
*/
1818
interface CriterionHandlerInterface
1919
{
2020
/**
21-
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\Query\CriterionInterface $criterion
21+
* @param TCriterion $criterion
2222
*/
2323
public function supports(CriterionInterface $criterion): bool;
2424

2525
/**
26-
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\Query\CriterionInterface $criterion
26+
* @param TCriterion $criterion
2727
*
2828
* @return string|\Doctrine\DBAL\Query\Expression\CompositeExpression
2929
*/

src/lib/Persistence/Legacy/Content/Type/Gateway.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ abstract public function insertGroup(Group $group): int;
3737

3838
abstract public function updateGroup(GroupUpdateStruct $group): void;
3939

40+
abstract public function countTypes(): int;
41+
4042
abstract public function countTypesInGroup(int $groupId): int;
4143

4244
abstract public function countGroupsForType(int $typeId, int $status): int;
@@ -174,7 +176,7 @@ abstract public function removeFieldDefinitionTranslation(
174176
abstract public function removeByUserAndVersion(int $userId, int $version): void;
175177

176178
/**
177-
* @return array<int,array<string,mixed>>
179+
* @return array{items: array<int,array<string,mixed>>, count: int}
178180
*/
179181
abstract public function findContentTypes(?ContentTypeQuery $query = null): array;
180182
}

src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ public function updateGroup(GroupUpdateStruct $group): void
207207
$query->execute();
208208
}
209209

210+
public function countTypes(): int
211+
{
212+
$query = $this->connection->createQueryBuilder();
213+
$query
214+
->select($this->dbPlatform->getCountExpression('id'))
215+
->from(self::CONTENT_TYPE_TABLE);
216+
217+
return (int)$query->execute()->fetchOne();
218+
}
219+
210220
public function countTypesInGroup(int $groupId): int
211221
{
212222
$query = $this->connection->createQueryBuilder();
@@ -1419,12 +1429,23 @@ public function removeByUserAndVersion(int $userId, int $version): void
14191429

14201430
public function findContentTypes(?ContentTypeQuery $query = null): array
14211431
{
1432+
$totalCount = $this->countTypes();
1433+
if ($totalCount === 0) {
1434+
return [
1435+
'count' => $totalCount,
1436+
'items' => [],
1437+
];
1438+
}
1439+
14221440
$queryBuilder = $this->getLoadTypeQueryBuilder();
14231441

14241442
if ($query === null) {
14251443
$queryBuilder->setMaxResults(ContentTypeQuery::DEFAULT_LIMIT);
14261444

1427-
return $queryBuilder->execute()->fetchAllAssociative();
1445+
return [
1446+
'count' => $totalCount,
1447+
'items' => $queryBuilder->execute()->fetchAllAssociative(),
1448+
];
14281449
}
14291450

14301451
if (!empty($query->getCriterion())) {
@@ -1442,7 +1463,10 @@ public function findContentTypes(?ContentTypeQuery $query = null): array
14421463
$queryBuilder->addOrderBy($column, $this->getQuerySortingDirection($sortClause->direction));
14431464
}
14441465

1445-
return $queryBuilder->execute()->fetchAllAssociative();
1466+
return [
1467+
'count' => $totalCount,
1468+
'items' => $queryBuilder->execute()->fetchAllAssociative(),
1469+
];
14461470
}
14471471

14481472
/**

src/lib/Persistence/Legacy/Content/Type/Gateway/ExceptionConversion.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ public function updateGroup(GroupUpdateStruct $group): void
5959
}
6060
}
6161

62+
public function countTypes(): int
63+
{
64+
try {
65+
return $this->innerGateway->countTypes();
66+
} catch (DBALException | PDOException $e) {
67+
throw DatabaseException::wrap($e);
68+
}
69+
}
70+
6271
public function countTypesInGroup(int $groupId): int
6372
{
6473
try {

src/lib/Persistence/Legacy/Content/Type/Handler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ public function findContentTypes(?ContentTypeQuery $query = null): array
200200
{
201201
$rows = $this->contentTypeGateway->findContentTypes($query);
202202
$items = $this->mapper->extractTypesFromRows(
203-
$rows,
203+
$rows['items'],
204204
true
205205
);
206206

207207
return [
208-
'count' => count($items),
208+
'count' => $rows['count'],
209209
'items' => $items,
210210
];
211211
}

tests/integration/Core/Repository/ContentTypeService/FindContentTypesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,26 @@ public function testFindContentTypes(ContentTypeQuery $query, array $expectedIde
4444
{
4545
$contentTypeService = self::getContentTypeService();
4646

47+
$expectedCount = $contentTypeService->findContentTypes(
48+
new ContentTypeQuery(
49+
null,
50+
[],
51+
0,
52+
0,
53+
)
54+
);
55+
$expectedCount = count($expectedCount->getContentTypes());
56+
4757
$contentTypes = $contentTypeService->findContentTypes($query);
58+
4859
$identifiers = array_map(
4960
static fn (ContentType $contentType): string => $contentType->getIdentifier(),
5061
$contentTypes->getContentTypes(),
5162
);
5263

5364
self::assertCount(count($expectedIdentifiers), $identifiers);
5465
self::assertEqualsCanonicalizing($expectedIdentifiers, $identifiers);
66+
self::assertSame($expectedCount, $contentTypes->getTotalCount());
5567
}
5668

5769
public function testFindContentTypesAscSortedByIdentifier(): void
@@ -73,6 +85,34 @@ public function testFindContentTypesAscSortedByIdentifier(): void
7385
self::assertSame(['article', 'file', 'folder', 'user'], $identifiers);
7486
}
7587

88+
public function testPagination(): void
89+
{
90+
$contentTypeService = self::getContentTypeService();
91+
92+
$collectedContentTypeIDs = [];
93+
$pageSize = 10;
94+
$noOfPages = 3;
95+
96+
for ($offset = 0; $offset < $noOfPages; $offset += $pageSize) {
97+
$searchResult = $contentTypeService->findContentTypes(
98+
new ContentTypeQuery(null, [], $offset, $pageSize),
99+
);
100+
101+
// an actual number of items on a current page
102+
self::assertCount($pageSize, $searchResult);
103+
104+
// check if results are not duplicated across multiple pages
105+
foreach ($searchResult->getContentTypes() as $contentType) {
106+
self::assertNotContains(
107+
$contentType->getIdentifier(),
108+
$collectedContentTypeIDs,
109+
"Content type '{$contentType->getIdentifier()}' exists on multiple pages"
110+
);
111+
$collectedContentTypeIDs[] = $contentType->getIdentifier();
112+
}
113+
}
114+
}
115+
76116
public function testFindContentTypesContainingFieldDefinitions(): void
77117
{
78118
$contentTypeService = self::getContentTypeService();

0 commit comments

Comments
 (0)