1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Tests\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Doctrine\DBAL\Connection; |
17
|
|
|
use Doctrine\DBAL\DBALException; |
18
|
|
|
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; |
19
|
|
|
use Doctrine\DBAL\Types\Type; |
20
|
|
|
use Doctrine\ORM\Configuration; |
21
|
|
|
use Doctrine\ORM\EntityManager; |
22
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
23
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
24
|
|
|
use Doctrine\ORM\OptimisticLockException; |
25
|
|
|
use Doctrine\ORM\Query; |
26
|
|
|
use Doctrine\ORM\QueryBuilder; |
27
|
|
|
use Doctrine\ORM\Version; |
28
|
|
|
use PHPUnit\Framework\TestCase; |
29
|
|
|
use Sonata\AdminBundle\Datagrid\Datagrid; |
30
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridInterface; |
31
|
|
|
use Sonata\AdminBundle\Exception\LockException; |
32
|
|
|
use Sonata\AdminBundle\Exception\ModelManagerException; |
33
|
|
|
use Sonata\AdminBundle\Filter\FilterInterface; |
34
|
|
|
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription; |
35
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\OrderByToSelectWalker; |
36
|
|
|
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery; |
37
|
|
|
use Sonata\DoctrineORMAdminBundle\Model\ModelManager; |
38
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType; |
39
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AbstractEntity; |
40
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity; |
41
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity; |
42
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity; |
43
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\SimpleEntity; |
44
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\UuidEntity; |
45
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\VersionedEntity; |
46
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Util\NonIntegerIdentifierTestClass; |
47
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
48
|
|
|
|
49
|
|
|
class ModelManagerTest extends TestCase |
50
|
|
|
{ |
51
|
|
|
public static function setUpBeforeClass() |
52
|
|
|
{ |
53
|
|
|
if (!Type::hasType('uuid')) { |
54
|
|
|
Type::addType('uuid', UuidType::class); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testSortParameters() |
59
|
|
|
{ |
60
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
61
|
|
|
|
62
|
|
|
$manager = new ModelManager($registry); |
63
|
|
|
|
64
|
|
|
$datagrid1 = $this->createMock(Datagrid::class); |
65
|
|
|
$datagrid2 = $this->createMock(Datagrid::class); |
66
|
|
|
|
67
|
|
|
$field1 = new FieldDescription(); |
68
|
|
|
$field1->setName('field1'); |
69
|
|
|
|
70
|
|
|
$field2 = new FieldDescription(); |
71
|
|
|
$field2->setName('field2'); |
72
|
|
|
|
73
|
|
|
$field3 = new FieldDescription(); |
74
|
|
|
$field3->setName('field3'); |
75
|
|
|
$field3->setOption('sortable', 'field3sortBy'); |
76
|
|
|
|
77
|
|
|
$datagrid1 |
78
|
|
|
->expects($this->any()) |
79
|
|
|
->method('getValues') |
80
|
|
|
->will($this->returnValue([ |
81
|
|
|
'_sort_by' => $field1, |
82
|
|
|
'_sort_order' => 'ASC', |
83
|
|
|
])); |
84
|
|
|
|
85
|
|
|
$datagrid2 |
86
|
|
|
->expects($this->any()) |
87
|
|
|
->method('getValues') |
88
|
|
|
->will($this->returnValue([ |
89
|
|
|
'_sort_by' => $field3, |
90
|
|
|
'_sort_order' => 'ASC', |
91
|
|
|
])); |
92
|
|
|
|
93
|
|
|
$parameters = $manager->getSortParameters($field1, $datagrid1); |
94
|
|
|
|
95
|
|
|
$this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
96
|
|
|
$this->assertEquals('field1', $parameters['filter']['_sort_by']); |
97
|
|
|
|
98
|
|
|
$parameters = $manager->getSortParameters($field2, $datagrid1); |
99
|
|
|
|
100
|
|
|
$this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
101
|
|
|
$this->assertEquals('field2', $parameters['filter']['_sort_by']); |
102
|
|
|
|
103
|
|
|
$parameters = $manager->getSortParameters($field3, $datagrid1); |
104
|
|
|
|
105
|
|
|
$this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
106
|
|
|
$this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
107
|
|
|
|
108
|
|
|
$parameters = $manager->getSortParameters($field3, $datagrid2); |
109
|
|
|
|
110
|
|
|
$this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
111
|
|
|
$this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getVersionDataProvider() |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
|
|
[true], |
118
|
|
|
[false], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @dataProvider getVersionDataProvider |
124
|
|
|
*/ |
125
|
|
|
public function testGetVersion($isVersioned) |
126
|
|
|
{ |
127
|
|
|
$object = new VersionedEntity(); |
128
|
|
|
|
129
|
|
|
$modelManager = $this->getMockBuilder(ModelManager::class) |
130
|
|
|
->disableOriginalConstructor() |
131
|
|
|
->setMethods(['getMetadata']) |
132
|
|
|
->getMock(); |
133
|
|
|
|
134
|
|
|
$metadata = $this->getMetadata(get_class($object), $isVersioned); |
135
|
|
|
|
136
|
|
|
$modelManager->expects($this->any()) |
137
|
|
|
->method('getMetadata') |
138
|
|
|
->will($this->returnValue($metadata)); |
139
|
|
|
|
140
|
|
|
if ($isVersioned) { |
141
|
|
|
$object->version = 123; |
142
|
|
|
|
143
|
|
|
$this->assertNotNull($modelManager->getLockVersion($object)); |
144
|
|
|
} else { |
145
|
|
|
$this->assertNull($modelManager->getLockVersion($object)); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function lockDataProvider() |
150
|
|
|
{ |
151
|
|
|
return [ |
152
|
|
|
[true, false], |
153
|
|
|
[true, true], |
154
|
|
|
[false, false], |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @dataProvider lockDataProvider |
160
|
|
|
*/ |
161
|
|
|
public function testLock($isVersioned, $expectsException) |
162
|
|
|
{ |
163
|
|
|
$object = new VersionedEntity(); |
164
|
|
|
|
165
|
|
|
$em = $this->getMockBuilder(EntityManager::class) |
166
|
|
|
->disableOriginalConstructor() |
167
|
|
|
->setMethods(['lock']) |
168
|
|
|
->getMock(); |
169
|
|
|
|
170
|
|
|
$modelManager = $this->getMockBuilder(ModelManager::class) |
171
|
|
|
->disableOriginalConstructor() |
172
|
|
|
->setMethods(['getMetadata', 'getEntityManager']) |
173
|
|
|
->getMock(); |
174
|
|
|
|
175
|
|
|
$modelManager->expects($this->any()) |
176
|
|
|
->method('getEntityManager') |
177
|
|
|
->will($this->returnValue($em)); |
178
|
|
|
|
179
|
|
|
$metadata = $this->getMetadata(get_class($object), $isVersioned); |
180
|
|
|
|
181
|
|
|
$modelManager->expects($this->any()) |
182
|
|
|
->method('getMetadata') |
183
|
|
|
->will($this->returnValue($metadata)); |
184
|
|
|
|
185
|
|
|
if ($expectsException) { |
186
|
|
|
$em->expects($this->once()) |
187
|
|
|
->method('lock') |
188
|
|
|
->will($this->throwException(OptimisticLockException::lockFailed($object))); |
189
|
|
|
|
190
|
|
|
$this->expectException(LockException::class); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$modelManager->lock($object, 123); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGetParentMetadataForProperty() |
197
|
|
|
{ |
198
|
|
|
if (version_compare(Version::VERSION, '2.5') < 0) { |
199
|
|
|
$this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
200
|
|
|
|
201
|
|
|
return; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$containerEntityClass = ContainerEntity::class; |
205
|
|
|
$associatedEntityClass = AssociatedEntity::class; |
206
|
|
|
$embeddedEntityClass = EmbeddedEntity::class; |
207
|
|
|
$modelManagerClass = ModelManager::class; |
208
|
|
|
|
209
|
|
|
$em = $this->createMock(EntityManager::class); |
210
|
|
|
|
211
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ModelManager $modelManager */ |
212
|
|
|
$modelManager = $this->getMockBuilder($modelManagerClass) |
213
|
|
|
->disableOriginalConstructor() |
214
|
|
|
->setMethods(['getMetadata', 'getEntityManager']) |
215
|
|
|
->getMock(); |
216
|
|
|
|
217
|
|
|
$modelManager->expects($this->any()) |
|
|
|
|
218
|
|
|
->method('getEntityManager') |
219
|
|
|
->will($this->returnValue($em)); |
220
|
|
|
|
221
|
|
|
$containerEntityMetadata = $this->getMetadataForContainerEntity(); |
222
|
|
|
$associatedEntityMetadata = $this->getMetadataForAssociatedEntity(); |
223
|
|
|
$embeddedEntityMetadata = $this->getMetadataForEmbeddedEntity(); |
224
|
|
|
|
225
|
|
|
$modelManager->expects($this->any())->method('getMetadata') |
|
|
|
|
226
|
|
|
->will( |
227
|
|
|
$this->returnValueMap( |
228
|
|
|
[ |
229
|
|
|
[$containerEntityClass, $containerEntityMetadata], |
230
|
|
|
[$embeddedEntityClass, $embeddedEntityMetadata], |
231
|
|
|
[$associatedEntityClass, $associatedEntityMetadata], |
232
|
|
|
] |
233
|
|
|
) |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
/** @var ClassMetadata $metadata */ |
237
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
238
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'plainField'); |
239
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
240
|
|
|
|
241
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
242
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.plainField'); |
243
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
244
|
|
|
|
245
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
246
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'embeddedEntity.plainField'); |
247
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
248
|
|
|
|
249
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
250
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.embeddedEntity.plainField'); |
251
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function getMetadataForEmbeddedEntity() |
255
|
|
|
{ |
256
|
|
|
$metadata = new ClassMetadata(EmbeddedEntity::class); |
257
|
|
|
|
258
|
|
|
$metadata->fieldMappings = [ |
259
|
|
|
'plainField' => [ |
260
|
|
|
'fieldName' => 'plainField', |
261
|
|
|
'columnName' => 'plainField', |
262
|
|
|
'type' => 'boolean', |
263
|
|
|
], |
264
|
|
|
]; |
265
|
|
|
|
266
|
|
|
return $metadata; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function getMetadataForAssociatedEntity() |
270
|
|
|
{ |
271
|
|
|
$embeddedEntityClass = EmbeddedEntity::class; |
272
|
|
|
|
273
|
|
|
$metadata = new ClassMetadata(AssociatedEntity::class); |
274
|
|
|
|
275
|
|
|
$metadata->fieldMappings = [ |
276
|
|
|
'plainField' => [ |
277
|
|
|
'fieldName' => 'plainField', |
278
|
|
|
'columnName' => 'plainField', |
279
|
|
|
'type' => 'string', |
280
|
|
|
], |
281
|
|
|
]; |
282
|
|
|
|
283
|
|
|
$metadata->embeddedClasses['embeddedEntity'] = [ |
284
|
|
|
'class' => $embeddedEntityClass, |
285
|
|
|
'columnPrefix' => 'embeddedEntity', |
286
|
|
|
]; |
287
|
|
|
|
288
|
|
|
$metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
289
|
|
|
|
290
|
|
|
return $metadata; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function getMetadataForContainerEntity() |
294
|
|
|
{ |
295
|
|
|
$containerEntityClass = ContainerEntity::class; |
296
|
|
|
$associatedEntityClass = AssociatedEntity::class; |
297
|
|
|
$embeddedEntityClass = EmbeddedEntity::class; |
298
|
|
|
|
299
|
|
|
$metadata = new ClassMetadata($containerEntityClass); |
300
|
|
|
|
301
|
|
|
$metadata->fieldMappings = [ |
302
|
|
|
'plainField' => [ |
303
|
|
|
'fieldName' => 'plainField', |
304
|
|
|
'columnName' => 'plainField', |
305
|
|
|
'type' => 'integer', |
306
|
|
|
], |
307
|
|
|
]; |
308
|
|
|
|
309
|
|
|
$metadata->associationMappings['associatedEntity'] = [ |
310
|
|
|
'fieldName' => 'associatedEntity', |
311
|
|
|
'targetEntity' => $associatedEntityClass, |
312
|
|
|
'sourceEntity' => $containerEntityClass, |
313
|
|
|
]; |
314
|
|
|
|
315
|
|
|
$metadata->embeddedClasses['embeddedEntity'] = [ |
316
|
|
|
'class' => $embeddedEntityClass, |
317
|
|
|
'columnPrefix' => 'embeddedEntity', |
318
|
|
|
]; |
319
|
|
|
|
320
|
|
|
$metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
321
|
|
|
|
322
|
|
|
return $metadata; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function testNonIntegerIdentifierType() |
326
|
|
|
{ |
327
|
|
|
$uuid = new NonIntegerIdentifierTestClass('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
328
|
|
|
$entity = new UuidEntity($uuid); |
329
|
|
|
|
330
|
|
|
$meta = $this->createMock(ClassMetadata::class); |
331
|
|
|
$meta->expects($this->any()) |
332
|
|
|
->method('getIdentifierValues') |
333
|
|
|
->willReturn([$entity->getId()]); |
334
|
|
|
$meta->expects($this->any()) |
335
|
|
|
->method('getTypeOfField') |
336
|
|
|
->willReturn(UuidType::NAME); |
337
|
|
|
|
338
|
|
|
$mf = $this->createMock(ClassMetadataFactory::class); |
339
|
|
|
$mf->expects($this->any()) |
340
|
|
|
->method('getMetadataFor') |
341
|
|
|
->willReturn($meta); |
342
|
|
|
|
343
|
|
|
$platform = $this->createMock(PostgreSqlPlatform::class); |
344
|
|
|
|
345
|
|
|
$conn = $this->createMock(Connection::class); |
346
|
|
|
$conn->expects($this->any()) |
347
|
|
|
->method('getDatabasePlatform') |
348
|
|
|
->willReturn($platform); |
349
|
|
|
|
350
|
|
|
$em = $this->createMock(EntityManager::class); |
351
|
|
|
$em->expects($this->any()) |
352
|
|
|
->method('getMetadataFactory') |
353
|
|
|
->willReturn($mf); |
354
|
|
|
$em->expects($this->any()) |
355
|
|
|
->method('getConnection') |
356
|
|
|
->willReturn($conn); |
357
|
|
|
|
358
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
359
|
|
|
$registry->expects($this->any()) |
360
|
|
|
->method('getManagerForClass') |
361
|
|
|
->willReturn($em); |
362
|
|
|
|
363
|
|
|
$manager = new ModelManager($registry); |
364
|
|
|
$result = $manager->getIdentifierValues($entity); |
365
|
|
|
|
366
|
|
|
$this->assertEquals($entity->getId()->toString(), $result[0]); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function testAssociationIdentifierType() |
370
|
|
|
{ |
371
|
|
|
$entity = new ContainerEntity(new AssociatedEntity(42, new EmbeddedEntity()), new EmbeddedEntity()); |
372
|
|
|
|
373
|
|
|
$meta = $this->createMock(ClassMetadata::class); |
374
|
|
|
$meta->expects($this->any()) |
375
|
|
|
->method('getIdentifierValues') |
376
|
|
|
->willReturn([$entity->getAssociatedEntity()->getPlainField()]); |
377
|
|
|
$meta->expects($this->any()) |
378
|
|
|
->method('getTypeOfField') |
379
|
|
|
->willReturn(null); |
380
|
|
|
|
381
|
|
|
$mf = $this->createMock(ClassMetadataFactory::class); |
382
|
|
|
$mf->expects($this->any()) |
383
|
|
|
->method('getMetadataFor') |
384
|
|
|
->willReturn($meta); |
385
|
|
|
|
386
|
|
|
$platform = $this->createMock(PostgreSqlPlatform::class); |
387
|
|
|
|
388
|
|
|
$conn = $this->createMock(Connection::class); |
389
|
|
|
$conn->expects($this->any()) |
390
|
|
|
->method('getDatabasePlatform') |
391
|
|
|
->willReturn($platform); |
392
|
|
|
|
393
|
|
|
$em = $this->createMock(EntityManager::class); |
394
|
|
|
$em->expects($this->any()) |
395
|
|
|
->method('getMetadataFactory') |
396
|
|
|
->willReturn($mf); |
397
|
|
|
$em->expects($this->any()) |
398
|
|
|
->method('getConnection') |
399
|
|
|
->willReturn($conn); |
400
|
|
|
|
401
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
402
|
|
|
$registry->expects($this->any()) |
403
|
|
|
->method('getManagerForClass') |
404
|
|
|
->willReturn($em); |
405
|
|
|
|
406
|
|
|
$manager = new ModelManager($registry); |
407
|
|
|
$result = $manager->getIdentifierValues($entity); |
408
|
|
|
|
409
|
|
|
$this->assertSame(42, $result[0]); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* [sortBy, sortOrder, isAddOrderBy]. |
414
|
|
|
* |
415
|
|
|
* @return array |
416
|
|
|
*/ |
417
|
|
|
public function getSortableInDataSourceIteratorDataProvider() |
418
|
|
|
{ |
419
|
|
|
return [ |
420
|
|
|
[null, null, false], |
421
|
|
|
['', 'ASC', false], |
422
|
|
|
['field', 'ASC', true], |
423
|
|
|
['field', null, true], |
424
|
|
|
]; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @dataProvider getSortableInDataSourceIteratorDataProvider |
429
|
|
|
* |
430
|
|
|
* @param string|null $sortBy |
431
|
|
|
* @param string|null $sortOrder |
432
|
|
|
* @param bool $isAddOrderBy |
433
|
|
|
*/ |
434
|
|
|
public function testSortableInDataSourceIterator($sortBy, $sortOrder, $isAddOrderBy) |
435
|
|
|
{ |
436
|
|
|
$datagrid = $this->getMockForAbstractClass(DatagridInterface::class); |
437
|
|
|
$configuration = $this->getMockBuilder(Configuration::class)->getMock(); |
438
|
|
|
$configuration->expects($this->any()) |
439
|
|
|
->method('getDefaultQueryHints') |
440
|
|
|
->willReturn([]); |
441
|
|
|
|
442
|
|
|
$em = $this->getMockBuilder(EntityManager::class) |
443
|
|
|
->disableOriginalConstructor() |
444
|
|
|
->getMock(); |
445
|
|
|
|
446
|
|
|
$em->expects($this->any()) |
447
|
|
|
->method('getConfiguration') |
448
|
|
|
->willReturn($configuration); |
449
|
|
|
|
450
|
|
|
$queryBuilder = $this->getMockBuilder(QueryBuilder::class) |
451
|
|
|
->setConstructorArgs([$em]) |
452
|
|
|
->getMock(); |
453
|
|
|
$query = new Query($em); |
454
|
|
|
|
455
|
|
|
$proxyQuery = $this->getMockBuilder(ProxyQuery::class) |
456
|
|
|
->setConstructorArgs([$queryBuilder]) |
457
|
|
|
->setMethods(['getSortBy', 'getSortOrder', 'getRootAliases']) |
458
|
|
|
->getMock(); |
459
|
|
|
|
460
|
|
|
$proxyQuery->expects($this->any()) |
461
|
|
|
->method('getSortOrder') |
462
|
|
|
->willReturn($sortOrder); |
463
|
|
|
|
464
|
|
|
$proxyQuery->expects($this->any()) |
465
|
|
|
->method('getSortBy') |
466
|
|
|
->willReturn($sortBy); |
467
|
|
|
|
468
|
|
|
$queryBuilder->expects($isAddOrderBy ? $this->atLeastOnce() : $this->never()) |
469
|
|
|
->method('addOrderBy'); |
470
|
|
|
|
471
|
|
|
$proxyQuery->expects($this->any()) |
472
|
|
|
->method('getRootAliases') |
473
|
|
|
->willReturn(['a']); |
474
|
|
|
|
475
|
|
|
$queryBuilder->expects($this->any()) |
476
|
|
|
->method('getQuery') |
477
|
|
|
->willReturn($query); |
478
|
|
|
|
479
|
|
|
$datagrid->expects($this->any()) |
480
|
|
|
->method('getQuery') |
481
|
|
|
->willReturn($proxyQuery); |
482
|
|
|
|
483
|
|
|
$registry = $this->getMockBuilder(RegistryInterface::class)->getMock(); |
484
|
|
|
$manager = new ModelManager($registry); |
485
|
|
|
$manager->getDataSourceIterator($datagrid, []); |
486
|
|
|
|
487
|
|
|
if ($isAddOrderBy) { |
488
|
|
|
$this->assertArrayHasKey($key = 'doctrine.customTreeWalkers', $hints = $query->getHints()); |
489
|
|
|
$this->assertContains(OrderByToSelectWalker::class, $hints[$key]); |
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
public function testModelReverseTransform() |
494
|
|
|
{ |
495
|
|
|
$class = SimpleEntity::class; |
496
|
|
|
|
497
|
|
|
$metadataFactory = $this->createMock(ClassMetadataFactory::class); |
498
|
|
|
$modelManager = $this->createMock(ObjectManager::class); |
499
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
500
|
|
|
|
501
|
|
|
$classMetadata = new ClassMetadata($class); |
502
|
|
|
$classMetadata->reflClass = new \ReflectionClass($class); |
503
|
|
|
|
504
|
|
|
$modelManager->expects($this->once()) |
505
|
|
|
->method('getMetadataFactory') |
506
|
|
|
->willReturn($metadataFactory); |
507
|
|
|
$metadataFactory->expects($this->once()) |
508
|
|
|
->method('getMetadataFor') |
509
|
|
|
->with($class) |
510
|
|
|
->willReturn($classMetadata); |
511
|
|
|
$registry->expects($this->once()) |
512
|
|
|
->method('getManagerForClass') |
513
|
|
|
->with($class) |
514
|
|
|
->willReturn($modelManager); |
515
|
|
|
|
516
|
|
|
$manager = new ModelManager($registry); |
517
|
|
|
$this->assertInstanceOf($class, $object = $manager->modelReverseTransform( |
518
|
|
|
$class, |
519
|
|
|
[ |
520
|
|
|
'schmeckles' => 42, |
521
|
|
|
'multi_word_property' => 'hello', |
522
|
|
|
] |
523
|
|
|
)); |
524
|
|
|
$this->assertSame(42, $object->getSchmeckles()); |
525
|
|
|
$this->assertSame('hello', $object->getMultiWordProperty()); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
public function testCollections() |
529
|
|
|
{ |
530
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
531
|
|
|
$model = new ModelManager($registry); |
532
|
|
|
|
533
|
|
|
$collection = $model->getModelCollectionInstance('whyDoWeEvenHaveThisParameter'); |
534
|
|
|
$this->assertInstanceOf(ArrayCollection::class, $collection); |
535
|
|
|
|
536
|
|
|
$item1 = 'item1'; |
537
|
|
|
$item2 = 'item2'; |
538
|
|
|
$model->collectionAddElement($collection, $item1); |
539
|
|
|
$model->collectionAddElement($collection, $item2); |
540
|
|
|
|
541
|
|
|
$this->assertTrue($model->collectionHasElement($collection, $item1)); |
542
|
|
|
|
543
|
|
|
$model->collectionRemoveElement($collection, $item1); |
544
|
|
|
|
545
|
|
|
$this->assertFalse($model->collectionHasElement($collection, $item1)); |
546
|
|
|
|
547
|
|
|
$model->collectionClear($collection); |
548
|
|
|
|
549
|
|
|
$this->assertTrue($collection->isEmpty()); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
public function testModelTransform() |
553
|
|
|
{ |
554
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
555
|
|
|
$model = new ModelManager($registry); |
556
|
|
|
|
557
|
|
|
$result = $model->modelTransform('thisIsNotUsed', 'doWeNeedThisMethod'); |
|
|
|
|
558
|
|
|
|
559
|
|
|
$this->assertSame('doWeNeedThisMethod', $result); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
public function testGetPaginationParameters() |
563
|
|
|
{ |
564
|
|
|
$datagrid = $this->createMock(DatagridInterface::class); |
565
|
|
|
$filter = $this->createMock(FilterInterface::class); |
566
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
567
|
|
|
|
568
|
|
|
$datagrid->expects($this->once()) |
569
|
|
|
->method('getValues') |
570
|
|
|
->willReturn(['_sort_by' => $filter]); |
571
|
|
|
|
572
|
|
|
$filter->expects($this->once()) |
573
|
|
|
->method('getName') |
574
|
|
|
->willReturn($name = 'test'); |
575
|
|
|
|
576
|
|
|
$model = new ModelManager($registry); |
577
|
|
|
|
578
|
|
|
$result = $model->getPaginationParameters($datagrid, $page = 5); |
579
|
|
|
|
580
|
|
|
$this->assertSame($page, $result['filter']['_page']); |
581
|
|
|
$this->assertSame($name, $result['filter']['_sort_by']); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
public function testGetModelInstanceException() |
585
|
|
|
{ |
586
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
587
|
|
|
|
588
|
|
|
$model = new ModelManager($registry); |
589
|
|
|
|
590
|
|
|
$this->expectException(\RuntimeException::class); |
591
|
|
|
|
592
|
|
|
$model->getModelInstance(AbstractEntity::class); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
public function testGetEntityManagerException() |
596
|
|
|
{ |
597
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
598
|
|
|
|
599
|
|
|
$model = new ModelManager($registry); |
600
|
|
|
|
601
|
|
|
$this->expectException(\RuntimeException::class); |
602
|
|
|
|
603
|
|
|
$model->getEntityManager(VersionedEntity::class); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
public function testGetNewFieldDescriptionInstanceException() |
607
|
|
|
{ |
608
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
609
|
|
|
|
610
|
|
|
$model = new ModelManager($registry); |
611
|
|
|
|
612
|
|
|
$this->expectException(\RuntimeException::class); |
613
|
|
|
|
614
|
|
|
$model->getNewFieldDescriptionInstance(VersionedEntity::class, [], []); |
|
|
|
|
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* @dataProvider createUpdateRemoveData |
619
|
|
|
*/ |
620
|
|
|
public function testCreate($exception) |
621
|
|
|
{ |
622
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
623
|
|
|
|
624
|
|
|
$entityManger = $this->createMock(EntityManager::class); |
625
|
|
|
|
626
|
|
|
$registry->expects($this->once()) |
627
|
|
|
->method('getManagerForClass') |
628
|
|
|
->willReturn($entityManger); |
629
|
|
|
|
630
|
|
|
$entityManger->expects($this->once()) |
631
|
|
|
->method('persist'); |
632
|
|
|
|
633
|
|
|
$entityManger->expects($this->once()) |
634
|
|
|
->method('flush') |
635
|
|
|
->willThrowException($exception); |
636
|
|
|
|
637
|
|
|
$model = new ModelManager($registry); |
638
|
|
|
|
639
|
|
|
$this->expectException(ModelManagerException::class); |
640
|
|
|
|
641
|
|
|
$model->create(new VersionedEntity()); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
public function createUpdateRemoveData() |
645
|
|
|
{ |
646
|
|
|
return [ |
647
|
|
|
'PDOException' => [ |
648
|
|
|
new \PDOException(), |
649
|
|
|
], |
650
|
|
|
'DBALException' => [ |
651
|
|
|
new DBALException(), |
652
|
|
|
], |
653
|
|
|
]; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* @dataProvider createUpdateRemoveData |
658
|
|
|
*/ |
659
|
|
|
public function testUpdate($exception) |
660
|
|
|
{ |
661
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
662
|
|
|
|
663
|
|
|
$entityManger = $this->createMock(EntityManager::class); |
664
|
|
|
|
665
|
|
|
$registry->expects($this->once()) |
666
|
|
|
->method('getManagerForClass') |
667
|
|
|
->willReturn($entityManger); |
668
|
|
|
|
669
|
|
|
$entityManger->expects($this->once()) |
670
|
|
|
->method('persist'); |
671
|
|
|
|
672
|
|
|
$entityManger->expects($this->once()) |
673
|
|
|
->method('flush') |
674
|
|
|
->willThrowException($exception); |
675
|
|
|
|
676
|
|
|
$model = new ModelManager($registry); |
677
|
|
|
|
678
|
|
|
$this->expectException(ModelManagerException::class); |
679
|
|
|
|
680
|
|
|
$model->update(new VersionedEntity()); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* @dataProvider createUpdateRemoveData |
685
|
|
|
*/ |
686
|
|
|
public function testRemove($exception) |
687
|
|
|
{ |
688
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
689
|
|
|
|
690
|
|
|
$entityManger = $this->createMock(EntityManager::class); |
691
|
|
|
|
692
|
|
|
$registry->expects($this->once()) |
693
|
|
|
->method('getManagerForClass') |
694
|
|
|
->willReturn($entityManger); |
695
|
|
|
|
696
|
|
|
$entityManger->expects($this->once()) |
697
|
|
|
->method('remove'); |
698
|
|
|
|
699
|
|
|
$entityManger->expects($this->once()) |
700
|
|
|
->method('flush') |
701
|
|
|
->willThrowException($exception); |
702
|
|
|
|
703
|
|
|
$model = new ModelManager($registry); |
704
|
|
|
|
705
|
|
|
$this->expectException(ModelManagerException::class); |
706
|
|
|
|
707
|
|
|
$model->delete(new VersionedEntity()); |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
public function testFindBadId() |
711
|
|
|
{ |
712
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
713
|
|
|
|
714
|
|
|
$model = new ModelManager($registry); |
715
|
|
|
|
716
|
|
|
$this->assertNull($model->find('notImportant', null)); |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
public function testGetUrlsafeIdentifierException() |
720
|
|
|
{ |
721
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
722
|
|
|
|
723
|
|
|
$model = new ModelManager($registry); |
724
|
|
|
|
725
|
|
|
$this->expectException(\RuntimeException::class); |
726
|
|
|
|
727
|
|
|
$model->getNormalizedIdentifier('test'); |
|
|
|
|
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
public function testGetUrlsafeIdentifierNull() |
731
|
|
|
{ |
732
|
|
|
$registry = $this->createMock(RegistryInterface::class); |
733
|
|
|
|
734
|
|
|
$model = new ModelManager($registry); |
735
|
|
|
|
736
|
|
|
$this->assertNull($model->getNormalizedIdentifier(null)); |
|
|
|
|
737
|
|
|
} |
738
|
|
|
|
739
|
|
|
private function getMetadata($class, $isVersioned) |
740
|
|
|
{ |
741
|
|
|
$metadata = new ClassMetadata($class); |
742
|
|
|
|
743
|
|
|
$metadata->isVersioned = $isVersioned; |
744
|
|
|
|
745
|
|
|
if ($isVersioned) { |
746
|
|
|
$versionField = 'version'; |
747
|
|
|
$metadata->versionField = $versionField; |
748
|
|
|
$metadata->reflFields[$versionField] = new \ReflectionProperty($class, $versionField); |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
return $metadata; |
752
|
|
|
} |
753
|
|
|
} |
754
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.