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\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
|
|
|
public function testGetParentMetadataForProperty() |
165
|
|
|
{ |
166
|
|
|
if (version_compare(Version::VERSION, '2.5') < 0) { |
167
|
|
|
$this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
168
|
|
|
|
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
173
|
|
|
$associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
174
|
|
|
$embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
175
|
|
|
$modelManagerClass = 'Sonata\DoctrineORMAdminBundle\Model\ModelManager'; |
176
|
|
|
|
177
|
|
|
$object = new ContainerEntity(new AssociatedEntity(), new EmbeddedEntity()); |
|
|
|
|
178
|
|
|
|
179
|
|
|
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
180
|
|
|
->disableOriginalConstructor() |
181
|
|
|
->getMock(); |
182
|
|
|
|
183
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ModelManager $modelManager */ |
184
|
|
|
$modelManager = $this->getMockBuilder($modelManagerClass) |
185
|
|
|
->disableOriginalConstructor() |
186
|
|
|
->setMethods(array('getMetadata', 'getEntityManager')) |
187
|
|
|
->getMock(); |
188
|
|
|
|
189
|
|
|
$modelManager->expects($this->any()) |
|
|
|
|
190
|
|
|
->method('getEntityManager') |
191
|
|
|
->will($this->returnValue($em)); |
192
|
|
|
|
193
|
|
|
$containerEntityMetadata = $this->getMetadataForContainerEntity(); |
194
|
|
|
$associatedEntityMetadata = $this->getMetadataForAssociatedEntity(); |
195
|
|
|
$embeddedEntityMetadata = $this->getMetadataForEmbeddedEntity(); |
196
|
|
|
|
197
|
|
|
$modelManager->expects($this->any())->method('getMetadata') |
|
|
|
|
198
|
|
|
->will( |
199
|
|
|
$this->returnValueMap( |
200
|
|
|
array( |
201
|
|
|
array($containerEntityClass, $containerEntityMetadata), |
202
|
|
|
array($embeddedEntityClass, $embeddedEntityMetadata), |
203
|
|
|
array($associatedEntityClass, $associatedEntityMetadata), |
204
|
|
|
) |
205
|
|
|
) |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
/** @var ClassMetadata $metadata */ |
209
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
210
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'plainField'); |
211
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
212
|
|
|
|
213
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
214
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.plainField'); |
215
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
216
|
|
|
|
217
|
|
|
list($metadata, $lastPropertyName) = $modelManager |
218
|
|
|
->getParentMetadataForProperty($containerEntityClass, 'embeddedEntity.plainField'); |
219
|
|
|
$this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getMetadataForEmbeddedEntity() |
223
|
|
|
{ |
224
|
|
|
$metadata = new ClassMetadata('Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'); |
225
|
|
|
|
226
|
|
|
$metadata->fieldMappings = array( |
227
|
|
|
'plainField' => array( |
228
|
|
|
'fieldName' => 'plainField', |
229
|
|
|
'columnName' => 'plainField', |
230
|
|
|
'type' => 'boolean', |
231
|
|
|
), |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
return $metadata; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function getMetadataForAssociatedEntity() |
238
|
|
|
{ |
239
|
|
|
$metadata = new ClassMetadata('Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'); |
240
|
|
|
|
241
|
|
|
$metadata->fieldMappings = array( |
242
|
|
|
'plainField' => array( |
243
|
|
|
'fieldName' => 'plainField', |
244
|
|
|
'columnName' => 'plainField', |
245
|
|
|
'type' => 'string', |
246
|
|
|
), |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
return $metadata; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function getMetadataForContainerEntity() |
253
|
|
|
{ |
254
|
|
|
$containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
255
|
|
|
$associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
256
|
|
|
$embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
257
|
|
|
|
258
|
|
|
$metadata = new ClassMetadata($containerEntityClass); |
259
|
|
|
|
260
|
|
|
$metadata->fieldMappings = array( |
261
|
|
|
'plainField' => array( |
262
|
|
|
'fieldName' => 'plainField', |
263
|
|
|
'columnName' => 'plainField', |
264
|
|
|
'type' => 'integer', |
265
|
|
|
), |
266
|
|
|
); |
267
|
|
|
|
268
|
|
|
$metadata->associationMappings['associatedEntity'] = array( |
269
|
|
|
'fieldName' => 'associatedEntity', |
270
|
|
|
'targetEntity' => $associatedEntityClass, |
271
|
|
|
'sourceEntity' => $containerEntityClass, |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
$metadata->embeddedClasses['embeddedEntity'] = array( |
275
|
|
|
'class' => $embeddedEntityClass, |
276
|
|
|
'columnPrefix' => 'embeddedEntity', |
277
|
|
|
); |
278
|
|
|
|
279
|
|
|
$metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
280
|
|
|
|
281
|
|
|
return $metadata; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
private function getMetadata($class, $isVersioned) |
285
|
|
|
{ |
286
|
|
|
$metadata = new ClassMetadata($class); |
287
|
|
|
|
288
|
|
|
$metadata->isVersioned = $isVersioned; |
289
|
|
|
|
290
|
|
|
if ($isVersioned) { |
291
|
|
|
$versionField = 'version'; |
292
|
|
|
$metadata->versionField = $versionField; |
293
|
|
|
$metadata->reflFields[$versionField] = new \ReflectionProperty($class, $versionField); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $metadata; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
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.