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' ;
2010-12-31 00:30:51 +03:00
require_once __DIR__ . '/../../Models/Global/GlobalNamespaceModel.php' ;
2011-12-20 01:56:19 +04: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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
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 );
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'ReflectionClass' , $cm -> reflClass );
2009-05-14 14:03:09 +04:00
$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 );
2010-04-19 00:47:03 +04:00
$this -> assertEquals ( ClassMetadata :: INHERITANCE_TYPE_NONE , $cm -> inheritanceType );
2009-01-05 23:18:56 +03:00
// Customize state
2010-04-19 00:47:03 +04:00
$cm -> setInheritanceType ( ClassMetadata :: INHERITANCE_TYPE_SINGLE_TABLE );
2009-01-05 23:18:56 +03:00
$cm -> setSubclasses ( array ( " One " , " Two " , " Three " ));
$cm -> setParentClasses ( array ( " UserParent " ));
$cm -> setCustomRepositoryClass ( " UserRepository " );
$cm -> setDiscriminatorColumn ( array ( 'name' => 'disc' , 'type' => 'integer' ));
2011-10-15 19:38:55 +04:00
$cm -> mapOneToOne ( array ( 'fieldName' => 'phonenumbers' , 'targetEntity' => 'CmsAddress' , 'mappedBy' => 'foo' ));
2011-03-29 22:04:14 +04:00
$cm -> markReadOnly ();
$cm -> addNamedQuery ( array ( 'name' => 'dql' , 'query' => 'foo' ));
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 );
2012-01-02 18:57:32 +04:00
$cm -> wakeupReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2009-01-05 23:18:56 +03:00
// Check state
$this -> assertTrue ( count ( $cm -> getReflectionProperties ()) > 0 );
2010-04-30 19:30:27 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS' , $cm -> namespace );
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'ReflectionClass' , $cm -> reflClass );
2009-05-14 14:03:09 +04:00
$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 );
2011-08-28 20:48:15 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\UserRepository' , $cm -> customRepositoryClassName );
2009-07-20 19:30:54 +04:00
$this -> assertEquals ( array ( 'name' => 'disc' , 'type' => 'integer' , 'fieldName' => 'disc' ), $cm -> discriminatorColumn );
2010-08-09 15:13:21 +04:00
$this -> assertTrue ( $cm -> associationMappings [ 'phonenumbers' ][ 'type' ] == ClassMetadata :: ONE_TO_ONE );
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-08-09 15:13:21 +04:00
$this -> assertTrue ( $oneOneMapping [ 'fetch' ] == ClassMetadata :: FETCH_LAZY );
$this -> assertEquals ( 'phonenumbers' , $oneOneMapping [ 'fieldName' ]);
2011-10-15 19:38:55 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $oneOneMapping [ 'targetEntity' ]);
2011-03-29 22:04:14 +04:00
$this -> assertTrue ( $cm -> isReadOnly );
2011-11-14 22:07:37 +04:00
$this -> assertEquals ( array ( 'dql' => array ( 'name' => 'dql' , 'query' => 'foo' , 'dql' => 'foo' )), $cm -> namedQueries );
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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2009-11-02 15:53:05 +03:00
// 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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2009-11-05 02:06:38 +03:00
$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' )),
),
));
2010-08-09 15:13:21 +04:00
$this -> assertEquals ( " DoctrineGlobal_User " , $cm -> associationMappings [ 'author' ][ 'targetEntity' ]);
2009-11-05 02:06:38 +03:00
}
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
public function testMapManyToManyJoinTableDefaults ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-02-09 20:13:49 +03:00
$cm -> mapManyToMany (
array (
'fieldName' => 'groups' ,
'targetEntity' => 'CmsGroup'
));
2011-12-20 01:56:19 +04:00
2010-02-09 20:13:49 +03:00
$assoc = $cm -> associationMappings [ 'groups' ];
2011-07-27 10:54:06 +04:00
//$this->assertInstanceOf('Doctrine\ORM\Mapping\ManyToManyMapping', $assoc);
2010-02-09 20:13:49 +03:00
$this -> assertEquals ( array (
2010-08-26 15:47:22 +04:00
'name' => 'cmsuser_cmsgroup' ,
'joinColumns' => array ( array ( 'name' => 'cmsuser_id' , 'referencedColumnName' => 'id' , 'onDelete' => 'CASCADE' )),
'inverseJoinColumns' => array ( array ( 'name' => 'cmsgroup_id' , 'referencedColumnName' => 'id' , 'onDelete' => 'CASCADE' ))
2010-08-09 15:13:21 +04:00
), $assoc [ 'joinTable' ]);
$this -> assertTrue ( $assoc [ 'isOnDeleteCascade' ]);
2010-07-10 14:04:02 +04:00
}
public function testSerializeManyToManyJoinTableCascade ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-07-10 14:04:02 +04:00
$cm -> mapManyToMany (
array (
'fieldName' => 'groups' ,
'targetEntity' => 'CmsGroup'
));
/* @var $assoc \Doctrine\ORM\Mapping\ManyToManyMapping */
$assoc = $cm -> associationMappings [ 'groups' ];
$assoc = unserialize ( serialize ( $assoc ));
2010-08-09 15:13:21 +04:00
$this -> assertTrue ( $assoc [ 'isOnDeleteCascade' ]);
2010-02-09 20:13:49 +03:00
}
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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2009-11-05 02:06:38 +03:00
$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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2009-11-05 02:06:38 +03:00
$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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-02-03 00:17:00 +03:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' );
$cm -> setVersionMapping ( $field );
}
public function testGetSingleIdentifierFieldName_MultipleIdentifierEntity_ThrowsException ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-02-03 00:17:00 +03:00
$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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-08-09 15:13:21 +04:00
$a1 = array ( 'fieldName' => 'foo' , 'sourceEntity' => 'stdClass' , 'targetEntity' => 'stdClass' , 'mappedBy' => 'foo' );
$a2 = array ( 'fieldName' => 'foo' , 'sourceEntity' => 'stdClass' , 'targetEntity' => 'stdClass' , 'mappedBy' => 'foo' );
2010-02-03 00:46:39 +03:00
2010-04-14 19:07:08 +04:00
$cm -> addInheritedAssociationMapping ( $a1 );
2010-02-03 00:46:39 +03:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' );
2010-04-14 19:07:08 +04:00
$cm -> addInheritedAssociationMapping ( $a2 );
2010-02-03 00:46:39 +03:00
}
2010-03-20 22:23:13 +03:00
public function testDuplicateColumnName_ThrowsMappingException ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-03-20 22:23:13 +03:00
$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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-03-20 22:23:13 +03:00
$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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-03-20 22:23:13 +03:00
$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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-03-20 22:23:13 +03:00
$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' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-03-20 22:23:13 +03:00
$cm -> mapOneToOne ( array ( 'fieldName' => 'name' , 'targetEntity' => 'CmsUser' ));
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' );
$cm -> mapField ( array ( 'fieldName' => 'name' , 'columnName' => 'name' ));
}
2011-12-20 01:56:19 +04:00
2011-06-26 12:10:57 +04:00
/**
* @ group DDC - 1224
*/
public function testGetTemporaryTableNameSchema ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-06-26 12:10:57 +04:00
$cm -> setTableName ( 'foo.bar' );
2011-12-20 01:56:19 +04:00
2011-06-26 12:10:57 +04:00
$this -> assertEquals ( 'foo_bar_id_tmp' , $cm -> getTemporaryIdTableName ());
}
2010-08-26 15:47:22 +04:00
public function testDefaultTableName ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-08-26 15:47:22 +04:00
// When table's name is not given
$primaryTable = array ();
$cm -> setPrimaryTable ( $primaryTable );
$this -> assertEquals ( 'CmsUser' , $cm -> getTableName ());
$this -> assertEquals ( 'CmsUser' , $cm -> table [ 'name' ]);
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-08-26 15:47:22 +04:00
// When joinTable's name is not given
$cm -> mapManyToMany ( array (
'fieldName' => 'user' ,
'targetEntity' => 'CmsUser' ,
'inversedBy' => 'users' ,
'joinTable' => array ( 'joinColumns' => array ( array ( 'referencedColumnName' => 'id' )),
'inverseJoinColumns' => array ( array ( 'referencedColumnName' => 'id' )))));
$this -> assertEquals ( 'cmsaddress_cmsuser' , $cm -> associationMappings [ 'user' ][ 'joinTable' ][ 'name' ]);
}
public function testDefaultJoinColumnName ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-08-26 15:47:22 +04:00
// this is really dirty, but it's the simpliest way to test whether
// joinColumn's name will be automatically set to user_id
$cm -> mapOneToOne ( array (
'fieldName' => 'user' ,
'targetEntity' => 'CmsUser' ,
'joinColumns' => array ( array ( 'referencedColumnName' => 'id' ))));
$this -> assertEquals ( 'user_id' , $cm -> associationMappings [ 'user' ][ 'joinColumns' ][ 0 ][ 'name' ]);
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-08-26 15:47:22 +04:00
$cm -> mapManyToMany ( array (
'fieldName' => 'user' ,
'targetEntity' => 'CmsUser' ,
'inversedBy' => 'users' ,
'joinTable' => array ( 'name' => 'user_CmsUser' ,
'joinColumns' => array ( array ( 'referencedColumnName' => 'id' )),
'inverseJoinColumns' => array ( array ( 'referencedColumnName' => 'id' )))));
$this -> assertEquals ( 'cmsaddress_id' , $cm -> associationMappings [ 'user' ][ 'joinTable' ][ 'joinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'cmsuser_id' , $cm -> associationMappings [ 'user' ][ 'joinTable' ][ 'inverseJoinColumns' ][ 0 ][ 'name' ]);
}
2010-11-27 19:38:12 +03:00
2011-12-23 20:13:21 +04:00
/**
* @ group DDC - 559
*/
public function testUnderscoreNamingStrategyDefaults ()
{
2011-12-24 18:01:25 +04:00
$namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy ( CASE_UPPER );
2011-12-23 20:13:21 +04:00
$oneToOneMetadata = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $namingStrategy );
$manyToManyMetadata = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $namingStrategy );
$oneToOneMetadata -> mapOneToOne ( array (
'fieldName' => 'user' ,
'targetEntity' => 'CmsUser'
));
$manyToManyMetadata -> mapManyToMany ( array (
'fieldName' => 'user' ,
'targetEntity' => 'CmsUser'
));
$this -> assertEquals ( array ( 'USER_ID' => 'ID' ), $oneToOneMetadata -> associationMappings [ 'user' ][ 'sourceToTargetKeyColumns' ]);
$this -> assertEquals ( array ( 'USER_ID' => 'USER_ID' ), $oneToOneMetadata -> associationMappings [ 'user' ][ 'joinColumnFieldNames' ]);
$this -> assertEquals ( array ( 'ID' => 'USER_ID' ), $oneToOneMetadata -> associationMappings [ 'user' ][ 'targetToSourceKeyColumns' ]);
$this -> assertEquals ( 'USER_ID' , $oneToOneMetadata -> associationMappings [ 'user' ][ 'joinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'ID' , $oneToOneMetadata -> associationMappings [ 'user' ][ 'joinColumns' ][ 0 ][ 'referencedColumnName' ]);
$this -> assertEquals ( 'CMS_ADDRESS_CMS_USER' , $manyToManyMetadata -> associationMappings [ 'user' ][ 'joinTable' ][ 'name' ]);
$this -> assertEquals ( array ( 'CMS_ADDRESS_ID' , 'CMS_USER_ID' ), $manyToManyMetadata -> associationMappings [ 'user' ][ 'joinTableColumns' ]);
$this -> assertEquals ( array ( 'CMS_ADDRESS_ID' => 'ID' ), $manyToManyMetadata -> associationMappings [ 'user' ][ 'relationToSourceKeyColumns' ]);
$this -> assertEquals ( array ( 'CMS_USER_ID' => 'ID' ), $manyToManyMetadata -> associationMappings [ 'user' ][ 'relationToTargetKeyColumns' ]);
$this -> assertEquals ( 'CMS_ADDRESS_ID' , $manyToManyMetadata -> associationMappings [ 'user' ][ 'joinTable' ][ 'joinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'CMS_USER_ID' , $manyToManyMetadata -> associationMappings [ 'user' ][ 'joinTable' ][ 'inverseJoinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'ID' , $manyToManyMetadata -> associationMappings [ 'user' ][ 'joinTable' ][ 'joinColumns' ][ 0 ][ 'referencedColumnName' ]);
$this -> assertEquals ( 'ID' , $manyToManyMetadata -> associationMappings [ 'user' ][ 'joinTable' ][ 'inverseJoinColumns' ][ 0 ][ 'referencedColumnName' ]);
$cm = new ClassMetadata ( 'DoctrineGlobal_Article' , $namingStrategy );
$cm -> mapManyToMany ( array ( 'fieldName' => 'author' , 'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsUser' ));
$this -> assertEquals ( 'DOCTRINE_GLOBAL_ARTICLE_CMS_USER' , $cm -> associationMappings [ 'author' ][ 'joinTable' ][ 'name' ]);
}
2010-11-27 19:38:12 +03:00
/**
* @ group DDC - 886
*/
public function testSetMultipleIdentifierSetsComposite ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-11-27 19:38:12 +03:00
$cm -> mapField ( array ( 'fieldName' => 'name' ));
$cm -> mapField ( array ( 'fieldName' => 'username' ));
$cm -> setIdentifier ( array ( 'name' , 'username' ));
$this -> assertTrue ( $cm -> isIdentifierComposite );
}
2010-12-22 02:17:50 +03:00
/**
* @ group DDC - 944
*/
public function testMappingNotFound ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-12-22 02:17:50 +03:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' , " No mapping found for field 'foo' on class 'Doctrine \T ests \ Models \ CMS \ CmsUser'. " );
$cm -> getFieldMapping ( 'foo' );
}
2010-12-31 00:30:51 +03:00
/**
* @ group DDC - 961
*/
public function testJoinTableMappingDefaults ()
{
$cm = new ClassMetadata ( 'DoctrineGlobal_Article' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-12-31 00:30:51 +03:00
$cm -> mapManyToMany ( array ( 'fieldName' => 'author' , 'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsUser' ));
$this -> assertEquals ( 'doctrineglobal_article_cmsuser' , $cm -> associationMappings [ 'author' ][ 'joinTable' ][ 'name' ]);
}
2011-01-02 14:01:05 +03:00
2011-01-01 20:17:19 +03:00
/**
* @ group DDC - 117
*/
public function testMapIdentifierAssociation ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\DDC117\DDC117ArticleDetails' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-01-01 20:17:19 +03:00
$cm -> mapOneToOne ( array (
'fieldName' => 'article' ,
'id' => true ,
'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article' ,
'joinColumns' => array (),
));
$this -> assertTrue ( $cm -> containsForeignIdentifier , " Identifier Association should set 'containsForeignIdentifier' boolean flag. " );
$this -> assertEquals ( array ( " article " ), $cm -> identifier );
}
/**
* @ group DDC - 117
*/
public function testOrphanRemovalIdentifierAssociation ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\DDC117\DDC117ArticleDetails' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-01-01 20:17:19 +03:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' , 'The orphan removal option is not allowed on an association that' );
$cm -> mapOneToOne ( array (
'fieldName' => 'article' ,
'id' => true ,
'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article' ,
'orphanRemoval' => true ,
'joinColumns' => array (),
));
}
/**
* @ group DDC - 117
*/
public function testInverseIdentifierAssocation ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\DDC117\DDC117ArticleDetails' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-01-01 20:17:19 +03:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' , 'An inverse association is not allowed to be identifier in' );
$cm -> mapOneToOne ( array (
'fieldName' => 'article' ,
'id' => true ,
'mappedBy' => 'details' , // INVERSE!
'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article' ,
'joinColumns' => array (),
));
}
/**
* @ group DDC - 117
*/
public function testIdentifierAssocationManyToMany ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\DDC117\DDC117ArticleDetails' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-01-01 20:17:19 +03:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' , 'Many-to-many or one-to-many associations are not allowed to be identifier in' );
$cm -> mapManyToMany ( array (
'fieldName' => 'article' ,
'id' => true ,
'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article' ,
'joinColumns' => array (),
));
}
2011-01-23 16:20:15 +03:00
/**
* @ group DDC - 996
*/
public function testEmptyFieldNameThrowsException ()
{
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' ,
" The field or association mapping misses the 'fieldName' attribute in entity 'Doctrine \T ests \ Models \ CMS \ CmsUser'. " );
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-01-23 16:20:15 +03:00
$cm -> mapField ( array ( 'fieldName' => '' ));
}
2011-03-07 00:45:09 +03:00
public function testRetrievalOfNamedQueries ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-03-07 00:45:09 +03:00
$this -> assertEquals ( 0 , count ( $cm -> getNamedQueries ()));
$cm -> addNamedQuery ( array (
'name' => 'userById' ,
'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
));
$this -> assertEquals ( 1 , count ( $cm -> getNamedQueries ()));
}
public function testExistanceOfNamedQuery ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-03-07 00:45:09 +03:00
$cm -> addNamedQuery ( array (
'name' => 'all' ,
'query' => 'SELECT u FROM __CLASS__ u'
));
$this -> assertTrue ( $cm -> hasNamedQuery ( 'all' ));
$this -> assertFalse ( $cm -> hasNamedQuery ( 'userById' ));
}
public function testRetrieveOfNamedQuery ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-03-07 00:45:09 +03:00
$cm -> addNamedQuery ( array (
'name' => 'userById' ,
'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
));
$this -> assertEquals ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1' , $cm -> getNamedQuery ( 'userById' ));
}
public function testNamingCollisionNamedQueryShouldThrowException ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-03-07 00:45:09 +03:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' );
$cm -> addNamedQuery ( array (
'name' => 'userById' ,
'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
));
$cm -> addNamedQuery ( array (
'name' => 'userById' ,
'query' => 'SELECT u FROM __CLASS__ u WHERE u.id = ?1'
));
}
2011-03-20 14:35:52 +03:00
/**
* @ group DDC - 1068
*/
public function testClassCaseSensitivity ()
{
$user = new \Doctrine\Tests\Models\CMS\CmsUser ();
$cm = new ClassMetadata ( 'DOCTRINE\TESTS\MODELS\CMS\CMSUSER' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-03-20 14:35:52 +03:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $cm -> name );
}
2011-09-04 16:13:20 +04:00
/**
* @ group DDC - 659
*/
public function testLifecycleCallbackNotFound ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2012-01-03 00:32:18 +04:00
$cm -> addLifecycleCallback ( 'notfound' , 'postLoad' );
2011-09-04 16:13:20 +04:00
$this -> setExpectedException ( " Doctrine \ ORM \ Mapping \ MappingException " , " Entity 'Doctrine \T ests \ Models \ CMS \ CmsUser' has no method 'notfound' to be registered as lifecycle callback. " );
2012-01-03 00:32:18 +04:00
$cm -> validateLifecycleCallbacks ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2011-09-04 16:13:20 +04:00
}
2011-10-22 13:28:07 +04:00
/**
* @ group ImproveErrorMessages
*/
public function testTargetEntityNotFound ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2012-01-02 20:06:22 +04:00
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2012-01-03 00:32:18 +04:00
$cm -> mapManyToOne ( array ( 'fieldName' => 'address' , 'targetEntity' => 'UnknownClass' ));
2011-10-22 13:28:07 +04:00
$this -> setExpectedException ( " Doctrine \ ORM \ Mapping \ MappingException " , " The target-entity Doctrine \T ests \ Models \ CMS \ UnknownClass cannot be found in 'Doctrine \T ests \ Models \ CMS \ CmsUser#address'. " );
2012-01-03 00:32:18 +04:00
$cm -> validateAssocations ();
2011-10-22 13:28:07 +04:00
}
2012-01-03 22:59:43 +04:00
/**
* @ group DDC - 984
* @ group DDC - 559
* @ group DDC - 1575
*/
public function testFullyQualifiedClassNameShouldBeGivenToNamingStrategy ()
{
$namingStrategy = new MyNamespacedNamingStrategy ();
$addressMetadata = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $namingStrategy );
$articleMetadata = new ClassMetadata ( 'DoctrineGlobal_Article' , $namingStrategy );
$routingMetadata = new ClassMetadata ( 'Doctrine\Tests\Models\Routing\RoutingLeg' , $namingStrategy );
$addressMetadata -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
$articleMetadata -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
$routingMetadata -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
$addressMetadata -> mapManyToMany ( array (
'fieldName' => 'user' ,
'targetEntity' => 'CmsUser'
));
$articleMetadata -> mapManyToMany ( array (
'fieldName' => 'author' ,
'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsUser'
));
$this -> assertEquals ( 'routing_routingleg' , $routingMetadata -> table [ 'name' ]);
$this -> assertEquals ( 'cms_cmsaddress_cms_cmsuser' , $addressMetadata -> associationMappings [ 'user' ][ 'joinTable' ][ 'name' ]);
$this -> assertEquals ( 'doctrineglobal_article_cms_cmsuser' , $articleMetadata -> associationMappings [ 'author' ][ 'joinTable' ][ 'name' ]);
}
}
class MyNamespacedNamingStrategy extends \Doctrine\ORM\Mapping\DefaultNamingStrategy
{
/**
* { @ inheritdoc }
*/
public function classToTableName ( $className )
{
if ( strpos ( $className , '\\' ) !== false ) {
$className = str_replace ( '\\' , '_' , str_replace ( 'Doctrine\Tests\Models\\' , '' , $className ));
}
return strtolower ( $className );
}
2011-01-02 14:01:05 +03:00
}