1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Resource\Tests\Repository\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
15
|
|
|
use Doctrine\ORM\EntityRepository; |
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
17
|
|
|
use Doctrine\ORM\Query; |
18
|
|
|
use Doctrine\ORM\Query\Expr; |
19
|
|
|
use Doctrine\ORM\QueryBuilder; |
20
|
|
|
use Lug\Component\Grid\DataSource\Doctrine\ORM\DataSourceBuilder; |
21
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
22
|
|
|
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository; |
23
|
|
|
use Lug\Component\Resource\Repository\RepositoryInterface; |
24
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
25
|
|
|
use Pagerfanta\Pagerfanta; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author GeLo <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class RepositoryTest extends \PHPUnit_Framework_TestCase |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Repository |
34
|
|
|
*/ |
35
|
|
|
private $repository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|EntityManager |
39
|
|
|
*/ |
40
|
|
|
private $entityManager; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
44
|
|
|
*/ |
45
|
|
|
private $classMetadata; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
49
|
|
|
*/ |
50
|
|
|
private $resource; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $class; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected function setUp() |
61
|
|
|
{ |
62
|
|
|
$this->entityManager = $this->createEntityManagerMock(); |
63
|
|
|
$this->classMetadata = $this->createClassMetadataMock(); |
64
|
|
|
$this->resource = $this->createResourceMock(); |
|
|
|
|
65
|
|
|
$this->class = $this->classMetadata->name = \stdClass::class; |
66
|
|
|
|
67
|
|
|
$this->repository = new Repository($this->entityManager, $this->classMetadata, $this->resource); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testInheritance() |
71
|
|
|
{ |
72
|
|
|
$this->assertInstanceOf(RepositoryInterface::class, $this->repository); |
73
|
|
|
$this->assertInstanceOf(EntityRepository::class, $this->repository); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testCreateDataSourceBuilder() |
77
|
|
|
{ |
78
|
|
|
$this->setUpQueryBuilder(); |
79
|
|
|
$dataSourceBuilder = $this->repository->createDataSourceBuilder(); |
80
|
|
|
|
81
|
|
|
$this->assertInstanceOf(DataSourceBuilder::class, $dataSourceBuilder); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testCreateQueryBuilder() |
85
|
|
|
{ |
86
|
|
|
$queryBuilder = $this->setUpQueryBuilder(); |
87
|
|
|
|
88
|
|
|
$this->assertSame($queryBuilder, $this->repository->createQueryBuilder()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testCreateQueryBuilderWithAlias() |
92
|
|
|
{ |
93
|
|
|
$queryBuilder = $this->setUpQueryBuilder(false, $alias = 'foo'); |
94
|
|
|
|
95
|
|
|
$this->assertSame($queryBuilder, $this->repository->createQueryBuilder($alias)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testCreateQueryBuilderWithIndexBy() |
99
|
|
|
{ |
100
|
|
|
$queryBuilder = $this->setUpQueryBuilder(false, null, $indexBy = 'index'); |
101
|
|
|
|
102
|
|
|
$this->assertSame($queryBuilder, $this->repository->createQueryBuilder(null, $indexBy)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testCreateQueryForCollectionBuilder() |
106
|
|
|
{ |
107
|
|
|
$queryBuilder = $this->setUpQueryBuilder(); |
108
|
|
|
|
109
|
|
|
$this->assertSame($queryBuilder, $this->repository->createQueryBuilderForCollection()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testCreateQueryBuilderForCollectionWithAlias() |
113
|
|
|
{ |
114
|
|
|
$queryBuilder = $this->setUpQueryBuilder(false, $alias = 'foo'); |
115
|
|
|
|
116
|
|
|
$this->assertSame($queryBuilder, $this->repository->createQueryBuilderForCollection($alias)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testCreateQueryBuilderForCollectionWithIndexBy() |
120
|
|
|
{ |
121
|
|
|
$queryBuilder = $this->setUpQueryBuilder(false, null, $indexBy = 'index'); |
122
|
|
|
|
123
|
|
|
$this->assertSame($queryBuilder, $this->repository->createQueryBuilderForCollection(null, $indexBy)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testFindOneByWithNullValue() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$queryBuilder = $this->setUpQueryBuilder($value = null); |
129
|
|
|
|
130
|
|
|
$queryBuilder |
|
|
|
|
131
|
|
|
->expects($this->once()) |
132
|
|
|
->method('getQuery') |
133
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
134
|
|
|
|
135
|
|
|
$query |
|
|
|
|
136
|
|
|
->expects($this->once()) |
137
|
|
|
->method('getOneOrNullResult') |
138
|
|
|
->will($this->returnValue($result = 'result')); |
139
|
|
|
|
140
|
|
|
$this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC'])); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
public function testFindOneByWithScalarValue() |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$queryBuilder = $this->setUpQueryBuilder($value = 'bar'); |
146
|
|
|
|
147
|
|
|
$queryBuilder |
|
|
|
|
148
|
|
|
->expects($this->once()) |
149
|
|
|
->method('getQuery') |
150
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
151
|
|
|
|
152
|
|
|
$query |
|
|
|
|
153
|
|
|
->expects($this->once()) |
154
|
|
|
->method('getOneOrNullResult') |
155
|
|
|
->will($this->returnValue($result = 'result')); |
156
|
|
|
|
157
|
|
|
$this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC'])); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
View Code Duplication |
public function testFindOneByWithArrayValue() |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$queryBuilder = $this->setUpQueryBuilder($value = ['bar']); |
163
|
|
|
|
164
|
|
|
$queryBuilder |
|
|
|
|
165
|
|
|
->expects($this->once()) |
166
|
|
|
->method('getQuery') |
167
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
168
|
|
|
|
169
|
|
|
$query |
|
|
|
|
170
|
|
|
->expects($this->once()) |
171
|
|
|
->method('getOneOrNullResult') |
172
|
|
|
->will($this->returnValue($result = 'result')); |
173
|
|
|
|
174
|
|
|
$this->assertSame($result, $this->repository->findOneBy(['foo' => $value], ['baz' => 'ASC'])); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function testFindBy() |
178
|
|
|
{ |
179
|
|
|
$queryBuilder = $this->setUpQueryBuilder($value = 'bar'); |
180
|
|
|
|
181
|
|
|
$queryBuilder |
|
|
|
|
182
|
|
|
->expects($this->once()) |
183
|
|
|
->method('setMaxResults') |
184
|
|
|
->with($this->identicalTo($limit = 5)) |
185
|
|
|
->will($this->returnSelf()); |
186
|
|
|
|
187
|
|
|
$queryBuilder |
188
|
|
|
->expects($this->once()) |
189
|
|
|
->method('setFirstResult') |
190
|
|
|
->with($this->identicalTo($offset = 10)) |
191
|
|
|
->will($this->returnSelf()); |
192
|
|
|
|
193
|
|
|
$queryBuilder |
194
|
|
|
->expects($this->once()) |
195
|
|
|
->method('getQuery') |
196
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
197
|
|
|
|
198
|
|
|
$query |
|
|
|
|
199
|
|
|
->expects($this->once()) |
200
|
|
|
->method('getResult') |
201
|
|
|
->will($this->returnValue($result = ['result'])); |
202
|
|
|
|
203
|
|
|
$this->assertSame($result, $this->repository->findBy(['foo' => $value], ['baz' => 'ASC'], $limit, $offset)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
public function testFindForIndex() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$this->setUpQueryBuilder($value = 'bar'); |
209
|
|
|
|
210
|
|
|
$pager = $this->repository->findForIndex(['foo' => $value], ['baz' => 'ASC']); |
211
|
|
|
|
212
|
|
|
$this->assertInstanceOf(Pagerfanta::class, $pager); |
213
|
|
|
$this->assertInstanceOf(DoctrineORMAdapter::class, $pager->getAdapter()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
View Code Duplication |
public function testFindForShow() |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
$queryBuilder = $this->setUpQueryBuilder($value = 'bar'); |
219
|
|
|
|
220
|
|
|
$queryBuilder |
|
|
|
|
221
|
|
|
->expects($this->once()) |
222
|
|
|
->method('getQuery') |
223
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
224
|
|
|
|
225
|
|
|
$query |
|
|
|
|
226
|
|
|
->expects($this->once()) |
227
|
|
|
->method('getOneOrNullResult') |
228
|
|
|
->will($this->returnValue($result = 'result')); |
229
|
|
|
|
230
|
|
|
$this->assertSame($result, $this->repository->findForShow(['foo' => $value], ['baz' => 'ASC'])); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
View Code Duplication |
public function testFindForUpdate() |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
$queryBuilder = $this->setUpQueryBuilder($value = 'bar'); |
236
|
|
|
|
237
|
|
|
$queryBuilder |
|
|
|
|
238
|
|
|
->expects($this->once()) |
239
|
|
|
->method('getQuery') |
240
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
241
|
|
|
|
242
|
|
|
$query |
|
|
|
|
243
|
|
|
->expects($this->once()) |
244
|
|
|
->method('getOneOrNullResult') |
245
|
|
|
->will($this->returnValue($result = 'result')); |
246
|
|
|
|
247
|
|
|
$this->assertSame($result, $this->repository->findForUpdate(['foo' => $value], ['baz' => 'ASC'])); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
View Code Duplication |
public function testFindForDelete() |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
$queryBuilder = $this->setUpQueryBuilder($value = 'bar'); |
253
|
|
|
|
254
|
|
|
$queryBuilder |
|
|
|
|
255
|
|
|
->expects($this->once()) |
256
|
|
|
->method('getQuery') |
257
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
258
|
|
|
|
259
|
|
|
$query |
|
|
|
|
260
|
|
|
->expects($this->once()) |
261
|
|
|
->method('getOneOrNullResult') |
262
|
|
|
->will($this->returnValue($result = 'result')); |
263
|
|
|
|
264
|
|
|
$this->assertSame($result, $this->repository->findForDelete(['foo' => $value], ['baz' => 'ASC'])); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param mixed $value |
269
|
|
|
* @param string|null $alias |
270
|
|
|
* @param string|null $indexBy |
271
|
|
|
* |
272
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder |
273
|
|
|
*/ |
274
|
|
|
private function setUpQueryBuilder($value = false, $alias = null, $indexBy = null) |
275
|
|
|
{ |
276
|
|
|
if ($alias === null) { |
277
|
|
|
$this->resource |
278
|
|
|
->expects($this->once()) |
279
|
|
|
->method('getName') |
280
|
|
|
->will($this->returnValue($resource = 'resource')); |
281
|
|
|
} else { |
282
|
|
|
$resource = $alias; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$this->entityManager |
|
|
|
|
286
|
|
|
->expects($this->once()) |
287
|
|
|
->method('createQueryBuilder') |
288
|
|
|
->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); |
289
|
|
|
|
290
|
|
|
$queryBuilder |
|
|
|
|
291
|
|
|
->expects($this->once()) |
292
|
|
|
->method('select') |
293
|
|
|
->with($this->identicalTo($resource)) |
294
|
|
|
->will($this->returnSelf()); |
295
|
|
|
|
296
|
|
|
$queryBuilder |
297
|
|
|
->expects($this->once()) |
298
|
|
|
->method('from') |
299
|
|
|
->with($this->identicalTo($this->class), $this->identicalTo($resource), $this->identicalTo($indexBy)) |
300
|
|
|
->will($this->returnSelf()); |
301
|
|
|
|
302
|
|
|
$queryBuilder |
303
|
|
|
->expects($this->any()) |
304
|
|
|
->method('getRootAliases') |
305
|
|
|
->will($this->returnValue([$resource])); |
306
|
|
|
|
307
|
|
|
if ($value === false) { |
308
|
|
|
return $queryBuilder; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$queryBuilder |
312
|
|
|
->expects($this->once()) |
313
|
|
|
->method('expr') |
314
|
|
|
->will($this->returnValue($expr = $this->createExprMock())); |
315
|
|
|
|
316
|
|
|
if ($value === null) { |
317
|
|
|
$expr |
|
|
|
|
318
|
|
|
->expects($this->once()) |
319
|
|
|
->method('isNull') |
320
|
|
|
->with($this->identicalTo($resource.'.'.($property = 'foo'))) |
321
|
|
|
->will($this->returnValue($expression = 'expression')); |
322
|
|
|
} else { |
323
|
|
|
$expr |
324
|
|
|
->expects($this->once()) |
325
|
|
|
->method(is_string($value) ? 'eq' : 'in') |
326
|
|
|
->with( |
327
|
|
|
$this->identicalTo($resource.'.'.($property = 'foo')), |
328
|
|
|
$this->matchesRegularExpression('/:'.$resource.'_'.$property.'_[a-z0-9]{22}/') |
329
|
|
|
) |
330
|
|
|
->will($this->returnValue($expression = 'expression')); |
331
|
|
|
|
332
|
|
|
$queryBuilder |
333
|
|
|
->expects($this->once()) |
334
|
|
|
->method('setParameter') |
335
|
|
|
->with( |
336
|
|
|
$this->matchesRegularExpression('/'.$resource.'_'.$property.'_[a-z0-9]{22}/'), |
337
|
|
|
$this->identicalTo($value) |
338
|
|
|
) |
339
|
|
|
->will($this->returnSelf()); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$queryBuilder |
343
|
|
|
->expects($this->once()) |
344
|
|
|
->method('andWhere') |
345
|
|
|
->with($this->identicalTo($expression)) |
346
|
|
|
->will($this->returnSelf()); |
347
|
|
|
|
348
|
|
|
$queryBuilder |
349
|
|
|
->expects($this->once()) |
350
|
|
|
->method('addOrderBy') |
351
|
|
|
->with($this->identicalTo($resource.'.'.($order = 'baz')), $this->identicalTo($sort = 'ASC')) |
352
|
|
|
->will($this->returnSelf()); |
353
|
|
|
|
354
|
|
|
return $queryBuilder; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EntityManager |
359
|
|
|
*/ |
360
|
|
|
private function createEntityManagerMock() |
361
|
|
|
{ |
362
|
|
|
return $this->getMockBuilder(EntityManager::class) |
363
|
|
|
->disableOriginalConstructor() |
364
|
|
|
->getMock(); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
369
|
|
|
*/ |
370
|
|
|
private function createClassMetadataMock() |
371
|
|
|
{ |
372
|
|
|
return $this->getMockBuilder(ClassMetadata::class) |
373
|
|
|
->disableOriginalConstructor() |
374
|
|
|
->getMock(); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
379
|
|
|
*/ |
380
|
|
|
private function createResourceMock() |
381
|
|
|
{ |
382
|
|
|
return $this->getMock(ResourceInterface::class); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder |
387
|
|
|
*/ |
388
|
|
|
private function createQueryBuilderMock() |
389
|
|
|
{ |
390
|
|
|
return $this->getMockBuilder(QueryBuilder::class) |
391
|
|
|
->disableOriginalConstructor() |
392
|
|
|
->getMock(); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Expr |
397
|
|
|
*/ |
398
|
|
|
private function createExprMock() |
399
|
|
|
{ |
400
|
|
|
return $this->getMock(Expr::class); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Query |
405
|
|
|
*/ |
406
|
|
|
private function createQueryMock() |
407
|
|
|
{ |
408
|
|
|
return $this->getMock(\stdClass::class, ['getOneOrNullResult', 'getResult']); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.