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\Translation\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\Query\Expr\Join; |
20
|
|
|
use Doctrine\ORM\QueryBuilder; |
21
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
22
|
|
|
use Lug\Component\Translation\Context\LocaleContextInterface; |
23
|
|
|
use Lug\Component\Translation\Model\TranslatableInterface; |
24
|
|
|
use Lug\Component\Translation\Model\TranslatableTrait; |
25
|
|
|
use Lug\Component\Translation\Model\TranslationInterface; |
26
|
|
|
use Lug\Component\Translation\Model\TranslationTrait; |
27
|
|
|
use Lug\Component\Translation\Repository\Doctrine\ORM\TranslatableRepository; |
28
|
|
|
use Lug\Component\Translation\Repository\TranslatableRepositoryInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @author GeLo <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class TranslatableRepositoryTest extends \PHPUnit_Framework_TestCase |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var TranslatableRepository |
37
|
|
|
*/ |
38
|
|
|
private $translatableRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|EntityManager |
42
|
|
|
*/ |
43
|
|
|
private $entityManager; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
47
|
|
|
*/ |
48
|
|
|
private $classMetadata; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
52
|
|
|
*/ |
53
|
|
|
private $resource; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
private $class; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
62
|
|
|
*/ |
63
|
|
|
private $localeContext; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected function setUp() |
69
|
|
|
{ |
70
|
|
|
$this->entityManager = $this->createEntityManagerMock(); |
71
|
|
|
$this->classMetadata = $this->createClassMetadataMock(); |
72
|
|
|
$this->resource = $this->createResourceMock(); |
73
|
|
|
$this->localeContext = $this->createLocaleContextMock(); |
74
|
|
|
$this->class = $this->classMetadata->name = TranslatableTest::class; |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->translatableRepository = new TranslatableRepository( |
77
|
|
|
$this->entityManager, |
78
|
|
|
$this->classMetadata, |
79
|
|
|
$this->resource, |
80
|
|
|
$this->localeContext |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testInheritance() |
85
|
|
|
{ |
86
|
|
|
$this->assertInstanceOf(TranslatableRepositoryInterface::class, $this->translatableRepository); |
87
|
|
|
$this->assertInstanceOf(EntityRepository::class, $this->translatableRepository); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testCreateQueryBuilder() |
91
|
|
|
{ |
92
|
|
|
$this->classMetadata |
|
|
|
|
93
|
|
|
->expects($this->once()) |
94
|
|
|
->method('getFieldNames') |
95
|
|
|
->will($this->returnValue([])); |
96
|
|
|
|
97
|
|
|
$this->classMetadata |
98
|
|
|
->expects($this->once()) |
99
|
|
|
->method('getAssociationMapping') |
100
|
|
|
->with($this->identicalTo('translations')) |
101
|
|
|
->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class])); |
102
|
|
|
|
103
|
|
|
$translationClassMetadata = $this->createClassMetadataMock(); |
104
|
|
|
$translationClassMetadata |
105
|
|
|
->expects($this->once()) |
106
|
|
|
->method('getFieldNames') |
107
|
|
|
->will($this->returnValue([])); |
108
|
|
|
|
109
|
|
|
$this->entityManager |
|
|
|
|
110
|
|
|
->expects($this->once()) |
111
|
|
|
->method('getClassMetadata') |
112
|
|
|
->with($this->identicalTo($translationClass)) |
113
|
|
|
->will($this->returnValue($translationClassMetadata)); |
114
|
|
|
|
115
|
|
|
$this->resource |
|
|
|
|
116
|
|
|
->expects($this->once()) |
117
|
|
|
->method('getName') |
118
|
|
|
->will($this->returnValue($name = 'resource')); |
119
|
|
|
|
120
|
|
|
$this->entityManager |
121
|
|
|
->expects($this->once()) |
122
|
|
|
->method('createQueryBuilder') |
123
|
|
|
->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); |
124
|
|
|
|
125
|
|
|
$queryBuilder |
126
|
|
|
->expects($this->once()) |
127
|
|
|
->method('select') |
128
|
|
|
->with($this->identicalTo($name)) |
129
|
|
|
->will($this->returnSelf()); |
130
|
|
|
|
131
|
|
|
$queryBuilder |
132
|
|
|
->expects($this->once()) |
133
|
|
|
->method('from') |
134
|
|
|
->with( |
135
|
|
|
$this->identicalTo($this->class), |
136
|
|
|
$this->identicalTo($name), |
137
|
|
|
$this->isNull() |
138
|
|
|
) |
139
|
|
|
->will($this->returnSelf()); |
140
|
|
|
|
141
|
|
|
$queryBuilder |
142
|
|
|
->expects($this->once()) |
143
|
|
|
->method('addSelect') |
144
|
|
|
->with($this->identicalTo('resource_translation')) |
145
|
|
|
->will($this->returnSelf()); |
146
|
|
|
|
147
|
|
|
$queryBuilder |
148
|
|
|
->expects($this->exactly(2)) |
149
|
|
|
->method('getRootAliases') |
150
|
|
|
->will($this->returnValue([$name])); |
151
|
|
|
|
152
|
|
|
$queryBuilder |
153
|
|
|
->expects($this->once()) |
154
|
|
|
->method('leftJoin') |
155
|
|
|
->with( |
156
|
|
|
$this->identicalTo($name.'.translations'), |
157
|
|
|
$this->identicalTo('resource_translation'), |
158
|
|
|
$this->isNull(), |
159
|
|
|
$this->isNull(), |
160
|
|
|
$this->isNull() |
161
|
|
|
) |
162
|
|
|
->will($this->returnSelf()); |
163
|
|
|
|
164
|
|
|
$this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilder()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testCreateQueryBuilderForCollection() |
168
|
|
|
{ |
169
|
|
|
$this->classMetadata |
|
|
|
|
170
|
|
|
->expects($this->once()) |
171
|
|
|
->method('getFieldNames') |
172
|
|
|
->will($this->returnValue([])); |
173
|
|
|
|
174
|
|
|
$this->classMetadata |
175
|
|
|
->expects($this->once()) |
176
|
|
|
->method('getAssociationMapping') |
177
|
|
|
->with($this->identicalTo('translations')) |
178
|
|
|
->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class])); |
179
|
|
|
|
180
|
|
|
$translationClassMetadata = $this->createClassMetadataMock(); |
181
|
|
|
$translationClassMetadata |
182
|
|
|
->expects($this->once()) |
183
|
|
|
->method('getFieldNames') |
184
|
|
|
->will($this->returnValue([])); |
185
|
|
|
|
186
|
|
|
$this->entityManager |
|
|
|
|
187
|
|
|
->expects($this->once()) |
188
|
|
|
->method('getClassMetadata') |
189
|
|
|
->with($this->identicalTo($translationClass)) |
190
|
|
|
->will($this->returnValue($translationClassMetadata)); |
191
|
|
|
|
192
|
|
|
$this->resource |
|
|
|
|
193
|
|
|
->expects($this->once()) |
194
|
|
|
->method('getName') |
195
|
|
|
->will($this->returnValue($name = 'resource')); |
196
|
|
|
|
197
|
|
|
$this->entityManager |
198
|
|
|
->expects($this->once()) |
199
|
|
|
->method('createQueryBuilder') |
200
|
|
|
->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); |
201
|
|
|
|
202
|
|
|
$queryBuilder |
203
|
|
|
->expects($this->once()) |
204
|
|
|
->method('select') |
205
|
|
|
->with($this->identicalTo($name)) |
206
|
|
|
->will($this->returnSelf()); |
207
|
|
|
|
208
|
|
|
$queryBuilder |
209
|
|
|
->expects($this->once()) |
210
|
|
|
->method('from') |
211
|
|
|
->with( |
212
|
|
|
$this->identicalTo($this->class), |
213
|
|
|
$this->identicalTo($name), |
214
|
|
|
$this->isNull() |
215
|
|
|
) |
216
|
|
|
->will($this->returnSelf()); |
217
|
|
|
|
218
|
|
|
$queryBuilder |
219
|
|
|
->expects($this->once()) |
220
|
|
|
->method('addSelect') |
221
|
|
|
->with($this->identicalTo('resource_translation')) |
222
|
|
|
->will($this->returnSelf()); |
223
|
|
|
|
224
|
|
|
$queryBuilder |
225
|
|
|
->expects($this->exactly(3)) |
226
|
|
|
->method('getRootAliases') |
227
|
|
|
->will($this->returnValue([$name])); |
228
|
|
|
|
229
|
|
|
$this->localeContext |
|
|
|
|
230
|
|
|
->expects($this->once()) |
231
|
|
|
->method('getLocales') |
232
|
|
|
->will($this->returnValue($locales = ['fr'])); |
233
|
|
|
|
234
|
|
|
$this->localeContext |
235
|
|
|
->expects($this->once()) |
236
|
|
|
->method('getFallbackLocale') |
237
|
|
|
->will($this->returnValue($fallbackLocale = 'en')); |
238
|
|
|
|
239
|
|
|
$queryBuilder |
240
|
|
|
->expects($this->once()) |
241
|
|
|
->method('expr') |
242
|
|
|
->will($this->returnValue($expr = $this->createExprMock())); |
243
|
|
|
|
244
|
|
|
$expr |
245
|
|
|
->expects($this->once()) |
246
|
|
|
->method('in') |
247
|
|
|
->with( |
248
|
|
|
$this->identicalTo('resource.locale'), |
249
|
|
|
$this->identicalTo(array_merge($locales, [$fallbackLocale])) |
250
|
|
|
) |
251
|
|
|
->will($this->returnValue($expression = 'expression')); |
252
|
|
|
|
253
|
|
|
$queryBuilder |
254
|
|
|
->expects($this->once()) |
255
|
|
|
->method('innerJoin') |
256
|
|
|
->with( |
257
|
|
|
$this->identicalTo($name.'.translations'), |
258
|
|
|
$this->identicalTo('resource_translation'), |
259
|
|
|
$this->identicalTo(Join::WITH), |
260
|
|
|
$this->identicalTo($expression), |
261
|
|
|
$this->isNull() |
262
|
|
|
) |
263
|
|
|
->will($this->returnSelf()); |
264
|
|
|
|
265
|
|
|
$this->assertSame($queryBuilder, $this->translatableRepository->createQueryBuilderForCollection()); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function testFindOneBy() |
269
|
|
|
{ |
270
|
|
|
$this->classMetadata |
|
|
|
|
271
|
|
|
->expects($this->once()) |
272
|
|
|
->method('getFieldNames') |
273
|
|
|
->will($this->returnValue([ |
274
|
|
|
$translatableField = 'translatable_field', |
275
|
|
|
$bothField = 'both_field', |
276
|
|
|
])); |
277
|
|
|
|
278
|
|
|
$this->classMetadata |
279
|
|
|
->expects($this->once()) |
280
|
|
|
->method('getAssociationMapping') |
281
|
|
|
->with($this->identicalTo('translations')) |
282
|
|
|
->will($this->returnValue(['targetEntity' => $translationClass = TranslationTest::class])); |
283
|
|
|
|
284
|
|
|
$translationClassMetadata = $this->createClassMetadataMock(); |
285
|
|
|
$translationClassMetadata |
286
|
|
|
->expects($this->once()) |
287
|
|
|
->method('getFieldNames') |
288
|
|
|
->will($this->returnValue([ |
289
|
|
|
$translationField = 'translation_field', |
290
|
|
|
$bothField, |
291
|
|
|
])); |
292
|
|
|
|
293
|
|
|
$this->entityManager |
|
|
|
|
294
|
|
|
->expects($this->once()) |
295
|
|
|
->method('getClassMetadata') |
296
|
|
|
->with($this->identicalTo($translationClass)) |
297
|
|
|
->will($this->returnValue($translationClassMetadata)); |
298
|
|
|
|
299
|
|
|
$this->resource |
|
|
|
|
300
|
|
|
->expects($this->once()) |
301
|
|
|
->method('getName') |
302
|
|
|
->will($this->returnValue($name = 'resource')); |
303
|
|
|
|
304
|
|
|
$this->entityManager |
305
|
|
|
->expects($this->once()) |
306
|
|
|
->method('createQueryBuilder') |
307
|
|
|
->will($this->returnValue($queryBuilder = $this->createQueryBuilderMock())); |
308
|
|
|
|
309
|
|
|
$queryBuilder |
310
|
|
|
->expects($this->once()) |
311
|
|
|
->method('select') |
312
|
|
|
->with($this->identicalTo($name)) |
313
|
|
|
->will($this->returnSelf()); |
314
|
|
|
|
315
|
|
|
$queryBuilder |
316
|
|
|
->expects($this->once()) |
317
|
|
|
->method('from') |
318
|
|
|
->with( |
319
|
|
|
$this->identicalTo($this->class), |
320
|
|
|
$this->identicalTo($name), |
321
|
|
|
$this->isNull() |
322
|
|
|
) |
323
|
|
|
->will($this->returnSelf()); |
324
|
|
|
|
325
|
|
|
$queryBuilder |
326
|
|
|
->expects($this->once()) |
327
|
|
|
->method('addSelect') |
328
|
|
|
->with($this->identicalTo('resource_translation')) |
329
|
|
|
->will($this->returnSelf()); |
330
|
|
|
|
331
|
|
|
$queryBuilder |
332
|
|
|
->expects($this->exactly(5)) |
333
|
|
|
->method('getRootAliases') |
334
|
|
|
->will($this->returnValue([$name])); |
335
|
|
|
|
336
|
|
|
$queryBuilder |
337
|
|
|
->expects($this->once()) |
338
|
|
|
->method('leftJoin') |
339
|
|
|
->with( |
340
|
|
|
$this->identicalTo($name.'.translations'), |
341
|
|
|
$this->identicalTo($translation = 'resource_translation'), |
342
|
|
|
$this->isNull(), |
343
|
|
|
$this->isNull(), |
344
|
|
|
$this->isNull() |
345
|
|
|
) |
346
|
|
|
->will($this->returnSelf()); |
347
|
|
|
|
348
|
|
|
$queryBuilder |
349
|
|
|
->expects($this->exactly(3)) |
350
|
|
|
->method('expr') |
351
|
|
|
->will($this->returnValue($expr = $this->createExprMock())); |
352
|
|
|
|
353
|
|
|
$expr |
354
|
|
|
->expects($this->at(0)) |
355
|
|
|
->method('eq') |
356
|
|
|
->with( |
357
|
|
|
$this->identicalTo($name.'.'.$translatableField), |
358
|
|
|
$this->matchesRegularExpression('/:'.$name.'_'.$translatableField.'_[a-z0-9]{22}/') |
359
|
|
|
) |
360
|
|
|
->will($this->returnValue($translatableWhere = 'translatable_where')); |
361
|
|
|
|
362
|
|
|
$expr |
363
|
|
|
->expects($this->at(1)) |
364
|
|
|
->method('eq') |
365
|
|
|
->with( |
366
|
|
|
$this->identicalTo($translation.'.'.$translationField), |
367
|
|
|
$this->matchesRegularExpression('/:'.$translation.'_'.$translationField.'_[a-z0-9]{22}/') |
368
|
|
|
) |
369
|
|
|
->will($this->returnValue($translationWhere = 'translation_where')); |
370
|
|
|
|
371
|
|
|
$expr |
372
|
|
|
->expects($this->at(2)) |
373
|
|
|
->method('eq') |
374
|
|
|
->with( |
375
|
|
|
$this->identicalTo($name.'.'.$bothField), |
376
|
|
|
$this->matchesRegularExpression('/:'.$name.'_'.$bothField.'_[a-z0-9]{22}/') |
377
|
|
|
) |
378
|
|
|
->will($this->returnValue($bothWhere = 'both_where')); |
379
|
|
|
|
380
|
|
|
$queryBuilder |
381
|
|
|
->expects($this->exactly(3)) |
382
|
|
|
->method('andWhere') |
383
|
|
|
->will($this->returnValueMap([ |
384
|
|
|
[$translatableWhere, $queryBuilder], |
385
|
|
|
[$translationWhere, $queryBuilder], |
386
|
|
|
[$bothWhere, $queryBuilder], |
387
|
|
|
])); |
388
|
|
|
|
389
|
|
|
$queryBuilder |
390
|
|
|
->expects($this->at(9)) |
391
|
|
|
->method('setParameter') |
392
|
|
|
->with( |
393
|
|
|
$this->matchesRegularExpression('/'.$name.'_'.$translatableField.'_[a-z0-9]{22}/'), |
394
|
|
|
$this->identicalTo($translatableValue = 'translatable_value'), |
395
|
|
|
$this->isNull() |
396
|
|
|
) |
397
|
|
|
->will($this->returnSelf()); |
398
|
|
|
|
399
|
|
|
$queryBuilder |
400
|
|
|
->expects($this->at(13)) |
401
|
|
|
->method('setParameter') |
402
|
|
|
->with( |
403
|
|
|
$this->matchesRegularExpression('/'.$translation.'_'.$translationField.'_[a-z0-9]{22}/'), |
404
|
|
|
$this->identicalTo($translationValue = 'translation_value'), |
405
|
|
|
$this->isNull() |
406
|
|
|
) |
407
|
|
|
->will($this->returnSelf()); |
408
|
|
|
|
409
|
|
|
$queryBuilder |
410
|
|
|
->expects($this->at(17)) |
411
|
|
|
->method('setParameter') |
412
|
|
|
->with( |
413
|
|
|
$this->matchesRegularExpression('/'.$name.'_'.$bothField.'_[a-z0-9]{22}/'), |
414
|
|
|
$this->identicalTo($bothValue = 'both_value'), |
415
|
|
|
$this->isNull() |
416
|
|
|
) |
417
|
|
|
->will($this->returnSelf()); |
418
|
|
|
|
419
|
|
|
$queryBuilder |
420
|
|
|
->expects($this->once()) |
421
|
|
|
->method('getQuery') |
422
|
|
|
->will($this->returnValue($query = $this->createQueryMock())); |
423
|
|
|
|
424
|
|
|
$query |
|
|
|
|
425
|
|
|
->expects($this->once()) |
426
|
|
|
->method('getOneOrNullResult') |
427
|
|
|
->will($this->returnValue($result = 'result')); |
428
|
|
|
|
429
|
|
|
$this->assertSame($result, $this->translatableRepository->findOneBy([ |
430
|
|
|
$translatableField => $translatableValue, |
431
|
|
|
$translationField => $translationValue, |
432
|
|
|
$bothField => $bothValue, |
433
|
|
|
])); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|EntityManager |
438
|
|
|
*/ |
439
|
|
|
private function createEntityManagerMock() |
440
|
|
|
{ |
441
|
|
|
return $this->createMock(EntityManager::class); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
446
|
|
|
*/ |
447
|
|
|
private function createClassMetadataMock() |
448
|
|
|
{ |
449
|
|
|
return $this->createMock(ClassMetadata::class); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
454
|
|
|
*/ |
455
|
|
|
private function createResourceMock() |
456
|
|
|
{ |
457
|
|
|
return $this->createMock(ResourceInterface::class); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LocaleContextInterface |
462
|
|
|
*/ |
463
|
|
|
private function createLocaleContextMock() |
464
|
|
|
{ |
465
|
|
|
return $this->createMock(LocaleContextInterface::class); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|QueryBuilder |
470
|
|
|
*/ |
471
|
|
|
private function createQueryBuilderMock() |
472
|
|
|
{ |
473
|
|
|
return $this->createMock(QueryBuilder::class); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Expr |
478
|
|
|
*/ |
479
|
|
|
private function createExprMock() |
480
|
|
|
{ |
481
|
|
|
return $this->createMock(Expr::class); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Query |
486
|
|
|
*/ |
487
|
|
|
private function createQueryMock() |
488
|
|
|
{ |
489
|
|
|
return $this->getMockBuilder(\stdClass::class) |
490
|
|
|
->setMethods(['getOneOrNullResult']) |
491
|
|
|
->getMock(); |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* @author GeLo <[email protected]> |
497
|
|
|
*/ |
498
|
|
|
class TranslatableTest implements TranslatableInterface |
|
|
|
|
499
|
|
|
{ |
500
|
|
|
use TranslatableTrait; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* @author GeLo <[email protected]> |
505
|
|
|
*/ |
506
|
|
|
class TranslationTest implements TranslationInterface |
|
|
|
|
507
|
|
|
{ |
508
|
|
|
use TranslationTrait; |
509
|
|
|
} |
510
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: