2009-12-05 00:40:03 +03:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
2013-05-09 14:10:37 +04:00
use Doctrine\ORM\Mapping\ClassMetadataInfo ;
2013-06-12 10:29:08 +04:00
use Doctrine\DBAL\Platforms\PostgreSqlPlatform ;
2009-12-05 00:40:03 +03:00
2013-05-09 14:10:37 +04:00
class DatabaseDriverTest extends DatabaseDriverTestCase
2009-12-05 00:40:03 +03:00
{
2009-12-05 00:58:16 +03:00
/**
* @ var \Doctrine\DBAL\Schema\AbstractSchemaManager
*/
2009-12-05 00:40:03 +03:00
protected $_sm = null ;
2009-12-05 00:58:16 +03:00
public function setUp ()
{
2010-06-20 21:34:09 +04:00
$this -> useModelSet ( 'cms' );
2009-12-05 00:58:16 +03:00
parent :: setUp ();
$this -> _sm = $this -> _em -> getConnection () -> getSchemaManager ();
}
2012-10-05 22:00:12 +04:00
/**
* @ group DDC - 2059
*/
public function testIssue2059 ()
{
if ( ! $this -> _em -> getConnection () -> getDatabasePlatform () -> supportsForeignKeyConstraints ()) {
$this -> markTestSkipped ( 'Platform does not support foreign keys.' );
}
$user = new \Doctrine\DBAL\Schema\Table ( " ddc2059_user " );
$user -> addColumn ( 'id' , 'integer' );
$user -> setPrimaryKey ( array ( 'id' ));
$project = new \Doctrine\DBAL\Schema\Table ( " ddc2059_project " );
$project -> addColumn ( 'id' , 'integer' );
$project -> addColumn ( 'user_id' , 'integer' );
$project -> addColumn ( 'user' , 'string' );
$project -> setPrimaryKey ( array ( 'id' ));
$project -> addForeignKeyConstraint ( 'ddc2059_user' , array ( 'user_id' ), array ( 'id' ));
$metadata = $this -> convertToClassMetadata ( array ( $project , $user ), array ());
$this -> assertTrue ( isset ( $metadata [ 'Ddc2059Project' ] -> fieldMappings [ 'user' ]));
$this -> assertTrue ( isset ( $metadata [ 'Ddc2059Project' ] -> associationMappings [ 'user2' ]));
}
2010-06-13 21:36:49 +04:00
public function testLoadMetadataFromDatabase ()
2009-12-05 00:40:03 +03:00
{
2010-02-07 15:36:30 +03:00
if ( ! $this -> _em -> getConnection () -> getDatabasePlatform () -> supportsForeignKeyConstraints ()) {
$this -> markTestSkipped ( 'Platform does not support foreign keys.' );
}
2009-12-05 00:40:03 +03:00
$table = new \Doctrine\DBAL\Schema\Table ( " dbdriver_foo " );
2010-02-26 00:51:30 +03:00
$table -> addColumn ( 'id' , 'integer' );
2009-12-05 00:40:03 +03:00
$table -> setPrimaryKey ( array ( 'id' ));
2010-05-25 21:35:12 +04:00
$table -> addColumn ( 'bar' , 'string' , array ( 'notnull' => false , 'length' => 200 ));
2009-12-05 00:40:03 +03:00
$this -> _sm -> dropAndCreateTable ( $table );
2010-06-13 21:36:49 +04:00
$metadatas = $this -> extractClassMetadata ( array ( " DbdriverFoo " ));
2010-06-20 21:34:09 +04:00
$this -> assertArrayHasKey ( 'DbdriverFoo' , $metadatas );
$metadata = $metadatas [ 'DbdriverFoo' ];
2009-12-05 12:39:11 +03:00
$this -> assertArrayHasKey ( 'id' , $metadata -> fieldMappings );
$this -> assertEquals ( 'id' , $metadata -> fieldMappings [ 'id' ][ 'fieldName' ]);
$this -> assertEquals ( 'id' , strtolower ( $metadata -> fieldMappings [ 'id' ][ 'columnName' ]));
$this -> assertEquals ( 'integer' , ( string ) $metadata -> fieldMappings [ 'id' ][ 'type' ]);
$this -> assertArrayHasKey ( 'bar' , $metadata -> fieldMappings );
$this -> assertEquals ( 'bar' , $metadata -> fieldMappings [ 'bar' ][ 'fieldName' ]);
$this -> assertEquals ( 'bar' , strtolower ( $metadata -> fieldMappings [ 'bar' ][ 'columnName' ]));
$this -> assertEquals ( 'string' , ( string ) $metadata -> fieldMappings [ 'bar' ][ 'type' ]);
$this -> assertEquals ( 200 , $metadata -> fieldMappings [ 'bar' ][ 'length' ]);
2010-05-25 21:35:12 +04:00
$this -> assertTrue ( $metadata -> fieldMappings [ 'bar' ][ 'nullable' ]);
2009-12-05 00:40:03 +03:00
}
2010-06-13 21:36:49 +04:00
public function testLoadMetadataWithForeignKeyFromDatabase ()
2009-12-05 00:40:03 +03:00
{
if ( ! $this -> _em -> getConnection () -> getDatabasePlatform () -> supportsForeignKeyConstraints ()) {
$this -> markTestSkipped ( 'Platform does not support foreign keys.' );
}
$tableB = new \Doctrine\DBAL\Schema\Table ( " dbdriver_bar " );
2010-02-26 00:51:30 +03:00
$tableB -> addColumn ( 'id' , 'integer' );
2009-12-05 00:40:03 +03:00
$tableB -> setPrimaryKey ( array ( 'id' ));
2009-12-05 12:39:11 +03:00
$this -> _sm -> dropAndCreateTable ( $tableB );
2009-12-05 00:40:03 +03:00
$tableA = new \Doctrine\DBAL\Schema\Table ( " dbdriver_baz " );
2010-02-26 00:51:30 +03:00
$tableA -> addColumn ( 'id' , 'integer' );
2009-12-05 00:40:03 +03:00
$tableA -> setPrimaryKey ( array ( 'id' ));
2010-02-26 00:51:30 +03:00
$tableA -> addColumn ( 'bar_id' , 'integer' );
2009-12-05 00:40:03 +03:00
$tableA -> addForeignKeyConstraint ( 'dbdriver_bar' , array ( 'bar_id' ), array ( 'id' ));
$this -> _sm -> dropAndCreateTable ( $tableA );
2010-06-13 21:36:49 +04:00
$metadatas = $this -> extractClassMetadata ( array ( " DbdriverBar " , " DbdriverBaz " ));
2010-06-20 21:34:09 +04:00
$this -> assertArrayHasKey ( 'DbdriverBaz' , $metadatas );
$bazMetadata = $metadatas [ 'DbdriverBaz' ];
2009-12-05 12:39:11 +03:00
2010-06-13 21:36:49 +04:00
$this -> assertArrayNotHasKey ( 'barId' , $bazMetadata -> fieldMappings , " The foreign Key field should not be inflected as 'barId' field, its an association. " );
$this -> assertArrayHasKey ( 'id' , $bazMetadata -> fieldMappings );
2009-12-05 12:39:11 +03:00
2010-06-13 21:36:49 +04:00
$bazMetadata -> associationMappings = \array_change_key_case ( $bazMetadata -> associationMappings , \CASE_LOWER );
2009-12-05 12:39:11 +03:00
2010-06-13 21:36:49 +04:00
$this -> assertArrayHasKey ( 'bar' , $bazMetadata -> associationMappings );
2010-08-09 15:13:21 +04:00
$this -> assertEquals ( ClassMetadataInfo :: MANY_TO_ONE , $bazMetadata -> associationMappings [ 'bar' ][ 'type' ]);
2009-12-05 12:39:11 +03:00
}
2010-06-20 21:34:09 +04:00
public function testDetectManyToManyTables ()
{
if ( ! $this -> _em -> getConnection () -> getDatabasePlatform () -> supportsForeignKeyConstraints ()) {
$this -> markTestSkipped ( 'Platform does not support foreign keys.' );
}
$metadatas = $this -> extractClassMetadata ( array ( " CmsUsers " , " CmsGroups " ));
$this -> assertArrayHasKey ( 'CmsUsers' , $metadatas , 'CmsUsers entity was not detected.' );
$this -> assertArrayHasKey ( 'CmsGroups' , $metadatas , 'CmsGroups entity was not detected.' );
2011-10-24 00:06:03 +04:00
$this -> assertEquals ( 2 , count ( $metadatas [ 'CmsUsers' ] -> associationMappings ));
2010-06-20 21:34:09 +04:00
$this -> assertArrayHasKey ( 'group' , $metadatas [ 'CmsUsers' ] -> associationMappings );
$this -> assertEquals ( 1 , count ( $metadatas [ 'CmsGroups' ] -> associationMappings ));
$this -> assertArrayHasKey ( 'user' , $metadatas [ 'CmsGroups' ] -> associationMappings );
}
2011-04-30 13:15:45 +04:00
public function testIgnoreManyToManyTableWithoutFurtherForeignKeyDetails ()
{
$tableB = new \Doctrine\DBAL\Schema\Table ( " dbdriver_bar " );
$tableB -> addColumn ( 'id' , 'integer' );
$tableB -> setPrimaryKey ( array ( 'id' ));
$tableA = new \Doctrine\DBAL\Schema\Table ( " dbdriver_baz " );
$tableA -> addColumn ( 'id' , 'integer' );
$tableA -> setPrimaryKey ( array ( 'id' ));
$tableMany = new \Doctrine\DBAL\Schema\Table ( " dbdriver_bar_baz " );
$tableMany -> addColumn ( 'bar_id' , 'integer' );
$tableMany -> addColumn ( 'baz_id' , 'integer' );
$tableMany -> addForeignKeyConstraint ( 'dbdriver_bar' , array ( 'bar_id' ), array ( 'id' ));
$metadatas = $this -> convertToClassMetadata ( array ( $tableA , $tableB ), array ( $tableMany ));
$this -> assertEquals ( 0 , count ( $metadatas [ 'DbdriverBaz' ] -> associationMappings ), " no association mappings should be detected. " );
}
2013-06-12 10:00:36 +04:00
public function testLoadMetadataFromDatabaseDetail ()
{
2013-06-12 10:29:08 +04:00
if ( ! $this -> _em -> getConnection () -> getDatabasePlatform () -> supportsForeignKeyConstraints ()) {
2013-06-12 10:00:36 +04:00
$this -> markTestSkipped ( 'Platform does not support foreign keys.' );
}
$table = new \Doctrine\DBAL\Schema\Table ( " dbdriver_foo " );
2013-06-12 10:29:08 +04:00
$table -> addColumn ( 'id' , 'integer' , array ( 'unsigned' => true ));
2013-06-12 10:00:36 +04:00
$table -> setPrimaryKey ( array ( 'id' ));
2013-06-12 10:29:08 +04:00
$table -> addColumn ( 'column_unsigned' , 'integer' , array ( 'unsigned' => true ));
$table -> addColumn ( 'column_comment' , 'string' , array ( 'comment' => 'test_comment' ));
$table -> addColumn ( 'column_default' , 'string' , array ( 'default' => 'test_default' ));
$table -> addColumn ( 'column_decimal' , 'decimal' , array ( 'precision' => 4 , 'scale' => 3 ));
$table -> addColumn ( 'column_index1' , 'string' );
$table -> addColumn ( 'column_index2' , 'string' );
$table -> addIndex ( array ( 'column_index1' , 'column_index2' ), 'index1' );
$table -> addColumn ( 'column_unique_index1' , 'string' );
$table -> addColumn ( 'column_unique_index2' , 'string' );
$table -> addUniqueIndex ( array ( 'column_unique_index1' , 'column_unique_index2' ), 'unique_index1' );
2013-06-12 10:00:36 +04:00
$this -> _sm -> dropAndCreateTable ( $table );
$metadatas = $this -> extractClassMetadata ( array ( " DbdriverFoo " ));
$this -> assertArrayHasKey ( 'DbdriverFoo' , $metadatas );
2013-06-12 10:29:08 +04:00
2013-06-12 10:00:36 +04:00
$metadata = $metadatas [ 'DbdriverFoo' ];
2013-06-12 10:29:08 +04:00
$this -> assertArrayHasKey ( 'id' , $metadata -> fieldMappings );
$this -> assertEquals ( 'id' , $metadata -> fieldMappings [ 'id' ][ 'fieldName' ]);
$this -> assertEquals ( 'id' , strtolower ( $metadata -> fieldMappings [ 'id' ][ 'columnName' ]));
$this -> assertEquals ( 'integer' , ( string ) $metadata -> fieldMappings [ 'id' ][ 'type' ]);
2013-06-12 10:00:36 +04:00
2013-06-12 10:29:08 +04:00
// FIXME: Condition here is fugly.
2013-09-13 18:29:31 +04:00
// NOTE: PostgreSQL and SQL SERVER do not support UNSIGNED integer
if ( ! $this -> _em -> getConnection () -> getDatabasePlatform () instanceof PostgreSqlPlatform AND
! $this -> _em -> getConnection () -> getDatabasePlatform () instanceof SQLServerPlatform ) {
2013-06-12 10:29:08 +04:00
$this -> assertArrayHasKey ( 'columnUnsigned' , $metadata -> fieldMappings );
$this -> assertTrue ( $metadata -> fieldMappings [ 'columnUnsigned' ][ 'unsigned' ]);
}
2013-06-12 10:00:36 +04:00
2013-06-12 10:29:08 +04:00
$this -> assertArrayHasKey ( 'columnComment' , $metadata -> fieldMappings );
$this -> assertEquals ( 'test_comment' , $metadata -> fieldMappings [ 'columnComment' ][ 'comment' ]);
2013-06-12 10:00:36 +04:00
2013-06-12 10:29:08 +04:00
$this -> assertArrayHasKey ( 'columnDefault' , $metadata -> fieldMappings );
$this -> assertEquals ( 'test_default' , $metadata -> fieldMappings [ 'columnDefault' ][ 'default' ]);
2013-06-12 10:00:36 +04:00
2013-06-12 10:29:08 +04:00
$this -> assertArrayHasKey ( 'columnDecimal' , $metadata -> fieldMappings );
$this -> assertEquals ( 4 , $metadata -> fieldMappings [ 'columnDecimal' ][ 'precision' ]);
$this -> assertEquals ( 3 , $metadata -> fieldMappings [ 'columnDecimal' ][ 'scale' ]);
2013-06-12 10:00:36 +04:00
2013-06-12 10:29:08 +04:00
$this -> assertTrue ( ! empty ( $metadata -> table [ 'indexes' ][ 'index1' ][ 'columns' ]));
$this -> assertEquals (
array ( 'column_index1' , 'column_index2' ),
$metadata -> table [ 'indexes' ][ 'index1' ][ 'columns' ]
);
2013-06-12 10:00:36 +04:00
2013-06-12 10:29:08 +04:00
$this -> assertTrue ( ! empty ( $metadata -> table [ 'uniqueConstraints' ][ 'unique_index1' ][ 'columns' ]));
$this -> assertEquals (
array ( 'column_unique_index1' , 'column_unique_index2' ),
$metadata -> table [ 'uniqueConstraints' ][ 'unique_index1' ][ 'columns' ]
);
2013-06-12 10:00:36 +04:00
}
2009-12-05 00:40:03 +03:00
}