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();
|
|
|
|
}
|
|
|
|
|
2009-12-05 00:40:03 +03:00
|
|
|
public function testCreateSimpleYamlFromDatabase()
|
|
|
|
{
|
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);
|
|
|
|
|
2009-12-05 12:39:11 +03:00
|
|
|
$metadata = $this->extractClassMetadata("DbdriverFoo");
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateYamlWithForeignKeyFromDatabase()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2009-12-05 12:39:11 +03:00
|
|
|
$metadata = $this->extractClassMetadata("DbdriverBaz");
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('bar', $metadata->fieldMappings);
|
|
|
|
$this->assertArrayHasKey('id', $metadata->fieldMappings);
|
|
|
|
|
|
|
|
$metadata->associationMappings = \array_change_key_case($metadata->associationMappings, \CASE_LOWER);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('bar', $metadata->associationMappings);
|
|
|
|
$this->assertType('Doctrine\ORM\Mapping\OneToOneMapping', $metadata->associationMappings['bar']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return ClassMetadata
|
|
|
|
*/
|
|
|
|
protected function extractClassMetadata($className)
|
|
|
|
{
|
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) {
|
|
|
|
if (strtolower(Inflector::classify($dbTableName)) != strtolower($className)) {
|
|
|
|
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);
|
|
|
|
return $class;
|
2009-12-05 12:39:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail("No class matching the name '".$className."' was found!");
|
2009-12-05 00:40:03 +03:00
|
|
|
}
|
|
|
|
}
|