From 4d3c4a704a4e7d4866b2ab95df01713c418a018a Mon Sep 17 00:00:00 2001 From: beberlei Date: Sat, 31 Oct 2009 22:23:36 +0000 Subject: [PATCH] [2.0] DDC-84 - Add Mysql Platform unittests for incremental changes of the metadata for the schematool update function. Fixed some quirks in the Unit-Test suite alongside, Fixed changes of length in SchemaTool update. --- lib/Doctrine/ORM/Tools/SchemaTool.php | 20 +- tests/Doctrine/Tests/DoctrineTestCase.php | 2 +- tests/Doctrine/Tests/Mocks/DriverMock.php | 13 +- .../SchemaTool/MySqlSchemaToolTest.php | 2 +- tests/Doctrine/Tests/ORM/Tools/AllTests.php | 2 + .../Tests/ORM/Tools/SchemaTool/AllTests.php | 31 ++ .../SchemaTool/DatabaseFixtureExporter.php | 46 +++ .../ORM/Tools/SchemaTool/DbFixture/Cms.php | 347 ++++++++++++++++++ .../SchemaTool/DbFixture/DecimalModel.php | 41 +++ .../SchemaTool/MysqlUpdateSchemaTest.php | 190 ++++++++++ .../Tools/SchemaTool/UpdateSchemaTestCase.php | 69 ++++ tests/Doctrine/Tests/OrmTestCase.php | 10 +- 12 files changed, 762 insertions(+), 11 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Tools/SchemaTool/AllTests.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/SchemaTool/DatabaseFixtureExporter.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/Cms.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/DecimalModel.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/SchemaTool/MysqlUpdateSchemaTest.php create mode 100644 tests/Doctrine/Tests/ORM/Tools/SchemaTool/UpdateSchemaTestCase.php diff --git a/lib/Doctrine/ORM/Tools/SchemaTool.php b/lib/Doctrine/ORM/Tools/SchemaTool.php index 4ec92ac64..7d75d517c 100644 --- a/lib/Doctrine/ORM/Tools/SchemaTool.php +++ b/lib/Doctrine/ORM/Tools/SchemaTool.php @@ -36,6 +36,7 @@ use Doctrine\DBAL\Types\Type, * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel + * @author Benjamin Eberlei */ class SchemaTool { @@ -553,12 +554,21 @@ class SchemaTool // 4. check for length change // 5. check for scale and precision change - /*if (isset($fieldMapping['scale'])) { - $columnInfo['length'] = $fieldMapping['precision']; - $columnInfo['scale'] = $fieldMapping['scale']; + if ($columnInfo['type'] == 'Decimal') { + /*// Doesn't work yet, see DDC-89 + if($columnInfo['length'] != $fieldMapping['precision'] || + $columnInfo['scale'] != $fieldMapping['scale']) { + + $columnInfo['length'] = $fieldMapping['precision']; + $columnInfo['scale'] = $fieldMapping['scale']; + $columnChanged = true; + }*/ } else { - $columnInfo['length'] = $fieldMapping['length']; - }*/ + if($columnInfo['length'] != $fieldMapping['length']) { + $columnInfo['length'] = $fieldMapping['length']; + $columnChanged = true; + } + } // 6. check for flexible and fixed length $fieldMapping['fixed'] = ( ! isset($fieldMapping['fixed'])) diff --git a/tests/Doctrine/Tests/DoctrineTestCase.php b/tests/Doctrine/Tests/DoctrineTestCase.php index e13f2aa11..e8323d294 100644 --- a/tests/Doctrine/Tests/DoctrineTestCase.php +++ b/tests/Doctrine/Tests/DoctrineTestCase.php @@ -5,6 +5,6 @@ namespace Doctrine\Tests; /** * Base testcase class for all Doctrine testcases. */ -class DoctrineTestCase extends \PHPUnit_Framework_TestCase +abstract class DoctrineTestCase extends \PHPUnit_Framework_TestCase { } \ No newline at end of file diff --git a/tests/Doctrine/Tests/Mocks/DriverMock.php b/tests/Doctrine/Tests/Mocks/DriverMock.php index a5ab81b1d..2c7fadc52 100644 --- a/tests/Doctrine/Tests/Mocks/DriverMock.php +++ b/tests/Doctrine/Tests/Mocks/DriverMock.php @@ -7,6 +7,8 @@ class DriverMock implements \Doctrine\DBAL\Driver { private $_platformMock; + private $_schemaManagerMock; + public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) { return new DriverConnectionMock(); @@ -39,7 +41,11 @@ class DriverMock implements \Doctrine\DBAL\Driver */ public function getSchemaManager(\Doctrine\DBAL\Connection $conn) { - return new SchemaManagerMock($conn); + if($this->_schemaManagerMock == null) { + return new SchemaManagerMock($conn); + } else { + return $this->_schemaManagerMock; + } } /* MOCK API */ @@ -49,6 +55,11 @@ class DriverMock implements \Doctrine\DBAL\Driver $this->_platformMock = $platform; } + public function setSchemaManager(\Doctrine\DBAL\Schema\AbstractSchemaManager $sm) + { + $this->_schemaManagerMock = $sm; + } + public function getName() { return 'mock'; diff --git a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php index a4a1fcec1..e5bd09e6b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SchemaTool/MySqlSchemaToolTest.php @@ -47,7 +47,7 @@ class MySqlSchemaToolTest extends \Doctrine\Tests\OrmFunctionalTestCase $sql = $tool->getCreateSchemaSql($classes); $this->assertEquals(1, count($sql)); - $this->assertEquals("CREATE TABLE decimal_model (id INT AUTO_INCREMENT NOT NULL, decimal NUMERIC(2, 5) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]); + $this->assertEquals("CREATE TABLE decimal_model (id INT AUTO_INCREMENT NOT NULL, decimal NUMERIC(5, 2) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]); } public function testGetCreateSchemaSql3() diff --git a/tests/Doctrine/Tests/ORM/Tools/AllTests.php b/tests/Doctrine/Tests/ORM/Tools/AllTests.php index 9f837d05e..46649b08e 100644 --- a/tests/Doctrine/Tests/ORM/Tools/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Tools/AllTests.php @@ -23,6 +23,8 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\ClassMetadataExporterTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\ConvertDoctrine1SchemaTest'); + $suite->addTest(SchemaTool\AllTests::suite()); + return $suite; } } diff --git a/tests/Doctrine/Tests/ORM/Tools/SchemaTool/AllTests.php b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/AllTests.php new file mode 100644 index 000000000..9e2e4d88b --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/AllTests.php @@ -0,0 +1,31 @@ +addTestSuite('Doctrine\Tests\ORM\Tools\SchemaTool\MysqlUpdateSchemaTest'); + + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'Orm_Tools_SchemaTool_AllTests::main') { + AllTests::main(); +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DatabaseFixtureExporter.php b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DatabaseFixtureExporter.php new file mode 100644 index 000000000..55fd35aaa --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DatabaseFixtureExporter.php @@ -0,0 +1,46 @@ +setBasePath(realpath(dirname(__FILE__)."/../../../../../../lib/")); +$classLoader->register(); + +$params = array( + 'driver' => 'pdo_mysql', + 'dbname' => $argv[3], + 'user' => $argv[1], + 'password' => $argv[2] +); + +$conn = \Doctrine\DBAL\DriverManager::getConnection($params); +$sm = $conn->getSchemaManager(); + +if(isset($argv[4])) { + $filterString = $argv[4]; +} else { + $filterString = false; +} + +$tables = $sm->listTables(); +$fixture = array(); +foreach($tables AS $tableName) { + if($filterString !== false && strpos($tableName, $filterString) === false) { + continue; + } + $fixture[$tableName] = $sm->listTableColumns($tableName); +} + +ksort($fixture); + +$regexp = '(Doctrine\\\\DBAL\\\\Types\\\\([a-zA-Z]+)Type::__set_state\(array\([\s]+\)\))'; + +$code = var_export($fixture, true); +$code = preg_replace( + $regexp, + 'Doctrine\\DBAL\\Types\\Type::getType(strtolower("\1"))', + $code +); + +echo "\n"; \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/Cms.php b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/Cms.php new file mode 100644 index 000000000..27050538d --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/Cms.php @@ -0,0 +1,347 @@ + + array ( + 0 => + array ( + 'name' => 'id', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => true, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + 1 => + array ( + 'name' => 'country', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 50, + 'unsigned' => false, + 'fixed' => false, + ), + 2 => + array ( + 'name' => 'zip', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 50, + 'unsigned' => false, + 'fixed' => false, + ), + 3 => + array ( + 'name' => 'city', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 50, + 'unsigned' => false, + 'fixed' => false, + ), + 4 => + array ( + 'name' => 'user_id', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => false, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + ), + 'cms_articles' => + array ( + 0 => + array ( + 'name' => 'id', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => true, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + 1 => + array ( + 'name' => 'topic', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 255, + 'unsigned' => false, + 'fixed' => false, + ), + 2 => + array ( + 'name' => 'text', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 255, + 'unsigned' => false, + 'fixed' => false, + ), + 3 => + array ( + 'name' => 'user_id', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => false, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + ), + 'cms_groups' => + array ( + 0 => + array ( + 'name' => 'id', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => true, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + 1 => + array ( + 'name' => 'name', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 50, + 'unsigned' => false, + 'fixed' => false, + ), + ), + 'cms_phonenumbers' => + array ( + 0 => + array ( + 'name' => 'phonenumber', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 50, + 'unsigned' => false, + 'fixed' => false, + ), + 1 => + array ( + 'name' => 'user_id', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => false, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + ), + 'cms_users' => + array ( + 0 => + array ( + 'name' => 'id', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => true, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + 1 => + array ( + 'name' => 'status', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 50, + 'unsigned' => false, + 'fixed' => false, + ), + 2 => + array ( + 'name' => 'username', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => true, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 255, + 'unsigned' => false, + 'fixed' => false, + ), + 3 => + array ( + 'name' => 'name', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("String")), + 'length' => 255, + 'unsigned' => false, + 'fixed' => false, + ), + ), + 'cms_users_groups' => + array ( + 0 => + array ( + 'name' => 'user_id', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => '0', + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + 1 => + array ( + 'name' => 'group_id', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => '0', + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + ), +); + +return $fixtures; +?> diff --git a/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/DecimalModel.php b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/DecimalModel.php new file mode 100644 index 000000000..55ae1737c --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/DbFixture/DecimalModel.php @@ -0,0 +1,41 @@ + + array ( + 0 => + array ( + 'name' => 'id', + 'values' => + array ( + ), + 'primary' => true, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => true, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Integer")), + 'length' => NULL, + 'unsigned' => false, + 'fixed' => false, + ), + 1 => + array ( + 'name' => 'decimal', + 'values' => + array ( + ), + 'primary' => false, + 'unique' => false, + 'default' => NULL, + 'notnull' => true, + 'autoincrement' => false, + 'type' => + Doctrine\DBAL\Types\Type::getType(strtolower("Decimal")), + 'length' => 5, + 'unsigned' => false, + 'fixed' => false, + ), + ), +); +?> diff --git a/tests/Doctrine/Tests/ORM/Tools/SchemaTool/MysqlUpdateSchemaTest.php b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/MysqlUpdateSchemaTest.php new file mode 100644 index 000000000..ab5fd44c9 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/MysqlUpdateSchemaTest.php @@ -0,0 +1,190 @@ +_getSchemaTool("Cms"); + $md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $md->mapField(array('fieldName' => 'street', 'type' => 'string')); + + $sql = $st->getUpdateSchemaSql(array($md)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals( + "ALTER TABLE cms_addresses ADD street VARCHAR(255) DEFAULT NULL", + $sql[0] + ); + } + + public function testChangeColumnName() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $classMetadata->fieldMappings['city']['columnName'] = 'the_city'; + + $sql = $st->getUpdateSchemaSql(array($classMetadata)); + + $this->assertEquals(2, count($sql)); + $this->assertEquals("ALTER TABLE cms_addresses ADD the_city VARCHAR(50) NOT NULL", $sql[0]); + $this->assertEquals("ALTER TABLE cms_addresses DROP city", $sql[1]); + } + + public function testChangeNullability() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $classMetadata->fieldMappings['city']['nullable'] = true; + + $sql = $st->getUpdateSchemaSql(array($classMetadata)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) DEFAULT NULL", $sql[0]); + } + + public function testChangeType() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $classMetadata->fieldMappings['city']['type'] = "text"; + + $sql = $st->getUpdateSchemaSql(array($classMetadata)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals("ALTER TABLE cms_addresses CHANGE city city TINYTEXT NOT NULL", $sql[0]); + } + + public function testChangeUniqueness() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $classMetadata->fieldMappings['city']['unique'] = true; + + $sql = $st->getUpdateSchemaSql(array($classMetadata)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals("ALTER TABLE cms_addresses CHANGE city city VARCHAR(50) NOT NULL UNIQUE", $sql[0]); + } + + public function testChangeLength() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $classMetadata->fieldMappings['city']['length'] = 200; + + $sql = $st->getUpdateSchemaSql(array($classMetadata)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals('ALTER TABLE cms_addresses CHANGE city city VARCHAR(200) NOT NULL', $sql[0]); + } + + public function testChangeDecimalLengthPrecision() + { + $this->markTestSkipped('Decimal Scale changes not supported yet, because of DDC-89.'); + + $decimalModel = new \Doctrine\Tests\Models\Generic\DecimalModel(); + + $st = $this->_getSchemaTool('DecimalModel'); + + $classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\Generic\DecimalModel"); + $classMetadata->fieldMappings['decimal']['precision'] = 10; + + $sql = $st->getUpdateSchemaSql(array($classMetadata)); + + $this->assertEquals(1, count($sql)); + // invalid sql, because not escaped + $this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(10, 2) NOT NULL', $sql[0]); + } + + public function testChangeDecimalLengthScale() + { + $this->markTestSkipped('Decimal Scale changes not supported yet, because of DDC-89.'); + + $decimalModel = new \Doctrine\Tests\Models\Generic\DecimalModel(); + + $st = $this->_getSchemaTool('DecimalModel'); + + $classMetadata = $this->_getMetadataFor("\Doctrine\Tests\Models\Generic\DecimalModel"); + $classMetadata->fieldMappings['decimal']['scale'] = 3; + + $sql = $st->getUpdateSchemaSql(array($classMetadata)); + + $this->assertEquals(1, count($sql)); + // invalid sql, because not escaped + $this->assertEquals('ALTER TABLE decimal_model CHANGE decimal decimal NUMERIC(5, 3) NOT NULL', $sql[0]); + } + + public function testChangeFixed() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $md->fieldMappings['city']['fixed'] = true; + + $sql = $st->getUpdateSchemaSql(array($md)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals( + "ALTER TABLE cms_addresses CHANGE city city CHAR(50) NOT NULL", + $sql[0] + ); + } + + public function testAddIndex() + { + $this->markTestSkipped('Not yet supported by SchemaTool, see DDC-90'); + + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + $md->primaryTable['indexes'] = array('searchCity' => array('columns' => array('city'))); + + $sql = $st->getUpdateSchemaSql(array($md)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals( + "CREATE INDEX searchCity (city)", + $sql[0] + ); + } + + public function testRemoveField() + { + $address = new \Doctrine\Tests\Models\CMS\CmsAddress; + + $st = $this->_getSchemaTool("Cms"); + $md = $this->_getMetadataFor("\Doctrine\Tests\Models\CMS\CmsAddress"); + unset($md->fieldMappings['city']); + + $sql = $st->getUpdateSchemaSql(array($md)); + + $this->assertEquals(1, count($sql)); + $this->assertEquals( + "ALTER TABLE cms_addresses DROP city", + $sql[0] + ); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/SchemaTool/UpdateSchemaTestCase.php b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/UpdateSchemaTestCase.php new file mode 100644 index 000000000..ff0938ffb --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/SchemaTool/UpdateSchemaTestCase.php @@ -0,0 +1,69 @@ +_createSchemaTool($fixtureName, $this->_createPlatform()); + } + + abstract protected function _createPlatform(); + + private function _createSchemaTool($fixtureName, $platform) + { + $fixture = include __DIR__."/DbFixture/".$fixtureName.".php"; + + $sm = new UpdateSchemaMock($fixture); + + $this->_em = $this->_getTestEntityManager(null, null, null, false); + $this->_em->getConnection()->setDatabasePlatform($platform); + $this->_em->getConnection()->getDriver()->setSchemaManager($sm); + + return new SchemaTool($this->_em); + } + + /** + * @param string $className + * @return \Doctrine\ORM\Mapping\ClassMetadata + */ + protected function _getMetadataFor($className) + { + return $this->_em->getClassMetadata($className); + } +} + +class UpdateSchemaMock extends \Doctrine\DBAL\Schema\AbstractSchemaManager +{ + private $_fixtureData; + + public function __construct($fixtureData) + { + $this->_fixtureData = $fixtureData; + } + + public function listTables() + { + return array_keys($this->_fixtureData); + } + + public function listTableColumns($tableName) + { + return $this->_fixtureData[$tableName]; + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/OrmTestCase.php b/tests/Doctrine/Tests/OrmTestCase.php index ac60d3711..5ddf146dc 100644 --- a/tests/Doctrine/Tests/OrmTestCase.php +++ b/tests/Doctrine/Tests/OrmTestCase.php @@ -5,7 +5,7 @@ namespace Doctrine\Tests; /** * Base testcase class for all ORM testcases. */ -class OrmTestCase extends DoctrineTestCase +abstract class OrmTestCase extends DoctrineTestCase { /** The metadata cache that is shared between all ORM tests (except functional tests). */ private static $_metadataCacheImpl = null; @@ -22,10 +22,14 @@ class OrmTestCase extends DoctrineTestCase * * @return Doctrine\ORM\EntityManager */ - protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null) + protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true) { $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl()); + if($withSharedMetadata) { + $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl()); + } else { + $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); + } $config->setQueryCacheImpl(self::getSharedQueryCacheImpl()); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Doctrine\Tests\Proxies');