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