Migrating DboMyqli to phpunit

This commit is contained in:
José Lorenzo Rodríguez 2010-06-09 23:00:35 -04:30
parent 0a3e27b000
commit 1b9eb035fd

View file

@ -140,22 +140,17 @@ class DboMysqliTest extends CakeTestCase {
* @var DboSource * @var DboSource
* @access public * @access public
*/ */
public $Db = null; public $Dbo = null;
/**
* Skip if cannot connect to mysqli
*
*/
public function skip() {
$this->_initDb();
$this->skipUnless($this->db->config['driver'] == 'mysqli', '%s MySQLi connection not available');
}
/** /**
* Sets up a Dbo class instance for testing * Sets up a Dbo class instance for testing
* *
*/ */
public function setUp() { public function setUp() {
$this->Dbo = ConnectionManager::getDataSource('test_suite');
if ($this->Dbo->config['driver'] !== 'mysqli') {
$this->markTestSkipped('The MySQLi extension is not available.');
}
$this->model = new MysqliTestModel(); $this->model = new MysqliTestModel();
} }
@ -174,50 +169,50 @@ class DboMysqliTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testIndexDetection() { public function testIndexDetection() {
$this->db->cacheSources = false; $this->Dbo->cacheSources = false;
$name = $this->db->fullTableName('simple'); $name = $this->Dbo->fullTableName('simple');
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));'); $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
$expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1)); $expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1));
$result = $this->db->index($name, false); $result = $this->Dbo->index($name, false);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->db->query('DROP TABLE ' . $name); $this->Dbo->query('DROP TABLE ' . $name);
$name = $this->db->fullTableName('with_a_key'); $name = $this->Dbo->fullTableName('with_a_key');
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));'); $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
); );
$result = $this->db->index($name, false); $result = $this->Dbo->index($name, false);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->db->query('DROP TABLE ' . $name); $this->Dbo->query('DROP TABLE ' . $name);
$name = $this->db->fullTableName('with_two_keys'); $name = $this->Dbo->fullTableName('with_two_keys');
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));'); $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
); );
$result = $this->db->index($name, false); $result = $this->Dbo->index($name, false);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->db->query('DROP TABLE ' . $name); $this->Dbo->query('DROP TABLE ' . $name);
$name = $this->db->fullTableName('with_compound_keys'); $name = $this->Dbo->fullTableName('with_compound_keys');
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));'); $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0), 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
); );
$result = $this->db->index($name, false); $result = $this->Dbo->index($name, false);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->db->query('DROP TABLE ' . $name); $this->Dbo->query('DROP TABLE ' . $name);
$name = $this->db->fullTableName('with_multiple_compound_keys'); $name = $this->Dbo->fullTableName('with_multiple_compound_keys');
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));'); $this->Dbo->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
@ -225,9 +220,9 @@ class DboMysqliTest extends CakeTestCase {
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0), 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0), 'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
); );
$result = $this->db->index($name, false); $result = $this->Dbo->index($name, false);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->db->query('DROP TABLE ' . $name); $this->Dbo->query('DROP TABLE ' . $name);
} }
/** /**
@ -236,43 +231,43 @@ class DboMysqliTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testColumn() { public function testColumn() {
$result = $this->db->column('varchar(50)'); $result = $this->Dbo->column('varchar(50)');
$expected = 'string'; $expected = 'string';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('text'); $result = $this->Dbo->column('text');
$expected = 'text'; $expected = 'text';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('int(11)'); $result = $this->Dbo->column('int(11)');
$expected = 'integer'; $expected = 'integer';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('int(11) unsigned'); $result = $this->Dbo->column('int(11) unsigned');
$expected = 'integer'; $expected = 'integer';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('tinyint(1)'); $result = $this->Dbo->column('tinyint(1)');
$expected = 'boolean'; $expected = 'boolean';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('boolean'); $result = $this->Dbo->column('boolean');
$expected = 'boolean'; $expected = 'boolean';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('float'); $result = $this->Dbo->column('float');
$expected = 'float'; $expected = 'float';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('float unsigned'); $result = $this->Dbo->column('float unsigned');
$expected = 'float'; $expected = 'float';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('double unsigned'); $result = $this->Dbo->column('double unsigned');
$expected = 'float'; $expected = 'float';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->column('decimal(14,7) unsigned'); $result = $this->Dbo->column('decimal(14,7) unsigned');
$expected = 'float'; $expected = 'float';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
@ -283,14 +278,15 @@ class DboMysqliTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testTransactions() { public function testTransactions() {
$this->db->testing = false; $this->Dbo->testing = false;
$result = $this->db->begin($this->model); $result = $this->Dbo->begin($this->model);
$this->assertTrue($result); $this->assertTrue($result);
$beginSqlCalls = Set::extract('/.[query=START TRANSACTION]', $this->db->_queriesLog); $log = $this->Dbo->getLog();
$beginSqlCalls = Set::extract('/.[query=START TRANSACTION]', $log['log']);
$this->assertEqual(1, count($beginSqlCalls)); $this->assertEqual(1, count($beginSqlCalls));
$result = $this->db->commit($this->model); $result = $this->Dbo->commit($this->model);
$this->assertTrue($result); $this->assertTrue($result);
} }
/** /**
@ -300,7 +296,7 @@ class DboMysqliTest extends CakeTestCase {
*/ */
function testFloatParsing() { function testFloatParsing() {
$model =& new Model(array('ds' => 'test_suite', 'table' => 'datatypes', 'name' => 'Datatype')); $model =& new Model(array('ds' => 'test_suite', 'table' => 'datatypes', 'name' => 'Datatype'));
$result = $this->db->describe($model); $result = $this->Dbo->describe($model);
$this->assertEqual((string)$result['float_field']['length'], '5,2'); $this->assertEqual((string)$result['float_field']['length'], '5,2');
} }
@ -311,23 +307,24 @@ class DboMysqliTest extends CakeTestCase {
* @return void * @return void
*/ */
function testReadTableParameters() { function testReadTableParameters() {
$this->db->cacheSources = $this->db->testing = false; $table = 'tinyint' . uniqid();
$this->db->query('CREATE TABLE ' . $this->db->fullTableName('tinyint') . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'); $this->Dbo->cacheSources = $this->Dbo->testing = false;
$result = $this->db->readTableParameters('tinyint'); $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($table) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;');
$result = $this->Dbo->readTableParameters($table);
$expected = array( $expected = array(
'charset' => 'utf8', 'charset' => 'utf8',
'collate' => 'utf8_unicode_ci', 'collate' => 'utf8_unicode_ci',
'engine' => 'InnoDB'); 'engine' => 'InnoDB');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint')); $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($table));
$this->db->query('CREATE TABLE ' . $this->db->fullTableName('tinyint') . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;'); $this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($table) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id)) ENGINE=MyISAM DEFAULT CHARSET=cp1250 COLLATE=cp1250_general_ci;');
$result = $this->db->readTableParameters('tinyint'); $result = $this->Dbo->readTableParameters($table);
$expected = array( $expected = array(
'charset' => 'cp1250', 'charset' => 'cp1250',
'collate' => 'cp1250_general_ci', 'collate' => 'cp1250_general_ci',
'engine' => 'MyISAM'); 'engine' => 'MyISAM');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint')); $this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($table));
} }
} }