|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ArpTest\DoctrineQueryFilter; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\QueryBuilder; |
|
8
|
|
|
use Arp\DoctrineQueryFilter\QueryBuilderInterface; |
|
9
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
10
|
|
|
use Doctrine\DBAL\Types\Types; |
|
11
|
|
|
use Doctrine\ORM\EntityManager; |
|
12
|
|
|
use Doctrine\ORM\Query\Expr; |
|
13
|
|
|
use Doctrine\ORM\Query\Parameter; |
|
14
|
|
|
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder; |
|
15
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Alex Patterson <[email protected]> |
|
20
|
|
|
* @package ArpTest\DoctrineQueryFilter |
|
21
|
|
|
*/ |
|
22
|
|
|
final class QueryBuilderTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var DoctrineQueryBuilder |
|
26
|
|
|
*/ |
|
27
|
|
|
private DoctrineQueryBuilder $doctrineQueryBuilder; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Prepare the test case dependencies |
|
31
|
|
|
*/ |
|
32
|
|
|
public function setUp(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->doctrineQueryBuilder = $this->createMock(DoctrineQueryBuilder::class); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Assert the class implement QueryBuilderInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testInstanceOfQueryBuilderInterface(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertInstanceOf(QueryBuilderInterface::class, $queryBuilder); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Assert calls to createQueryBuilder will return a new instance of itself |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testCreateQueryBuilderWillReturnAnewInstanceOfItself(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
53
|
|
|
|
|
54
|
|
|
/** @var EntityManager|MockObject $entityManager */ |
|
55
|
|
|
$entityManager = $this->createMock(EntityManager::class); |
|
56
|
|
|
|
|
57
|
|
|
/** @var DoctrineQueryBuilder|MockObject $newDoctrineQueryBuilder */ |
|
58
|
|
|
$newDoctrineQueryBuilder = $this->createMock(DoctrineQueryBuilder::class); |
|
59
|
|
|
|
|
60
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
|
|
|
|
|
61
|
|
|
->method('getEntityManager') |
|
62
|
|
|
->willReturn($entityManager); |
|
63
|
|
|
|
|
64
|
|
|
$entityManager->expects($this->once()) |
|
65
|
|
|
->method('createQueryBuilder') |
|
66
|
|
|
->willReturn($newDoctrineQueryBuilder); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertInstanceOf(QueryBuilder::class, $queryBuilder->createQueryBuilder()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Assert that calls to getEntityManager will return the internal query builder's entity manager instance |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testGetEntityManagerWillReturnTheConfiguredEntityManagerInstance(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
77
|
|
|
|
|
78
|
|
|
/** @var EntityManager|MockObject $entityManager */ |
|
79
|
|
|
$entityManager = $this->createMock(EntityManager::class); |
|
80
|
|
|
|
|
81
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
82
|
|
|
->method('getEntityManager') |
|
83
|
|
|
->willReturn($entityManager); |
|
84
|
|
|
|
|
85
|
|
|
$queryBuilder->getEntityManager(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Assert that calls to expr() will return the internal query builder's Expr instance |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testGetQueryBuilderWillReturnTheConfiguredExprInstance(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
94
|
|
|
|
|
95
|
|
|
/** @var Expr|MockObject $expr */ |
|
96
|
|
|
$expr = $this->createMock(Expr::class); |
|
97
|
|
|
|
|
98
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
99
|
|
|
->method('expr') |
|
100
|
|
|
->willReturn($expr); |
|
101
|
|
|
|
|
102
|
|
|
$queryBuilder->expr(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Assert that calls to expr() will return the internal query builder's DQL parts |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testGetEntityManagerWillReturnTheConfiguredExprInstance(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
111
|
|
|
|
|
112
|
|
|
$parts = [ |
|
113
|
|
|
'foo' => 'bar', |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
117
|
|
|
->method('getDQLParts') |
|
118
|
|
|
->willReturn($parts); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertSame($parts, $queryBuilder->getQueryParts()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Assert that calls to innerJoin() will proxy to the internal query builder |
|
125
|
|
|
*/ |
|
126
|
|
|
public function testInnerJoinWillProxyToInternalQueryBuilder(): void |
|
127
|
|
|
{ |
|
128
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
129
|
|
|
|
|
130
|
|
|
$name = 'foo'; |
|
131
|
|
|
$alias = 'a'; |
|
132
|
|
|
$type = Expr\Join::ON; |
|
133
|
|
|
$condition = '1 = 1'; |
|
134
|
|
|
$indexBy = null; |
|
135
|
|
|
|
|
136
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
137
|
|
|
->method('innerJoin') |
|
138
|
|
|
->with($name, $alias, $type, $condition, $indexBy); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertSame($queryBuilder, $queryBuilder->innerJoin($name, $alias, $type, $condition, $indexBy)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Assert that calls to leftJoin() will proxy to the internal query builder |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testLeftJoinWillProxyToInternalQueryBuilder(): void |
|
147
|
|
|
{ |
|
148
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
149
|
|
|
|
|
150
|
|
|
$name = 'bar'; |
|
151
|
|
|
$alias = 'b'; |
|
152
|
|
|
$type = Expr\Join::ON; |
|
153
|
|
|
$condition = 'a.test = b.hello'; |
|
154
|
|
|
$indexBy = 'a.name'; |
|
155
|
|
|
|
|
156
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
157
|
|
|
->method('leftJoin') |
|
158
|
|
|
->with($name, $alias, $type, $condition, $indexBy); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertSame($queryBuilder, $queryBuilder->leftJoin($name, $alias, $type, $condition, $indexBy)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Assert that calls to getParameters() will proxy to the internal query builder |
|
165
|
|
|
*/ |
|
166
|
|
|
public function testGetParametersWillProxyToInternalQueryBuilder(): void |
|
167
|
|
|
{ |
|
168
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
169
|
|
|
|
|
170
|
|
|
/** @var ArrayCollection|MockObject $parameters */ |
|
171
|
|
|
$parameters = $this->createMock(ArrayCollection::class); |
|
172
|
|
|
|
|
173
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
174
|
|
|
->method('getParameters') |
|
175
|
|
|
->willReturn($parameters); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertSame($parameters, $queryBuilder->getParameters()); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Assert that calls to setParameters() will proxy to the internal query builder |
|
182
|
|
|
*/ |
|
183
|
|
|
public function testSetParametersWillProxyToInternalQueryBuilder(): void |
|
184
|
|
|
{ |
|
185
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
186
|
|
|
|
|
187
|
|
|
/** @var ArrayCollection|MockObject $parameters */ |
|
188
|
|
|
$parameters = $this->createMock(ArrayCollection::class); |
|
189
|
|
|
|
|
190
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
191
|
|
|
->method('setParameters') |
|
192
|
|
|
->with($parameters); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertSame($queryBuilder, $queryBuilder->setParameters($parameters)); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Assert that calls to setParameter() will proxy to the internal query builder |
|
199
|
|
|
*/ |
|
200
|
|
|
public function testSetParameterWillProxyToInternalQueryBuilder(): void |
|
201
|
|
|
{ |
|
202
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
203
|
|
|
|
|
204
|
|
|
$name = 'Foo'; |
|
205
|
|
|
$value = 'This is a test value'; |
|
206
|
|
|
$type = Types::STRING; |
|
207
|
|
|
|
|
208
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
209
|
|
|
->method('setParameter') |
|
210
|
|
|
->with($name, $value, $type); |
|
211
|
|
|
|
|
212
|
|
|
$this->assertSame($queryBuilder, $queryBuilder->setParameter($name, $value, $type)); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Assert that calls to mergeParameters() will proxy to the internal query builder |
|
217
|
|
|
*/ |
|
218
|
|
|
public function testMergeParametersWillProxyToInternalQueryBuilder(): void |
|
219
|
|
|
{ |
|
220
|
|
|
$queryBuilder = new QueryBuilder($this->doctrineQueryBuilder); |
|
221
|
|
|
|
|
222
|
|
|
$bParams = $addArgs = []; |
|
223
|
|
|
$b = [ |
|
224
|
|
|
'bar' => 456, |
|
225
|
|
|
'test' => 'This is value from B' |
|
226
|
|
|
]; |
|
227
|
|
|
foreach ($b as $key => $value) { |
|
228
|
|
|
/** @var Parameter|MockObject $parameter */ |
|
229
|
|
|
$parameter = $this->createMock(Parameter::class); |
|
230
|
|
|
$bParams[] = $parameter; |
|
231
|
|
|
$addArgs[] = [$parameter]; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** @var ArrayCollection|MockObject $params */ |
|
235
|
|
|
$params = $this->createMock(ArrayCollection::class); |
|
236
|
|
|
$this->doctrineQueryBuilder->expects($this->once()) |
|
237
|
|
|
->method('getParameters') |
|
238
|
|
|
->willReturn($params); |
|
239
|
|
|
|
|
240
|
|
|
/** @var QueryBuilderInterface|MockObject $newQueryBuilder */ |
|
241
|
|
|
$newQueryBuilder = $this->createMock(QueryBuilderInterface::class); |
|
242
|
|
|
|
|
243
|
|
|
/** @var ArrayCollection|MockObject $newParams */ |
|
244
|
|
|
$newParams = $this->createMock(ArrayCollection::class); |
|
245
|
|
|
|
|
246
|
|
|
$newQueryBuilder->expects($this->once()) |
|
247
|
|
|
->method('getParameters') |
|
248
|
|
|
->willReturn($newParams); |
|
249
|
|
|
|
|
250
|
|
|
$newParams->expects($this->once()) |
|
251
|
|
|
->method('getIterator') |
|
252
|
|
|
->willReturn(new \ArrayIterator($bParams)); |
|
253
|
|
|
|
|
254
|
|
|
$params->expects($this->exactly(count($bParams))) |
|
255
|
|
|
->method('add') |
|
256
|
|
|
->withConsecutive(...$addArgs); |
|
257
|
|
|
|
|
258
|
|
|
$this->assertSame($queryBuilder, $queryBuilder->mergeParameters($newQueryBuilder)); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..