1 | <?php |
||
30 | class ModelManagerTest extends PHPUnit_Framework_TestCase |
||
31 | { |
||
32 | public static function setUpBeforeClass() |
||
33 | { |
||
34 | if (!Type::hasType('uuid')) { |
||
35 | Type::addType('uuid', 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType'); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | public function testSortParameters() |
||
40 | { |
||
41 | $registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
||
42 | |||
43 | $manager = new ModelManager($registry); |
||
44 | |||
45 | $datagrid1 = $this->createMock('Sonata\AdminBundle\Datagrid\Datagrid'); |
||
46 | $datagrid2 = $this->createMock('Sonata\AdminBundle\Datagrid\Datagrid'); |
||
47 | |||
48 | $field1 = new FieldDescription(); |
||
49 | $field1->setName('field1'); |
||
50 | |||
51 | $field2 = new FieldDescription(); |
||
52 | $field2->setName('field2'); |
||
53 | |||
54 | $field3 = new FieldDescription(); |
||
55 | $field3->setName('field3'); |
||
56 | $field3->setOption('sortable', 'field3sortBy'); |
||
57 | |||
58 | $datagrid1 |
||
59 | ->expects($this->any()) |
||
60 | ->method('getValues') |
||
61 | ->will($this->returnValue(array( |
||
62 | '_sort_by' => $field1, |
||
63 | '_sort_order' => 'ASC', |
||
64 | ))); |
||
65 | |||
66 | $datagrid2 |
||
67 | ->expects($this->any()) |
||
68 | ->method('getValues') |
||
69 | ->will($this->returnValue(array( |
||
70 | '_sort_by' => $field3, |
||
71 | '_sort_order' => 'ASC', |
||
72 | ))); |
||
73 | |||
74 | $parameters = $manager->getSortParameters($field1, $datagrid1); |
||
75 | |||
76 | $this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
||
77 | $this->assertEquals('field1', $parameters['filter']['_sort_by']); |
||
78 | |||
79 | $parameters = $manager->getSortParameters($field2, $datagrid1); |
||
80 | |||
81 | $this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
||
82 | $this->assertEquals('field2', $parameters['filter']['_sort_by']); |
||
83 | |||
84 | $parameters = $manager->getSortParameters($field3, $datagrid1); |
||
85 | |||
86 | $this->assertEquals('ASC', $parameters['filter']['_sort_order']); |
||
87 | $this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
||
88 | |||
89 | $parameters = $manager->getSortParameters($field3, $datagrid2); |
||
90 | |||
91 | $this->assertEquals('DESC', $parameters['filter']['_sort_order']); |
||
92 | $this->assertEquals('field3sortBy', $parameters['filter']['_sort_by']); |
||
93 | } |
||
94 | |||
95 | public function getVersionDataProvider() |
||
96 | { |
||
97 | return array( |
||
98 | array(true), |
||
99 | array(false), |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @dataProvider getVersionDataProvider |
||
105 | */ |
||
106 | public function testGetVersion($isVersioned) |
||
107 | { |
||
108 | $object = new VersionedEntity(); |
||
109 | |||
110 | $modelManager = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Model\ModelManager') |
||
111 | ->disableOriginalConstructor() |
||
112 | ->setMethods(array('getMetadata')) |
||
113 | ->getMock(); |
||
114 | |||
115 | $metadata = $this->getMetadata(get_class($object), $isVersioned); |
||
116 | |||
117 | $modelManager->expects($this->any()) |
||
118 | ->method('getMetadata') |
||
119 | ->will($this->returnValue($metadata)); |
||
120 | |||
121 | if ($isVersioned) { |
||
122 | $object->version = 123; |
||
123 | |||
124 | $this->assertNotNull($modelManager->getLockVersion($object)); |
||
125 | } else { |
||
126 | $this->assertNull($modelManager->getLockVersion($object)); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public function lockDataProvider() |
||
131 | { |
||
132 | return array( |
||
133 | array(true, false), |
||
134 | array(true, true), |
||
135 | array(false, false), |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @dataProvider lockDataProvider |
||
141 | */ |
||
142 | public function testLock($isVersioned, $expectsException) |
||
143 | { |
||
144 | $object = new VersionedEntity(); |
||
145 | |||
146 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
||
147 | ->disableOriginalConstructor() |
||
148 | ->setMethods(array('lock')) |
||
149 | ->getMock(); |
||
150 | |||
151 | $modelManager = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Model\ModelManager') |
||
152 | ->disableOriginalConstructor() |
||
153 | ->setMethods(array('getMetadata', 'getEntityManager')) |
||
154 | ->getMock(); |
||
155 | |||
156 | $modelManager->expects($this->any()) |
||
157 | ->method('getEntityManager') |
||
158 | ->will($this->returnValue($em)); |
||
159 | |||
160 | $metadata = $this->getMetadata(get_class($object), $isVersioned); |
||
161 | |||
162 | $modelManager->expects($this->any()) |
||
163 | ->method('getMetadata') |
||
164 | ->will($this->returnValue($metadata)); |
||
165 | |||
166 | if ($expectsException) { |
||
167 | $em->expects($this->once()) |
||
168 | ->method('lock') |
||
169 | ->will($this->throwException(OptimisticLockException::lockFailed($object))); |
||
170 | |||
171 | $this->setExpectedException('Sonata\AdminBundle\Exception\LockException'); |
||
172 | } |
||
173 | |||
174 | $modelManager->lock($object, 123); |
||
175 | } |
||
176 | |||
177 | public function testGetParentMetadataForProperty() |
||
178 | { |
||
179 | if (version_compare(Version::VERSION, '2.5') < 0) { |
||
180 | $this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
||
181 | |||
182 | return; |
||
183 | } |
||
184 | |||
185 | $containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
||
186 | $associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
||
187 | $embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
||
188 | $modelManagerClass = 'Sonata\DoctrineORMAdminBundle\Model\ModelManager'; |
||
189 | |||
190 | $object = new ContainerEntity(new AssociatedEntity(), new EmbeddedEntity()); |
||
|
|||
191 | |||
192 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
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() |
||
247 | |||
248 | public function getMetadataForAssociatedEntity() |
||
262 | |||
263 | public function getMetadataForContainerEntity() |
||
294 | |||
295 | public function testNonIntegerIdentifierType() |
||
296 | { |
||
297 | $uuid = new NonIntegerIdentifierTestClass('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
||
298 | $entity = new UuidEntity($uuid); |
||
299 | |||
300 | $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo'); |
||
301 | $meta->expects($this->any()) |
||
338 | |||
339 | public function testAssociationIdentifierType() |
||
381 | |||
382 | /** |
||
383 | * [sortBy, sortOrder, isAddOrderBy]. |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | public function getSortableInDataSourceIteratorDataProvider() |
||
396 | |||
397 | /** |
||
398 | * @dataProvider getSortableInDataSourceIteratorDataProvider |
||
399 | * |
||
400 | * @param string|null $sortBy |
||
401 | * @param string|null $sortOrder |
||
402 | * @param bool $isAddOrderBy |
||
403 | */ |
||
404 | public function testSortableInDataSourceIterator($sortBy, $sortOrder, $isAddOrderBy) |
||
453 | |||
454 | private function getMetadata($class, $isVersioned) |
||
468 | } |
||
469 |
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.