2009-06-06 01:40:47 +04:00
< ? php
namespace Doctrine\Tests\ORM\Mapping ;
2012-07-30 04:04:59 +04:00
use Doctrine\ORM\Events ;
use Doctrine\ORM\Event\LifecycleEventArgs ;
use Doctrine\Tests\Models\Company\CompanyFixContract ;
use Doctrine\Tests\Models\Company\CompanyFlexContract ;
use Doctrine\Tests\Models\Company\ContractSubscriber ;
2012-07-30 05:03:29 +04:00
use Doctrine\Tests\Models\Company\FlexUltraContractSubscriber ;
2012-07-30 04:04:59 +04:00
use Doctrine\ORM\Mapping\ClassMetadata ;
use Doctrine\ORM\Mapping\ClassMetadataInfo ;
2009-06-06 01:40:47 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
2010-02-02 00:48:27 +03:00
abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
2009-06-06 01:40:47 +04:00
{
2010-02-02 00:48:27 +03:00
abstract protected function _loadDriver ();
2010-09-24 01:10:31 +04:00
public function createClassMetadata ( $entityClassName )
2009-06-06 01:40:47 +04:00
{
2010-02-02 00:48:27 +03:00
$mappingDriver = $this -> _loadDriver ();
2010-09-24 01:10:31 +04:00
$class = new ClassMetadata ( $entityClassName );
2012-01-02 20:06:22 +04:00
$class -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2010-09-24 01:10:31 +04:00
$mappingDriver -> loadMetadataForClass ( $entityClassName , $class );
2010-02-02 00:48:27 +03:00
return $class ;
2009-08-13 15:03:26 +04:00
}
2010-02-02 00:48:27 +03:00
2012-03-25 20:34:52 +04:00
/**
* @ param \Doctrine\ORM\EntityManager $entityClassName
* @ return \Doctrine\ORM\Mapping\ClassMetadataFactory
*/
protected function createClassMetadataFactory ( \Doctrine\ORM\EntityManager $em = null )
{
$driver = $this -> _loadDriver ();
$em = $em ? : $this -> _getTestEntityManager ();
$factory = new \Doctrine\ORM\Mapping\ClassMetadataFactory ();
$em -> getConfiguration () -> setMetadataDriverImpl ( $driver );
$factory -> setEntityManager ( $em );
return $factory ;
}
2010-09-24 01:10:31 +04:00
public function testLoadMapping ()
{
$entityClassName = 'Doctrine\Tests\ORM\Mapping\User' ;
return $this -> createClassMetadata ( $entityClassName );
}
2010-02-02 00:48:27 +03:00
/**
* @ depends testLoadMapping
* @ param ClassMetadata $class
*/
public function testEntityTableNameAndInheritance ( $class )
2009-08-13 15:03:26 +04:00
{
2009-06-06 01:40:47 +04:00
$this -> assertEquals ( 'cms_users' , $class -> getTableName ());
2010-03-15 20:19:00 +03:00
$this -> assertEquals ( ClassMetadata :: INHERITANCE_TYPE_NONE , $class -> inheritanceType );
2010-02-02 00:48:27 +03:00
return $class ;
}
2010-06-14 00:59:56 +04:00
/**
* @ depends testEntityTableNameAndInheritance
* @ param ClassMetadata $class
*/
public function testEntityIndexes ( $class )
{
$this -> assertArrayHasKey ( 'indexes' , $class -> table , 'ClassMetadata should have indexes key in table property.' );
$this -> assertEquals ( array (
'name_idx' => array ( 'columns' => array ( 'name' )),
0 => array ( 'columns' => array ( 'user_email' ))
), $class -> table [ 'indexes' ]);
return $class ;
}
2010-04-28 22:27:53 +04:00
/**
* @ depends testEntityTableNameAndInheritance
* @ param ClassMetadata $class
*/
public function testEntityUniqueConstraints ( $class )
{
$this -> assertArrayHasKey ( 'uniqueConstraints' , $class -> table ,
'ClassMetadata should have uniqueConstraints key in table property when Unique Constraints are set.' );
2011-11-15 23:03:13 +04:00
2010-04-28 22:27:53 +04:00
$this -> assertEquals ( array (
" search_idx " => array ( " columns " => array ( " name " , " user_email " ))
), $class -> table [ 'uniqueConstraints' ]);
return $class ;
}
2010-05-01 05:28:18 +04:00
/**
* @ depends testEntityTableNameAndInheritance
* @ param ClassMetadata $class
*/
2012-01-26 17:36:56 +04:00
public function testEntityOptions ( $class )
{
$this -> assertArrayHasKey ( 'options' , $class -> table , 'ClassMetadata should have options key in table property.' );
$this -> assertEquals ( array (
'foo' => 'bar' , 'baz' => array ( 'key' => 'val' )
), $class -> table [ 'options' ]);
return $class ;
}
/**
* @ depends testEntityOptions
* @ param ClassMetadata $class
*/
2010-05-01 05:28:18 +04:00
public function testEntitySequence ( $class )
{
2011-02-20 00:50:58 +03:00
$this -> assertInternalType ( 'array' , $class -> sequenceGeneratorDefinition , 'No Sequence Definition set on this driver.' );
2010-05-01 05:28:18 +04:00
$this -> assertEquals (
array (
'sequenceName' => 'tablename_seq' ,
'allocationSize' => 100 ,
'initialValue' => 1 ,
),
$class -> sequenceGeneratorDefinition
);
}
2011-11-30 01:20:00 +04:00
2011-11-28 22:41:54 +04:00
public function testEntityCustomGenerator ()
{
$class = $this -> createClassMetadata ( 'Doctrine\Tests\ORM\Mapping\Animal' );
2011-11-30 01:20:00 +04:00
$this -> assertEquals ( ClassMetadata :: GENERATOR_TYPE_CUSTOM ,
2011-11-28 22:41:54 +04:00
$class -> generatorType , " Generator Type " );
$this -> assertEquals (
2012-01-08 16:20:35 +04:00
array ( " class " => " stdClass " ),
2011-11-28 22:41:54 +04:00
$class -> customGeneratorDefinition ,
" Custom Generator Definition " );
}
2010-05-01 05:28:18 +04:00
2010-04-28 22:27:53 +04:00
2010-02-02 00:48:27 +03:00
/**
* @ depends testEntityTableNameAndInheritance
* @ param ClassMetadata $class
*/
public function testFieldMappings ( $class )
{
2012-12-16 15:57:53 +04:00
$this -> assertEquals ( 4 , count ( $class -> fieldMappings ));
2009-06-06 01:40:47 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'name' ]));
2010-02-02 00:48:27 +03:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'email' ]));
2012-12-16 15:57:53 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'version' ]));
2010-02-02 00:48:27 +03:00
2010-06-22 22:24:05 +04:00
return $class ;
}
2012-12-16 15:57:53 +04:00
/**
* @ depends testFieldMappings
* @ param ClassMetadata $class
*/
public function testVersionedField ( $class )
{
$this -> assertTrue ( $class -> isVersioned );
$this -> assertEquals ( " version " , $class -> versionField );
}
2010-06-22 22:24:05 +04:00
/**
* @ depends testEntityTableNameAndInheritance
* @ param ClassMetadata $class
*/
public function testFieldMappingsColumnNames ( $class )
{
$this -> assertEquals ( " id " , $class -> fieldMappings [ 'id' ][ 'columnName' ]);
$this -> assertEquals ( " name " , $class -> fieldMappings [ 'name' ][ 'columnName' ]);
$this -> assertEquals ( " user_email " , $class -> fieldMappings [ 'email' ][ 'columnName' ]);
return $class ;
}
/**
* @ depends testEntityTableNameAndInheritance
* @ param ClassMetadata $class
*/
public function testStringFieldMappings ( $class )
{
2009-06-06 01:40:47 +04:00
$this -> assertEquals ( 'string' , $class -> fieldMappings [ 'name' ][ 'type' ]);
2010-06-22 22:24:05 +04:00
$this -> assertEquals ( 50 , $class -> fieldMappings [ 'name' ][ 'length' ]);
2010-01-28 02:00:55 +03:00
$this -> assertTrue ( $class -> fieldMappings [ 'name' ][ 'nullable' ]);
$this -> assertTrue ( $class -> fieldMappings [ 'name' ][ 'unique' ]);
2010-02-02 00:48:27 +03:00
2012-01-27 14:05:47 +04:00
$expected = array ( 'foo' => 'bar' , 'baz' => array ( 'key' => 'val' ));
$this -> assertEquals ( $expected , $class -> fieldMappings [ 'name' ][ 'options' ]);
2010-02-02 00:48:27 +03:00
return $class ;
}
/**
* @ depends testFieldMappings
* @ param ClassMetadata $class
*/
public function testIdentifier ( $class )
{
2009-06-06 01:40:47 +04:00
$this -> assertEquals ( array ( 'id' ), $class -> identifier );
2011-11-15 23:03:13 +04:00
$this -> assertEquals ( 'integer' , $class -> fieldMappings [ 'id' ][ 'type' ]);
2010-03-15 20:19:00 +03:00
$this -> assertEquals ( ClassMetadata :: GENERATOR_TYPE_AUTO , $class -> generatorType , " ID-Generator is not ClassMetadata::GENERATOR_TYPE_AUTO " );
2010-02-02 00:48:27 +03:00
return $class ;
}
/**
* @ depends testIdentifier
* @ param ClassMetadata $class
*/
public function testAssocations ( $class )
{
2009-06-06 01:40:47 +04:00
$this -> assertEquals ( 3 , count ( $class -> associationMappings ));
2010-02-02 00:48:27 +03:00
return $class ;
}
/**
* @ depends testAssocations
* @ param ClassMetadata $class
*/
public function testOwningOneToOneAssocation ( $class )
{
2009-06-06 01:40:47 +04:00
$this -> assertTrue ( isset ( $class -> associationMappings [ 'address' ]));
2010-08-09 15:13:21 +04:00
$this -> assertTrue ( $class -> associationMappings [ 'address' ][ 'isOwningSide' ]);
$this -> assertEquals ( 'user' , $class -> associationMappings [ 'address' ][ 'inversedBy' ]);
2010-01-22 18:10:13 +03:00
// Check cascading
2010-08-09 15:13:21 +04:00
$this -> assertTrue ( $class -> associationMappings [ 'address' ][ 'isCascadeRemove' ]);
$this -> assertFalse ( $class -> associationMappings [ 'address' ][ 'isCascadePersist' ]);
$this -> assertFalse ( $class -> associationMappings [ 'address' ][ 'isCascadeRefresh' ]);
$this -> assertFalse ( $class -> associationMappings [ 'address' ][ 'isCascadeDetach' ]);
$this -> assertFalse ( $class -> associationMappings [ 'address' ][ 'isCascadeMerge' ]);
2010-02-02 00:48:27 +03:00
return $class ;
}
/**
* @ depends testOwningOneToOneAssocation
* @ param ClassMetadata $class
*/
public function testInverseOneToManyAssociation ( $class )
{
2009-06-06 01:40:47 +04:00
$this -> assertTrue ( isset ( $class -> associationMappings [ 'phonenumbers' ]));
2010-08-09 15:13:21 +04:00
$this -> assertFalse ( $class -> associationMappings [ 'phonenumbers' ][ 'isOwningSide' ]);
$this -> assertTrue ( $class -> associationMappings [ 'phonenumbers' ][ 'isCascadePersist' ]);
2011-09-06 08:58:16 +04:00
$this -> assertTrue ( $class -> associationMappings [ 'phonenumbers' ][ 'isCascadeRemove' ]);
2010-08-09 15:13:21 +04:00
$this -> assertFalse ( $class -> associationMappings [ 'phonenumbers' ][ 'isCascadeRefresh' ]);
$this -> assertFalse ( $class -> associationMappings [ 'phonenumbers' ][ 'isCascadeDetach' ]);
$this -> assertFalse ( $class -> associationMappings [ 'phonenumbers' ][ 'isCascadeMerge' ]);
2011-07-31 13:32:57 +04:00
$this -> assertTrue ( $class -> associationMappings [ 'phonenumbers' ][ 'orphanRemoval' ]);
2010-02-02 00:48:27 +03:00
2010-02-14 22:38:22 +03:00
// Test Order By
2010-08-09 15:13:21 +04:00
$this -> assertEquals ( array ( 'number' => 'ASC' ), $class -> associationMappings [ 'phonenumbers' ][ 'orderBy' ]);
2010-02-14 22:38:22 +03:00
2010-02-02 00:48:27 +03:00
return $class ;
}
/**
* @ depends testInverseOneToManyAssociation
* @ param ClassMetadata $class
*/
public function testManyToManyAssociationWithCascadeAll ( $class )
{
2009-06-06 01:40:47 +04:00
$this -> assertTrue ( isset ( $class -> associationMappings [ 'groups' ]));
2010-08-09 15:13:21 +04:00
$this -> assertTrue ( $class -> associationMappings [ 'groups' ][ 'isOwningSide' ]);
2010-01-22 18:10:13 +03:00
// Make sure that cascade-all works as expected
2010-08-09 15:13:21 +04:00
$this -> assertTrue ( $class -> associationMappings [ 'groups' ][ 'isCascadeRemove' ]);
$this -> assertTrue ( $class -> associationMappings [ 'groups' ][ 'isCascadePersist' ]);
$this -> assertTrue ( $class -> associationMappings [ 'groups' ][ 'isCascadeRefresh' ]);
$this -> assertTrue ( $class -> associationMappings [ 'groups' ][ 'isCascadeDetach' ]);
$this -> assertTrue ( $class -> associationMappings [ 'groups' ][ 'isCascadeMerge' ]);
2010-01-28 02:00:55 +03:00
2010-08-09 15:13:21 +04:00
$this -> assertFalse ( isset ( $class -> associationMappings [ 'groups' ][ 'orderBy' ]));
2010-02-14 22:38:22 +03:00
2010-02-02 00:48:27 +03:00
return $class ;
}
/**
* @ depends testManyToManyAssociationWithCascadeAll
* @ param ClassMetadata $class
*/
public function testLifecycleCallbacks ( $class )
{
$this -> assertEquals ( count ( $class -> lifecycleCallbacks ), 2 );
$this -> assertEquals ( $class -> lifecycleCallbacks [ 'prePersist' ][ 0 ], 'doStuffOnPrePersist' );
$this -> assertEquals ( $class -> lifecycleCallbacks [ 'postPersist' ][ 0 ], 'doStuffOnPostPersist' );
return $class ;
}
/**
2010-03-15 22:29:07 +03:00
* @ depends testManyToManyAssociationWithCascadeAll
* @ param ClassMetadata $class
*/
2010-03-17 01:41:09 +03:00
public function testLifecycleCallbacksSupportMultipleMethodNames ( $class )
{
2010-03-15 22:29:07 +03:00
$this -> assertEquals ( count ( $class -> lifecycleCallbacks [ 'prePersist' ]), 2 );
$this -> assertEquals ( $class -> lifecycleCallbacks [ 'prePersist' ][ 1 ], 'doOtherStuffOnPrePersistToo' );
return $class ;
}
/**
* @ depends testLifecycleCallbacksSupportMultipleMethodNames
2010-02-02 00:48:27 +03:00
* @ param ClassMetadata $class
*/
public function testJoinColumnUniqueAndNullable ( $class )
{
2010-01-28 02:00:55 +03:00
// Non-Nullability of Join Column
2010-08-09 15:13:21 +04:00
$this -> assertFalse ( $class -> associationMappings [ 'groups' ][ 'joinTable' ][ 'joinColumns' ][ 0 ][ 'nullable' ]);
$this -> assertFalse ( $class -> associationMappings [ 'groups' ][ 'joinTable' ][ 'joinColumns' ][ 0 ][ 'unique' ]);
2010-02-02 00:48:27 +03:00
return $class ;
}
/**
* @ depends testJoinColumnUniqueAndNullable
* @ param ClassMetadata $class
*/
public function testColumnDefinition ( $class )
{
$this -> assertEquals ( " CHAR(32) NOT NULL " , $class -> fieldMappings [ 'email' ][ 'columnDefinition' ]);
2010-08-09 15:13:21 +04:00
$this -> assertEquals ( " INT NULL " , $class -> associationMappings [ 'groups' ][ 'joinTable' ][ 'inverseJoinColumns' ][ 0 ][ 'columnDefinition' ]);
2010-02-02 00:48:27 +03:00
return $class ;
2009-06-06 01:40:47 +04:00
}
2010-03-17 17:20:18 +03:00
/**
* @ depends testColumnDefinition
* @ param ClassMetadata $class
*/
2011-06-28 23:50:14 +04:00
public function testJoinColumnOnDelete ( $class )
2010-03-17 17:20:18 +03:00
{
2010-08-09 15:13:21 +04:00
$this -> assertEquals ( 'CASCADE' , $class -> associationMappings [ 'address' ][ 'joinColumns' ][ 0 ][ 'onDelete' ]);
2010-03-17 17:20:18 +03:00
return $class ;
}
2010-09-24 01:10:31 +04:00
/**
* @ group DDC - 514
*/
public function testDiscriminatorColumnDefaults ()
{
if ( strpos ( get_class ( $this ), 'PHPMappingDriver' ) !== false ) {
$this -> markTestSkipped ( 'PHP Mapping Drivers have no defaults.' );
}
$class = $this -> createClassMetadata ( 'Doctrine\Tests\ORM\Mapping\Animal' );
$this -> assertEquals (
2012-07-08 11:06:52 +04:00
array ( 'name' => 'discr' , 'type' => 'string' , 'length' => '32' , 'fieldName' => 'discr' , 'columnDefinition' => null ),
2010-09-24 01:10:31 +04:00
$class -> discriminatorColumn
);
}
2011-11-15 23:03:13 +04:00
2011-09-08 18:56:05 +04:00
/**
* @ group DDC - 869
*/
2011-09-08 19:55:55 +04:00
public function testMappedSuperclassWithRepository ()
2011-09-08 18:56:05 +04:00
{
$em = $this -> _getTestEntityManager ();
2012-03-25 20:34:52 +04:00
$factory = $this -> createClassMetadataFactory ( $em );
2011-11-15 23:03:13 +04:00
2011-09-08 18:56:05 +04:00
$class = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC869\DDC869CreditCardPayment' );
2011-11-15 23:03:13 +04:00
2011-09-08 18:56:05 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'value' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'creditCardNumber' ]));
$this -> assertEquals ( $class -> customRepositoryClassName , " Doctrine \T ests \ Models \ DDC869 \ DDC869PaymentRepository " );
2011-11-15 23:03:13 +04:00
$this -> assertInstanceOf ( " Doctrine \T ests \ Models \ DDC869 \ DDC869PaymentRepository " ,
2011-09-08 18:56:05 +04:00
$em -> getRepository ( " Doctrine \T ests \ Models \ DDC869 \ DDC869CreditCardPayment " ));
$this -> assertTrue ( $em -> getRepository ( " Doctrine \T ests \ Models \ DDC869 \ DDC869ChequePayment " ) -> isTrue ());
2011-11-15 23:03:13 +04:00
2011-09-08 18:56:05 +04:00
$class = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC869\DDC869ChequePayment' );
2011-11-15 23:03:13 +04:00
2011-09-08 18:56:05 +04:00
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'id' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'value' ]));
$this -> assertTrue ( isset ( $class -> fieldMappings [ 'serialNumber' ]));
$this -> assertEquals ( $class -> customRepositoryClassName , " Doctrine \T ests \ Models \ DDC869 \ DDC869PaymentRepository " );
2011-11-15 23:03:13 +04:00
$this -> assertInstanceOf ( " Doctrine \T ests \ Models \ DDC869 \ DDC869PaymentRepository " ,
2011-09-08 18:56:05 +04:00
$em -> getRepository ( " Doctrine \T ests \ Models \ DDC869 \ DDC869ChequePayment " ));
$this -> assertTrue ( $em -> getRepository ( " Doctrine \T ests \ Models \ DDC869 \ DDC869ChequePayment " ) -> isTrue ());
}
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
/**
* @ group DDC - 1476
*/
public function testDefaultFieldType ()
{
2012-02-02 06:57:39 +04:00
$factory = $this -> createClassMetadataFactory ();
$class = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC1476\DDC1476EntityWithDefaultFieldType' );
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertArrayHasKey ( 'id' , $class -> fieldMappings );
$this -> assertArrayHasKey ( 'name' , $class -> fieldMappings );
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertArrayHasKey ( 'type' , $class -> fieldMappings [ 'id' ]);
$this -> assertArrayHasKey ( 'type' , $class -> fieldMappings [ 'name' ]);
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertEquals ( 'string' , $class -> fieldMappings [ 'id' ][ 'type' ]);
$this -> assertEquals ( 'string' , $class -> fieldMappings [ 'name' ][ 'type' ]);
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertArrayHasKey ( 'fieldName' , $class -> fieldMappings [ 'id' ]);
$this -> assertArrayHasKey ( 'fieldName' , $class -> fieldMappings [ 'name' ]);
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertEquals ( 'id' , $class -> fieldMappings [ 'id' ][ 'fieldName' ]);
$this -> assertEquals ( 'name' , $class -> fieldMappings [ 'name' ][ 'fieldName' ]);
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertArrayHasKey ( 'columnName' , $class -> fieldMappings [ 'id' ]);
$this -> assertArrayHasKey ( 'columnName' , $class -> fieldMappings [ 'name' ]);
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertEquals ( 'id' , $class -> fieldMappings [ 'id' ][ 'columnName' ]);
$this -> assertEquals ( 'name' , $class -> fieldMappings [ 'name' ][ 'columnName' ]);
2011-11-15 23:03:13 +04:00
2011-11-14 19:15:43 +04:00
$this -> assertEquals ( ClassMetadataInfo :: GENERATOR_TYPE_NONE , $class -> generatorType );
}
2011-12-20 01:56:19 +04:00
2011-12-05 23:35:49 +04:00
/**
* @ group DDC - 1170
*/
public function testIdentifierColumnDefinition ()
{
$class = $this -> createClassMetadata ( __NAMESPACE__ . '\DDC1170Entity' );
2011-12-20 01:56:19 +04:00
2011-12-05 23:35:49 +04:00
$this -> assertArrayHasKey ( 'id' , $class -> fieldMappings );
$this -> assertArrayHasKey ( 'value' , $class -> fieldMappings );
2011-12-20 01:56:19 +04:00
2011-12-05 23:35:49 +04:00
$this -> assertArrayHasKey ( 'columnDefinition' , $class -> fieldMappings [ 'id' ]);
$this -> assertArrayHasKey ( 'columnDefinition' , $class -> fieldMappings [ 'value' ]);
2011-12-20 01:56:19 +04:00
2011-12-05 23:35:49 +04:00
$this -> assertEquals ( " INT unsigned NOT NULL " , $class -> fieldMappings [ 'id' ][ 'columnDefinition' ]);
$this -> assertEquals ( " VARCHAR(255) NOT NULL " , $class -> fieldMappings [ 'value' ][ 'columnDefinition' ]);
}
2011-12-23 20:41:03 +04:00
/**
* @ group DDC - 559
*/
public function testNamingStrategy ()
{
$em = $this -> _getTestEntityManager ();
2012-03-25 20:34:52 +04:00
$factory = $this -> createClassMetadataFactory ( $em );
2011-12-23 20:41:03 +04:00
2011-12-24 18:01:25 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Mapping\DefaultNamingStrategy' , $em -> getConfiguration () -> getNamingStrategy ());
$em -> getConfiguration () -> setNamingStrategy ( new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy ( CASE_UPPER ));
$this -> assertInstanceOf ( 'Doctrine\ORM\Mapping\UnderscoreNamingStrategy' , $em -> getConfiguration () -> getNamingStrategy ());
2011-12-23 20:41:03 +04:00
$class = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC1476\DDC1476EntityWithDefaultFieldType' );
$this -> assertEquals ( 'ID' , $class -> columnNames [ 'id' ]);
$this -> assertEquals ( 'NAME' , $class -> columnNames [ 'name' ]);
$this -> assertEquals ( 'DDC1476ENTITY_WITH_DEFAULT_FIELD_TYPE' , $class -> table [ 'name' ]);
}
2012-02-14 05:22:49 +04:00
/**
* @ group DDC - 807
* @ group DDC - 553
*/
public function testDiscriminatorColumnDefinition ()
{
$class = $this -> createClassMetadata ( __NAMESPACE__ . '\DDC807Entity' );
$this -> assertArrayHasKey ( 'columnDefinition' , $class -> discriminatorColumn );
$this -> assertArrayHasKey ( 'name' , $class -> discriminatorColumn );
$this -> assertEquals ( " ENUM('ONE','TWO') " , $class -> discriminatorColumn [ 'columnDefinition' ]);
$this -> assertEquals ( " dtype " , $class -> discriminatorColumn [ 'name' ]);
}
2012-03-25 19:54:09 +04:00
/**
* @ group DDC - 889
*/
2012-03-25 20:34:52 +04:00
public function testInvalidEntityOrMappedSuperClassShouldMentionParentClasses ()
{
2013-01-20 23:31:22 +04:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' , 'Class "Doctrine\Tests\Models\DDC889\DDC889Class" sub class of "Doctrine\Tests\Models\DDC889\DDC889SuperClass" is not a valid entity or mapped super class.' );
2012-03-25 20:34:52 +04:00
$this -> createClassMetadata ( 'Doctrine\Tests\Models\DDC889\DDC889Class' );
}
/**
* @ group DDC - 889
*/
public function testIdentifierRequiredShouldMentionParentClasses ()
2012-03-25 19:54:09 +04:00
{
2012-03-25 20:34:52 +04:00
$factory = $this -> createClassMetadataFactory ();
2013-01-20 23:31:22 +04:00
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' , 'No identifier/primary key specified for Entity "Doctrine\Tests\Models\DDC889\DDC889Entity" sub class of "Doctrine\Tests\Models\DDC889\DDC889SuperClass". Every Entity must have an identifier/primary key.' );
2012-03-25 20:34:52 +04:00
$factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC889\DDC889Entity' );
2012-03-25 19:54:09 +04:00
}
2012-02-27 04:10:19 +04:00
2012-06-18 20:41:20 +04:00
public function testNamedQuery ()
{
$driver = $this -> _loadDriver ();
$class = $this -> createClassMetadata ( __NAMESPACE__ . '\User' );
$this -> assertCount ( 1 , $class -> getNamedQueries (), sprintf ( " Named queries not processed correctly by driver %s " , get_class ( $driver )));
}
2012-02-27 04:10:19 +04:00
/**
* @ group DDC - 1663
*/
public function testNamedNativeQuery ()
{
2013-01-20 23:31:22 +04:00
2012-02-27 04:10:19 +04:00
$class = $this -> createClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
2012-03-07 05:31:23 +04:00
//named native query
2012-02-27 04:10:19 +04:00
$this -> assertCount ( 3 , $class -> namedNativeQueries );
$this -> assertArrayHasKey ( 'find-all' , $class -> namedNativeQueries );
$this -> assertArrayHasKey ( 'find-by-id' , $class -> namedNativeQueries );
2012-03-07 05:31:23 +04:00
$findAllQuery = $class -> getNamedNativeQuery ( 'find-all' );
2012-02-27 04:10:19 +04:00
$this -> assertEquals ( 'find-all' , $findAllQuery [ 'name' ]);
$this -> assertEquals ( 'mapping-find-all' , $findAllQuery [ 'resultSetMapping' ]);
$this -> assertEquals ( 'SELECT id, country, city FROM cms_addresses' , $findAllQuery [ 'query' ]);
2012-03-07 05:31:23 +04:00
$findByIdQuery = $class -> getNamedNativeQuery ( 'find-by-id' );
2012-02-27 04:10:19 +04:00
$this -> assertEquals ( 'find-by-id' , $findByIdQuery [ 'name' ]);
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $findByIdQuery [ 'resultClass' ]);
$this -> assertEquals ( 'SELECT * FROM cms_addresses WHERE id = ?' , $findByIdQuery [ 'query' ]);
2012-03-07 05:31:23 +04:00
$countQuery = $class -> getNamedNativeQuery ( 'count' );
2012-02-27 04:10:19 +04:00
$this -> assertEquals ( 'count' , $countQuery [ 'name' ]);
$this -> assertEquals ( 'mapping-count' , $countQuery [ 'resultSetMapping' ]);
$this -> assertEquals ( 'SELECT COUNT(*) AS count FROM cms_addresses' , $countQuery [ 'query' ]);
2012-03-07 05:31:23 +04:00
// result set mapping
$this -> assertCount ( 3 , $class -> sqlResultSetMappings );
$this -> assertArrayHasKey ( 'mapping-count' , $class -> sqlResultSetMappings );
$this -> assertArrayHasKey ( 'mapping-find-all' , $class -> sqlResultSetMappings );
$this -> assertArrayHasKey ( 'mapping-without-fields' , $class -> sqlResultSetMappings );
2013-01-20 23:31:22 +04:00
2012-03-07 05:31:23 +04:00
$findAllMapping = $class -> getSqlResultSetMapping ( 'mapping-find-all' );
$this -> assertEquals ( 'mapping-find-all' , $findAllMapping [ 'name' ]);
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $findAllMapping [ 'entities' ][ 0 ][ 'entityClass' ]);
$this -> assertEquals ( array ( 'name' => 'id' , 'column' => 'id' ), $findAllMapping [ 'entities' ][ 0 ][ 'fields' ][ 0 ]);
$this -> assertEquals ( array ( 'name' => 'city' , 'column' => 'city' ), $findAllMapping [ 'entities' ][ 0 ][ 'fields' ][ 1 ]);
$this -> assertEquals ( array ( 'name' => 'country' , 'column' => 'country' ), $findAllMapping [ 'entities' ][ 0 ][ 'fields' ][ 2 ]);
$withoutFieldsMapping = $class -> getSqlResultSetMapping ( 'mapping-without-fields' );
$this -> assertEquals ( 'mapping-without-fields' , $withoutFieldsMapping [ 'name' ]);
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $withoutFieldsMapping [ 'entities' ][ 0 ][ 'entityClass' ]);
$this -> assertEquals ( array (), $withoutFieldsMapping [ 'entities' ][ 0 ][ 'fields' ]);
2013-01-20 23:31:22 +04:00
2012-03-07 05:31:23 +04:00
$countMapping = $class -> getSqlResultSetMapping ( 'mapping-count' );
$this -> assertEquals ( 'mapping-count' , $countMapping [ 'name' ]);
$this -> assertEquals ( array ( 'name' => 'count' ), $countMapping [ 'columns' ][ 0 ]);
2012-02-27 04:10:19 +04:00
}
/**
* @ group DDC - 1663
*/
public function testSqlResultSetMapping ()
{
2012-03-07 05:31:23 +04:00
$userMetadata = $this -> createClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$personMetadata = $this -> createClassMetadata ( 'Doctrine\Tests\Models\Company\CompanyPerson' );
// user asserts
2012-03-11 19:10:15 +04:00
$this -> assertCount ( 4 , $userMetadata -> getSqlResultSetMappings ());
2012-03-07 05:31:23 +04:00
$mapping = $userMetadata -> getSqlResultSetMapping ( 'mappingJoinedAddress' );
$this -> assertEquals ( array (), $mapping [ 'columns' ]);
$this -> assertEquals ( 'mappingJoinedAddress' , $mapping [ 'name' ]);
$this -> assertNull ( $mapping [ 'entities' ][ 0 ][ 'discriminatorColumn' ]);
$this -> assertEquals ( array ( 'name' => 'id' , 'column' => 'id' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 0 ]);
$this -> assertEquals ( array ( 'name' => 'name' , 'column' => 'name' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 1 ]);
$this -> assertEquals ( array ( 'name' => 'status' , 'column' => 'status' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 2 ]);
$this -> assertEquals ( array ( 'name' => 'address.zip' , 'column' => 'zip' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 3 ]);
$this -> assertEquals ( array ( 'name' => 'address.city' , 'column' => 'city' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 4 ]);
$this -> assertEquals ( array ( 'name' => 'address.country' , 'column' => 'country' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 5 ]);
$this -> assertEquals ( array ( 'name' => 'address.id' , 'column' => 'a_id' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 6 ]);
$this -> assertEquals ( $userMetadata -> name , $mapping [ 'entities' ][ 0 ][ 'entityClass' ]);
$mapping = $userMetadata -> getSqlResultSetMapping ( 'mappingJoinedPhonenumber' );
$this -> assertEquals ( array (), $mapping [ 'columns' ]);
$this -> assertEquals ( 'mappingJoinedPhonenumber' , $mapping [ 'name' ]);
$this -> assertNull ( $mapping [ 'entities' ][ 0 ][ 'discriminatorColumn' ]);
$this -> assertEquals ( array ( 'name' => 'id' , 'column' => 'id' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 0 ]);
$this -> assertEquals ( array ( 'name' => 'name' , 'column' => 'name' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 1 ]);
$this -> assertEquals ( array ( 'name' => 'status' , 'column' => 'status' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 2 ]);
$this -> assertEquals ( array ( 'name' => 'phonenumbers.phonenumber' , 'column' => 'number' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 3 ]);
$this -> assertEquals ( $userMetadata -> name , $mapping [ 'entities' ][ 0 ][ 'entityClass' ]);
$mapping = $userMetadata -> getSqlResultSetMapping ( 'mappingUserPhonenumberCount' );
$this -> assertEquals ( array ( 'name' => 'numphones' ), $mapping [ 'columns' ][ 0 ]);
$this -> assertEquals ( 'mappingUserPhonenumberCount' , $mapping [ 'name' ]);
$this -> assertNull ( $mapping [ 'entities' ][ 0 ][ 'discriminatorColumn' ]);
$this -> assertEquals ( array ( 'name' => 'id' , 'column' => 'id' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 0 ]);
$this -> assertEquals ( array ( 'name' => 'name' , 'column' => 'name' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 1 ]);
$this -> assertEquals ( array ( 'name' => 'status' , 'column' => 'status' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 2 ]);
$this -> assertEquals ( $userMetadata -> name , $mapping [ 'entities' ][ 0 ][ 'entityClass' ]);
2012-03-11 19:10:15 +04:00
$mapping = $userMetadata -> getSqlResultSetMapping ( 'mappingMultipleJoinsEntityResults' );
$this -> assertEquals ( array ( 'name' => 'numphones' ), $mapping [ 'columns' ][ 0 ]);
$this -> assertEquals ( 'mappingMultipleJoinsEntityResults' , $mapping [ 'name' ]);
$this -> assertNull ( $mapping [ 'entities' ][ 0 ][ 'discriminatorColumn' ]);
$this -> assertEquals ( array ( 'name' => 'id' , 'column' => 'u_id' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 0 ]);
$this -> assertEquals ( array ( 'name' => 'name' , 'column' => 'u_name' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 1 ]);
$this -> assertEquals ( array ( 'name' => 'status' , 'column' => 'u_status' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 2 ]);
$this -> assertEquals ( $userMetadata -> name , $mapping [ 'entities' ][ 0 ][ 'entityClass' ]);
$this -> assertNull ( $mapping [ 'entities' ][ 1 ][ 'discriminatorColumn' ]);
$this -> assertEquals ( array ( 'name' => 'id' , 'column' => 'a_id' ), $mapping [ 'entities' ][ 1 ][ 'fields' ][ 0 ]);
$this -> assertEquals ( array ( 'name' => 'zip' , 'column' => 'a_zip' ), $mapping [ 'entities' ][ 1 ][ 'fields' ][ 1 ]);
$this -> assertEquals ( array ( 'name' => 'country' , 'column' => 'a_country' ), $mapping [ 'entities' ][ 1 ][ 'fields' ][ 2 ]);
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $mapping [ 'entities' ][ 1 ][ 'entityClass' ]);
2012-03-07 05:31:23 +04:00
//person asserts
$this -> assertCount ( 1 , $personMetadata -> getSqlResultSetMappings ());
$mapping = $personMetadata -> getSqlResultSetMapping ( 'mappingFetchAll' );
$this -> assertEquals ( array (), $mapping [ 'columns' ]);
$this -> assertEquals ( 'mappingFetchAll' , $mapping [ 'name' ]);
$this -> assertEquals ( 'discriminator' , $mapping [ 'entities' ][ 0 ][ 'discriminatorColumn' ]);
$this -> assertEquals ( array ( 'name' => 'id' , 'column' => 'id' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 0 ]);
$this -> assertEquals ( array ( 'name' => 'name' , 'column' => 'name' ), $mapping [ 'entities' ][ 0 ][ 'fields' ][ 1 ]);
$this -> assertEquals ( $personMetadata -> name , $mapping [ 'entities' ][ 0 ][ 'entityClass' ]);
2012-02-27 04:10:19 +04:00
}
2012-02-02 06:57:39 +04:00
/*
* @ group DDC - 964
*/
public function testAssociationOverridesMapping ()
{
$factory = $this -> createClassMetadataFactory ();
$adminMetadata = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC964\DDC964Admin' );
$guestMetadata = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC964\DDC964Guest' );
2013-01-20 23:31:22 +04:00
2012-02-02 06:57:39 +04:00
// assert groups association mappings
$this -> assertArrayHasKey ( 'groups' , $guestMetadata -> associationMappings );
$this -> assertArrayHasKey ( 'groups' , $adminMetadata -> associationMappings );
$guestGroups = $guestMetadata -> associationMappings [ 'groups' ];
$adminGroups = $adminMetadata -> associationMappings [ 'groups' ];
// assert not override attributes
$this -> assertEquals ( $guestGroups [ 'fieldName' ], $adminGroups [ 'fieldName' ]);
$this -> assertEquals ( $guestGroups [ 'type' ], $adminGroups [ 'type' ]);
$this -> assertEquals ( $guestGroups [ 'mappedBy' ], $adminGroups [ 'mappedBy' ]);
$this -> assertEquals ( $guestGroups [ 'inversedBy' ], $adminGroups [ 'inversedBy' ]);
$this -> assertEquals ( $guestGroups [ 'isOwningSide' ], $adminGroups [ 'isOwningSide' ]);
$this -> assertEquals ( $guestGroups [ 'fetch' ], $adminGroups [ 'fetch' ]);
$this -> assertEquals ( $guestGroups [ 'isCascadeRemove' ], $adminGroups [ 'isCascadeRemove' ]);
$this -> assertEquals ( $guestGroups [ 'isCascadePersist' ], $adminGroups [ 'isCascadePersist' ]);
$this -> assertEquals ( $guestGroups [ 'isCascadeRefresh' ], $adminGroups [ 'isCascadeRefresh' ]);
$this -> assertEquals ( $guestGroups [ 'isCascadeMerge' ], $adminGroups [ 'isCascadeMerge' ]);
$this -> assertEquals ( $guestGroups [ 'isCascadeDetach' ], $adminGroups [ 'isCascadeDetach' ]);
// assert not override attributes
$this -> assertEquals ( 'ddc964_users_groups' , $guestGroups [ 'joinTable' ][ 'name' ]);
$this -> assertEquals ( 'user_id' , $guestGroups [ 'joinTable' ][ 'joinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'group_id' , $guestGroups [ 'joinTable' ][ 'inverseJoinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( array ( 'user_id' => 'id' ), $guestGroups [ 'relationToSourceKeyColumns' ]);
$this -> assertEquals ( array ( 'group_id' => 'id' ), $guestGroups [ 'relationToTargetKeyColumns' ]);
$this -> assertEquals ( array ( 'user_id' , 'group_id' ), $guestGroups [ 'joinTableColumns' ]);
$this -> assertEquals ( 'ddc964_users_admingroups' , $adminGroups [ 'joinTable' ][ 'name' ]);
$this -> assertEquals ( 'adminuser_id' , $adminGroups [ 'joinTable' ][ 'joinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'admingroup_id' , $adminGroups [ 'joinTable' ][ 'inverseJoinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( array ( 'adminuser_id' => 'id' ), $adminGroups [ 'relationToSourceKeyColumns' ]);
$this -> assertEquals ( array ( 'admingroup_id' => 'id' ), $adminGroups [ 'relationToTargetKeyColumns' ]);
$this -> assertEquals ( array ( 'adminuser_id' , 'admingroup_id' ), $adminGroups [ 'joinTableColumns' ]);
// assert address association mappings
$this -> assertArrayHasKey ( 'address' , $guestMetadata -> associationMappings );
$this -> assertArrayHasKey ( 'address' , $adminMetadata -> associationMappings );
$guestAddress = $guestMetadata -> associationMappings [ 'address' ];
$adminAddress = $adminMetadata -> associationMappings [ 'address' ];
// assert not override attributes
$this -> assertEquals ( $guestAddress [ 'fieldName' ], $adminAddress [ 'fieldName' ]);
$this -> assertEquals ( $guestAddress [ 'type' ], $adminAddress [ 'type' ]);
$this -> assertEquals ( $guestAddress [ 'mappedBy' ], $adminAddress [ 'mappedBy' ]);
$this -> assertEquals ( $guestAddress [ 'inversedBy' ], $adminAddress [ 'inversedBy' ]);
$this -> assertEquals ( $guestAddress [ 'isOwningSide' ], $adminAddress [ 'isOwningSide' ]);
$this -> assertEquals ( $guestAddress [ 'fetch' ], $adminAddress [ 'fetch' ]);
$this -> assertEquals ( $guestAddress [ 'isCascadeRemove' ], $adminAddress [ 'isCascadeRemove' ]);
$this -> assertEquals ( $guestAddress [ 'isCascadePersist' ], $adminAddress [ 'isCascadePersist' ]);
$this -> assertEquals ( $guestAddress [ 'isCascadeRefresh' ], $adminAddress [ 'isCascadeRefresh' ]);
$this -> assertEquals ( $guestAddress [ 'isCascadeMerge' ], $adminAddress [ 'isCascadeMerge' ]);
$this -> assertEquals ( $guestAddress [ 'isCascadeDetach' ], $adminAddress [ 'isCascadeDetach' ]);
2013-01-20 23:31:22 +04:00
2012-02-02 06:57:39 +04:00
// assert override
$this -> assertEquals ( 'address_id' , $guestAddress [ 'joinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( array ( 'address_id' => 'id' ), $guestAddress [ 'sourceToTargetKeyColumns' ]);
$this -> assertEquals ( array ( 'address_id' => 'address_id' ), $guestAddress [ 'joinColumnFieldNames' ]);
$this -> assertEquals ( array ( 'id' => 'address_id' ), $guestAddress [ 'targetToSourceKeyColumns' ]);
$this -> assertEquals ( 'adminaddress_id' , $adminAddress [ 'joinColumns' ][ 0 ][ 'name' ]);
$this -> assertEquals ( array ( 'adminaddress_id' => 'id' ), $adminAddress [ 'sourceToTargetKeyColumns' ]);
$this -> assertEquals ( array ( 'adminaddress_id' => 'adminaddress_id' ), $adminAddress [ 'joinColumnFieldNames' ]);
$this -> assertEquals ( array ( 'id' => 'adminaddress_id' ), $adminAddress [ 'targetToSourceKeyColumns' ]);
}
2012-04-14 09:10:44 +04:00
/**
* @ group DDC - 964
*/
public function testAttributeOverridesMapping ()
{
$factory = $this -> createClassMetadataFactory ();
$guestMetadata = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC964\DDC964Guest' );
$adminMetadata = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\DDC964\DDC964Admin' );
$this -> assertTrue ( $adminMetadata -> fieldMappings [ 'id' ][ 'id' ]);
$this -> assertEquals ( 'id' , $adminMetadata -> fieldMappings [ 'id' ][ 'fieldName' ]);
$this -> assertEquals ( 'user_id' , $adminMetadata -> fieldMappings [ 'id' ][ 'columnName' ]);
$this -> assertEquals ( array ( 'user_id' => 'id' , 'user_name' => 'name' ), $adminMetadata -> fieldNames );
$this -> assertEquals ( array ( 'id' => 'user_id' , 'name' => 'user_name' ), $adminMetadata -> columnNames );
$this -> assertEquals ( 150 , $adminMetadata -> fieldMappings [ 'id' ][ 'length' ]);
$this -> assertEquals ( 'name' , $adminMetadata -> fieldMappings [ 'name' ][ 'fieldName' ]);
$this -> assertEquals ( 'user_name' , $adminMetadata -> fieldMappings [ 'name' ][ 'columnName' ]);
$this -> assertEquals ( 250 , $adminMetadata -> fieldMappings [ 'name' ][ 'length' ]);
$this -> assertTrue ( $adminMetadata -> fieldMappings [ 'name' ][ 'nullable' ]);
$this -> assertFalse ( $adminMetadata -> fieldMappings [ 'name' ][ 'unique' ]);
$this -> assertTrue ( $guestMetadata -> fieldMappings [ 'id' ][ 'id' ]);
$this -> assertEquals ( 'guest_id' , $guestMetadata -> fieldMappings [ 'id' ][ 'columnName' ]);
$this -> assertEquals ( 'id' , $guestMetadata -> fieldMappings [ 'id' ][ 'fieldName' ]);
$this -> assertEquals ( array ( 'guest_id' => 'id' , 'guest_name' => 'name' ), $guestMetadata -> fieldNames );
$this -> assertEquals ( array ( 'id' => 'guest_id' , 'name' => 'guest_name' ), $guestMetadata -> columnNames );
$this -> assertEquals ( 140 , $guestMetadata -> fieldMappings [ 'id' ][ 'length' ]);
$this -> assertEquals ( 'name' , $guestMetadata -> fieldMappings [ 'name' ][ 'fieldName' ]);
$this -> assertEquals ( 'guest_name' , $guestMetadata -> fieldMappings [ 'name' ][ 'columnName' ]);
$this -> assertEquals ( 240 , $guestMetadata -> fieldMappings [ 'name' ][ 'length' ]);
$this -> assertFalse ( $guestMetadata -> fieldMappings [ 'name' ][ 'nullable' ]);
$this -> assertTrue ( $guestMetadata -> fieldMappings [ 'name' ][ 'unique' ]);
}
2012-07-30 04:46:38 +04:00
/**
* @ group DDC - 1955
*/
public function testEntityListeners ()
{
2012-08-01 05:20:21 +04:00
if ( ! ( $this instanceof AnnotationDriverTest )
&& ! ( $this instanceof XmlMappingDriverTest )) {
2012-07-30 04:46:38 +04:00
$this -> markTestIncomplete ();
}
$em = $this -> _getTestEntityManager ();
$factory = $this -> createClassMetadataFactory ( $em );
$superClass = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\Company\CompanyContract' );
$flexClass = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\Company\CompanyFixContract' );
$fixClass = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\Company\CompanyFlexContract' );
$ultraClass = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\Company\CompanyFlexUltraContract' );
$this -> assertArrayHasKey ( Events :: prePersist , $superClass -> entityListeners );
$this -> assertArrayHasKey ( Events :: postPersist , $superClass -> entityListeners );
$this -> assertCount ( 1 , $superClass -> entityListeners [ Events :: prePersist ]);
$this -> assertCount ( 1 , $superClass -> entityListeners [ Events :: postPersist ]);
$postPersist = $superClass -> entityListeners [ Events :: postPersist ][ 0 ];
$prePersist = $superClass -> entityListeners [ Events :: prePersist ][ 0 ];
$this -> assertEquals ( 'Doctrine\Tests\Models\Company\ContractSubscriber' , $postPersist [ 'class' ]);
$this -> assertEquals ( 'Doctrine\Tests\Models\Company\ContractSubscriber' , $prePersist [ 'class' ]);
$this -> assertEquals ( 'postPersistHandler' , $postPersist [ 'method' ]);
$this -> assertEquals ( 'prePersistHandler' , $prePersist [ 'method' ]);
//Inherited listeners
$this -> assertEquals ( $fixClass -> entityListeners , $superClass -> entityListeners );
$this -> assertEquals ( $flexClass -> entityListeners , $superClass -> entityListeners );
//overrited listeners
$this -> assertArrayHasKey ( Events :: postPersist , $ultraClass -> entityListeners );
$this -> assertArrayHasKey ( Events :: prePersist , $ultraClass -> entityListeners );
$this -> assertCount ( 1 , $ultraClass -> entityListeners [ Events :: postPersist ]);
$this -> assertCount ( 3 , $ultraClass -> entityListeners [ Events :: prePersist ]);
$postPersist = $ultraClass -> entityListeners [ Events :: postPersist ][ 0 ];
$prePersist = $ultraClass -> entityListeners [ Events :: prePersist ][ 0 ];
$this -> assertEquals ( 'Doctrine\Tests\Models\Company\ContractSubscriber' , $postPersist [ 'class' ]);
$this -> assertEquals ( 'Doctrine\Tests\Models\Company\ContractSubscriber' , $prePersist [ 'class' ]);
$this -> assertEquals ( 'postPersistHandler' , $postPersist [ 'method' ]);
$this -> assertEquals ( 'prePersistHandler' , $prePersist [ 'method' ]);
$prePersist = $ultraClass -> entityListeners [ Events :: prePersist ][ 1 ];
$this -> assertEquals ( 'Doctrine\Tests\Models\Company\FlexUltraContractSubscriber' , $prePersist [ 'class' ]);
2012-07-30 05:03:29 +04:00
$this -> assertEquals ( 'prePersistHandler1' , $prePersist [ 'method' ]);
2012-07-30 04:46:38 +04:00
$prePersist = $ultraClass -> entityListeners [ Events :: prePersist ][ 2 ];
$this -> assertEquals ( 'Doctrine\Tests\Models\Company\FlexUltraContractSubscriber' , $prePersist [ 'class' ]);
2012-07-30 05:03:29 +04:00
$this -> assertEquals ( 'prePersistHandler2' , $prePersist [ 'method' ]);
2012-07-30 04:46:38 +04:00
}
2012-07-30 04:04:59 +04:00
/**
* @ group DDC - 1955
*/
public function testCallEntityListeners ()
{
2012-08-01 05:20:21 +04:00
if ( ! ( $this instanceof AnnotationDriverTest )
&& ! ( $this instanceof XmlMappingDriverTest )) {
2012-07-30 04:04:59 +04:00
$this -> markTestIncomplete ();
}
$em = $this -> _getTestEntityManager ();
$factory = $this -> createClassMetadataFactory ( $em );
$flexClass = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\Company\CompanyFixContract' );
$fixClass = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\Company\CompanyFlexContract' );
2012-07-30 05:03:29 +04:00
$ultraClass = $factory -> getMetadataFor ( 'Doctrine\Tests\Models\Company\CompanyFlexUltraContract' );
2012-07-30 04:04:59 +04:00
2012-07-31 05:26:55 +04:00
ContractSubscriber :: $prePersistCalls = array ();
ContractSubscriber :: $postPersistCalls = array ();
FlexUltraContractSubscriber :: $prePersistCalls = array ();
2012-07-30 04:04:59 +04:00
$fix = new CompanyFixContract ();
$fixArg = new LifecycleEventArgs ( $fix , $em );
$flex = new CompanyFlexContract ();
$flexArg = new LifecycleEventArgs ( $fix , $em );
2012-07-30 05:03:29 +04:00
$ultra = new CompanyFlexContract ();
$ultraArg = new LifecycleEventArgs ( $ultra , $em );
2012-07-30 04:04:59 +04:00
$fixClass -> dispatchEntityListeners ( Events :: prePersist , $fix , $fixArg );
$flexClass -> dispatchEntityListeners ( Events :: prePersist , $flex , $flexArg );
2012-07-30 05:03:29 +04:00
$ultraClass -> dispatchEntityListeners ( Events :: prePersist , $ultra , $ultraArg );
$this -> assertCount ( 3 , ContractSubscriber :: $prePersistCalls );
$this -> assertCount ( 2 , FlexUltraContractSubscriber :: $prePersistCalls );
2012-07-30 04:04:59 +04:00
$this -> assertSame ( $fix , ContractSubscriber :: $prePersistCalls [ 0 ][ 0 ]);
$this -> assertSame ( $fixArg , ContractSubscriber :: $prePersistCalls [ 0 ][ 1 ]);
2012-07-30 05:03:29 +04:00
$this -> assertSame ( $flex , ContractSubscriber :: $prePersistCalls [ 1 ][ 0 ]);
$this -> assertSame ( $flexArg , ContractSubscriber :: $prePersistCalls [ 1 ][ 1 ]);
$this -> assertSame ( $ultra , ContractSubscriber :: $prePersistCalls [ 2 ][ 0 ]);
$this -> assertSame ( $ultraArg , ContractSubscriber :: $prePersistCalls [ 2 ][ 1 ]);
$this -> assertSame ( $ultra , FlexUltraContractSubscriber :: $prePersistCalls [ 0 ][ 0 ]);
$this -> assertSame ( $ultraArg , FlexUltraContractSubscriber :: $prePersistCalls [ 0 ][ 1 ]);
$this -> assertSame ( $ultra , FlexUltraContractSubscriber :: $prePersistCalls [ 1 ][ 0 ]);
$this -> assertSame ( $ultraArg , FlexUltraContractSubscriber :: $prePersistCalls [ 1 ][ 1 ]);
2012-07-30 04:04:59 +04:00
$this -> assertCount ( 1 , ContractSubscriber :: $instances );
2012-07-31 05:26:55 +04:00
$this -> assertEmpty ( ContractSubscriber :: $postPersistCalls );
2012-07-30 04:04:59 +04:00
}
2009-08-13 15:03:26 +04:00
}
2009-06-06 01:40:47 +04:00
2010-02-14 22:38:22 +03:00
/**
* @ Entity
* @ HasLifecycleCallbacks
2010-06-14 00:59:56 +04:00
* @ Table (
* name = " cms_users " ,
* uniqueConstraints = { @ UniqueConstraint ( name = " search_idx " , columns = { " name " , " user_email " })},
2012-01-26 17:36:56 +04:00
* indexes = { @ Index ( name = " name_idx " , columns = { " name " }), @ Index ( name = " 0 " , columns = { " user_email " })},
* options = { " foo " : " bar " , " baz " : { " key " : " val " }}
2010-06-14 00:59:56 +04:00
* )
2012-06-18 20:41:20 +04:00
* @ NamedQueries ({ @ NamedQuery ( name = " all " , query = " SELECT u FROM __CLASS__ u " )})
2010-02-14 22:38:22 +03:00
*/
class User
{
2010-05-01 05:28:18 +04:00
/**
* @ Id
* @ Column ( type = " integer " )
* @ generatedValue ( strategy = " AUTO " )
* @ SequenceGenerator ( sequenceName = " tablename_seq " , initialValue = 1 , allocationSize = 100 )
**/
2010-02-14 22:38:22 +03:00
public $id ;
/**
2012-01-27 14:05:47 +04:00
* @ Column ( length = 50 , nullable = true , unique = true , options = { " foo " : " bar " , " baz " : { " key " : " val " }})
2010-02-14 22:38:22 +03:00
*/
public $name ;
2010-02-02 00:48:27 +03:00
2010-02-14 22:38:22 +03:00
/**
* @ Column ( name = " user_email " , columnDefinition = " CHAR(32) NOT NULL " )
*/
public $email ;
2009-08-25 01:05:55 +04:00
2010-02-14 22:38:22 +03:00
/**
2010-04-10 02:00:36 +04:00
* @ OneToOne ( targetEntity = " Address " , cascade = { " remove " }, inversedBy = " user " )
2011-06-28 23:50:14 +04:00
* @ JoinColumn ( onDelete = " CASCADE " )
2010-02-14 22:38:22 +03:00
*/
public $address ;
/**
2011-07-31 13:32:57 +04:00
* @ OneToMany ( targetEntity = " Phonenumber " , mappedBy = " user " , cascade = { " persist " }, orphanRemoval = true )
2010-02-27 00:26:06 +03:00
* @ OrderBy ({ " number " = " ASC " })
2010-02-14 22:38:22 +03:00
*/
public $phonenumbers ;
/**
* @ ManyToMany ( targetEntity = " Group " , cascade = { " all " })
* @ JoinTable ( name = " cms_user_groups " ,
* joinColumns = { @ JoinColumn ( name = " user_id " , referencedColumnName = " id " , nullable = false , unique = false )},
* inverseJoinColumns = { @ JoinColumn ( name = " group_id " , referencedColumnName = " id " , columnDefinition = " INT NULL " )}
* )
*/
public $groups ;
2012-12-16 15:57:53 +04:00
/**
* @ Column ( type = " integer " )
* @ Version
*/
public $version ;
2010-03-15 22:29:07 +03:00
2010-02-14 22:38:22 +03:00
/**
* @ PrePersist
*/
2009-08-25 01:05:55 +04:00
public function doStuffOnPrePersist ()
{
}
2010-03-15 22:29:07 +03:00
/**
* @ PrePersist
*/
public function doOtherStuffOnPrePersistToo () {
}
2010-02-14 22:38:22 +03:00
/**
* @ PostPersist
*/
2009-08-25 01:05:55 +04:00
public function doStuffOnPostPersist ()
{
2010-02-02 00:48:27 +03:00
2009-08-25 01:05:55 +04:00
}
2010-04-24 00:16:16 +04:00
public static function loadMetadata ( ClassMetadataInfo $metadata )
{
$metadata -> setInheritanceType ( ClassMetadataInfo :: INHERITANCE_TYPE_NONE );
$metadata -> setPrimaryTable ( array (
'name' => 'cms_users' ,
2012-01-26 17:36:56 +04:00
'options' => array ( 'foo' => 'bar' , 'baz' => array ( 'key' => 'val' )),
2010-04-24 00:16:16 +04:00
));
$metadata -> setChangeTrackingPolicy ( ClassMetadataInfo :: CHANGETRACKING_DEFERRED_IMPLICIT );
$metadata -> addLifecycleCallback ( 'doStuffOnPrePersist' , 'prePersist' );
$metadata -> addLifecycleCallback ( 'doOtherStuffOnPrePersistToo' , 'prePersist' );
$metadata -> addLifecycleCallback ( 'doStuffOnPostPersist' , 'postPersist' );
$metadata -> mapField ( array (
'id' => true ,
'fieldName' => 'id' ,
'type' => 'integer' ,
'columnName' => 'id' ,
));
$metadata -> mapField ( array (
'fieldName' => 'name' ,
'type' => 'string' ,
'length' => 50 ,
'unique' => true ,
'nullable' => true ,
'columnName' => 'name' ,
2012-01-27 14:05:47 +04:00
'options' => array ( 'foo' => 'bar' , 'baz' => array ( 'key' => 'val' )),
2010-04-24 00:16:16 +04:00
));
$metadata -> mapField ( array (
'fieldName' => 'email' ,
'type' => 'string' ,
'columnName' => 'user_email' ,
'columnDefinition' => 'CHAR(32) NOT NULL' ,
));
2012-12-16 15:57:53 +04:00
$mapping = array ( 'fieldName' => 'version' , 'type' => 'integer' );
$metadata -> setVersionMapping ( $mapping );
$metadata -> mapField ( $mapping );
2010-04-24 00:16:16 +04:00
$metadata -> setIdGeneratorType ( ClassMetadataInfo :: GENERATOR_TYPE_AUTO );
$metadata -> mapOneToOne ( array (
'fieldName' => 'address' ,
'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Address' ,
2011-11-15 23:03:13 +04:00
'cascade' =>
2010-04-24 00:16:16 +04:00
array (
0 => 'remove' ,
),
'mappedBy' => NULL ,
'inversedBy' => 'user' ,
2011-11-15 23:03:13 +04:00
'joinColumns' =>
2010-04-24 00:16:16 +04:00
array (
2011-11-15 23:03:13 +04:00
0 =>
2010-04-24 00:16:16 +04:00
array (
'name' => 'address_id' ,
'referencedColumnName' => 'id' ,
'onDelete' => 'CASCADE' ,
),
),
'orphanRemoval' => false ,
));
$metadata -> mapOneToMany ( array (
'fieldName' => 'phonenumbers' ,
'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Phonenumber' ,
2011-11-15 23:03:13 +04:00
'cascade' =>
2010-04-24 00:16:16 +04:00
array (
1 => 'persist' ,
),
'mappedBy' => 'user' ,
2011-07-31 13:32:57 +04:00
'orphanRemoval' => true ,
2011-11-15 23:03:13 +04:00
'orderBy' =>
2010-04-24 00:16:16 +04:00
array (
'number' => 'ASC' ,
),
));
$metadata -> mapManyToMany ( array (
'fieldName' => 'groups' ,
'targetEntity' => 'Doctrine\\Tests\\ORM\\Mapping\\Group' ,
2011-11-15 23:03:13 +04:00
'cascade' =>
2010-04-24 00:16:16 +04:00
array (
0 => 'remove' ,
1 => 'persist' ,
2 => 'refresh' ,
3 => 'merge' ,
4 => 'detach' ,
),
'mappedBy' => NULL ,
2011-11-15 23:03:13 +04:00
'joinTable' =>
2010-04-24 00:16:16 +04:00
array (
'name' => 'cms_users_groups' ,
2011-11-15 23:03:13 +04:00
'joinColumns' =>
2010-04-24 00:16:16 +04:00
array (
2011-11-15 23:03:13 +04:00
0 =>
2010-04-24 00:16:16 +04:00
array (
'name' => 'user_id' ,
'referencedColumnName' => 'id' ,
'unique' => false ,
'nullable' => false ,
),
),
2011-11-15 23:03:13 +04:00
'inverseJoinColumns' =>
2010-04-24 00:16:16 +04:00
array (
2011-11-15 23:03:13 +04:00
0 =>
2010-04-24 00:16:16 +04:00
array (
'name' => 'group_id' ,
'referencedColumnName' => 'id' ,
'columnDefinition' => 'INT NULL' ,
),
),
),
'orderBy' => NULL ,
));
2010-04-28 22:27:53 +04:00
$metadata -> table [ 'uniqueConstraints' ] = array (
'search_idx' => array ( 'columns' => array ( 'name' , 'user_email' )),
);
2010-06-14 00:59:56 +04:00
$metadata -> table [ 'indexes' ] = array (
'name_idx' => array ( 'columns' => array ( 'name' )), 0 => array ( 'columns' => array ( 'user_email' ))
);
2010-05-01 05:28:18 +04:00
$metadata -> setSequenceGeneratorDefinition ( array (
'sequenceName' => 'tablename_seq' ,
'allocationSize' => 100 ,
'initialValue' => 1 ,
));
2012-06-18 20:41:20 +04:00
$metadata -> addNamedQuery ( array (
'name' => 'all' ,
'query' => 'SELECT u FROM __CLASS__ u'
));
2010-04-24 00:16:16 +04:00
}
2010-09-22 01:14:45 +04:00
}
2010-09-24 01:10:31 +04:00
/**
* @ Entity
* @ InheritanceType ( " SINGLE_TABLE " )
* @ DiscriminatorMap ({ " cat " = " Cat " , " dog " = " Dog " })
2012-07-08 11:06:52 +04:00
* @ DiscriminatorColumn ( name = " discr " , length = 32 , type = " string " )
2010-09-24 01:10:31 +04:00
*/
abstract class Animal
{
/**
2011-11-28 22:54:54 +04:00
* @ Id @ Column ( type = " string " ) @ GeneratedValue ( strategy = " CUSTOM " )
2012-01-08 16:20:35 +04:00
* @ CustomIdGenerator ( class = " stdClass " )
2010-09-24 01:10:31 +04:00
*/
public $id ;
public static function loadMetadata ( ClassMetadataInfo $metadata )
{
2011-11-29 00:21:46 +04:00
$metadata -> setIdGeneratorType ( ClassMetadataInfo :: GENERATOR_TYPE_CUSTOM );
2012-01-08 16:20:35 +04:00
$metadata -> setCustomGeneratorDefinition ( array ( " class " => " stdClass " ));
2010-09-24 01:10:31 +04:00
}
}
/** @Entity */
class Cat extends Animal
{
public static function loadMetadata ( ClassMetadataInfo $metadata )
{
2011-11-15 23:03:13 +04:00
2010-09-24 01:10:31 +04:00
}
}
/** @Entity */
class Dog extends Animal
{
public static function loadMetadata ( ClassMetadataInfo $metadata )
{
2011-11-15 23:03:13 +04:00
2010-09-24 01:10:31 +04:00
}
2011-12-05 23:35:49 +04:00
}
/**
* @ Entity
*/
class DDC1170Entity
{
/**
2011-12-20 01:56:19 +04:00
* @ param string $value
2011-12-05 23:35:49 +04:00
*/
function __construct ( $value = null )
{
$this -> value = $value ;
}
2011-12-20 01:56:19 +04:00
2011-12-05 23:35:49 +04:00
/**
* @ Id
* @ GeneratedValue ( strategy = " NONE " )
* @ Column ( type = " integer " , columnDefinition = " INT unsigned NOT NULL " )
**/
private $id ;
/**
* @ Column ( columnDefinition = " VARCHAR(255) NOT NULL " )
*/
private $value ;
2011-12-20 01:56:19 +04:00
2011-12-05 23:35:49 +04:00
/**
* @ return integer
*/
public function getId ()
{
return $this -> id ;
}
/**
* @ return string
*/
public function getValue ()
{
return $this -> value ;
}
2011-12-20 01:56:19 +04:00
2011-12-05 23:35:49 +04:00
public static function loadMetadata ( ClassMetadataInfo $metadata )
{
$metadata -> mapField ( array (
'id' => true ,
'fieldName' => 'id' ,
'columnDefinition' => 'INT unsigned NOT NULL' ,
));
$metadata -> mapField ( array (
'fieldName' => 'value' ,
'columnDefinition' => 'VARCHAR(255) NOT NULL'
));
$metadata -> setIdGeneratorType ( ClassMetadataInfo :: GENERATOR_TYPE_NONE );
}
2011-10-15 19:38:55 +04:00
}
2012-02-14 05:22:49 +04:00
/**
* @ Entity
* @ InheritanceType ( " SINGLE_TABLE " )
* @ DiscriminatorMap ({ " ONE " = " DDC807SubClasse1 " , " TWO " = " DDC807SubClasse2 " })
* @ DiscriminatorColumn ( name = " dtype " , columnDefinition = " ENUM('ONE','TWO') " )
*/
class DDC807Entity
{
/**
* @ Id
* @ Column ( type = " integer " )
* @ GeneratedValue ( strategy = " NONE " )
**/
public $id ;
2013-01-20 23:31:22 +04:00
2012-02-14 05:22:49 +04:00
public static function loadMetadata ( ClassMetadataInfo $metadata )
{
$metadata -> mapField ( array (
'id' => true ,
'fieldName' => 'id' ,
));
$metadata -> setDiscriminatorColumn ( array (
'name' => " dtype " ,
'type' => " string " ,
'columnDefinition' => " ENUM('ONE','TWO') "
));
$metadata -> setIdGeneratorType ( ClassMetadataInfo :: GENERATOR_TYPE_NONE );
}
}
class DDC807SubClasse1 {}
class DDC807SubClasse2 {}
2011-10-15 19:38:55 +04:00
class Address {}
class Phonenumber {}
2011-12-22 02:56:25 +04:00
class Group {}