From: Chuck Hagenbuch Date: Wed, 6 Jan 2010 19:12:09 +0000 (-0500) Subject: First pass moving tests that are testing adapter functionality, not migration X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=d323a29c43a63555e33e239f9c8271676c5f3848;p=horde.git First pass moving tests that are testing adapter functionality, not migration functionality, to the adapter test suites. Also adding a few checks to make sure that tables are renamed, not copied. --- diff --git a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php index 6bae108d1..3b1979dc0 100644 --- a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php +++ b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php @@ -506,13 +506,142 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, $this->_conn->selectValue($sql)); } + public function testCreateTableAddsId() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[] = $col->getName(); + } + sort($columns); + $this->assertEquals(array('foo', 'id'), $columns); + } + + public function testCreateTableWithNotNullColumn() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('null' => false)); + $table->end(); + + try { + $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testCreateTableWithDefaults() + { + $table = $this->_conn->createTable('testings'); + $table->column('one', 'string', array('default' => 'hello')); + $table->column('two', 'boolean', array('default' => true)); + $table->column('three', 'boolean', array('default' => false)); + $table->column('four', 'integer', array('default' => 1)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + + $this->assertEquals('hello', $columns['one']->getDefault()); + $this->assertTrue($columns['two']->getDefault()); + $this->assertFalse($columns['three']->getDefault()); + $this->assertEquals(1, $columns['four']->getDefault()); + } + + public function testCreateTableWithLimits() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('limit' => 80)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + $this->assertEquals(80, $columns['foo']->getLimit()); + } + + public function testCreateTableWithBinaryColumn() + { + try { + $table = $this->_conn->createTable('binary_testings'); + $table->column('data', 'binary', array('null' => false)); + $table->end(); + } catch (Exception $e) { $this->fail('Unexepected exception raised'); } + + $columns = $this->_conn->columns('binary_testings'); + + foreach ($columns as $c) { + if ($c->getName() == 'data') { $dataColumn = $c; } + } + $this->assertEquals('', $dataColumn->getDefault()); + } + public function testRenameTable() { + // Simple rename then select test $this->_createTestTable('sports'); $this->_conn->renameTable('sports', 'my_sports'); $sql = "SELECT id FROM my_sports WHERE id = 1"; $this->assertEquals("1", $this->_conn->selectValue($sql)); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + + // Rename then insert test + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + $this->_conn->dropTable('octopi'); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM octopuses WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + } + + public function testRenameTableWithAnIndex() + { + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + $this->_conn->addIndex('octopuses', 'url'); + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + $indexes = $this->_conn->indexes('octopi'); + $this->assertEquals('url', $indexes[0]->columns[0]); + + $this->_conn->dropTable('octopi'); } public function testDropTable() @@ -556,6 +685,64 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase $this->fail("Column exists where it shouldn't have"); } + public function testChangeColumn() + { + $this->_createTestUsersTable(); + + $this->_conn->addColumn('users', 'age', 'integer'); + $oldColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertTrue($found); + + $this->_conn->changeColumn('users', 'age', 'string'); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertFalse($found); + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; } + } + $this->assertTrue($found); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false)); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertFalse($found); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == false) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true)); + } + public function testChangeColumnDefault() { $this->_createTestTable('sports'); @@ -608,6 +795,11 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase public function testRenameColumn() { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'nick_name'); + $this->assertTrue(in_array('nick_name', $this->_columnNames('users'))); + $this->_createTestTable('sports'); $beforeChange = $this->_getColumn('sports', 'is_college'); @@ -619,6 +811,45 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase $this->assertEquals('tinyint(1)', $afterChange->getSqlType()); } + public function testRenameColumnWithSqlReservedWord() + { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'group'); + $this->assertTrue(in_array('group', $this->_columnNames('users'))); + } + + public function testAddIndex() + { + $this->_createTestUsersTable(); + + // Limit size of last_name and key columns to support Firebird index limitations + $this->_conn->addColumn('users', 'last_name', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'key', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'administrator', 'boolean'); + + $this->_conn->addIndex('users', 'last_name'); + $this->_conn->removeIndex('users', 'last_name'); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name'))); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name')); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', 'last_name_and_first_name'); + + // quoting + $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true)); + $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true)); + + $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'), + array('name' => "named_admin")); + + $this->_conn->removeIndex('users', array('name' => 'named_admin')); + } + public function testAddIndexDefault() { $this->_createTestTable('sports'); @@ -882,6 +1113,63 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase $this->assertEquals("test NOT NULL", $result); } + public function testAddColumnNotNullWithoutDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => '')); + + try { + $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testAddColumnNotNullWithDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')"); + + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default')); + + try { + $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testAddRemoveSingleField() + { + $this->_createTestUsersTable(); + + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->addColumn('users', 'last_name', 'string'); + $this->assertTrue(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->removeColumn('users', 'last_name'); + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + } + + public function testAddRename() + { + $this->_createTestUsersTable(); + + $this->_conn->delete('DELETE FROM users'); + + $this->_conn->addColumn('users', 'girlfriend', 'string'); + $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')"); + + $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend'); + + $bob = (object)$this->_conn->selectOne('SELECT * FROM users'); + $this->assertEquals('bobette', $bob->exgirlfriend); + } + public function testDistinct() { $result = $this->_conn->distinct("test"); @@ -971,6 +1259,9 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase $this->_conn->dropTable('unit_tests'); } catch (Exception $e) {} try { + $this->_conn->dropTable('testings'); + } catch (Exception $e) {} + try { $this->_conn->dropTable('users'); } catch (Exception $e) {} try { @@ -987,6 +1278,15 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase } catch (Exception $e) {} } + protected function _columnNames($tableName) + { + $columns = array(); + foreach ($this->_conn->columns($tableName) as $c) { + $columns[] = $c->getName(); + } + return $columns; + } + /** * Get a column by name */ diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php index 3238d7d64..05a568aad 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php @@ -528,13 +528,142 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, $this->_conn->selectValue($sql)); } + public function testCreateTableAddsId() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[] = $col->getName(); + } + sort($columns); + $this->assertEquals(array('foo', 'id'), $columns); + } + + public function testCreateTableWithNotNullColumn() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('null' => false)); + $table->end(); + + try { + $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testCreateTableWithDefaults() + { + $table = $this->_conn->createTable('testings'); + $table->column('one', 'string', array('default' => 'hello')); + $table->column('two', 'boolean', array('default' => true)); + $table->column('three', 'boolean', array('default' => false)); + $table->column('four', 'integer', array('default' => 1)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + + $this->assertEquals('hello', $columns['one']->getDefault()); + $this->assertTrue($columns['two']->getDefault()); + $this->assertFalse($columns['three']->getDefault()); + $this->assertEquals(1, $columns['four']->getDefault()); + } + + public function testCreateTableWithLimits() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('limit' => 80)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + $this->assertEquals(80, $columns['foo']->getLimit()); + } + + public function testCreateTableWithBinaryColumn() + { + try { + $table = $this->_conn->createTable('binary_testings'); + $table->column('data', 'binary', array('null' => false)); + $table->end(); + } catch (Exception $e) { $this->fail('Unexepected exception raised'); } + + $columns = $this->_conn->columns('binary_testings'); + + foreach ($columns as $c) { + if ($c->getName() == 'data') { $dataColumn = $c; } + } + $this->assertEquals('', $dataColumn->getDefault()); + } + public function testRenameTable() { + // Simple rename then select test $this->_createTestTable('sports'); $this->_conn->renameTable('sports', 'my_sports'); $sql = "SELECT id FROM my_sports WHERE id = 1"; $this->assertEquals("1", $this->_conn->selectValue($sql)); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + + // Rename then insert test + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + $this->_conn->dropTable('octopi'); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM octopuses WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + } + + public function testRenameTableWithAnIndex() + { + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + $this->_conn->addIndex('octopuses', 'url'); + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + $indexes = $this->_conn->indexes('octopi'); + $this->assertEquals('url', $indexes[0]->columns[0]); + + $this->_conn->dropTable('octopi'); } public function testDropTable() @@ -578,6 +707,64 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $this->fail("Column exists where it shouldn't have"); } + public function testChangeColumn() + { + $this->_createTestUsersTable(); + + $this->_conn->addColumn('users', 'age', 'integer'); + $oldColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertTrue($found); + + $this->_conn->changeColumn('users', 'age', 'string'); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertFalse($found); + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; } + } + $this->assertTrue($found); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false)); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertFalse($found); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == false) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true)); + } + public function testChangeColumnDefault() { $this->_createTestTable('sports'); @@ -630,6 +817,11 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase public function testRenameColumn() { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'nick_name'); + $this->assertTrue(in_array('nick_name', $this->_columnNames('users'))); + $this->_createTestTable('sports'); $beforeChange = $this->_getColumn('sports', 'is_college'); @@ -641,6 +833,45 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $this->assertEquals('tinyint(1)', $afterChange->getSqlType()); } + public function testRenameColumnWithSqlReservedWord() + { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'group'); + $this->assertTrue(in_array('group', $this->_columnNames('users'))); + } + + public function testAddIndex() + { + $this->_createTestUsersTable(); + + // Limit size of last_name and key columns to support Firebird index limitations + $this->_conn->addColumn('users', 'last_name', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'key', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'administrator', 'boolean'); + + $this->_conn->addIndex('users', 'last_name'); + $this->_conn->removeIndex('users', 'last_name'); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name'))); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name')); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', 'last_name_and_first_name'); + + // quoting + $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true)); + $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true)); + + $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'), + array('name' => "named_admin")); + + $this->_conn->removeIndex('users', array('name' => 'named_admin')); + } + public function testAddIndexDefault() { $this->_createTestTable('sports'); @@ -904,6 +1135,64 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $this->assertEquals("test NOT NULL", $result); } + public function testAddColumnNotNullWithoutDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => '')); + + try { + $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + + } + + public function testAddColumnNotNullWithDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')"); + + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default')); + + try { + $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testAddRemoveSingleField() + { + $this->_createTestUsersTable(); + + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->addColumn('users', 'last_name', 'string'); + $this->assertTrue(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->removeColumn('users', 'last_name'); + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + } + + public function testAddRename() + { + $this->_createTestUsersTable(); + + $this->_conn->delete('DELETE FROM users'); + + $this->_conn->addColumn('users', 'girlfriend', 'string'); + $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')"); + + $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend'); + + $bob = (object)$this->_conn->selectOne('SELECT * FROM users'); + $this->assertEquals('bobette', $bob->exgirlfriend); + } + public function testDistinct() { $result = $this->_conn->distinct("test"); @@ -993,6 +1282,9 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $this->_conn->dropTable('unit_tests'); } catch (Exception $e) {} try { + $this->_conn->dropTable('testings'); + } catch (Exception $e) {} + try { $this->_conn->dropTable('users'); } catch (Exception $e) {} try { @@ -1009,6 +1301,15 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase } catch (Exception $e) {} } + protected function _columnNames($tableName) + { + $columns = array(); + foreach ($this->_conn->columns($tableName) as $c) { + $columns[] = $c->getName(); + } + return $columns; + } + /** * Get a column by name */ diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php index a5023e8fb..0b2e2950b 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php @@ -430,13 +430,142 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, $this->_conn->selectValue($sql)); } + public function testCreateTableAddsId() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[] = $col->getName(); + } + sort($columns); + $this->assertEquals(array('foo', 'id'), $columns); + } + + public function testCreateTableWithNotNullColumn() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('null' => false)); + $table->end(); + + try { + $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testCreateTableWithDefaults() + { + $table = $this->_conn->createTable('testings'); + $table->column('one', 'string', array('default' => 'hello')); + $table->column('two', 'boolean', array('default' => true)); + $table->column('three', 'boolean', array('default' => false)); + $table->column('four', 'integer', array('default' => 1)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + + $this->assertEquals('hello', $columns['one']->getDefault()); + $this->assertTrue($columns['two']->getDefault()); + $this->assertFalse($columns['three']->getDefault()); + $this->assertEquals(1, $columns['four']->getDefault()); + } + + public function testCreateTableWithLimits() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('limit' => 80)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + $this->assertEquals(80, $columns['foo']->getLimit()); + } + + public function testCreateTableWithBinaryColumn() + { + try { + $table = $this->_conn->createTable('binary_testings'); + $table->column('data', 'binary', array('null' => false)); + $table->end(); + } catch (Exception $e) { $this->fail('Unexepected exception raised'); } + + $columns = $this->_conn->columns('binary_testings'); + + foreach ($columns as $c) { + if ($c->getName() == 'data') { $dataColumn = $c; } + } + $this->assertEquals('', $dataColumn->getDefault()); + } + public function testRenameTable() { + // Simple rename then select test $this->_createTestTable('sports'); $this->_conn->renameTable('sports', 'my_sports'); $sql = "SELECT id FROM my_sports WHERE id = 1"; $this->assertEquals("1", $this->_conn->selectValue($sql)); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + + // Rename then insert test + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + $this->_conn->dropTable('octopi'); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM octopuses WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + } + + public function testRenameTableWithAnIndex() + { + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + $this->_conn->addIndex('octopuses', 'url'); + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + $indexes = $this->_conn->indexes('octopi'); + $this->assertEquals('url', $indexes[0]->columns[0]); + + $this->_conn->dropTable('octopi'); } public function testDropTable() @@ -480,6 +609,64 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase $this->fail("Column exists where it shouldn't have"); } + public function testChangeColumn() + { + $this->_createTestUsersTable(); + + $this->_conn->addColumn('users', 'age', 'integer'); + $oldColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertTrue($found); + + $this->_conn->changeColumn('users', 'age', 'string'); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertFalse($found); + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; } + } + $this->assertTrue($found); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false)); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertFalse($found); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == false) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true)); + } + public function testChangeColumnDefault() { $this->_createTestTable('sports'); @@ -532,6 +719,11 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase public function testRenameColumn() { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'nick_name'); + $this->assertTrue(in_array('nick_name', $this->_columnNames('users'))); + $this->_createTestTable('sports'); $beforeChange = $this->_getColumn('sports', 'is_college'); @@ -543,6 +735,45 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase $this->assertEquals('boolean', $afterChange->getSqlType()); } + public function testRenameColumnWithSqlReservedWord() + { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'group'); + $this->assertTrue(in_array('group', $this->_columnNames('users'))); + } + + public function testAddIndex() + { + $this->_createTestUsersTable(); + + // Limit size of last_name and key columns to support Firebird index limitations + $this->_conn->addColumn('users', 'last_name', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'key', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'administrator', 'boolean'); + + $this->_conn->addIndex('users', 'last_name'); + $this->_conn->removeIndex('users', 'last_name'); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name'))); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name')); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', 'last_name_and_first_name'); + + // quoting + $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true)); + $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true)); + + $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'), + array('name' => "named_admin")); + + $this->_conn->removeIndex('users', array('name' => 'named_admin')); + } + public function testAddIndexDefault() { $this->_createTestTable('sports'); @@ -774,6 +1005,64 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase $this->assertEquals("test NOT NULL", $result); } + public function testAddColumnNotNullWithoutDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => '')); + + try { + $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + + } + + public function testAddColumnNotNullWithDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')"); + + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default')); + + try { + $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testAddRemoveSingleField() + { + $this->_createTestUsersTable(); + + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->addColumn('users', 'last_name', 'string'); + $this->assertTrue(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->removeColumn('users', 'last_name'); + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + } + + public function testAddRename() + { + $this->_createTestUsersTable(); + + $this->_conn->delete('DELETE FROM users'); + + $this->_conn->addColumn('users', 'girlfriend', 'string'); + $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')"); + + $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend'); + + $bob = (object)$this->_conn->selectOne('SELECT * FROM users'); + $this->assertEquals('bobette', $bob->exgirlfriend); + } + public function testDistinct() { $result = $this->_conn->distinct("test"); @@ -863,6 +1152,9 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase $this->_conn->dropTable('unit_tests'); } catch (Exception $e) {} try { + $this->_conn->dropTable('testings'); + } catch (Exception $e) {} + try { $this->_conn->dropTable('users'); } catch (Exception $e) {} try { @@ -879,6 +1171,15 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase } catch (Exception $e) {} } + protected function _columnNames($tableName) + { + $columns = array(); + foreach ($this->_conn->columns($tableName) as $c) { + $columns[] = $c->getName(); + } + return $columns; + } + /** * Get a column by name */ @@ -903,5 +1204,4 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase if ($columns == $indexes) return $index; } } - } diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php index 4df2c77f5..322630196 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php @@ -423,13 +423,138 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, $this->_conn->selectValue($sql)); } + public function testCreateTableAddsId() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[] = $col->getName(); + } + sort($columns); + $this->assertEquals(array('foo', 'id'), $columns); + } + + public function testCreateTableWithNotNullColumn() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('null' => false)); + $table->end(); + + try { + $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testCreateTableWithDefaults() + { + $table = $this->_conn->createTable('testings'); + $table->column('one', 'string', array('default' => 'hello')); + $table->column('two', 'boolean', array('default' => true)); + $table->column('three', 'boolean', array('default' => false)); + $table->column('four', 'integer', array('default' => 1)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + + $this->assertEquals('hello', $columns['one']->getDefault()); + $this->assertTrue($columns['two']->getDefault()); + $this->assertFalse($columns['three']->getDefault()); + $this->assertEquals(1, $columns['four']->getDefault()); + } + + public function testCreateTableWithLimits() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string', array('limit' => 80)); + $table->end(); + + $columns = array(); + foreach ($this->_conn->columns('testings') as $col) { + $columns[$col->getName()] = $col; + } + $this->assertEquals(80, $columns['foo']->getLimit()); + } + + public function testCreateTableWithBinaryColumn() + { + try { + $table = $this->_conn->createTable('binary_testings'); + $table->column('data', 'binary', array('null' => false)); + $table->end(); + } catch (Exception $e) { $this->fail('Unexepected exception raised'); } + + $columns = $this->_conn->columns('binary_testings'); + + foreach ($columns as $c) { + if ($c->getName() == 'data') { $dataColumn = $c; } + } + $this->assertEquals('', $dataColumn->getDefault()); + } + public function testRenameTable() { + // Simple rename then select test $this->_createTestTable('sports'); $this->_conn->renameTable('sports', 'my_sports'); $sql = "SELECT id FROM my_sports WHERE id = 1"; $this->assertEquals("1", $this->_conn->selectValue($sql)); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM sports WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + + // Rename then insert test + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + // Make sure the old table name isn't still there + try { + $sql = "SELECT id FROM octopuses WHERE id = 1"; + $this->_conn->execute($sql); + } catch (Exception $e) { + return; + } + $this->fail("Table exists where it shouldn't have"); + } + + public function testRenameTableWithAnIndex() + { + $table = $this->_conn->createTable('octopuses'); + $table->column('url', 'string'); + $table->end(); + $this->_conn->addIndex('octopuses', 'url'); + $this->_conn->renameTable('octopuses', 'octopi'); + + $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; + $this->_conn->execute($sql); + + $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', + $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); + + $indexes = $this->_conn->indexes('octopi'); + $this->assertEquals('url', $indexes[0]->columns[0]); } public function testDropTable() @@ -473,6 +598,64 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $this->fail("Column exists where it shouldn't have"); } + public function testChangeColumn() + { + $this->_createTestUsersTable(); + + $this->_conn->addColumn('users', 'age', 'integer'); + $oldColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertTrue($found); + + $this->_conn->changeColumn('users', 'age', 'string'); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } + } + $this->assertFalse($found); + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; } + } + $this->assertTrue($found); + + $found = false; + foreach ($oldColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false)); + + $newColumns = $this->_conn->columns('users', "User Columns"); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == true) { $found = true; } + } + $this->assertFalse($found); + + $found = false; + foreach ($newColumns as $c) { + if ($c->getName() == 'approved' && $c->getType() == 'boolean' && + $c->getDefault() == false) { $found = true; } + } + $this->assertTrue($found); + + // changeColumn() throws exception on error + $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true)); + } + public function testChangeColumnDefault() { $this->_createTestTable('sports'); @@ -525,6 +708,11 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase public function testRenameColumn() { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'nick_name'); + $this->assertTrue(in_array('nick_name', $this->_columnNames('users'))); + $this->_createTestTable('sports'); $beforeChange = $this->_getColumn('sports', 'is_college'); @@ -536,6 +724,45 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $this->assertEquals('boolean', $afterChange->getSqlType()); } + public function testRenameColumnWithSqlReservedWord() + { + $this->_createTestUsersTable(); + + $this->_conn->renameColumn('users', 'first_name', 'group'); + $this->assertTrue(in_array('group', $this->_columnNames('users'))); + } + + public function testAddIndex() + { + $this->_createTestUsersTable(); + + // Limit size of last_name and key columns to support Firebird index limitations + $this->_conn->addColumn('users', 'last_name', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'key', 'string', array('limit' => 100)); + $this->_conn->addColumn('users', 'administrator', 'boolean'); + + $this->_conn->addIndex('users', 'last_name'); + $this->_conn->removeIndex('users', 'last_name'); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name'))); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name')); + + $this->_conn->addIndex('users', array('last_name', 'first_name')); + $this->_conn->removeIndex('users', 'last_name_and_first_name'); + + // quoting + $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true)); + $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true)); + + $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'), + array('name' => "named_admin")); + + $this->_conn->removeIndex('users', array('name' => 'named_admin')); + } + public function testAddIndexDefault() { $this->_createTestTable('sports'); @@ -790,6 +1017,64 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $this->assertEquals('test NOT NULL', $result); } + public function testAddColumnNotNullWithoutDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => '')); + + try { + $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + + } + + public function testAddColumnNotNullWithDefault() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'string'); + $table->end(); + + $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')"); + + $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default')); + + try { + $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)"); + } catch (Exception $e) { return; } + $this->fail('Expected exception wasn\'t raised'); + } + + public function testAddRemoveSingleField() + { + $this->_createTestUsersTable(); + + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->addColumn('users', 'last_name', 'string'); + $this->assertTrue(in_array('last_name', $this->_columnNames('users'))); + + $this->_conn->removeColumn('users', 'last_name'); + $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); + } + + public function testAddRename() + { + $this->_createTestUsersTable(); + + $this->_conn->delete('DELETE FROM users'); + + $this->_conn->addColumn('users', 'girlfriend', 'string'); + $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')"); + + $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend'); + + $bob = (object)$this->_conn->selectOne('SELECT * FROM users'); + $this->assertEquals('bobette', $bob->exgirlfriend); + } + public function testDistinct() { $result = $this->_conn->distinct('test'); @@ -870,6 +1155,15 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $table->end(); } + protected function _columnNames($tableName) + { + $columns = array(); + foreach ($this->_conn->columns($tableName) as $c) { + $columns[] = $c->getName(); + } + return $columns; + } + /** * Get a column by name */ @@ -894,5 +1188,4 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase if ($columns == $indexes) return $index; } } - } diff --git a/framework/Db/test/Horde/Db/Migration/BaseTest.php b/framework/Db/test/Horde/Db/Migration/BaseTest.php index 35dd8ef30..5ba207a01 100644 --- a/framework/Db/test/Horde/Db/Migration/BaseTest.php +++ b/framework/Db/test/Horde/Db/Migration/BaseTest.php @@ -28,328 +28,12 @@ require_once dirname(dirname(__FILE__)) . '/fixtures/migrations_with_decimal/1_g */ class Horde_Db_Migration_BaseTest extends PHPUnit_Framework_TestCase { - public function setUp() - { - $this->_conn = Horde_Db_Adapter::factory(array( - 'adapter' => 'pdo_sqlite', - 'dbname' => ':memory:', - )); - - /* -CREATE TABLE users ( - id int(11) auto_increment, - company_id int(11), - name varchar(255) default '', - first_name varchar(40) default '', - approved tinyint(1) default '1', - type varchar(255) default '', - created_at datetime default '0000-00-00 00:00:00', - created_on date default '0000-00-00', - updated_at datetime default '0000-00-00 00:00:00', - updated_on date default '0000-00-00', - PRIMARY KEY (id) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; - */ - $table = $this->_conn->createTable('users'); - $table->column('company_id', 'integer', array('limit' => 11)); - $table->column('name', 'string', array('limit' => 255, 'default' => '')); - $table->column('first_name', 'string', array('limit' => 40, 'default' => '')); - $table->column('approved', 'boolean', array('default' => true)); - $table->column('type', 'string', array('limit' => 255, 'default' => '')); - $table->column('created_at', 'datetime', array('default' => '0000-00-00 00:00:00')); - $table->column('created_on', 'date', array('default' => '0000-00-00')); - $table->column('updated_at', 'datetime', array('default' => '0000-00-00 00:00:00')); - $table->column('updated_on', 'date', array('default' => '0000-00-00')); - $table->end(); - /* -mike: - id: 1 - company_id: 1 - name: Mike Naberezny - first_name: Mike - approved: 1 - type: User - created_at: '2008-01-01 12:20:00' - created_on: '2008-01-01' - updated_at: '2008-01-01 12:20:00' - updated_on: '2008-01-01' - -derek: - id: 2 - company_id: 1 - name: Derek DeVries - first_name: Derek - approved: 1 - type: User - created_at: '' - created_on: '' - updated_at: '' - updated_on: '' - -client: - id: 3 - company_id: 1 - name: Extreme - first_name: Engineer - approved: 1 - type: Client - created_at: '2008-01-01 12:20:00' - created_on: '2008-01-01' - updated_at: '2008-01-01 12:20:00' - updated_on: '2008-01-01' - */ - } - - public function testAddIndex() - { - // Limit size of last_name and key columns to support Firebird index limitations - $this->_conn->addColumn('users', 'last_name', 'string', array('limit' => 100)); - $this->_conn->addColumn('users', 'key', 'string', array('limit' => 100)); - $this->_conn->addColumn('users', 'administrator', 'boolean'); - - $this->_conn->addIndex('users', 'last_name'); - $this->_conn->removeIndex('users', 'last_name'); - - $this->_conn->addIndex('users', array('last_name', 'first_name')); - $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name'))); - - $this->_conn->addIndex('users', array('last_name', 'first_name')); - $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name')); - - $this->_conn->addIndex('users', array('last_name', 'first_name')); - $this->_conn->removeIndex('users', 'last_name_and_first_name'); - - // quoting - - $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true)); - $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true)); - - $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'), - array('name' => "named_admin")); - - $this->_conn->removeIndex('users', array('name' => 'named_admin')); - } - - public function testCreateTableAddsId() - { - $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string'); - $table->end(); - - $columns = array(); - foreach ($this->_conn->columns('testings') as $col) { - $columns[] = $col->getName(); - } - sort($columns); - $this->assertEquals(array('foo', 'id'), $columns); - } - - public function testCreateTableWithNotNullColumn() - { - $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string', array('null' => false)); - $table->end(); - - try { - $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)"); - } catch (Exception $e) { return; } - $this->fail('Expected exception wasn\'t raised'); - } - - /** - * @see Horde_Db_Adapter_Abstract_ColumnTest - */ - public function testCreateTableWithDefaults() - { - $table = $this->_conn->createTable('testings'); - $table->column('one', 'string', array('default' => 'hello')); - $table->column('two', 'boolean', array('default' => true)); - $table->column('three', 'boolean', array('default' => false)); - $table->column('four', 'integer', array('default' => 1)); - $table->end(); - - $columns = array(); - foreach ($this->_conn->columns('testings') as $col) { - $columns[$col->getName()] = $col; - } - - $this->assertEquals('hello', $columns['one']->getDefault()); - $this->assertTrue($columns['two']->getDefault()); - $this->assertFalse($columns['three']->getDefault()); - $this->assertEquals(1, $columns['four']->getDefault()); - } - - public function testCreateTableWithLimits() - { - $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string', array('limit' => 80)); - $table->end(); - - $columns = array(); - foreach ($this->_conn->columns('testings') as $col) { - $columns[$col->getName()] = $col; - } - $this->assertEquals(80, $columns['foo']->getLimit()); - } - - public function testAddColumnNotNullWithoutDefault() - { - $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string'); - $table->end(); - $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => '')); - - try { - $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)"); - } catch (Exception $e) { return; } - $this->fail('Expected exception wasn\'t raised'); - - } - - public function testAddColumnNotNullWithDefault() - { - $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string'); - $table->end(); - - $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')"); - - $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default')); - - try { - $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)"); - } catch (Exception $e) { return; } - $this->fail('Expected exception wasn\'t raised'); - } - - public function testAddRemoveSingleField() - { - $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); - - $this->_conn->addColumn('users', 'last_name', 'string'); - $this->assertTrue(in_array('last_name', $this->_columnNames('users'))); - - $this->_conn->removeColumn('users', 'last_name'); - $this->assertFalse(in_array('last_name', $this->_columnNames('users'))); - } - - public function testAddRename() - { - $this->_conn->delete('DELETE FROM users'); - - $this->_conn->addColumn('users', 'girlfriend', 'string'); - $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')"); - - $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend'); - - $bob = (object)$this->_conn->selectOne('SELECT * FROM users'); - $this->assertEquals('bobette', $bob->exgirlfriend); - } - - public function testRenameColumn() - { - $this->_conn->renameColumn('users', 'first_name', 'nick_name'); - $this->assertTrue(in_array('nick_name', $this->_columnNames('users'))); - } - - public function testRenameColumnWithSqlReservedWord() - { - $this->_conn->renameColumn('users', 'first_name', 'group'); - $this->assertTrue(in_array('group', $this->_columnNames('users'))); - } - - public function testRenameTable() - { - $table = $this->_conn->createTable('octopuses'); - $table->column('url', 'string'); - $table->end(); - - $this->_conn->renameTable('octopuses', 'octopi'); - - $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; - $this->_conn->execute($sql); - - $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', - $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); - } - - public function testRenameTableWithAnIndex() - { - $table = $this->_conn->createTable('octopuses'); - $table->column('url', 'string'); - $table->end(); - $this->_conn->addIndex('octopuses', 'url'); - $this->_conn->renameTable('octopuses', 'octopi'); - - $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')"; - $this->_conn->execute($sql); - - $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg', - $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1")); - - $indexes = $this->_conn->indexes('octopi'); - $this->assertEquals('url', $indexes[0]->columns[0]); - } - - public function testChangeColumn() - { - $this->_conn->addColumn('users', 'age', 'integer'); - $oldColumns = $this->_conn->columns('users', "User Columns"); - - $found = false; - foreach ($oldColumns as $c) { - if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } - } - $this->assertTrue($found); - - $this->_conn->changeColumn('users', 'age', 'string'); - - $newColumns = $this->_conn->columns('users', "User Columns"); - - $found = false; - foreach ($newColumns as $c) { - if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; } - } - $this->assertFalse($found); - $found = false; - foreach ($newColumns as $c) { - if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; } - } - $this->assertTrue($found); - - $found = false; - foreach ($oldColumns as $c) { - if ($c->getName() == 'approved' && $c->getType() == 'boolean' && - $c->getDefault() == true) { $found = true; } - } - $this->assertTrue($found); - - // changeColumn() throws exception on error - $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false)); - - $newColumns = $this->_conn->columns('users', "User Columns"); - - $found = false; - foreach ($newColumns as $c) { - if ($c->getName() == 'approved' && $c->getType() == 'boolean' && - $c->getDefault() == true) { $found = true; } - } - $this->assertFalse($found); - - $found = false; - foreach ($newColumns as $c) { - if ($c->getName() == 'approved' && $c->getType() == 'boolean' && - $c->getDefault() == false) { $found = true; } - } - $this->assertTrue($found); - - // changeColumn() throws exception on error - $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true)); - } - + /** These tests need support for pulling default properties for an object from a table definition **/ + /* public function testChangeColumnWithNilDefault() { - $this->markTestSkipped(); + $this->_createTestUsersTable(); + $this->_conn->addColumn('users', 'contributor', 'boolean', array('default' => true)); $user = new User; $this->assertTrue($user->contributor); @@ -363,7 +47,8 @@ client: public function testChangeColumnWithNewDefault() { - $this->markTestSkipped(); + $this->_createTestUsersTable(); + $this->_conn->addColumn('users', 'administrator', 'boolean', array('default' => true)); $user = new User; $this->assertTrue($user->administrator); @@ -377,7 +62,8 @@ client: public function testChangeColumnDefault() { - $this->markTestSkipped(); + $this->_createTestUsersTable(); + $this->_conn->changeColumnDefault('users', 'first_name', 'Tester'); $user = new User; @@ -386,12 +72,22 @@ client: public function testChangeColumnDefaultToNull() { - $this->markTestSkipped(); + $this->_createTestUsersTable(); + $this->_conn->changeColumnDefault('users', 'first_name', null); $user = new User; $this->assertNull($user->first_name); } + */ + + public function setUp() + { + $this->_conn = Horde_Db_Adapter::factory(array( + 'adapter' => 'pdo_sqlite', + 'dbname' => ':memory:', + )); + } public function testAddTable() { @@ -444,31 +140,4 @@ client: } catch (Exception $e) {} $this->assertType('Horde_Db_Exception', $e); } - - public function testCreateTableWithBinaryColumn() - { - try { - $table = $this->_conn->createTable('binary_testings'); - $table->column('data', 'binary', array('null' => false)); - $table->end(); - } catch (Exception $e) { $this->fail('Unexepected exception raised'); } - - $columns = $this->_conn->columns('binary_testings'); - - foreach ($columns as $c) { - if ($c->getName() == 'data') { $dataColumn = $c; } - } - $this->assertEquals('', $dataColumn->getDefault()); - } - - - protected function _columnNames($tableName) - { - $columns = array(); - foreach ($this->_conn->columns($tableName) as $c) { - $columns[] = $c->getName(); - } - return $columns; - } - }