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