1 | <?php |
||
29 | class ModelManagerTest extends PHPUnit_Framework_TestCase |
||
30 | { |
||
31 | public static function setUpBeforeClass() |
||
37 | |||
38 | public function testSortParameters() |
||
39 | { |
||
40 | $registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
||
41 | |||
42 | $manager = new ModelManager($registry); |
||
43 | |||
44 | $datagrid1 = $this->createMock('Sonata\AdminBundle\Datagrid\Datagrid'); |
||
45 | $datagrid2 = $this->createMock('Sonata\AdminBundle\Datagrid\Datagrid'); |
||
46 | |||
47 | $field1 = new FieldDescription(); |
||
48 | $field1->setName('field1'); |
||
49 | |||
50 | $field2 = new FieldDescription(); |
||
51 | $field2->setName('field2'); |
||
52 | |||
53 | $field3 = new FieldDescription(); |
||
54 | $field3->setName('field3'); |
||
55 | $field3->setOption('sortable', 'field3sortBy'); |
||
56 | |||
57 | $datagrid1 |
||
58 | ->expects($this->any()) |
||
59 | ->method('getValues') |
||
60 | ->will($this->returnValue(array( |
||
61 | '_sort_by' => $field1, |
||
62 | '_sort_order' => 'ASC', |
||
63 | ))); |
||
64 | |||
65 | $datagrid2 |
||
66 | ->expects($this->any()) |
||
67 | ->method('getValues') |
||
68 | ->will($this->returnValue(array( |
||
69 | '_sort_by' => $field3, |
||
70 | '_sort_order' => 'ASC', |
||
71 | ))); |
||
72 | |||
73 | $parameters = $manager->getSortParameters($field1, $datagrid1); |
||
74 | |||
75 | $this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
||
76 | $this->assertEquals('field1', $parameters['filter']['_sort_by']); |
||
77 | |||
78 | $parameters = $manager->getSortParameters($field2, $datagrid1); |
||
79 | |||
80 | $this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
||
81 | $this->assertEquals('field2', $parameters['filter']['_sort_by']); |
||
82 | |||
83 | $parameters = $manager->getSortParameters($field3, $datagrid1); |
||
84 | |||
85 | $this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
||
86 | $this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
||
87 | |||
88 | $parameters = $manager->getSortParameters($field3, $datagrid2); |
||
89 | |||
90 | $this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
||
91 | $this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
||
92 | } |
||
93 | |||
94 | public function getVersionDataProvider() |
||
101 | |||
102 | /** |
||
103 | * @dataProvider getVersionDataProvider |
||
104 | */ |
||
105 | public function testGetVersion($isVersioned) |
||
128 | |||
129 | public function lockDataProvider() |
||
137 | |||
138 | /** |
||
139 | * @dataProvider lockDataProvider |
||
140 | */ |
||
141 | public function testLock($isVersioned, $expectsException) |
||
175 | |||
176 | public function testGetParentMetadataForProperty() |
||
177 | { |
||
178 | if (version_compare(Version::VERSION, '2.5') < 0) { |
||
179 | $this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
||
180 | |||
181 | return; |
||
182 | } |
||
183 | |||
184 | $containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
||
185 | $associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
||
186 | $embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
||
187 | $modelManagerClass = 'Sonata\DoctrineORMAdminBundle\Model\ModelManager'; |
||
188 | |||
189 | $object = new ContainerEntity(new AssociatedEntity(), new EmbeddedEntity()); |
||
|
|||
190 | |||
191 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
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() |
||
246 | |||
247 | public function getMetadataForAssociatedEntity() |
||
261 | |||
262 | public function getMetadataForContainerEntity() |
||
293 | |||
294 | public function testNonIntegerIdentifierType() |
||
295 | { |
||
296 | $uuid = new NonIntegerIdentifierTestClass('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
||
297 | $entity = new UuidEntity($uuid); |
||
298 | |||
299 | $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo'); |
||
300 | $meta->expects($this->any()) |
||
301 | ->method('getIdentifierValues') |
||
302 | ->willReturn(array($entity->getId())); |
||
303 | $meta->expects($this->any()) |
||
304 | ->method('getTypeOfField') |
||
305 | ->willReturn(UuidType::NAME); |
||
306 | |||
307 | $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory'); |
||
308 | $mf->expects($this->any()) |
||
309 | ->method('getMetadataFor') |
||
310 | ->willReturn($meta); |
||
311 | |||
312 | $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform'); |
||
313 | |||
314 | $conn = $this->createMock('Doctrine\DBAL\Connection'); |
||
315 | $conn->expects($this->any()) |
||
316 | ->method('getDatabasePlatform') |
||
317 | ->willReturn($platform); |
||
318 | |||
319 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
320 | $em->expects($this->any()) |
||
321 | ->method('getMetadataFactory') |
||
322 | ->willReturn($mf); |
||
323 | $em->expects($this->any()) |
||
324 | ->method('getConnection') |
||
325 | ->willReturn($conn); |
||
326 | |||
327 | $registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
||
328 | $registry->expects($this->any()) |
||
329 | ->method('getManagerForClass') |
||
330 | ->willReturn($em); |
||
331 | |||
332 | $manager = new ModelManager($registry); |
||
333 | $result = $manager->getIdentifierValues($entity); |
||
334 | |||
335 | $this->assertEquals($entity->getId()->toString(), $result[0]); |
||
336 | } |
||
337 | |||
338 | public function testAssociationIdentifierType() |
||
339 | { |
||
340 | $entity = new ContainerEntity(new AssociatedEntity(42), new EmbeddedEntity()); |
||
341 | |||
342 | $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo'); |
||
343 | $meta->expects($this->any()) |
||
344 | ->method('getIdentifierValues') |
||
345 | ->willReturn(array($entity->getAssociatedEntity()->getPlainField())); |
||
346 | $meta->expects($this->any()) |
||
347 | ->method('getTypeOfField') |
||
348 | ->willReturn(null); |
||
349 | |||
350 | $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory'); |
||
351 | $mf->expects($this->any()) |
||
352 | ->method('getMetadataFor') |
||
353 | ->willReturn($meta); |
||
354 | |||
355 | $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform'); |
||
356 | |||
357 | $conn = $this->createMock('Doctrine\DBAL\Connection'); |
||
358 | $conn->expects($this->any()) |
||
359 | ->method('getDatabasePlatform') |
||
360 | ->willReturn($platform); |
||
361 | |||
362 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
363 | $em->expects($this->any()) |
||
364 | ->method('getMetadataFactory') |
||
365 | ->willReturn($mf); |
||
366 | $em->expects($this->any()) |
||
367 | ->method('getConnection') |
||
368 | ->willReturn($conn); |
||
369 | |||
370 | $registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
||
371 | $registry->expects($this->any()) |
||
372 | ->method('getManagerForClass') |
||
373 | ->willReturn($em); |
||
374 | |||
375 | $manager = new ModelManager($registry); |
||
376 | $result = $manager->getIdentifierValues($entity); |
||
377 | |||
378 | $this->assertSame(42, $result[0]); |
||
379 | } |
||
380 | |||
381 | private function getMetadata($class, $isVersioned) |
||
395 | } |
||
396 |
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.