1 | <?php |
||
28 | class ModelManagerTest extends \PHPUnit_Framework_TestCase |
||
29 | { |
||
30 | public static function setUpBeforeClass() |
||
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() |
||
100 | |||
101 | /** |
||
102 | * @dataProvider getVersionDataProvider |
||
103 | */ |
||
104 | public function testGetVersion($isVersioned) |
||
127 | |||
128 | public function lockDataProvider() |
||
136 | |||
137 | /** |
||
138 | * @dataProvider lockDataProvider |
||
139 | */ |
||
140 | public function testLock($isVersioned, $expectsException) |
||
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() |
||
350 | |||
351 | private function getMetadata($class, $isVersioned) |
||
352 | { |
||
353 | $metadata = new ClassMetadata($class); |
||
354 | |||
355 | $metadata->isVersioned = $isVersioned; |
||
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.