diff --git a/module/Core/src/Tag/Paginator/Adapter/TagsPaginatorAdapter.php b/module/Core/src/Tag/Paginator/Adapter/TagsPaginatorAdapter.php index 23d7af48..5b1da6f7 100644 --- a/module/Core/src/Tag/Paginator/Adapter/TagsPaginatorAdapter.php +++ b/module/Core/src/Tag/Paginator/Adapter/TagsPaginatorAdapter.php @@ -11,11 +11,18 @@ class TagsPaginatorAdapter extends AbstractTagsPaginatorAdapter { public function getSlice(int $offset, int $length): iterable { - return $this->repo->match(Spec::andX( + $conditions = [ new WithApiKeySpecsEnsuringJoin($this->apiKey), Spec::orderBy('name'), Spec::limit($length), Spec::offset($offset), - )); + ]; + + $searchTerm = $this->params->searchTerm(); + if ($searchTerm !== null) { + $conditions[] = Spec::like('name', $searchTerm); + } + + return $this->repo->match(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 new file mode 100644 index 00000000..e0835718 --- /dev/null +++ b/module/Core/test-db/Tag/Paginator/Adapter/TagsPaginatorAdapterTest.php @@ -0,0 +1,52 @@ +repo = $this->getEntityManager()->getRepository(Tag::class); + } + + /** + * @test + * @dataProvider provideFilters + */ + public function expectedListOfTagsIsReturned(?string $searchTerm, int $offset, int $length, int $expected): void + { + $names = ['foo', 'bar', 'baz', 'another']; + foreach ($names as $name) { + $this->getEntityManager()->persist(new Tag($name)); + } + $this->getEntityManager()->flush(); + + $adapter = new TagsPaginatorAdapter($this->repo, TagsParams::fromRawData(['searchTerm' => $searchTerm]), null); + + self::assertCount($expected, $adapter->getSlice($offset, $length)); + self::assertEquals(4, $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]; + } +}