2009-12-08 21:52:26 +00:00
< ? php
namespace Doctrine\Tests\ORM\Tools ;
2016-04-21 18:28:04 +03:00
use Doctrine\ORM\Mapping\ClassMetadata ;
2016-12-08 18:01:04 +01:00
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs ;
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs ;
2009-12-08 21:52:26 +00:00
use Doctrine\ORM\Tools\SchemaTool ;
2010-03-18 22:36:27 +00:00
use Doctrine\ORM\Tools\ToolEvents ;
2016-12-08 18:01:04 +01:00
use Doctrine\Tests\Models\CMS\CmsAddress ;
use Doctrine\Tests\Models\CMS\CmsArticle ;
use Doctrine\Tests\Models\CMS\CmsComment ;
use Doctrine\Tests\Models\CMS\CmsEmployee ;
use Doctrine\Tests\Models\CMS\CmsGroup ;
use Doctrine\Tests\Models\CMS\CmsPhonenumber ;
use Doctrine\Tests\Models\CMS\CmsUser ;
use Doctrine\Tests\Models\Forum\ForumAvatar ;
use Doctrine\Tests\Models\Forum\ForumUser ;
use Doctrine\Tests\Models\NullDefault\NullDefaultColumn ;
2016-05-11 01:55:12 +07:00
use Doctrine\Tests\OrmTestCase ;
2009-12-08 21:52:26 +00:00
2016-05-11 01:55:12 +07:00
class SchemaToolTest extends OrmTestCase
2009-12-08 21:52:26 +00:00
{
2013-03-11 00:08:58 +00:00
public function testAddUniqueIndexForUniqueFieldAnnotation ()
2009-12-08 21:52:26 +00:00
{
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
2016-12-07 23:33:41 +01:00
$classes = [
2016-12-08 18:01:04 +01:00
$em -> getClassMetadata ( CmsAddress :: class ),
$em -> getClassMetadata ( CmsArticle :: class ),
$em -> getClassMetadata ( CmsComment :: class ),
$em -> getClassMetadata ( CmsEmployee :: class ),
$em -> getClassMetadata ( CmsGroup :: class ),
$em -> getClassMetadata ( CmsPhonenumber :: class ),
$em -> getClassMetadata ( CmsUser :: class ),
2016-12-07 23:33:41 +01:00
];
2009-12-08 21:52:26 +00:00
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
2011-03-05 10:08:12 +01:00
$this -> assertTrue ( $schema -> hasTable ( 'cms_users' ), " Table cms_users should exist. " );
2016-12-07 23:33:41 +01:00
$this -> assertTrue ( $schema -> getTable ( 'cms_users' ) -> columnsAreIndexed ([ 'username' ]), " username column should be indexed. " );
2009-12-08 21:52:26 +00:00
}
2010-01-20 22:35:18 +00:00
2012-01-26 14:36:56 +01:00
public function testAnnotationOptionsAttribute ()
2012-01-11 15:58:57 +01:00
{
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
2016-12-07 23:33:41 +01:00
$classes = [
2016-12-08 18:01:04 +01:00
$em -> getClassMetadata ( TestEntityWithAnnotationOptionsAttribute :: class ),
2016-12-07 23:33:41 +01:00
];
2012-01-11 15:58:57 +01:00
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
2016-12-07 23:33:41 +01:00
$expected = [ 'foo' => 'bar' , 'baz' => [ 'key' => 'val' ]];
2012-12-01 19:41:51 +01:00
$this -> assertEquals ( $expected , $schema -> getTable ( 'TestEntityWithAnnotationOptionsAttribute' ) -> getOptions (), " options annotation are passed to the tables options " );
2012-01-26 14:36:56 +01:00
$this -> assertEquals ( $expected , $schema -> getTable ( 'TestEntityWithAnnotationOptionsAttribute' ) -> getColumn ( 'test' ) -> getCustomSchemaOptions (), " options annotation are passed to the columns customSchemaOptions " );
2012-01-11 15:58:57 +01:00
}
2010-01-20 22:35:18 +00:00
/**
* @ group DDC - 200
*/
public function testPassColumnDefinitionToJoinColumn ()
{
$customColumnDef = " MEDIUMINT(6) UNSIGNED NOT NULL " ;
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
2016-12-08 18:01:04 +01:00
$avatar = $em -> getClassMetadata ( ForumAvatar :: class );
2010-01-20 22:35:18 +00:00
$avatar -> fieldMappings [ 'id' ][ 'columnDefinition' ] = $customColumnDef ;
2016-12-08 18:01:04 +01:00
$user = $em -> getClassMetadata ( ForumUser :: class );
2010-01-20 22:35:18 +00:00
2016-12-07 23:33:41 +01:00
$classes = [ $avatar , $user ];
2010-01-20 22:35:18 +00:00
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
$this -> assertTrue ( $schema -> hasTable ( 'forum_users' ));
$table = $schema -> getTable ( " forum_users " );
$this -> assertTrue ( $table -> hasColumn ( 'avatar_id' ));
$this -> assertEquals ( $customColumnDef , $table -> getColumn ( 'avatar_id' ) -> getColumnDefinition ());
}
2010-03-18 22:36:27 +00:00
/**
* @ group DDC - 283
*/
public function testPostGenerateEvents ()
{
$listener = new GenerateSchemaEventListener ();
$em = $this -> _getTestEntityManager ();
$em -> getEventManager () -> addEventListener (
2016-12-07 23:33:41 +01:00
[ ToolEvents :: postGenerateSchemaTable , ToolEvents :: postGenerateSchema ], $listener
2010-03-18 22:36:27 +00:00
);
$schemaTool = new SchemaTool ( $em );
2016-12-07 23:33:41 +01:00
$classes = [
2016-12-08 18:01:04 +01:00
$em -> getClassMetadata ( CmsAddress :: class ),
$em -> getClassMetadata ( CmsArticle :: class ),
$em -> getClassMetadata ( CmsComment :: class ),
$em -> getClassMetadata ( CmsEmployee :: class ),
$em -> getClassMetadata ( CmsGroup :: class ),
$em -> getClassMetadata ( CmsPhonenumber :: class ),
$em -> getClassMetadata ( CmsUser :: class ),
2016-12-07 23:33:41 +01:00
];
2010-03-18 22:36:27 +00:00
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
$this -> assertEquals ( count ( $classes ), $listener -> tableCalls );
$this -> assertTrue ( $listener -> schemaCalled );
}
2014-12-03 19:37:53 +00:00
public function testNullDefaultNotAddedToCustomSchemaOptions ()
{
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
2016-12-08 18:01:04 +01:00
$customSchemaOptions = $schemaTool -> getSchemaFromMetadata ([ $em -> getClassMetadata ( NullDefaultColumn :: class )])
2014-12-08 01:34:30 +01:00
-> getTable ( 'NullDefaultColumn' )
-> getColumn ( 'nullDefault' )
2014-12-03 19:37:53 +00:00
-> getCustomSchemaOptions ();
2016-12-07 23:33:41 +01:00
$this -> assertSame ([], $customSchemaOptions );
2014-12-03 19:37:53 +00:00
}
2015-04-08 10:49:21 +01:00
/**
* @ group DDC - 3671
*/
public function testSchemaHasProperIndexesFromUniqueConstraintAnnotation ()
{
2015-09-30 11:34:58 +02:00
$em = $this -> _getTestEntityManager ();
2015-04-08 10:49:21 +01:00
$schemaTool = new SchemaTool ( $em );
2015-09-30 11:34:58 +02:00
$classes = [
2016-12-08 18:01:04 +01:00
$em -> getClassMetadata ( UniqueConstraintAnnotationModel :: class ),
2015-04-08 10:49:21 +01:00
];
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
$this -> assertTrue ( $schema -> hasTable ( 'unique_constraint_annotation_table' ));
$table = $schema -> getTable ( 'unique_constraint_annotation_table' );
$this -> assertEquals ( 2 , count ( $table -> getIndexes ()));
$this -> assertTrue ( $table -> hasIndex ( 'primary' ));
$this -> assertTrue ( $table -> hasIndex ( 'uniq_hash' ));
}
2015-09-30 11:34:58 +02:00
public function testRemoveUniqueIndexOverruledByPrimaryKey ()
{
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
$classes = [
2016-12-08 18:01:04 +01:00
$em -> getClassMetadata ( FirstEntity :: class ),
$em -> getClassMetadata ( SecondEntity :: class )
2015-09-30 11:34:58 +02:00
];
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
$this -> assertTrue ( $schema -> hasTable ( 'first_entity' ), " Table first_entity should exist. " );
$indexes = $schema -> getTable ( 'first_entity' ) -> getIndexes ();
$this -> assertCount ( 1 , $indexes , " there should be only one index " );
$this -> assertTrue ( current ( $indexes ) -> isPrimary (), " index should be primary " );
}
2016-04-21 18:28:04 +03:00
2017-06-21 06:34:45 +02:00
public function testSetDiscriminatorColumnWithoutLength () : void
2016-04-21 18:28:04 +03:00
{
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
2017-06-21 06:34:45 +02:00
$metadata = $em -> getClassMetadata ( FirstEntity :: class );
2016-04-21 18:28:04 +03:00
$metadata -> setInheritanceType ( ClassMetadata :: INHERITANCE_TYPE_SINGLE_TABLE );
$metadata -> setDiscriminatorColumn ([ 'name' => 'discriminator' , 'type' => 'string' ]);
$schema = $schemaTool -> getSchemaFromMetadata ([ $metadata ]);
$this -> assertTrue ( $schema -> hasTable ( 'first_entity' ));
$table = $schema -> getTable ( 'first_entity' );
$this -> assertTrue ( $table -> hasColumn ( 'discriminator' ));
$column = $table -> getColumn ( 'discriminator' );
$this -> assertEquals ( 255 , $column -> getLength ());
}
2010-03-18 22:36:27 +00:00
}
2012-01-11 15:58:57 +01:00
/**
* @ Entity
2012-01-26 14:36:56 +01:00
* @ Table ( options = { " foo " : " bar " , " baz " : { " key " : " val " }})
2012-01-11 15:58:57 +01:00
*/
2012-01-26 14:36:56 +01:00
class TestEntityWithAnnotationOptionsAttribute
2012-01-11 15:58:57 +01:00
{
/** @Id @Column */
private $id ;
/**
* @ Column ( type = " string " , options = { " foo " : " bar " , " baz " : { " key " : " val " }})
*/
private $test ;
}
2010-03-18 22:36:27 +00:00
class GenerateSchemaEventListener
{
public $tableCalls = 0 ;
public $schemaCalled = false ;
public function postGenerateSchemaTable ( GenerateSchemaTableEventArgs $eventArgs )
{
$this -> tableCalls ++ ;
}
public function postGenerateSchema ( GenerateSchemaEventArgs $eventArgs )
{
$this -> schemaCalled = true ;
}
2012-11-12 14:05:24 +01:00
}
2015-04-08 10:49:21 +01:00
/**
* @ Entity
* @ Table ( name = " unique_constraint_annotation_table " , uniqueConstraints = {
* @ UniqueConstraint ( name = " uniq_hash " , columns = { " hash " })
* })
*/
class UniqueConstraintAnnotationModel
{
/** @Id @Column */
private $id ;
/**
* @ Column ( name = " hash " , type = " string " , length = 8 , nullable = false , unique = true )
*/
private $hash ;
}
2015-09-30 11:34:58 +02:00
/**
* @ Entity
* @ Table ( name = " first_entity " )
*/
class FirstEntity
{
/**
* @ Id
* @ Column ( name = " id " )
*/
public $id ;
/**
* @ OneToOne ( targetEntity = " SecondEntity " )
* @ JoinColumn ( name = " id " , referencedColumnName = " fist_entity_id " )
*/
public $secondEntity ;
/**
* @ Column ( name = " name " )
*/
public $name ;
}
/**
* @ Entity
* @ Table ( name = " second_entity " )
*/
class SecondEntity
{
/**
* @ Id
* @ Column ( name = " fist_entity_id " )
*/
public $fist_entity_id ;
/**
* @ Column ( name = " name " )
*/
public $name ;
2016-12-07 23:33:41 +01:00
}