cakephp2-php8/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php

842 lines
26 KiB
PHP
Raw Normal View History

<?php
/**
* DboMysqlTest file
*
* PHP 5
*
2009-11-06 06:46:59 +00:00
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
2010-01-26 19:18:20 +00:00
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
2010-01-26 19:18:20 +00:00
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
2009-11-06 06:00:11 +00:00
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 1.2.0
2009-11-06 06:51:51 +00:00
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql'));
/**
* DboMysqlTestDb class
*
* @package cake
* @subpackage cake.tests.cases.libs.model.datasources
*/
class DboMysqlTestDb extends DboMysql {
/**
* simulated property
*
* @var array
* @access public
*/
public $simulated = array();
/**
* testing property
*
* @var bool true
* @access public
*/
public $testing = true;
/**
* execute method
*
* @param mixed $sql
* @access protected
* @return void
*/
function _execute($sql) {
if ($this->testing) {
$this->simulated[] = $sql;
return null;
}
return parent::_execute($sql);
}
/**
* getLastQuery method
*
* @access public
* @return void
*/
function getLastQuery() {
return $this->simulated[count($this->simulated) - 1];
}
}
/**
* MysqlTestModel class
*
* @package cake
* @subpackage cake.tests.cases.libs.model.datasources
*/
class MysqlTestModel extends Model {
/**
* name property
*
* @var string 'MysqlTestModel'
* @access public
*/
public $name = 'MysqlTestModel';
/**
* useTable property
*
* @var bool false
* @access public
*/
public $useTable = false;
/**
* find method
*
* @param mixed $conditions
* @param mixed $fields
* @param mixed $order
* @param mixed $recursive
* @access public
* @return void
*/
function find($conditions = null, $fields = null, $order = null, $recursive = null) {
return $conditions;
}
/**
* findAll method
*
* @param mixed $conditions
* @param mixed $fields
* @param mixed $order
* @param mixed $recursive
* @access public
* @return void
*/
function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
return $conditions;
}
/**
* schema method
*
* @access public
* @return void
*/
function schema() {
return array(
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
}
}
/**
* DboMysqlTest class
*
* @package cake
* @subpackage cake.tests.cases.libs.model.datasources.dbo
*/
class DboMysqlTest extends CakeTestCase {
public $fixtures = array('core.binary_test');
/**
* The Dbo instance to be tested
*
* @var DboSource
* @access public
*/
2010-06-10 01:33:46 +00:00
public $Dbo = null;
/**
* Sets up a Dbo class instance for testing
*
*/
public function setUp() {
$this->Dbo = ConnectionManager::getDataSource('test');
if ($this->Dbo->config['driver'] !== 'mysql') {
$this->markTestSkipped('The MySQL extension is not available.');
}
$this->_debug = Configure::read('debug');
Configure::write('debug', 1);
$this->model = new MysqlTestModel();
}
/**
* Sets up a Dbo class instance for testing
*
*/
public function tearDown() {
unset($this->model);
ClassRegistry::flush();
Configure::write('debug', $this->_debug);
}
/**
* Test Dbo value method
*
*/
public function testQuoting() {
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->fields($this->model);
$expected = array(
'`MysqlTestModel`.`id`',
'`MysqlTestModel`.`client_id`',
'`MysqlTestModel`.`name`',
'`MysqlTestModel`.`login`',
'`MysqlTestModel`.`passwd`',
'`MysqlTestModel`.`addr_1`',
'`MysqlTestModel`.`addr_2`',
'`MysqlTestModel`.`zip_code`',
'`MysqlTestModel`.`city`',
'`MysqlTestModel`.`country`',
'`MysqlTestModel`.`phone`',
'`MysqlTestModel`.`fax`',
'`MysqlTestModel`.`url`',
'`MysqlTestModel`.`email`',
'`MysqlTestModel`.`comments`',
'`MysqlTestModel`.`last_login`',
'`MysqlTestModel`.`created`',
'`MysqlTestModel`.`updated`'
);
$this->assertEqual($result, $expected);
$expected = 1.2;
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->value(1.2, 'float');
$this->assertEqual($expected, $result);
$expected = "'1,2'";
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->value('1,2', 'float');
$this->assertEqual($expected, $result);
$expected = "'4713e29446'";
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->value('4713e29446');
$this->assertEqual($expected, $result);
$expected = 'NULL';
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->value('', 'integer');
$this->assertEqual($expected, $result);
2010-06-10 01:33:46 +00:00
$expected = 0;
$result = $this->Dbo->value('', 'boolean');
$this->assertEqual($expected, $result);
$expected = 10010001;
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->value(10010001);
$this->assertEqual($expected, $result);
$expected = "'00010010001'";
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->value('00010010001');
$this->assertEqual($expected, $result);
}
/**
* test that localized floats don't cause trouble.
*
* @return void
*/
function testLocalizedFloats() {
$restore = setlocale(LC_ALL, null);
setlocale(LC_ALL, 'de_DE');
$result = $this->db->value(3.141593, 'float');
$this->assertEqual((string)$result, '3.141593');
$result = $this->db->value(3.141593);
$this->assertEqual((string)$result, '3.141593');
setlocale(LC_ALL, $restore);
}
/**
* testTinyintCasting method
*
* @access public
* @return void
*/
function testTinyintCasting() {
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = false;
$tableName = 'tinyint_' . uniqid();
$this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
$this->model = new CakeTestModel(array(
'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test'
));
$result = $this->model->schema();
$this->assertEqual($result['bool']['type'], 'boolean');
$this->assertEqual($result['small_int']['type'], 'integer');
2010-06-10 01:33:46 +00:00
$this->assertTrue((bool)$this->model->save(array('bool' => 5, 'small_int' => 5)));
$result = $this->model->find('first');
$this->assertIdentical($result['Tinyint']['bool'], '1');
$this->assertIdentical($result['Tinyint']['small_int'], '5');
$this->model->deleteAll(true);
2010-06-10 01:33:46 +00:00
$this->assertTrue((bool)$this->model->save(array('bool' => 0, 'small_int' => 100)));
$result = $this->model->find('first');
$this->assertIdentical($result['Tinyint']['bool'], '0');
$this->assertIdentical($result['Tinyint']['small_int'], '100');
$this->model->deleteAll(true);
2010-06-10 01:33:46 +00:00
$this->assertTrue((bool)$this->model->save(array('bool' => true, 'small_int' => 0)));
$result = $this->model->find('first');
$this->assertIdentical($result['Tinyint']['bool'], '1');
$this->assertIdentical($result['Tinyint']['small_int'], '0');
$this->model->deleteAll(true);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
}
/**
* testIndexDetection method
*
* @return void
*/
public function testIndexDetection() {
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = false;
2010-06-10 01:33:46 +00:00
$name = $this->Dbo->fullTableName('simple');
$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));
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->index('simple', false);
$this->assertEqual($expected, $result);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $name);
2010-06-10 01:33:46 +00:00
$name = $this->Dbo->fullTableName('with_a_key');
$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(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->index('with_a_key', false);
$this->assertEqual($expected, $result);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $name);
2010-06-10 01:33:46 +00:00
$name = $this->Dbo->fullTableName('with_two_keys');
$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(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->index('with_two_keys', false);
$this->assertEqual($expected, $result);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $name);
2010-06-10 01:33:46 +00:00
$name = $this->Dbo->fullTableName('with_compound_keys');
$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(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->index('with_compound_keys', false);
$this->assertEqual($expected, $result);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $name);
2010-06-10 01:33:46 +00:00
$name = $this->Dbo->fullTableName('with_multiple_compound_keys');
$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(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->index('with_multiple_compound_keys', false);
$this->assertEqual($expected, $result);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $name);
}
/**
* testBuildColumn method
*
* @access public
* @return void
*/
function testBuildColumn() {
2010-06-10 01:33:46 +00:00
$restore = $this->Dbo->columns;
$this->Dbo->columns = array('varchar(255)' => 1);
$data = array(
'name' => 'testName',
'type' => 'varchar(255)',
'default',
'null' => true,
'key',
'comment' => 'test'
);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->buildColumn($data);
$expected = '`testName` DEFAULT NULL COMMENT \'test\'';
$this->assertEqual($result, $expected);
$data = array(
'name' => 'testName',
'type' => 'varchar(255)',
'default',
'null' => true,
'key',
'charset' => 'utf8',
'collate' => 'utf8_unicode_ci'
);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->buildColumn($data);
$expected = '`testName` CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$this->Dbo->columns = $restore;
}
/**
* MySQL 4.x returns index data in a different format,
* Using a mock ensure that MySQL 4.x output is properly parsed.
*
* @return void
*/
function testIndexOnMySQL4Output() {
2010-06-10 01:33:46 +00:00
$name = $this->Dbo->fullTableName('simple');
2010-06-10 01:33:46 +00:00
$mockDbo = $this->getMock('DboMysql', array('query'));
$columnData = array(
array('0' => array(
'Table' => 'with_compound_keys',
'Non_unique' => '0',
'Key_name' => 'PRIMARY',
'Seq_in_index' => '1',
'Column_name' => 'id',
'Collation' => 'A',
'Cardinality' => '0',
'Sub_part' => NULL,
'Packed' => NULL,
'Null' => '',
'Index_type' => 'BTREE',
'Comment' => ''
)),
array('0' => array(
'Table' => 'with_compound_keys',
'Non_unique' => '1',
'Key_name' => 'pointless_bool',
'Seq_in_index' => '1',
'Column_name' => 'bool',
'Collation' => 'A',
'Cardinality' => NULL,
'Sub_part' => NULL,
'Packed' => NULL,
'Null' => 'YES',
'Index_type' => 'BTREE',
'Comment' => ''
)),
array('0' => array(
'Table' => 'with_compound_keys',
'Non_unique' => '1',
'Key_name' => 'pointless_small_int',
'Seq_in_index' => '1',
'Column_name' => 'small_int',
'Collation' => 'A',
'Cardinality' => NULL,
'Sub_part' => NULL,
'Packed' => NULL,
'Null' => 'YES',
'Index_type' => 'BTREE',
'Comment' => ''
)),
array('0' => array(
'Table' => 'with_compound_keys',
'Non_unique' => '1',
'Key_name' => 'one_way',
'Seq_in_index' => '1',
'Column_name' => 'bool',
'Collation' => 'A',
'Cardinality' => NULL,
'Sub_part' => NULL,
'Packed' => NULL,
'Null' => 'YES',
'Index_type' => 'BTREE',
'Comment' => ''
)),
array('0' => array(
'Table' => 'with_compound_keys',
'Non_unique' => '1',
'Key_name' => 'one_way',
'Seq_in_index' => '2',
'Column_name' => 'small_int',
'Collation' => 'A',
'Cardinality' => NULL,
'Sub_part' => NULL,
'Packed' => NULL,
'Null' => 'YES',
'Index_type' => 'BTREE',
'Comment' => ''
))
);
2010-06-10 01:33:46 +00:00
$mockDbo->expects($this->once())
->method('query')
->with('SHOW INDEX FROM ' . $name)
->will($this->returnValue($columnData));
$result = $mockDbo->index($name, false);
$expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1),
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
);
$this->assertEqual($result, $expected);
}
/**
* testColumn method
*
* @return void
*/
public function testColumn() {
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('varchar(50)');
$expected = 'string';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('text');
$expected = 'text';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('int(11)');
$expected = 'integer';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('int(11) unsigned');
$expected = 'integer';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('tinyint(1)');
$expected = 'boolean';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('boolean');
$expected = 'boolean';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('float');
$expected = 'float';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('float unsigned');
$expected = 'float';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('double unsigned');
$expected = 'float';
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->column('decimal(14,7) unsigned');
$expected = 'float';
$this->assertEqual($result, $expected);
}
/**
* testAlterSchemaIndexes method
*
* @access public
* @return void
*/
function testAlterSchemaIndexes() {
App::import('Model', 'CakeSchema');
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = $this->Dbo->testing = false;
2010-06-10 01:33:46 +00:00
$schema1 = new CakeSchema(array(
'name' => 'AlterTest1',
'connection' => 'test',
'altertest' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
'group1' => array('type' => 'integer', 'null' => true),
'group2' => array('type' => 'integer', 'null' => true)
)));
2010-06-10 01:33:46 +00:00
$this->Dbo->query($this->Dbo->createSchema($schema1));
2010-06-10 01:33:46 +00:00
$schema2 = new CakeSchema(array(
'name' => 'AlterTest2',
'connection' => 'test',
'altertest' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
'group1' => array('type' => 'integer', 'null' => true),
'group2' => array('type' => 'integer', 'null' => true),
'indexes' => array(
'name_idx' => array('column' => 'name', 'unique' => 0),
'group_idx' => array('column' => 'group1', 'unique' => 0),
'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0),
'PRIMARY' => array('column' => 'id', 'unique' => 1))
)));
2010-06-10 01:33:46 +00:00
$this->Dbo->query($this->Dbo->alterSchema($schema2->compare($schema1)));
2010-06-10 01:33:46 +00:00
$indexes = $this->Dbo->index('altertest');
$this->assertEqual($schema2->tables['altertest']['indexes'], $indexes);
// Change three indexes, delete one and add another one
2010-06-10 01:33:46 +00:00
$schema3 = new CakeSchema(array(
'name' => 'AlterTest3',
'connection' => 'test',
'altertest' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
'group1' => array('type' => 'integer', 'null' => true),
'group2' => array('type' => 'integer', 'null' => true),
'indexes' => array(
'name_idx' => array('column' => 'name', 'unique' => 1),
'group_idx' => array('column' => 'group2', 'unique' => 0),
'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0),
'id_name_idx' => array('column' => array('id', 'name'), 'unique' => 0))
)));
2010-06-10 01:33:46 +00:00
$this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2)));
2010-06-10 01:33:46 +00:00
$indexes = $this->Dbo->index('altertest');
$this->assertEqual($schema3->tables['altertest']['indexes'], $indexes);
// Compare us to ourself.
$this->assertEqual($schema3->compare($schema3), array());
// Drop the indexes
2010-06-10 01:33:46 +00:00
$this->Dbo->query($this->Dbo->alterSchema($schema1->compare($schema3)));
2010-06-10 01:33:46 +00:00
$indexes = $this->Dbo->index('altertest');
$this->assertEqual(array(), $indexes);
2010-06-10 01:33:46 +00:00
$this->Dbo->query($this->Dbo->dropSchema($schema1));
}
/**
* test saving and retrieval of blobs
*
* @return void
*/
function testBlobSaving() {
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = false;
$data = "GIF87ab
Ò4A¿¿¿ˇˇˇ,b
¢îè©ÀÌ#¥⁄ã≥fi:¯Üá¶jV∂ÓúÎL≥çÀóËıÎ…>ï vFE%ÒâLFI<†µw˝±≈£7˘ç^H“≤« >Éâ*∑ÇnÖA•Ù|flêèj£:=ÿ6óUàµ5'∂®àA¬ñ∆ˆGE(gt≈àÚyÁó«7 VìöÇ√˙Ç™
k”:;kÀAõ{*¡€Î˚˚[;;";
$model = new AppModel(array('name' => 'BinaryTest', 'ds' => 'test'));
$model->save(compact('data'));
$result = $model->find('first');
$this->assertEqual($result['BinaryTest']['data'], $data);
}
/**
* test altering the table settings with schema.
*
* @return void
*/
function testAlteringTableParameters() {
App::import('Model', 'CakeSchema');
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = $this->Dbo->testing = false;
2010-06-10 01:33:46 +00:00
$schema1 = new CakeSchema(array(
'name' => 'AlterTest1',
'connection' => 'test',
'altertest' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
'tableParameters' => array(
'charset' => 'latin1',
'collate' => 'latin1_general_ci',
'engine' => 'MyISAM'
)
)
));
2010-06-10 01:33:46 +00:00
$this->Dbo->query($this->Dbo->createSchema($schema1));
$schema2 = new CakeSchema(array(
'name' => 'AlterTest1',
'connection' => 'test',
'altertest' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
'tableParameters' => array(
'charset' => 'utf8',
'collate' => 'utf8_general_ci',
'engine' => 'InnoDB'
)
)
));
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->alterSchema($schema2->compare($schema1));
$this->assertPattern('/DEFAULT CHARSET=utf8/', $result);
$this->assertPattern('/ENGINE=InnoDB/', $result);
$this->assertPattern('/COLLATE=utf8_general_ci/', $result);
2010-06-10 01:33:46 +00:00
$this->Dbo->query($result);
$result = $this->Dbo->listDetailedSources('altertest');
$this->assertEqual($result['Collation'], 'utf8_general_ci');
$this->assertEqual($result['Engine'], 'InnoDB');
$this->assertEqual($result['charset'], 'utf8');
2010-06-10 01:33:46 +00:00
$this->Dbo->query($this->Dbo->dropSchema($schema1));
}
/**
* test alterSchema on two tables.
*
* @return void
*/
function testAlteringTwoTables() {
$schema1 = new CakeSchema(array(
'name' => 'AlterTest1',
'connection' => 'test',
'altertest' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
),
'other_table' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'name' => array('type' => 'string', 'null' => false, 'length' => 50),
)
));
$schema2 = new CakeSchema(array(
'name' => 'AlterTest1',
'connection' => 'test',
'altertest' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
),
'other_table' => array(
'id' => array('type' => 'integer', 'null' => false, 'default' => 0),
'field_two' => array('type' => 'string', 'null' => false, 'length' => 50),
)
));
$result = $this->db->alterSchema($schema2->compare($schema1));
$this->assertEqual(2, substr_count($result, 'field_two'), 'Too many fields');
}
/**
* testReadTableParameters method
*
* @access public
* @return void
*/
function testReadTableParameters() {
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = $this->Dbo->testing = false;
$tableName = 'tinyint_' . uniqid();
$this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (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($tableName);
$expected = array(
'charset' => 'utf8',
'collate' => 'utf8_unicode_ci',
'engine' => 'InnoDB');
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
$this->Dbo->query('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (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->Dbo->readTableParameters($tableName);
$expected = array(
'charset' => 'cp1250',
'collate' => 'cp1250_general_ci',
'engine' => 'MyISAM');
$this->assertEqual($result, $expected);
2010-06-10 01:33:46 +00:00
$this->Dbo->query('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
}
/**
* testBuildTableParameters method
*
* @access public
* @return void
*/
function testBuildTableParameters() {
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = $this->Dbo->testing = false;
$data = array(
'charset' => 'utf8',
'collate' => 'utf8_unicode_ci',
'engine' => 'InnoDB');
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->buildTableParameters($data);
$expected = array(
'DEFAULT CHARSET=utf8',
'COLLATE=utf8_unicode_ci',
'ENGINE=InnoDB');
$this->assertEqual($result, $expected);
}
/**
* testBuildTableParameters method
*
* @access public
* @return void
*/
function testGetCharsetName() {
2010-06-10 01:33:46 +00:00
$this->Dbo->cacheSources = $this->Dbo->testing = false;
$result = $this->Dbo->getCharsetName('utf8_unicode_ci');
$this->assertEqual($result, 'utf8');
2010-06-10 01:33:46 +00:00
$result = $this->Dbo->getCharsetName('cp1250_general_ci');
$this->assertEqual($result, 'cp1250');
}
/**
* test that changing the virtualFieldSeparator allows for __ fields.
*
* @return void
*/
function testVirtualFieldSeparators() {
$model = new CakeTestModel(array('table' => 'binary_tests', 'ds' => 'test', 'name' => 'BinaryTest'));
$model->virtualFields = array(
'other__field' => 'SUM(id)'
);
$this->db->virtualFieldSeparator = '_$_';
$result = $this->db->fields($model, null, array('data', 'other__field'));
$expected = array('`BinaryTest`.`data`', '(SUM(id)) AS BinaryTest_$_other__field');
$this->assertEqual($result, $expected);
}
/**
* test that a describe() gets additional fieldParameters
*
* @return void
*/
function testDescribeGettingFieldParameters() {
$schema = new CakeSchema(array(
'connection' => 'test',
'testdescribes' => array(
'id' => array('type' => 'integer', 'key' => 'primary'),
'stringy' => array(
'type' => 'string',
'null' => true,
'charset' => 'cp1250',
'collate' => 'cp1250_general_ci',
),
'other_col' => array(
'type' => 'string',
'null' => false,
'charset' => 'latin1',
'comment' => 'Test Comment'
)
)
));
$this->db->execute($this->db->createSchema($schema));
$model = new CakeTestModel(array('table' => 'testdescribes', 'name' => 'Testdescribes'));
$result = $this->db->describe($model);
$this->assertEqual($result['stringy']['collate'], 'cp1250_general_ci');
$this->assertEqual($result['stringy']['charset'], 'cp1250');
$this->assertEqual($result['other_col']['comment'], 'Test Comment');
$this->db->execute($this->db->dropSchema($schema));
}
}