/
DoctrineGenericPaginationApplierTest.php
59 lines (49 loc) · 1.68 KB
/
DoctrineGenericPaginationApplierTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Tests\Integration\DDDStarterPack\Aggregate\Infrastructure\Doctrine\Repository\Filter;
use DDDStarterPack\Aggregate\Domain\Repository\Test\DoctrineUtil;
use PHPUnit\Framework\TestCase;
use Tests\Support\Model\Doctrine\DoctrinePaginationApplier;
use Tests\Support\Model\Person;
use Tests\Support\TestFilterBuilder;
use Tests\Tool\EntityManagerBuilder;
class DoctrineGenericPaginationApplierTest extends TestCase
{
/**
* @return array<array-key, array<array-key, int>>
*/
public function paginationDataProvider(): array
{
return [
[1, 10, 0],
[2, 10, 10],
[3, 10, 20],
];
}
/**
* @test
*
* @dataProvider paginationDataProvider
*/
public function apply_pagination_to_query_builder(int $page, int $perPage, int $offset): void
{
$em = EntityManagerBuilder::create()->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select('p')->from(Person::class, 'p');
$filterParamsBuilder = new TestFilterBuilder();
$filterParamsBuilder->addApplier(new DoctrinePaginationApplier());
$filterParams = $filterParamsBuilder->build(['page' => $page, 'per_page' => $perPage]);
$filterParams->applyToTarget($qb);
$expected = sprintf(
'SELECT %s FROM %s %s',
'p',
Person::class,
'p'
);
/** @var string $actual */
$actual = $qb->getQuery()->getDQL();
DoctrineUtil::assertDQLEquals($expected, $actual);
self::assertEquals($offset, $qb->getFirstResult());
self::assertEquals($perPage, $qb->getMaxResults());
}
}