2009-01-05 23:18:56 +03:00
|
|
|
<?php
|
2009-01-06 20:22:23 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests\ORM\Mapping;
|
2009-01-06 20:22:23 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
2009-07-16 00:18:40 +04:00
|
|
|
use Doctrine\ORM\Events;
|
2009-01-22 22:38:10 +03:00
|
|
|
|
2009-01-24 19:56:44 +03:00
|
|
|
require_once __DIR__ . '/../../TestInit.php';
|
2009-01-05 23:18:56 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
|
2009-01-06 20:22:23 +03:00
|
|
|
{
|
2009-02-18 10:59:11 +03:00
|
|
|
public function testClassMetadataInstanceSerialization()
|
|
|
|
{
|
2009-01-22 22:38:10 +03:00
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
2009-01-05 23:18:56 +03:00
|
|
|
|
|
|
|
// Test initial state
|
2009-01-12 16:34:41 +03:00
|
|
|
$this->assertTrue(count($cm->getReflectionProperties()) == 0);
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->assertTrue($cm->reflClass instanceof \ReflectionClass);
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->rootEntityName);
|
|
|
|
$this->assertEquals(array(), $cm->subClasses);
|
|
|
|
$this->assertEquals(array(), $cm->parentClasses);
|
2009-01-05 23:18:56 +03:00
|
|
|
|
|
|
|
// Customize state
|
|
|
|
$cm->setSubclasses(array("One", "Two", "Three"));
|
|
|
|
$cm->setParentClasses(array("UserParent"));
|
|
|
|
$cm->setCustomRepositoryClass("UserRepository");
|
|
|
|
$cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer'));
|
2009-01-12 16:34:41 +03:00
|
|
|
$cm->mapOneToOne(array('fieldName' => 'phonenumbers', 'targetEntity' => 'Bar', 'mappedBy' => 'foo'));
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->assertTrue($cm->getAssociationMapping('phonenumbers') instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->assertEquals(1, count($cm->associationMappings));
|
2009-01-05 23:18:56 +03:00
|
|
|
|
|
|
|
$serialized = serialize($cm);
|
|
|
|
$cm = unserialize($serialized);
|
|
|
|
|
|
|
|
// Check state
|
|
|
|
$this->assertTrue(count($cm->getReflectionProperties()) > 0);
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->assertTrue($cm->reflClass instanceof \ReflectionClass);
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
|
|
|
|
$this->assertEquals('UserParent', $cm->rootEntityName);
|
2009-08-17 21:58:16 +04:00
|
|
|
$this->assertEquals(array('Doctrine\Tests\Models\CMS\One', 'Doctrine\Tests\Models\CMS\Two', 'Doctrine\Tests\Models\CMS\Three'), $cm->subClasses);
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->assertEquals(array('UserParent'), $cm->parentClasses);
|
2010-03-15 20:19:00 +03:00
|
|
|
$this->assertEquals('UserRepository', $cm->customRepositoryClassName);
|
2009-07-20 19:30:54 +04:00
|
|
|
$this->assertEquals(array('name' => 'disc', 'type' => 'integer', 'fieldName' => 'disc'), $cm->discriminatorColumn);
|
2009-01-22 22:38:10 +03:00
|
|
|
$this->assertTrue($cm->getAssociationMapping('phonenumbers') instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
|
2009-05-14 14:03:09 +04:00
|
|
|
$this->assertEquals(1, count($cm->associationMappings));
|
2009-01-12 16:34:41 +03:00
|
|
|
$oneOneMapping = $cm->getAssociationMapping('phonenumbers');
|
2010-03-15 20:19:00 +03:00
|
|
|
$this->assertEquals('phonenumbers', $oneOneMapping->sourceFieldName);
|
|
|
|
$this->assertEquals('Doctrine\Tests\Models\CMS\Bar', $oneOneMapping->targetEntityName);
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|
2009-11-02 15:53:05 +03:00
|
|
|
|
|
|
|
public function testFieldIsNullable()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
|
|
|
|
// Explicit Nullable
|
|
|
|
$cm->mapField(array('fieldName' => 'status', 'nullable' => true, 'type' => 'string', 'length' => 50));
|
|
|
|
$this->assertTrue($cm->isNullable('status'));
|
|
|
|
|
|
|
|
// Explicit Not Nullable
|
|
|
|
$cm->mapField(array('fieldName' => 'username', 'nullable' => false, 'type' => 'string', 'length' => 50));
|
|
|
|
$this->assertFalse($cm->isNullable('username'));
|
|
|
|
|
|
|
|
// Implicit Not Nullable
|
|
|
|
$cm->mapField(array('fieldName' => 'name', 'type' => 'string', 'length' => 50));
|
|
|
|
$this->assertFalse($cm->isNullable('name'), "By default a field should not be nullable.");
|
|
|
|
}
|
2009-11-05 02:06:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-115
|
|
|
|
*/
|
|
|
|
public function testMapAssocationInGlobalNamespace()
|
|
|
|
{
|
|
|
|
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
|
|
|
|
|
|
|
|
$cm = new ClassMetadata('DoctrineGlobal_Article');
|
|
|
|
$cm->mapManyToMany(array(
|
|
|
|
'fieldName' => 'author',
|
|
|
|
'targetEntity' => 'DoctrineGlobal_User',
|
|
|
|
'joinTable' => array(
|
|
|
|
'name' => 'bar',
|
|
|
|
'joinColumns' => array(array('name' => 'bar_id', 'referencedColumnName' => 'id')),
|
|
|
|
'inverseJoinColumns' => array(array('name' => 'baz_id', 'referencedColumnName' => 'id')),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals("DoctrineGlobal_User", $cm->associationMappings['author']->targetEntityName);
|
|
|
|
}
|
2010-02-09 20:13:49 +03:00
|
|
|
|
|
|
|
public function testMapManyToManyJoinTableDefaults()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
$cm->mapManyToMany(
|
|
|
|
array(
|
|
|
|
'fieldName' => 'groups',
|
|
|
|
'targetEntity' => 'CmsGroup'
|
|
|
|
));
|
|
|
|
|
|
|
|
$assoc = $cm->associationMappings['groups'];
|
|
|
|
$this->assertTrue($assoc instanceof \Doctrine\ORM\Mapping\ManyToManyMapping);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
'name' => 'CmsUser_CmsGroup',
|
2010-04-10 02:00:36 +04:00
|
|
|
'joinColumns' => array(array('name' => 'CmsUser_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')),
|
|
|
|
'inverseJoinColumns' => array(array('name' => 'CmsGroup_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE'))
|
2010-02-09 20:13:49 +03:00
|
|
|
), $assoc->joinTable);
|
|
|
|
}
|
2009-11-05 02:06:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-115
|
|
|
|
*/
|
|
|
|
public function testSetDiscriminatorMapInGlobalNamespace()
|
|
|
|
{
|
|
|
|
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
|
|
|
|
|
|
|
|
$cm = new ClassMetadata('DoctrineGlobal_User');
|
|
|
|
$cm->setDiscriminatorMap(array('descr' => 'DoctrineGlobal_Article', 'foo' => 'DoctrineGlobal_User'));
|
|
|
|
|
|
|
|
$this->assertEquals("DoctrineGlobal_Article", $cm->discriminatorMap['descr']);
|
|
|
|
$this->assertEquals("DoctrineGlobal_User", $cm->discriminatorMap['foo']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-115
|
|
|
|
*/
|
|
|
|
public function testSetSubClassesInGlobalNamespace()
|
|
|
|
{
|
|
|
|
require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";
|
|
|
|
|
|
|
|
$cm = new ClassMetadata('DoctrineGlobal_User');
|
|
|
|
$cm->setSubclasses(array('DoctrineGlobal_Article'));
|
|
|
|
|
|
|
|
$this->assertEquals("DoctrineGlobal_Article", $cm->subClasses[0]);
|
|
|
|
}
|
2010-02-03 00:17:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DDC-268
|
|
|
|
*/
|
|
|
|
public function testSetInvalidVersionMapping_ThrowsException()
|
|
|
|
{
|
|
|
|
$field = array();
|
|
|
|
$field['fieldName'] = 'foo';
|
|
|
|
$field['type'] = 'string';
|
|
|
|
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->setVersionMapping($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSingleIdentifierFieldName_MultipleIdentifierEntity_ThrowsException()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
$cm->isIdentifierComposite = true;
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->getSingleIdentifierFieldName();
|
|
|
|
}
|
2010-02-03 00:46:39 +03:00
|
|
|
|
|
|
|
public function testDuplicateAssociationMappingException()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
2010-02-09 20:13:49 +03:00
|
|
|
$a1 = new \Doctrine\ORM\Mapping\OneToOneMapping(array('fieldName' => 'foo', 'sourceEntity' => 'stdClass', 'targetEntity' => 'stdClass', 'mappedBy' => 'foo'));
|
|
|
|
$a2 = new \Doctrine\ORM\Mapping\OneToOneMapping(array('fieldName' => 'foo', 'sourceEntity' => 'stdClass', 'targetEntity' => 'stdClass', 'mappedBy' => 'foo'));
|
2010-02-03 00:46:39 +03:00
|
|
|
|
|
|
|
$cm->addAssociationMapping($a1);
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->addAssociationMapping($a2);
|
|
|
|
}
|
2010-03-20 22:23:13 +03:00
|
|
|
|
|
|
|
public function testDuplicateColumnName_ThrowsMappingException()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
$cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->mapField(array('fieldName' => 'username', 'columnName' => 'name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDuplicateColumnName_DiscriminatorColumn_ThrowsMappingException()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
$cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->setDiscriminatorColumn(array('name' => 'name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDuplicateColumnName_DiscriminatorColumn2_ThrowsMappingException()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
$cm->setDiscriminatorColumn(array('name' => 'name'));
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDuplicateFieldAndAssocationMapping1_ThrowsException()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
$cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->mapOneToOne(array('fieldName' => 'name', 'targetEntity' => 'CmsUser'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDuplicateFieldAndAssocationMapping2_ThrowsException()
|
|
|
|
{
|
|
|
|
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
|
|
|
|
$cm->mapOneToOne(array('fieldName' => 'name', 'targetEntity' => 'CmsUser'));
|
|
|
|
|
|
|
|
$this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
|
|
|
|
$cm->mapField(array('fieldName' => 'name', 'columnName' => 'name'));
|
|
|
|
}
|
2009-01-05 23:18:56 +03:00
|
|
|
}
|