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