/
DoctrineGenericSortApplierTest.php
118 lines (93 loc) · 3.27 KB
/
DoctrineGenericSortApplierTest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Tests\Integration\DDDStarterPack\Aggregate\Infrastructure\Doctrine\Repository\Filter;
use DDDStarterPack\Aggregate\Domain\Repository\Test\DoctrineUtil;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use Tests\Support\Model\Doctrine\DoctrineSortApplier;
use Tests\Support\Model\Person;
use Tests\Support\TestFilterBuilder;
use Tests\Tool\EntityManagerBuilder;
class DoctrineGenericSortApplierTest extends TestCase
{
private QueryBuilder $qb;
protected function setUp(): void
{
$em = EntityManagerBuilder::create()->getEntityManager();
$this->qb = $em->createQueryBuilder();
}
/**
* @test
*/
public function apply_sorting_to_query_builder(): void
{
$this->qb->select('p')->from(Person::class, 'p');
$expected = sprintf('SELECT %s FROM %s %s', 'p', Person::class, 'p');
DoctrineUtil::assertDQLEquals(
$expected,
(string) $this->qb->getQuery()->getDQL()
);
$filterParamsBuilder = new TestFilterBuilder();
$filterParamsBuilder->addApplier(new DoctrineSortApplier());
$filterParams = $filterParamsBuilder->build(['sort_field' => 'name', 'sort_direction' => 'ASC']);
$filterParams->applyToTarget($this->qb);
$expected = sprintf(
'SELECT %s FROM %s %s ORDER BY %s.name ASC',
'p',
Person::class,
'p',
'p'
);
DoctrineUtil::assertDQLEquals(
$expected,
(string) $this->qb->getQuery()->getDQL()
);
}
/**
* @test
*/
public function it_should_not_apply_filter_if_sort_field_is_null(): void
{
$this->qb->select('p')->from(Person::class, 'p');
$expected = sprintf('SELECT %s FROM %s %s', 'p', Person::class, 'p');
DoctrineUtil::assertDQLEquals(
$expected,
(string) $this->qb->getQuery()->getDQL()
);
$filterParamsBuilder = new TestFilterBuilder();
$filterParamsBuilder->addApplier(new DoctrineSortApplier());
$filterParams = $filterParamsBuilder->build(['sort_field' => null, 'sort_direction' => 'ASC']);
$filterParams->applyToTarget($this->qb);
DoctrineUtil::assertDQLEquals(
$expected,
(string) $this->qb->getQuery()->getDQL()
);
}
/**
* @test
*/
public function it_should_use_default_sort_direction_if_not_present(): void
{
$this->qb->select('p')->from(Person::class, 'p');
$expected = sprintf('SELECT %s FROM %s %s', 'p', Person::class, 'p');
DoctrineUtil::assertDQLEquals(
$expected,
(string) $this->qb->getQuery()->getDQL()
);
$filterParamsBuilder = new TestFilterBuilder();
$filterParamsBuilder->addApplier(new DoctrineSortApplier());
$filterParams = $filterParamsBuilder->build(['sort_field' => 'name']);
$filterParams->applyToTarget($this->qb);
$expected = sprintf(
'SELECT %s FROM %s %s ORDER BY %s.name ASC',
'p',
Person::class,
'p',
'p'
);
DoctrineUtil::assertDQLEquals(
$expected,
(string) $this->qb->getQuery()->getDQL()
);
}
}