2009-12-05 00:40:03 +03:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
require_once __DIR__ . '/../../TestInit.php' ;
2010-05-25 21:35:12 +04:00
use Doctrine\ORM\Mapping\ClassMetadataInfo ,
Doctrine\Common\Util\Inflector ;
2009-12-05 00:40:03 +03:00
class DatabaseDriverTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
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 ()
{
parent :: setUp ();
$this -> _sm = $this -> _em -> getConnection () -> getSchemaManager ();
}
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 " ));
$this -> assertArrayHasKey ( 'dbdriver_foo' , $metadatas );
$metadata = $metadatas [ 'dbdriver_foo' ];
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 " ));
$this -> assertArrayHasKey ( 'dbdriver_baz' , $metadatas );
$bazMetadata = $metadatas [ 'dbdriver_baz' ];
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 );
$this -> assertType ( 'Doctrine\ORM\Mapping\OneToOneMapping' , $bazMetadata -> associationMappings [ 'bar' ]);
2009-12-05 12:39:11 +03:00
}
/**
*
* @ param string $className
* @ return ClassMetadata
*/
2010-06-13 21:36:49 +04:00
protected function extractClassMetadata ( array $classNames )
2009-12-05 12:39:11 +03:00
{
2010-06-13 21:36:49 +04:00
$classNames = array_map ( 'strtolower' , $classNames );
$metadatas = array ();
2010-04-14 18:31:50 +04:00
$driver = new \Doctrine\ORM\Mapping\Driver\DatabaseDriver ( $this -> _sm );
2010-05-25 21:35:12 +04:00
foreach ( $driver -> getAllClassNames () as $dbTableName ) {
2010-06-13 21:36:49 +04:00
if ( ! in_array ( strtolower ( Inflector :: classify ( $dbTableName )), $classNames )) {
2010-05-25 21:35:12 +04:00
continue ;
2009-12-05 12:39:11 +03:00
}
2010-05-25 21:35:12 +04:00
$class = new ClassMetadataInfo ( $dbTableName );
$driver -> loadMetadataForClass ( $dbTableName , $class );
2010-06-13 21:36:49 +04:00
$metadatas [ strtolower ( $dbTableName )] = $class ;
2009-12-05 12:39:11 +03:00
}
2010-06-13 21:36:49 +04:00
if ( count ( $metadatas ) != count ( $classNames )) {
$this -> fail ( " Have not found all classes matching the names ' " . implode ( " , " , $classNames ) . " ' only tables " . implode ( " , " , array_keys ( $metadatas )));
}
return $metadatas ;
2009-12-05 00:40:03 +03:00
}
}