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