2009-12-09 00:52:26 +03:00
< ? php
namespace Doctrine\Tests\ORM\Tools ;
use Doctrine\ORM\Tools\SchemaTool ;
2010-03-19 01:36:27 +03:00
use Doctrine\ORM\Tools\ToolEvents ;
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs ;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs ;
2009-12-09 00:52:26 +03:00
require_once __DIR__ . '/../../TestInit.php' ;
class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
{
public function testAddUniqueIndexForUniqueFieldAnnocation ()
{
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
$classes = array (
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsArticle' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsComment' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsEmployee' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsGroup' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsPhonenumber' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' ),
);
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
2011-03-05 12:08:12 +03:00
$this -> assertTrue ( $schema -> hasTable ( 'cms_users' ), " Table cms_users should exist. " );
$this -> assertTrue ( $schema -> getTable ( 'cms_users' ) -> columnsAreIndexed ( array ( 'username' )), " username column should be indexed. " );
2009-12-09 00:52:26 +03:00
}
2010-01-21 01:35:18 +03:00
2012-01-26 17:36:56 +04:00
public function testAnnotationOptionsAttribute ()
2012-01-11 18:58:57 +04:00
{
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
$classes = array (
2012-01-26 17:36:56 +04:00
$em -> getClassMetadata ( __NAMESPACE__ . '\\TestEntityWithAnnotationOptionsAttribute' ),
2012-01-11 18:58:57 +04:00
);
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
$expected = array ( 'foo' => 'bar' , 'baz' => array ( 'key' => 'val' ));
2012-01-26 17:36:56 +04:00
$this -> assertEquals ( $expected , $schema -> getTable ( 'TestEntityWithAnnotationOptionsAttribute' ) -> getOptions (), " options annotation are passed to the tables optionss " );
$this -> assertEquals ( $expected , $schema -> getTable ( 'TestEntityWithAnnotationOptionsAttribute' ) -> getColumn ( 'test' ) -> getCustomSchemaOptions (), " options annotation are passed to the columns customSchemaOptions " );
2012-01-11 18:58:57 +04:00
}
2010-01-21 01:35:18 +03:00
/**
* @ group DDC - 200
*/
public function testPassColumnDefinitionToJoinColumn ()
{
$customColumnDef = " MEDIUMINT(6) UNSIGNED NOT NULL " ;
$em = $this -> _getTestEntityManager ();
$schemaTool = new SchemaTool ( $em );
$avatar = $em -> getClassMetadata ( 'Doctrine\Tests\Models\Forum\ForumAvatar' );
$avatar -> fieldMappings [ 'id' ][ 'columnDefinition' ] = $customColumnDef ;
$user = $em -> getClassMetadata ( 'Doctrine\Tests\Models\Forum\ForumUser' );
$classes = array ( $avatar , $user );
$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-19 01:36:27 +03:00
/**
* @ group DDC - 283
*/
public function testPostGenerateEvents ()
{
$listener = new GenerateSchemaEventListener ();
$em = $this -> _getTestEntityManager ();
$em -> getEventManager () -> addEventListener (
array ( ToolEvents :: postGenerateSchemaTable , ToolEvents :: postGenerateSchema ), $listener
);
$schemaTool = new SchemaTool ( $em );
$classes = array (
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsAddress' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsArticle' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsComment' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsEmployee' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsGroup' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsPhonenumber' ),
$em -> getClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' ),
);
$schema = $schemaTool -> getSchemaFromMetadata ( $classes );
$this -> assertEquals ( count ( $classes ), $listener -> tableCalls );
$this -> assertTrue ( $listener -> schemaCalled );
}
}
2012-01-11 18:58:57 +04:00
/**
* @ Entity
2012-01-26 17:36:56 +04:00
* @ Table ( options = { " foo " : " bar " , " baz " : { " key " : " val " }})
2012-01-11 18:58:57 +04:00
*/
2012-01-26 17:36:56 +04:00
class TestEntityWithAnnotationOptionsAttribute
2012-01-11 18:58:57 +04:00
{
/** @Id @Column */
private $id ;
/**
* @ Column ( type = " string " , options = { " foo " : " bar " , " baz " : { " key " : " val " }})
*/
private $test ;
}
2010-03-19 01:36:27 +03:00
class GenerateSchemaEventListener
{
public $tableCalls = 0 ;
public $schemaCalled = false ;
public function postGenerateSchemaTable ( GenerateSchemaTableEventArgs $eventArgs )
{
$this -> tableCalls ++ ;
}
public function postGenerateSchema ( GenerateSchemaEventArgs $eventArgs )
{
$this -> schemaCalled = true ;
}
2009-12-09 00:52:26 +03:00
}