2010-02-03 00:17:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Mapping;
|
|
|
|
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\ORM\Events;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
class AnnotationDriverTest extends AbstractMappingDriverTest
|
2010-02-03 00:17:00 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2010-02-09 20:13:49 +03:00
|
|
|
public function testColumnWithMissingTypeDefaultsToString()
|
2010-02-03 00:17:00 +03:00
|
|
|
{
|
2010-02-14 22:38:22 +03:00
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\ORM\Mapping\ColumnWithoutType');
|
2010-08-08 14:29:14 +04:00
|
|
|
$annotationDriver = $this->_loadDriver();
|
2010-02-03 00:17:00 +03:00
|
|
|
|
|
|
|
$annotationDriver->loadMetadataForClass('Doctrine\Tests\ORM\Mapping\InvalidColumn', $cm);
|
2010-02-09 20:13:49 +03:00
|
|
|
$this->assertEquals('string', $cm->fieldMappings['id']['type']);
|
2010-02-03 00:17:00 +03:00
|
|
|
}
|
2010-02-14 22:38:22 +03:00
|
|
|
|
2010-03-19 02:04:21 +03:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2010-02-14 22:38:22 +03:00
|
|
|
|
|
|
|
protected function _loadDriver()
|
|
|
|
{
|
2011-05-25 02:26:20 +04:00
|
|
|
return $this->createAnnotationDriver();
|
2010-02-14 22:38:22 +03:00
|
|
|
}
|
2010-03-19 02:04:21 +03:00
|
|
|
|
|
|
|
protected function _ensureIsLoaded($entityClassName)
|
|
|
|
{
|
|
|
|
new $entityClassName;
|
|
|
|
}
|
2010-09-22 01:14:45 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-671
|
|
|
|
*
|
|
|
|
* Entities for this test are in AbstractMappingDriverTest
|
|
|
|
*/
|
|
|
|
public function testJoinTablesWithMappedSuperclassForAnnotationDriver()
|
|
|
|
{
|
2010-09-22 01:53:26 +04:00
|
|
|
$annotationDriver = $this->_loadDriver();
|
|
|
|
$annotationDriver->addPaths(array(__DIR__ . '/../../Models/DirectoryTree/'));
|
2010-09-22 01:14:45 +04:00
|
|
|
|
2010-09-22 01:53:26 +04:00
|
|
|
$em = $this->_getTestEntityManager();
|
|
|
|
$em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
|
2010-11-27 22:53:26 +03:00
|
|
|
$factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory();
|
|
|
|
$factory->setEntityManager($em);
|
2010-09-22 01:14:45 +04:00
|
|
|
|
2010-09-22 01:53:26 +04:00
|
|
|
$classPage = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\File');
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\DirectoryTree\File', $classPage->associationMappings['parentDirectory']['sourceEntity']);
|
2010-09-22 01:14:45 +04:00
|
|
|
|
2010-09-22 01:53:26 +04:00
|
|
|
$classDirectory = $factory->getMetadataFor('Doctrine\Tests\Models\DirectoryTree\Directory');
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\DirectoryTree\Directory', $classDirectory->associationMappings['parentDirectory']['sourceEntity']);
|
2010-09-22 01:14:45 +04:00
|
|
|
}
|
2010-12-28 13:59:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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',
|
2010-12-31 16:39:01 +03:00
|
|
|
"It is illegal to put an inverse side one-to-many or many-to-many association on ".
|
2010-12-28 13:59:51 +03:00
|
|
|
"mapped superclass 'Doctrine\Tests\ORM\Mapping\InvalidMappedSuperClass#users'");
|
|
|
|
$usingInvalidMsc = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\UsingInvalidMappedSuperClass');
|
|
|
|
}
|
2011-03-04 00:51:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
}
|
2011-03-05 01:00:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
2011-06-05 17:00:49 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
}
|
2011-11-08 13:01:22 +04:00
|
|
|
|
|
|
|
public function testInvalidFetchOptionThrowsException()
|
|
|
|
{
|
|
|
|
$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',
|
|
|
|
"Entity 'Doctrine\Tests\ORM\Mapping\InvalidFetchOption' has a mapping with invalid fetch mode 'eager");
|
|
|
|
$cm = $factory->getMetadataFor('Doctrine\Tests\ORM\Mapping\InvalidFetchOption');
|
|
|
|
}
|
2010-02-03 00:17:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
*/
|
2010-02-14 22:38:22 +03:00
|
|
|
class ColumnWithoutType
|
2010-02-03 00:17:00 +03:00
|
|
|
{
|
|
|
|
/** @Id @Column */
|
|
|
|
public $id;
|
|
|
|
}
|
2010-12-28 13:59:51 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @MappedSuperclass
|
|
|
|
*/
|
|
|
|
class InvalidMappedSuperClass
|
|
|
|
{
|
|
|
|
/**
|
2010-12-31 16:39:01 +03:00
|
|
|
* @ManyToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsUser", mappedBy="invalid")
|
2010-12-28 13:59:51 +03:00
|
|
|
*/
|
|
|
|
private $users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
*/
|
|
|
|
class UsingInvalidMappedSuperClass extends InvalidMappedSuperClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Id @Column(type="integer") @GeneratedValue
|
|
|
|
*/
|
|
|
|
private $id;
|
2011-03-04 00:51:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @MappedSuperclass
|
|
|
|
* @InheritanceType("JOINED")
|
|
|
|
* @DiscriminatorMap({"test" = "ColumnWithoutType"})
|
|
|
|
*/
|
|
|
|
class MappedSuperClassInheritence
|
|
|
|
{
|
|
|
|
|
2011-03-05 01:00:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
|
|
|
|
2011-06-05 17:00:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
2011-11-08 13:01:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Entity
|
|
|
|
*/
|
|
|
|
class InvalidFetchOption
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @OneToMany(targetEntity="Doctrine\Tests\Models\CMS\CmsUser", fetch="eager")
|
|
|
|
*/
|
|
|
|
private $collection;
|
|
|
|
}
|