From d00a56bec094ecee6a7686b1ed26c21f9a815ef2 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 6 Jan 2022 12:22:05 +0100 Subject: [PATCH] Fixed query to count tags when a search term is present --- .../Adapter/AbstractTagsPaginatorAdapter.php | 15 ++++++--- .../Adapter/TagsPaginatorAdapterTest.php | 31 +++++++++++-------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/module/Core/src/Tag/Paginator/Adapter/AbstractTagsPaginatorAdapter.php b/module/Core/src/Tag/Paginator/Adapter/AbstractTagsPaginatorAdapter.php index f6331f5e..ba6bc78d 100644 --- a/module/Core/src/Tag/Paginator/Adapter/AbstractTagsPaginatorAdapter.php +++ b/module/Core/src/Tag/Paginator/Adapter/AbstractTagsPaginatorAdapter.php @@ -23,11 +23,18 @@ abstract class AbstractTagsPaginatorAdapter implements AdapterInterface public function getNbResults(): int { - return (int) $this->repo->matchSingleScalarResult(Spec::andX( - // FIXME I don't think using Spec::selectNew is the correct thing here, ideally it should be Spec::select, - // but seems to be the only way to use Spec::COUNT(...) + $conditions = [ + // FIXME I don't think using Spec::selectNew is the correct thing in this context. + // Ideally it should be Spec::select, but seems to be the only way to use Spec::COUNT(...). Spec::selectNew(Tag::class, Spec::COUNT('id', true)), new WithApiKeySpecsEnsuringJoin($this->apiKey), - )); + ]; + + $searchTerm = $this->params->searchTerm(); + if ($searchTerm !== null) { + $conditions[] = Spec::like('name', $searchTerm); + } + + return (int) $this->repo->matchSingleScalarResult(Spec::andX(...$conditions)); } } diff --git a/module/Core/test-db/Tag/Paginator/Adapter/TagsPaginatorAdapterTest.php b/module/Core/test-db/Tag/Paginator/Adapter/TagsPaginatorAdapterTest.php index 62d515ab..db3e444a 100644 --- a/module/Core/test-db/Tag/Paginator/Adapter/TagsPaginatorAdapterTest.php +++ b/module/Core/test-db/Tag/Paginator/Adapter/TagsPaginatorAdapterTest.php @@ -23,8 +23,13 @@ class TagsPaginatorAdapterTest extends DatabaseTestCase * @test * @dataProvider provideFilters */ - public function expectedListOfTagsIsReturned(?string $searchTerm, int $offset, int $length, int $expected): void - { + public function expectedListOfTagsIsReturned( + ?string $searchTerm, + int $offset, + int $length, + int $expectedSliceSize, + int $expectedTotalCount, + ): void { $names = ['foo', 'bar', 'baz', 'another']; foreach ($names as $name) { $this->getEntityManager()->persist(new Tag($name)); @@ -33,20 +38,20 @@ class TagsPaginatorAdapterTest extends DatabaseTestCase $adapter = new TagsPaginatorAdapter($this->repo, TagsParams::fromRawData(['searchTerm' => $searchTerm]), null); - self::assertCount($expected, $adapter->getSlice($offset, $length)); - self::assertEquals(4, $adapter->getNbResults()); + self::assertCount($expectedSliceSize, $adapter->getSlice($offset, $length)); + self::assertEquals($expectedTotalCount, $adapter->getNbResults()); } public function provideFilters(): iterable { - yield [null, 0, 10, 4]; - yield [null, 2, 10, 2]; - yield [null, 1, 3, 3]; - yield [null, 3, 3, 1]; - yield [null, 0, 2, 2]; - yield ['ba', 0, 10, 2]; - yield ['ba', 0, 1, 1]; - yield ['foo', 0, 10, 1]; - yield ['a', 0, 10, 3]; + yield [null, 0, 10, 4, 4]; + yield [null, 2, 10, 2, 4]; + yield [null, 1, 3, 3, 4]; + yield [null, 3, 3, 1, 4]; + yield [null, 0, 2, 2, 4]; + yield ['ba', 0, 10, 2, 2]; + yield ['ba', 0, 1, 1, 2]; + yield ['foo', 0, 10, 1, 1]; + yield ['a', 0, 10, 3, 3]; } }