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\DBAL\Types\Type; |
15
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
16
|
|
|
use Doctrine\ORM\OptimisticLockException; |
17
|
|
|
use Doctrine\ORM\Version; |
18
|
|
|
use Sonata\DoctrineORMAdminBundle\Admin\FieldDescription; |
19
|
|
|
use Sonata\DoctrineORMAdminBundle\Model\ModelManager; |
20
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType; |
21
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity; |
22
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity; |
23
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity; |
24
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\UuidEntity; |
25
|
|
|
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\VersionedEntity; |
26
|
|
|
|
27
|
|
|
class ModelManagerTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
public static function setUpBeforeClass() |
30
|
|
|
{ |
31
|
|
|
if (!Type::hasType('uuid')) { |
32
|
|
|
Type::addType('uuid', 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType'); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testSortParameters() |
37
|
|
|
{ |
38
|
|
|
$registry = $this->getMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
39
|
|
|
|
40
|
|
|
$manager = new ModelManager($registry); |
41
|
|
|
|
42
|
|
|
$datagrid1 = $this->getMockBuilder('\Sonata\AdminBundle\Datagrid\Datagrid')->disableOriginalConstructor()->getMock(); |
43
|
|
|
$datagrid2 = $this->getMockBuilder('\Sonata\AdminBundle\Datagrid\Datagrid')->disableOriginalConstructor()->getMock(); |
44
|
|
|
|
45
|
|
|
$field1 = new FieldDescription(); |
46
|
|
|
$field1->setName('field1'); |
47
|
|
|
|
48
|
|
|
$field2 = new FieldDescription(); |
49
|
|
|
$field2->setName('field2'); |
50
|
|
|
|
51
|
|
|
$field3 = new FieldDescription(); |
52
|
|
|
$field3->setName('field3'); |
53
|
|
|
$field3->setOption('sortable', 'field3sortBy'); |
54
|
|
|
|
55
|
|
|
$datagrid1 |
56
|
|
|
->expects($this->any()) |
57
|
|
|
->method('getValues') |
58
|
|
|
->will($this->returnValue(array( |
59
|
|
|
'_sort_by' => $field1, |
60
|
|
|
'_sort_order' => 'ASC', |
61
|
|
|
))); |
62
|
|
|
|
63
|
|
|
$datagrid2 |
64
|
|
|
->expects($this->any()) |
65
|
|
|
->method('getValues') |
66
|
|
|
->will($this->returnValue(array( |
67
|
|
|
'_sort_by' => $field3, |
68
|
|
|
'_sort_order' => 'ASC', |
69
|
|
|
))); |
70
|
|
|
|
71
|
|
|
$parameters = $manager->getSortParameters($field1, $datagrid1); |
72
|
|
|
|
73
|
|
|
$this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
74
|
|
|
$this->assertEquals('field1', $parameters['filter']['_sort_by']); |
75
|
|
|
|
76
|
|
|
$parameters = $manager->getSortParameters($field2, $datagrid1); |
77
|
|
|
|
78
|
|
|
$this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
79
|
|
|
$this->assertEquals('field2', $parameters['filter']['_sort_by']); |
80
|
|
|
|
81
|
|
|
$parameters = $manager->getSortParameters($field3, $datagrid1); |
82
|
|
|
|
83
|
|
|
$this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
84
|
|
|
$this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
85
|
|
|
|
86
|
|
|
$parameters = $manager->getSortParameters($field3, $datagrid2); |
87
|
|
|
|
88
|
|
|
$this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
89
|
|
|
$this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getVersionDataProvider() |
93
|
|
|
{ |
94
|
|
|
return array( |
95
|
|
|
array(true), |
96
|
|
|
array(false), |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @dataProvider getVersionDataProvider |
102
|
|
|
*/ |
103
|
|
|
public function testGetVersion($isVersioned) |
104
|
|
|
{ |
105
|
|
|
$object = new VersionedEntity(); |
106
|
|
|
|
107
|
|
|
$modelManager = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Model\ModelManager') |
108
|
|
|
->disableOriginalConstructor() |
109
|
|
|
->setMethods(array('getMetadata')) |
110
|
|
|
->getMock(); |
111
|
|
|
|
112
|
|
|
$metadata = $this->getMetadata(get_class($object), $isVersioned); |
113
|
|
|
|
114
|
|
|
$modelManager->expects($this->any()) |
115
|
|
|
->method('getMetadata') |
116
|
|
|
->will($this->returnValue($metadata)); |
117
|
|
|
|
118
|
|
|
if ($isVersioned) { |
119
|
|
|
$object->version = 123; |
120
|
|
|
|
121
|
|
|
$this->assertNotNull($modelManager->getLockVersion($object)); |
122
|
|
|
} else { |
123
|
|
|
$this->assertNull($modelManager->getLockVersion($object)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function lockDataProvider() |
128
|
|
|
{ |
129
|
|
|
return array( |
130
|
|
|
array(true, false), |
131
|
|
|
array(true, true), |
132
|
|
|
array(false, false), |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @dataProvider lockDataProvider |
138
|
|
|
*/ |
139
|
|
|
public function testLock($isVersioned, $expectsException) |
140
|
|
|
{ |
141
|
|
|
$object = new VersionedEntity(); |
142
|
|
|
|
143
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
144
|
|
|
->disableOriginalConstructor() |
145
|
|
|
->setMethods(array('lock')) |
146
|
|
|
->getMock(); |
147
|
|
|
|
148
|
|
|
$modelManager = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Model\ModelManager') |
149
|
|
|
->disableOriginalConstructor() |
150
|
|
|
->setMethods(array('getMetadata', 'getEntityManager')) |
151
|
|
|
->getMock(); |
152
|
|
|
|
153
|
|
|
$modelManager->expects($this->any()) |
154
|
|
|
->method('getEntityManager') |
155
|
|
|
->will($this->returnValue($em)); |
156
|
|
|
|
157
|
|
|
$metadata = $this->getMetadata(get_class($object), $isVersioned); |
158
|
|
|
|
159
|
|
|
$modelManager->expects($this->any()) |
160
|
|
|
->method('getMetadata') |
161
|
|
|
->will($this->returnValue($metadata)); |
162
|
|
|
|
163
|
|
|
if ($expectsException) { |
164
|
|
|
$em->expects($this->once()) |
165
|
|
|
->method('lock') |
166
|
|
|
->will($this->throwException(OptimisticLockException::lockFailed($object))); |
167
|
|
|
|
168
|
|
|
$this->setExpectedException('Sonata\AdminBundle\Exception\LockException'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$modelManager->lock($object, 123); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testGetParentMetadataForProperty() |
175
|
|
|
{ |
176
|
|
|
if (version_compare(Version::VERSION, '2.5') < 0) { |
177
|
|
|
$this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
178
|
|
|
|
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
183
|
|
|
$associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
184
|
|
|
$embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
185
|
|
|
$modelManagerClass = 'Sonata\DoctrineORMAdminBundle\Model\ModelManager'; |
186
|
|
|
|
187
|
|
|
$object = new ContainerEntity(new AssociatedEntity(), new EmbeddedEntity()); |
|
|
|
|
188
|
|
|
|
189
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
190
|
|
|
->disableOriginalConstructor() |
191
|
|
|
->getMock(); |
192
|
|
|
|
193
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ModelManager $modelManager */ |
194
|
|
|
$modelManager = $this->getMockBuilder($modelManagerClass) |
195
|
|
|
->disableOriginalConstructor() |
196
|
|
|
->setMethods(array('getMetadata', 'getEntityManager')) |
197
|
|
|
->getMock(); |
198
|
|
|
|
199
|
|
|
$modelManager->expects($this->any()) |
|
|
|
|
200
|
|
|
->method('getEntityManager') |
201
|
|
|
->will($this->returnValue($em)); |
202
|
|
|
|
203
|
|
|
$containerEntityMetadata = $this->getMetadataForContainerEntity(); |
204
|
|
|
$associatedEntityMetadata = $this->getMetadataForAssociatedEntity(); |
205
|
|
|
$embeddedEntityMetadata = $this->getMetadataForEmbeddedEntity(); |
206
|
|
|
|
207
|
|
|
$modelManager->expects($this->any())->method('getMetadata') |
|
|
|
|
208
|
|
|
->will( |
209
|
|
|
$this->returnValueMap( |
210
|
|
|
array( |
211
|
|
|
array($containerEntityClass, $containerEntityMetadata), |
212
|
|
|
array($embeddedEntityClass, $embeddedEntityMetadata), |
213
|
|
|
array($associatedEntityClass, $associatedEntityMetadata), |
214
|
|
|
) |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
/** @var ClassMetadata $metadata */ |
219
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
220
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'plainField'); |
221
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
222
|
|
|
|
223
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
224
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.plainField'); |
225
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
226
|
|
|
|
227
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
228
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'embeddedEntity.plainField'); |
229
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function getMetadataForEmbeddedEntity() |
233
|
|
|
{ |
234
|
|
|
$metadata = new ClassMetadata('Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'); |
235
|
|
|
|
236
|
|
|
$metadata->fieldMappings = array( |
237
|
|
|
'plainField' => array( |
238
|
|
|
'fieldName' => 'plainField', |
239
|
|
|
'columnName' => 'plainField', |
240
|
|
|
'type' => 'boolean', |
241
|
|
|
), |
242
|
|
|
); |
243
|
|
|
|
244
|
|
|
return $metadata; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function getMetadataForAssociatedEntity() |
248
|
|
|
{ |
249
|
|
|
$metadata = new ClassMetadata('Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'); |
250
|
|
|
|
251
|
|
|
$metadata->fieldMappings = array( |
252
|
|
|
'plainField' => array( |
253
|
|
|
'fieldName' => 'plainField', |
254
|
|
|
'columnName' => 'plainField', |
255
|
|
|
'type' => 'string', |
256
|
|
|
), |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
return $metadata; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function getMetadataForContainerEntity() |
263
|
|
|
{ |
264
|
|
|
$containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
265
|
|
|
$associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
266
|
|
|
$embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
267
|
|
|
|
268
|
|
|
$metadata = new ClassMetadata($containerEntityClass); |
269
|
|
|
|
270
|
|
|
$metadata->fieldMappings = array( |
271
|
|
|
'plainField' => array( |
272
|
|
|
'fieldName' => 'plainField', |
273
|
|
|
'columnName' => 'plainField', |
274
|
|
|
'type' => 'integer', |
275
|
|
|
), |
276
|
|
|
); |
277
|
|
|
|
278
|
|
|
$metadata->associationMappings['associatedEntity'] = array( |
279
|
|
|
'fieldName' => 'associatedEntity', |
280
|
|
|
'targetEntity' => $associatedEntityClass, |
281
|
|
|
'sourceEntity' => $containerEntityClass, |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
$metadata->embeddedClasses['embeddedEntity'] = array( |
285
|
|
|
'class' => $embeddedEntityClass, |
286
|
|
|
'columnPrefix' => 'embeddedEntity', |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
$metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
290
|
|
|
|
291
|
|
|
return $metadata; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function testNonIntegerIdentifierType() |
295
|
|
|
{ |
296
|
|
|
$uuid = new Uuid('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
297
|
|
|
$entity = new UuidEntity($uuid); |
298
|
|
|
|
299
|
|
|
$meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo') |
300
|
|
|
->disableOriginalConstructor() |
301
|
|
|
->getMock(); |
302
|
|
|
$meta->expects($this->any()) |
303
|
|
|
->method('getIdentifierValues') |
304
|
|
|
->willReturn(array($entity->getId())); |
305
|
|
|
$meta->expects($this->any()) |
306
|
|
|
->method('getTypeOfField') |
307
|
|
|
->willReturn(UuidType::NAME); |
308
|
|
|
|
309
|
|
|
$mf = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataFactory') |
310
|
|
|
->disableOriginalConstructor() |
311
|
|
|
->getMock(); |
312
|
|
|
$mf->expects($this->any()) |
313
|
|
|
->method('getMetadataFor') |
314
|
|
|
->willReturn($meta); |
315
|
|
|
|
316
|
|
|
$platform = $this->getMockBuilder('Doctrine\DBAL\Platforms\PostgreSqlPlatform') |
317
|
|
|
->disableOriginalConstructor() |
318
|
|
|
->getMock(); |
319
|
|
|
|
320
|
|
|
$conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
321
|
|
|
->disableOriginalConstructor() |
322
|
|
|
->getMock(); |
323
|
|
|
$conn->expects($this->any()) |
324
|
|
|
->method('getDatabasePlatform') |
325
|
|
|
->willReturn($platform); |
326
|
|
|
|
327
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
328
|
|
|
->disableOriginalConstructor() |
329
|
|
|
->getMock(); |
330
|
|
|
$em->expects($this->any()) |
331
|
|
|
->method('getMetadataFactory') |
332
|
|
|
->willReturn($mf); |
333
|
|
|
$em->expects($this->any()) |
334
|
|
|
->method('getConnection') |
335
|
|
|
->willReturn($conn); |
336
|
|
|
|
337
|
|
|
$registry = $this->getMockBuilder('Symfony\Bridge\Doctrine\RegistryInterface') |
338
|
|
|
->disableOriginalConstructor() |
339
|
|
|
->getMock(); |
340
|
|
|
$registry->expects($this->any()) |
341
|
|
|
->method('getManagerForClass') |
342
|
|
|
->willReturn($em); |
343
|
|
|
|
344
|
|
|
$manager = new ModelManager($registry); |
345
|
|
|
$result = $manager->getIdentifierValues($entity); |
346
|
|
|
|
347
|
|
|
$this->assertEquals($entity->getId()->toString(), $result[0]); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
private function getMetadata($class, $isVersioned) |
351
|
|
|
{ |
352
|
|
|
$metadata = new ClassMetadata($class); |
353
|
|
|
|
354
|
|
|
$metadata->isVersioned = $isVersioned; |
355
|
|
|
|
356
|
|
|
if ($isVersioned) { |
357
|
|
|
$versionField = 'version'; |
358
|
|
|
$metadata->versionField = $versionField; |
359
|
|
|
$metadata->reflFields[$versionField] = new \ReflectionProperty($class, $versionField); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $metadata; |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
class Uuid |
|
|
|
|
367
|
|
|
{ |
368
|
|
|
/** |
369
|
|
|
* @var string |
370
|
|
|
*/ |
371
|
|
|
private $uuid; |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param string $uuid |
375
|
|
|
*/ |
376
|
|
|
public function __construct($uuid) |
377
|
|
|
{ |
378
|
|
|
$this->uuid = $uuid; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @return string |
383
|
|
|
*/ |
384
|
|
|
public function __toString() |
385
|
|
|
{ |
386
|
|
|
return $this->toString(); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @return string |
391
|
|
|
*/ |
392
|
|
|
public function toString() |
393
|
|
|
{ |
394
|
|
|
return $this->uuid; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.