1 | <?php |
||
31 | class ModelManagerTest extends TestCase |
||
32 | { |
||
33 | public static function setUpBeforeClass() |
||
39 | |||
40 | public function testSortParameters() |
||
95 | |||
96 | public function getVersionDataProvider() |
||
103 | |||
104 | /** |
||
105 | * @dataProvider getVersionDataProvider |
||
106 | */ |
||
107 | public function testGetVersion($isVersioned) |
||
130 | |||
131 | public function lockDataProvider() |
||
139 | |||
140 | /** |
||
141 | * @dataProvider lockDataProvider |
||
142 | */ |
||
143 | public function testLock($isVersioned, $expectsException) |
||
177 | |||
178 | public function testGetParentMetadataForProperty() |
||
179 | { |
||
180 | if (version_compare(Version::VERSION, '2.5') < 0) { |
||
181 | $this->markTestSkipped('Test for embeddables needs to run on Doctrine >= 2.5'); |
||
182 | |||
183 | return; |
||
184 | } |
||
185 | |||
186 | $containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
||
187 | $associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
||
188 | $embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
||
189 | $modelManagerClass = 'Sonata\DoctrineORMAdminBundle\Model\ModelManager'; |
||
190 | |||
191 | $object = new ContainerEntity(new AssociatedEntity(null, new EmbeddedEntity()), new EmbeddedEntity()); |
||
|
|||
192 | |||
193 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
194 | |||
195 | /** @var \PHPUnit_Framework_MockObject_MockObject|ModelManager $modelManager */ |
||
196 | $modelManager = $this->getMockBuilder($modelManagerClass) |
||
197 | ->disableOriginalConstructor() |
||
198 | ->setMethods(['getMetadata', 'getEntityManager']) |
||
199 | ->getMock(); |
||
200 | |||
201 | $modelManager->expects($this->any()) |
||
202 | ->method('getEntityManager') |
||
203 | ->will($this->returnValue($em)); |
||
204 | |||
205 | $containerEntityMetadata = $this->getMetadataForContainerEntity(); |
||
206 | $associatedEntityMetadata = $this->getMetadataForAssociatedEntity(); |
||
207 | $embeddedEntityMetadata = $this->getMetadataForEmbeddedEntity(); |
||
208 | |||
209 | $modelManager->expects($this->any())->method('getMetadata') |
||
210 | ->will( |
||
211 | $this->returnValueMap( |
||
212 | [ |
||
213 | [$containerEntityClass, $containerEntityMetadata], |
||
214 | [$embeddedEntityClass, $embeddedEntityMetadata], |
||
215 | [$associatedEntityClass, $associatedEntityMetadata], |
||
216 | ] |
||
217 | ) |
||
218 | ); |
||
219 | |||
220 | /** @var ClassMetadata $metadata */ |
||
221 | list($metadata, $lastPropertyName) = $modelManager |
||
222 | ->getParentMetadataForProperty($containerEntityClass, 'plainField'); |
||
223 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'integer'); |
||
224 | |||
225 | list($metadata, $lastPropertyName) = $modelManager |
||
226 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.plainField'); |
||
227 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'string'); |
||
228 | |||
229 | list($metadata, $lastPropertyName) = $modelManager |
||
230 | ->getParentMetadataForProperty($containerEntityClass, 'embeddedEntity.plainField'); |
||
231 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
232 | |||
233 | list($metadata, $lastPropertyName) = $modelManager |
||
234 | ->getParentMetadataForProperty($containerEntityClass, 'associatedEntity.embeddedEntity.plainField'); |
||
235 | $this->assertEquals($metadata->fieldMappings[$lastPropertyName]['type'], 'boolean'); |
||
236 | } |
||
237 | |||
238 | public function getMetadataForEmbeddedEntity() |
||
239 | { |
||
240 | $metadata = new ClassMetadata('Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'); |
||
241 | |||
242 | $metadata->fieldMappings = [ |
||
243 | 'plainField' => [ |
||
244 | 'fieldName' => 'plainField', |
||
245 | 'columnName' => 'plainField', |
||
246 | 'type' => 'boolean', |
||
247 | ], |
||
248 | ]; |
||
249 | |||
250 | return $metadata; |
||
251 | } |
||
252 | |||
253 | public function getMetadataForAssociatedEntity() |
||
254 | { |
||
255 | $embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
||
256 | |||
257 | $metadata = new ClassMetadata('Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'); |
||
258 | |||
259 | $metadata->fieldMappings = [ |
||
260 | 'plainField' => [ |
||
261 | 'fieldName' => 'plainField', |
||
262 | 'columnName' => 'plainField', |
||
263 | 'type' => 'string', |
||
264 | ], |
||
265 | ]; |
||
266 | |||
267 | $metadata->embeddedClasses['embeddedEntity'] = [ |
||
268 | 'class' => $embeddedEntityClass, |
||
269 | 'columnPrefix' => 'embeddedEntity', |
||
270 | ]; |
||
271 | |||
272 | $metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
||
273 | |||
274 | return $metadata; |
||
275 | } |
||
276 | |||
277 | public function getMetadataForContainerEntity() |
||
278 | { |
||
279 | $containerEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ContainerEntity'; |
||
280 | $associatedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\AssociatedEntity'; |
||
281 | $embeddedEntityClass = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\Embeddable\EmbeddedEntity'; |
||
282 | |||
283 | $metadata = new ClassMetadata($containerEntityClass); |
||
284 | |||
285 | $metadata->fieldMappings = [ |
||
286 | 'plainField' => [ |
||
287 | 'fieldName' => 'plainField', |
||
288 | 'columnName' => 'plainField', |
||
289 | 'type' => 'integer', |
||
290 | ], |
||
291 | ]; |
||
292 | |||
293 | $metadata->associationMappings['associatedEntity'] = [ |
||
294 | 'fieldName' => 'associatedEntity', |
||
295 | 'targetEntity' => $associatedEntityClass, |
||
296 | 'sourceEntity' => $containerEntityClass, |
||
297 | ]; |
||
298 | |||
299 | $metadata->embeddedClasses['embeddedEntity'] = [ |
||
300 | 'class' => $embeddedEntityClass, |
||
301 | 'columnPrefix' => 'embeddedEntity', |
||
302 | ]; |
||
303 | |||
304 | $metadata->inlineEmbeddable('embeddedEntity', $this->getMetadataForEmbeddedEntity()); |
||
305 | |||
306 | return $metadata; |
||
307 | } |
||
308 | |||
309 | public function testNonIntegerIdentifierType() |
||
310 | { |
||
311 | $uuid = new NonIntegerIdentifierTestClass('efbcfc4b-8c43-4d42-aa4c-d707e55151ac'); |
||
312 | $entity = new UuidEntity($uuid); |
||
313 | |||
314 | $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo'); |
||
315 | $meta->expects($this->any()) |
||
316 | ->method('getIdentifierValues') |
||
317 | ->willReturn([$entity->getId()]); |
||
318 | $meta->expects($this->any()) |
||
319 | ->method('getTypeOfField') |
||
320 | ->willReturn(UuidType::NAME); |
||
321 | |||
322 | $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory'); |
||
323 | $mf->expects($this->any()) |
||
324 | ->method('getMetadataFor') |
||
325 | ->willReturn($meta); |
||
326 | |||
327 | $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform'); |
||
328 | |||
329 | $conn = $this->createMock('Doctrine\DBAL\Connection'); |
||
330 | $conn->expects($this->any()) |
||
331 | ->method('getDatabasePlatform') |
||
332 | ->willReturn($platform); |
||
333 | |||
334 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
335 | $em->expects($this->any()) |
||
336 | ->method('getMetadataFactory') |
||
337 | ->willReturn($mf); |
||
338 | $em->expects($this->any()) |
||
339 | ->method('getConnection') |
||
340 | ->willReturn($conn); |
||
341 | |||
342 | $registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
||
343 | $registry->expects($this->any()) |
||
344 | ->method('getManagerForClass') |
||
345 | ->willReturn($em); |
||
346 | |||
347 | $manager = new ModelManager($registry); |
||
348 | $result = $manager->getIdentifierValues($entity); |
||
349 | |||
350 | $this->assertEquals($entity->getId()->toString(), $result[0]); |
||
351 | } |
||
352 | |||
353 | public function testAssociationIdentifierType() |
||
354 | { |
||
355 | $entity = new ContainerEntity(new AssociatedEntity(42, new EmbeddedEntity()), new EmbeddedEntity()); |
||
356 | |||
357 | $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo'); |
||
358 | $meta->expects($this->any()) |
||
359 | ->method('getIdentifierValues') |
||
360 | ->willReturn([$entity->getAssociatedEntity()->getPlainField()]); |
||
361 | $meta->expects($this->any()) |
||
362 | ->method('getTypeOfField') |
||
363 | ->willReturn(null); |
||
364 | |||
365 | $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory'); |
||
366 | $mf->expects($this->any()) |
||
367 | ->method('getMetadataFor') |
||
368 | ->willReturn($meta); |
||
369 | |||
370 | $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform'); |
||
371 | |||
372 | $conn = $this->createMock('Doctrine\DBAL\Connection'); |
||
373 | $conn->expects($this->any()) |
||
374 | ->method('getDatabasePlatform') |
||
375 | ->willReturn($platform); |
||
376 | |||
377 | $em = $this->createMock('Doctrine\ORM\EntityManager'); |
||
378 | $em->expects($this->any()) |
||
379 | ->method('getMetadataFactory') |
||
380 | ->willReturn($mf); |
||
381 | $em->expects($this->any()) |
||
382 | ->method('getConnection') |
||
383 | ->willReturn($conn); |
||
384 | |||
385 | $registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
||
386 | $registry->expects($this->any()) |
||
387 | ->method('getManagerForClass') |
||
388 | ->willReturn($em); |
||
389 | |||
390 | $manager = new ModelManager($registry); |
||
391 | $result = $manager->getIdentifierValues($entity); |
||
392 | |||
393 | $this->assertSame(42, $result[0]); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * [sortBy, sortOrder, isAddOrderBy]. |
||
398 | * |
||
399 | * @return array |
||
400 | */ |
||
401 | public function getSortableInDataSourceIteratorDataProvider() |
||
410 | |||
411 | /** |
||
412 | * @dataProvider getSortableInDataSourceIteratorDataProvider |
||
413 | * |
||
414 | * @param string|null $sortBy |
||
415 | * @param string|null $sortOrder |
||
416 | * @param bool $isAddOrderBy |
||
417 | */ |
||
418 | public function testSortableInDataSourceIterator($sortBy, $sortOrder, $isAddOrderBy) |
||
467 | |||
468 | public function testModelReverseTransform() |
||
469 | { |
||
470 | $class = 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\SimpleEntity'; |
||
471 | |||
472 | $metadataFactory = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory'); |
||
473 | $modelManager = $this->createMock('Doctrine\Common\Persistence\ObjectManager'); |
||
474 | $registry = $this->createMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
||
475 | |||
476 | $classMetadata = new ClassMetadataInfo($class); |
||
477 | $classMetadata->reflClass = new \ReflectionClass($class); |
||
478 | |||
479 | $modelManager->expects($this->once()) |
||
480 | ->method('getMetadataFactory') |
||
481 | ->willReturn($metadataFactory); |
||
482 | $metadataFactory->expects($this->once()) |
||
483 | ->method('getMetadataFor') |
||
484 | ->with($class) |
||
485 | ->willReturn($classMetadata); |
||
486 | $registry->expects($this->once()) |
||
487 | ->method('getManagerForClass') |
||
488 | ->with($class) |
||
489 | ->willReturn($modelManager); |
||
490 | |||
491 | $manager = new ModelManager($registry); |
||
492 | $this->assertInstanceOf($class, $object = $manager->modelReverseTransform( |
||
493 | $class, |
||
494 | [ |
||
495 | 'schmeckles' => 42, |
||
496 | 'multi_word_property' => 'hello', |
||
497 | ] |
||
498 | )); |
||
499 | $this->assertSame(42, $object->getSchmeckles()); |
||
500 | $this->assertSame('hello', $object->getMultiWordProperty()); |
||
501 | } |
||
502 | |||
503 | private function getMetadata($class, $isVersioned) |
||
517 | } |
||
518 |
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.