123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
-
- namespace Doctrine\Tests\ORM\Mapping;
-
- use Doctrine\ORM\Mapping\ClassMetadata;
- use Doctrine\ORM\Events;
-
- require_once __DIR__ . '/../../TestInit.php';
-
- class AnnotationDriverTest extends AbstractMappingDriverTest
- {
- /**
- * @group DDC-268
- */
- public function testLoadMetadataForNonEntityThrowsException()
- {
- $cm = new ClassMetadata('stdClass');
- $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache());
- $annotationDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
-
- $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
- $annotationDriver->loadMetadataForClass('stdClass', $cm);
- }
-
- /**
- * @group DDC-268
- */
- public function testColumnWithMissingTypeDefaultsToString()
- {
- $cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\ColumnWithoutType');
- $annotationDriver = $this->_loadDriver();
-
- $annotationDriver->loadMetadataForClass('Doctrine\Tests\ORM\Mapping\InvalidColumn', $cm);
- $this->assertEquals('string', $cm->fieldMappings['id']['type']);
- }
-
- /**
- * @group DDC-318
- */
- public function testGetAllClassNamesIsIdempotent()
- {
- $annotationDriver = $this->_loadDriverForCMSModels();
- $original = $annotationDriver->getAllClassNames();
-
- $annotationDriver = $this->_loadDriverForCMSModels();
- $afterTestReset = $annotationDriver->getAllClassNames();
-
- $this->assertEquals($original, $afterTestReset);
- }
-
- /**
- * @group DDC-318
- */
- public function testGetAllClassNamesIsIdempotentEvenWithDifferentDriverInstances()
- {
- $annotationDriver = $this->_loadDriverForCMSModels();
- $original = $annotationDriver->getAllClassNames();
-
- $annotationDriver = $this->_loadDriverForCMSModels();
- $afterTestReset = $annotationDriver->getAllClassNames();
-
- $this->assertEquals($original, $afterTestReset);
- }
-
- /**
- * @group DDC-318
- */
- public function testGetAllClassNamesReturnsAlreadyLoadedClassesIfAppropriate()
- {
- $rightClassName = 'Doctrine\Tests\Models\CMS\CmsUser';
- $this->_ensureIsLoaded($rightClassName);
-
- $annotationDriver = $this->_loadDriverForCMSModels();
- $classes = $annotationDriver->getAllClassNames();
-
- $this->assertContains($rightClassName, $classes);
- }
-
- /**
- * @group DDC-318
- */
- public function testGetClassNamesReturnsOnlyTheAppropriateClasses()
- {
- $extraneousClassName = 'Doctrine\Tests\Models\ECommerce\ECommerceCart';
- $this->_ensureIsLoaded($extraneousClassName);
-
- $annotationDriver = $this->_loadDriverForCMSModels();
- $classes = $annotationDriver->getAllClassNames();
-
- $this->assertNotContains($extraneousClassName, $classes);
- }
-
- protected function _loadDriverForCMSModels()
- {
- $annotationDriver = $this->_loadDriver();
- $annotationDriver->addPaths(array(__DIR__ . '/../../Models/CMS/'));
- return $annotationDriver;
- }
-
- protected function _loadDriver()
- {
- return $this->createAnnotationDriver();
- }
-
- protected function _ensureIsLoaded($entityClassName)
- {
- new $entityClassName;
- }
-
- /**
- * @group DDC-671
- *
- * Entities for this test are in AbstractMappingDriverTest
- */
- public function testJoinTablesWithMappedSuperclassForAnnotationDriver()
- {
- $annotationDriver = $this->_loadDriver();
- $annotationDriver->addPaths(array(__DIR__ . '/../../Models/DirectoryTree/'));
-
- $em = $this->_getTestEntityManager();
- $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
- $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
- $factory->setEntityManager($em);
-
- $classPage = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\File');
- $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\File', $classPage->associationMappings['parentDirectory']['sourceEntity']);
-
- $classDirectory = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\Directory');
- $this->assertEquals('Doctrine\Tests\Models\DirectoryTree\Directory', $classDirectory->associationMappings['parentDirectory']['sourceEntity']);
- }
-
- /**
- * @group DDC-945
- */
- public function testInvalidMappedSuperClassWithManyToManyAssociation()
- {
- $annotationDriver = $this->_loadDriver();
-
- $em = $this->_getTestEntityManager();
- $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
- $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
- $factory->setEntityManager($em);
-
- $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
- "It is illegal to put an inverse side one-to-many or many-to-many association on ".
- "mapped superclass 'Doctrine\Tests\ORM\Mapping\InvalidMappedSuperClass#users'");
- $usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\UsingInvalidMappedSuperClass');
- }
-
- /**
- * @group DDC-1050
- */
- public function testInvalidMappedSuperClassWithInheritanceInformation()
- {
- $annotationDriver = $this->_loadDriver();
-
- $em = $this->_getTestEntityManager();
- $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
- $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
- $factory->setEntityManager($em);
-
- $this->setExpectedException('Doctrine\ORM\Mapping\MappingException',
- "Its not supported to define inheritance information on a mapped ".
- "superclass 'Doctrine\Tests\ORM\Mapping\MappedSuperClassInheritence'.");
- $usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\MappedSuperClassInheritence');
- }
-
- /**
- * @group DDC-1034
- */
- public function testInheritanceSkipsParentLifecycleCallbacks()
- {
- $annotationDriver = $this->_loadDriver();
-
- $cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\AnnotationChild');
- $em = $this->_getTestEntityManager();
- $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
- $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
- $factory->setEntityManager($em);
-
- $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\AnnotationChild');
- $this->assertEquals(array("postLoad" => array("postLoad"), "preUpdate" => array("preUpdate")), $cm->lifecycleCallbacks);
-
- $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\AnnotationParent');
- $this->assertEquals(array("postLoad" => array("postLoad"), "preUpdate" => array("preUpdate")), $cm->lifecycleCallbacks);
- }
-
- /**
- * @group DDC-1156
- */
- public function testMappedSuperclassInMiddleOfInheritanceHierachy()
- {
- $annotationDriver = $this->_loadDriver();
-
- $em = $this->_getTestEntityManager();
- $em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
- $factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
- $factory->setEntityManager($em);
-
- $cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\ChildEntity');
- }
- }
-
- /**
- * @Entity
- */
- class ColumnWithoutType
- {
- /** @Id @Column */
- public $id;
- }
-
- /**
- * @MappedSuperclass
- */
- class InvalidMappedSuperClass
- {
- /**
- * @ManyToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsUser", mappedBy="invalid")
- */
- private $users;
- }
-
- /**
- * @Entity
- */
- class UsingInvalidMappedSuperClass extends InvalidMappedSuperClass
- {
- /**
- * @Id @Column(type="integer") @GeneratedValue
- */
- private $id;
- }
-
- /**
- * @MappedSuperclass
- * @InheritanceType("JOINED")
- * @DiscriminatorMap({"test" = "ColumnWithoutType"})
- */
- class MappedSuperClassInheritence
- {
-
- }
-
- /**
- * @Entity
- * @InheritanceType("JOINED")
- * @DiscriminatorMap({"parent" = "AnnotationParent", "child" = "AnnotationChild"})
- * @HasLifecycleCallbacks
- */
- class AnnotationParent
- {
- /**
- * @Id @Column(type="integer") @GeneratedValue
- */
- private $id;
-
- /**
- * @PostLoad
- */
- public function postLoad()
- {
-
- }
-
- /**
- * @PreUpdate
- */
- public function preUpdate()
- {
-
- }
- }
-
- /**
- * @Entity
- * @HasLifecycleCallbacks
- */
- class AnnotationChild extends AnnotationParent
- {
-
- }
-
- /**
- * @Entity
- * @InheritanceType("SINGLE_TABLE")
- * @DiscriminatorMap({"s"="SuperEntity", "c"="ChildEntity"})
- */
- class SuperEntity
- {
- /** @Id @Column(type="string") */
- private $id;
- }
-
- /**
- * @MappedSuperclass
- */
- class MiddleMappedSuperclass extends SuperEntity
- {
- /** @Column(type="string") */
- private $name;
- }
-
- /**
- * @Entity
- */
- class ChildEntity extends MiddleMappedSuperclass
- {
- /**
- * @Column(type="string")
- */
- private $text;
- }
|