1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\DoctrineQueryFilter\Filter; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\Constant\WhereType; |
8
|
|
|
use Arp\DoctrineQueryFilter\Filter\AndX; |
9
|
|
|
use Arp\DoctrineQueryFilter\Filter\Exception\FilterException; |
10
|
|
|
use Arp\DoctrineQueryFilter\QueryBuilderInterface; |
11
|
|
|
use Doctrine\ORM\Query\Expr; |
12
|
|
|
use Doctrine\ORM\Query\Expr\Andx as DoctrineAndx; |
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers \Arp\DoctrineQueryFilter\Filter\AndX |
17
|
|
|
* @covers \Arp\DoctrineQueryFilter\Filter\AbstractComposite |
18
|
|
|
* |
19
|
|
|
* @author Alex Patterson <[email protected]> |
20
|
|
|
* @package ArpTest\DoctrineQueryFilter\Filter |
21
|
|
|
*/ |
22
|
|
|
final class AndXTest extends AbstractFilterTest |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Assert that the filter implements FilterInterface |
26
|
|
|
*/ |
27
|
|
|
public function testImplementsFilterInterface(): void |
28
|
|
|
{ |
29
|
|
|
$criteria = [ |
30
|
|
|
// no filters provided |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$filter = new AndX($this->queryFilterManager, $this->typecaster); |
34
|
|
|
|
35
|
|
|
$this->queryBuilder->expects($this->never())->method('createQueryBuilder'); |
36
|
|
|
|
37
|
|
|
$filter->filter($this->queryBuilder, $this->metadata, $criteria); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Assert that the andX filter will correctly apply the required filters |
42
|
|
|
* |
43
|
|
|
* @param string|null $whereType |
44
|
|
|
* |
45
|
|
|
* @dataProvider getFilterWillApplyTheProvidedConditionsData |
46
|
|
|
* |
47
|
|
|
* @throws FilterException |
48
|
|
|
*/ |
49
|
|
|
public function testFilterWillApplyTheProvidedConditions(?string $whereType): void |
50
|
|
|
{ |
51
|
|
|
$criteria = [ |
52
|
|
|
'conditions' => [ |
53
|
|
|
[ |
54
|
|
|
'name' => 'eq', |
55
|
|
|
'field' => 'test', |
56
|
|
|
'value' => 1, |
57
|
|
|
], |
58
|
|
|
[ |
59
|
|
|
'name' => 'meq', |
60
|
|
|
'field' => 'hello', |
61
|
|
|
'value' => 'test', |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
if (isset($whereType)) { |
67
|
|
|
$criteria['where'] = $whereType; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$filter = new AndX($this->queryFilterManager, $this->typecaster); |
71
|
|
|
|
72
|
|
|
$className = 'FooEntity'; |
73
|
|
|
|
74
|
|
|
/** @var QueryBuilderInterface|MockObject $qb */ |
75
|
|
|
$qb = $this->createMock(QueryBuilderInterface::class); |
76
|
|
|
|
77
|
|
|
$this->queryBuilder->expects($this->once()) |
78
|
|
|
->method('createQueryBuilder') |
79
|
|
|
->willReturn($qb); |
80
|
|
|
|
81
|
|
|
$this->metadata->expects($this->once()) |
82
|
|
|
->method('getName') |
83
|
|
|
->willReturn($className); |
84
|
|
|
|
85
|
|
|
$this->queryFilterManager->expects($this->once()) |
86
|
|
|
->method('filter') |
87
|
|
|
->with($qb, $className, ['filters' => $criteria['conditions']]); |
88
|
|
|
|
89
|
|
|
/** @var DoctrineAndx|MockObject $where */ |
90
|
|
|
$where = $this->createMock(DoctrineAndx::class); |
91
|
|
|
|
92
|
|
|
$queryParts = [ |
93
|
|
|
'where' => $where, |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$qb->expects($this->once()) |
97
|
|
|
->method('getQueryParts') |
98
|
|
|
->willReturn($queryParts); |
99
|
|
|
|
100
|
|
|
/** @var Expr|MockObject $expr */ |
101
|
|
|
$expr = $this->createMock(Expr::class); |
102
|
|
|
|
103
|
|
|
$this->queryBuilder->expects($this->once()) |
104
|
|
|
->method('expr') |
105
|
|
|
->willReturn($expr); |
106
|
|
|
|
107
|
|
|
/** @var DoctrineAndx|MockObject $doctrineAndX */ |
108
|
|
|
$doctrineAndX = $this->createMock(DoctrineAndx::class); |
109
|
|
|
|
110
|
|
|
$expr->expects($this->once()) |
111
|
|
|
->method('andX') |
112
|
|
|
->willReturn($doctrineAndX); |
113
|
|
|
|
114
|
|
|
/** @var Expr\Comparison[] $whereParts */ |
115
|
|
|
$whereParts = [ |
116
|
|
|
$this->createMock(Expr\Comparison::class), |
117
|
|
|
$this->createMock(Expr\Comparison::class), |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
$where->expects($this->once()) |
121
|
|
|
->method('getParts') |
122
|
|
|
->willReturn($whereParts); |
123
|
|
|
|
124
|
|
|
$doctrineAndX->expects($this->once()) |
125
|
|
|
->method('addMultiple') |
126
|
|
|
->with($whereParts); |
127
|
|
|
|
128
|
|
|
$this->queryBuilder->expects($this->once()) |
129
|
|
|
->method('andWhere') |
130
|
|
|
->with($doctrineAndX); |
131
|
|
|
|
132
|
|
|
$this->queryBuilder->expects($this->once()) |
133
|
|
|
->method('mergeParameters') |
134
|
|
|
->with($qb); |
135
|
|
|
|
136
|
|
|
$filter->filter($this->queryBuilder, $this->metadata, $criteria); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function getFilterWillApplyTheProvidedConditionsData(): array |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
|
|
[null], |
146
|
|
|
[WhereType::AND], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|