1
0
mirror of synced 2025-01-18 22:41:43 +03:00

[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.

This commit is contained in:
beberlei 2009-10-31 22:23:36 +00:00
parent a680970347
commit 4d3c4a704a
12 changed files with 762 additions and 11 deletions

View File

@ -36,6 +36,7 @@ use Doctrine\DBAL\Types\Type,
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
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']))

View File

@ -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
{
}

View File

@ -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';

View File

@ -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()

View File

@ -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;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Orm_Tools_SchemaTool_AllTests::main');
}
require_once __DIR__ . '/../../../TestInit.php';
class AllTests
{
public static function main()
{
\PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Schema Tool');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaTool\MysqlUpdateSchemaTest');
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Orm_Tools_SchemaTool_AllTests::main') {
AllTests::main();
}

View File

@ -0,0 +1,46 @@
<?php
require_once realpath(dirname(__FILE__)."/../../../../../../lib/Doctrine/Common/IsolatedClassLoader.php");
$classLoader = new \Doctrine\Common\IsolatedClassLoader('Doctrine');
$classLoader->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 "<?php\n\return ";
echo $code;
echo ";\n?>\n";

View File

@ -0,0 +1,347 @@
<?php
$fixtures = array (
'cms_addresses' =>
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;
?>

View File

@ -0,0 +1,41 @@
<?php
return array (
'decimal_model' =>
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,
),
),
);
?>

View File

@ -0,0 +1,190 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
require_once __DIR__ . '/../../../TestInit.php';
class MysqlUpdateSchemaTest extends UpdateSchemaTestCase
{
protected function _createPlatform()
{
return new \Doctrine\DBAL\Platforms\MySqlPlatform();
}
public function testAddField()
{
$address = new \Doctrine\Tests\Models\CMS\CmsAddress;
$st = $this->_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]
);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Doctrine\Tests\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\SchemaTool;
require_once __DIR__ . '/../../../TestInit.php';
abstract class UpdateSchemaTestCase extends \Doctrine\Tests\OrmTestCase
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $_em = null;
/**
*
* @param string $fixtureName
* @return \Doctrine\ORM\Tools\SchemaTool
*/
protected function _getSchemaTool($fixtureName)
{
return $this->_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];
}
}

View File

@ -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');