2008-05-30 11:40:08 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
|
* DboMysqlTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
|
|
|
|
* Licensed under The MIT License
|
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
|
* @package Cake.Test.Case.Model.Datasource.Database
|
2008-10-30 17:30:26 +00:00
|
|
|
|
* @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)
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-12-09 05:55:24 +00:00
|
|
|
|
App::uses('Model', 'Model');
|
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
App::uses('Mysql', 'Model/Datasource/Database');
|
|
|
|
|
App::uses('CakeSchema', 'Model');
|
|
|
|
|
|
2010-11-03 23:44:22 +00:00
|
|
|
|
require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
|
2008-07-27 03:36:30 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2010-11-03 23:44:22 +00:00
|
|
|
|
* DboMysqlTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
|
* @package Cake.Test.Case.Model.Datasource.Database
|
2008-06-02 19:22:55 +00:00
|
|
|
|
*/
|
2010-11-03 23:44:22 +00:00
|
|
|
|
class DboMysqlTest extends CakeTestCase {
|
2008-06-02 19:22:55 +00:00
|
|
|
|
/**
|
2010-11-03 23:44:22 +00:00
|
|
|
|
* autoFixtures property
|
2008-11-05 13:44:05 +00:00
|
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
|
* @var bool false
|
|
|
|
|
*/
|
2010-11-03 23:44:22 +00:00
|
|
|
|
public $autoFixtures = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
|
/**
|
2010-11-03 23:44:22 +00:00
|
|
|
|
* fixtures property
|
2008-11-05 13:44:05 +00:00
|
|
|
|
*
|
2010-11-03 23:44:22 +00:00
|
|
|
|
* @var array
|
2008-06-02 19:22:55 +00:00
|
|
|
|
*/
|
2010-11-03 23:44:22 +00:00
|
|
|
|
public $fixtures = array(
|
|
|
|
|
'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
|
|
|
|
|
'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test',
|
|
|
|
|
'core.binary_test'
|
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* The Dbo instance to be tested
|
|
|
|
|
*
|
2009-03-17 21:10:28 +00:00
|
|
|
|
* @var DboSource
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-06-10 01:33:46 +00:00
|
|
|
|
public $Dbo = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets up a Dbo class instance for testing
|
|
|
|
|
*
|
|
|
|
|
*/
|
2010-06-10 01:49:25 +00:00
|
|
|
|
public function setUp() {
|
2010-09-20 02:58:30 +00:00
|
|
|
|
$this->Dbo = ConnectionManager::getDataSource('test');
|
2011-01-06 03:44:33 +00:00
|
|
|
|
if (!($this->Dbo instanceof Mysql)) {
|
2010-06-10 01:49:25 +00:00
|
|
|
|
$this->markTestSkipped('The MySQL extension is not available.');
|
|
|
|
|
}
|
|
|
|
|
$this->_debug = Configure::read('debug');
|
|
|
|
|
Configure::write('debug', 1);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$this->model = new MysqlTestModel();
|
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets up a Dbo class instance for testing
|
|
|
|
|
*
|
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
|
public function tearDown() {
|
2009-10-24 16:53:22 +00:00
|
|
|
|
unset($this->model);
|
|
|
|
|
ClassRegistry::flush();
|
2008-12-09 04:54:58 +00:00
|
|
|
|
Configure::write('debug', $this->_debug);
|
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Test Dbo value method
|
|
|
|
|
*
|
2010-10-15 21:32:37 +00:00
|
|
|
|
* @group quoting
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
|
public function testQuoting() {
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->fields($this->model);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$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`'
|
|
|
|
|
);
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
|
|
$expected = 1.2;
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->value(1.2, 'float');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
|
|
|
|
$expected = "'1,2'";
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->value('1,2', 'float');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
|
|
|
|
$expected = "'4713e29446'";
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->value('4713e29446');
|
2008-07-27 02:49:02 +00:00
|
|
|
|
|
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
|
|
|
|
$expected = 'NULL';
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->value('', 'integer');
|
2008-07-27 02:49:02 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-05 13:44:05 +00:00
|
|
|
|
|
2010-11-03 23:44:22 +00:00
|
|
|
|
$expected = "'0'";
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->value('', 'boolean');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
|
|
|
|
$expected = 10010001;
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->value(10010001);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
|
|
|
|
$expected = "'00010010001'";
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->value('00010010001');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2010-08-21 04:19:30 +00:00
|
|
|
|
/**
|
|
|
|
|
* test that localized floats don't cause trouble.
|
|
|
|
|
*
|
2010-10-15 21:32:37 +00:00
|
|
|
|
* @group quoting
|
2010-08-21 04:19:30 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
|
public function testLocalizedFloats() {
|
2011-04-25 03:31:44 +00:00
|
|
|
|
$this->skipIf(DS === '\\', 'The locale is not supported in Windows and affect the others tests.');
|
|
|
|
|
|
2010-08-21 04:19:30 +00:00
|
|
|
|
$restore = setlocale(LC_ALL, null);
|
|
|
|
|
setlocale(LC_ALL, 'de_DE');
|
|
|
|
|
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$result = $this->Dbo->value(3.141593, 'float');
|
2010-08-21 04:19:30 +00:00
|
|
|
|
$this->assertEqual((string)$result, '3.141593');
|
2010-10-15 23:11:17 +00:00
|
|
|
|
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$result = $this->Dbo->value(3.141593);
|
2010-08-21 04:19:30 +00:00
|
|
|
|
$this->assertEqual((string)$result, '3.141593');
|
|
|
|
|
|
|
|
|
|
setlocale(LC_ALL, $restore);
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
|
/**
|
|
|
|
|
* testTinyintCasting method
|
2008-11-05 13:44:05 +00:00
|
|
|
|
*
|
2010-10-15 21:32:37 +00:00
|
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
|
public function testTinyintCasting() {
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$this->Dbo->cacheSources = false;
|
|
|
|
|
$tableName = 'tinyint_' . uniqid();
|
2010-10-16 18:31:01 +00:00
|
|
|
|
$this->Dbo->rawQuery('CREATE TABLE ' . $this->Dbo->fullTableName($tableName) . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
|
|
$this->model = new CakeTestModel(array(
|
2010-09-20 02:58:30 +00:00
|
|
|
|
'name' => 'Tinyint', 'table' => $tableName, 'ds' => 'test'
|
2008-05-30 11:40:08 +00:00
|
|
|
|
));
|
2008-08-27 08:01:00 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$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)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$result = $this->model->find('first');
|
2011-01-04 04:06:48 +00:00
|
|
|
|
$this->assertIdentical($result['Tinyint']['bool'], true);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$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)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$result = $this->model->find('first');
|
2011-01-04 04:06:48 +00:00
|
|
|
|
$this->assertIdentical($result['Tinyint']['bool'], false);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$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)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$result = $this->model->find('first');
|
2011-01-04 04:06:48 +00:00
|
|
|
|
$this->assertIdentical($result['Tinyint']['bool'], true);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
$this->assertIdentical($result['Tinyint']['small_int'], '0');
|
|
|
|
|
$this->model->deleteAll(true);
|
|
|
|
|
|
2010-10-16 18:31:01 +00:00
|
|
|
|
$this->Dbo->rawQuery('DROP TABLE ' . $this->Dbo->fullTableName($tableName));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-11-05 13:44:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* testIndexDetection method
|
|
|
|
|
*
|
2010-10-15 21:57:36 +00:00
|
|
|
|
* @group indices
|
2008-11-05 13:44:05 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
|
public function testIndexDetection() {
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$this->Dbo->cacheSources = false;
|
2008-11-05 13:44:05 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$name = $this->Dbo->fullTableName('simple');
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1));
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->index('simple', false);
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('DROP TABLE ' . $name);
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2010-10-15 23:11:17 +00:00
|
|
|
|
|
2008-11-05 13:44:05 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$name = $this->Dbo->fullTableName('with_a_key');
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));');
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$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);
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('DROP TABLE ' . $name);
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$name = $this->Dbo->fullTableName('with_two_keys');
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('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` ));');
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$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);
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('DROP TABLE ' . $name);
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$name = $this->Dbo->fullTableName('with_compound_keys');
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('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` ));');
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$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);
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('DROP TABLE ' . $name);
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$name = $this->Dbo->fullTableName('with_multiple_compound_keys');
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('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` ));');
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$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);
|
2010-10-15 21:32:37 +00:00
|
|
|
|
$this->Dbo->rawQuery('DROP TABLE ' . $name);
|
2008-11-05 13:44:05 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2009-09-29 15:25:42 +00:00
|
|
|
|
/**
|
|
|
|
|
* testBuildColumn method
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
|
public function testBuildColumn() {
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$restore = $this->Dbo->columns;
|
|
|
|
|
$this->Dbo->columns = array('varchar(255)' => 1);
|
2009-09-29 15:25:42 +00:00
|
|
|
|
$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);
|
2009-09-29 15:25:42 +00:00
|
|
|
|
$expected = '`testName` DEFAULT NULL COMMENT \'test\'';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2009-09-29 15:25:42 +00:00
|
|
|
|
|
|
|
|
|
$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);
|
2009-09-29 15:25:42 +00:00
|
|
|
|
$expected = '`testName` CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$this->Dbo->columns = $restore;
|
2009-09-29 15:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-02-08 23:09:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* MySQL 4.x returns index data in a different format,
|
|
|
|
|
* Using a mock ensure that MySQL 4.x output is properly parsed.
|
|
|
|
|
*
|
2010-10-15 21:57:36 +00:00
|
|
|
|
* @group indices
|
2009-02-08 23:09:08 +00:00
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
|
public function testIndexOnMySQL4Output() {
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$name = $this->Dbo->fullTableName('simple');
|
2009-02-08 23:09:08 +00:00
|
|
|
|
|
2011-01-06 03:44:33 +00:00
|
|
|
|
$mockDbo = $this->getMock('Mysql', array('connect', '_execute', 'getVersion'));
|
2009-03-17 21:10:28 +00:00
|
|
|
|
$columnData = array(
|
2009-02-08 23:09:08 +00:00
|
|
|
|
array('0' => array(
|
2009-03-17 21:10:28 +00:00
|
|
|
|
'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',
|
2009-02-08 23:09:08 +00:00
|
|
|
|
'Comment' => ''
|
2009-03-17 21:10:28 +00:00
|
|
|
|
)),
|
2009-02-08 23:09:08 +00:00
|
|
|
|
array('0' => array(
|
2009-03-17 21:10:28 +00:00
|
|
|
|
'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',
|
2009-02-08 23:09:08 +00:00
|
|
|
|
'Comment' => ''
|
|
|
|
|
)),
|
|
|
|
|
array('0' => array(
|
2009-03-17 21:10:28 +00:00
|
|
|
|
'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',
|
2009-02-08 23:09:08 +00:00
|
|
|
|
'Comment' => ''
|
2009-03-17 21:10:28 +00:00
|
|
|
|
)),
|
2009-02-08 23:09:08 +00:00
|
|
|
|
array('0' => array(
|
2009-03-17 21:10:28 +00:00
|
|
|
|
'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',
|
2009-02-08 23:09:08 +00:00
|
|
|
|
'Comment' => ''
|
2009-03-17 21:10:28 +00:00
|
|
|
|
)),
|
2009-02-08 23:09:08 +00:00
|
|
|
|
array('0' => array(
|
2009-03-17 21:10:28 +00:00
|
|
|
|
'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' => ''
|
2009-02-08 23:09:08 +00:00
|
|
|
|
))
|
|
|
|
|
);
|
2010-10-15 21:57:36 +00:00
|
|
|
|
|
|
|
|
|
$mockDbo->expects($this->once())->method('getVersion')->will($this->returnValue('4.1'));
|
|
|
|
|
$resultMock = $this->getMock('PDOStatement', array('fetch'));
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$mockDbo->expects($this->once())
|
2010-10-15 21:57:36 +00:00
|
|
|
|
->method('_execute')
|
2010-06-10 01:33:46 +00:00
|
|
|
|
->with('SHOW INDEX FROM ' . $name)
|
2010-10-15 21:57:36 +00:00
|
|
|
|
->will($this->returnValue($resultMock));
|
2010-10-15 23:11:17 +00:00
|
|
|
|
|
2010-10-15 21:57:36 +00:00
|
|
|
|
foreach ($columnData as $i => $data) {
|
|
|
|
|
$resultMock->expects($this->at($i))->method('fetch')->will($this->returnValue((object) $data));
|
|
|
|
|
}
|
2009-02-08 23:09:08 +00:00
|
|
|
|
|
|
|
|
|
$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),
|
|
|
|
|
);
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2009-02-08 23:09:08 +00:00
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-11-06 02:15:12 +00:00
|
|
|
|
/**
|
|
|
|
|
* testColumn method
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
|
public function testColumn() {
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('varchar(50)');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'string';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('text');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'text';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('int(11)');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'integer';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('int(11) unsigned');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'integer';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('tinyint(1)');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'boolean';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('boolean');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'boolean';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('float');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'float';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('float unsigned');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'float';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('double unsigned');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'float';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$result = $this->Dbo->column('decimal(14,7) unsigned');
|
2008-11-06 02:15:12 +00:00
|
|
|
|
$expected = 'float';
|
2011-05-16 22:49:00 +00:00
|
|
|
|
$this->assertEqual($expected, $result);
|
2008-11-06 02:15:12 +00:00
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-12-09 04:54:58 +00:00
|
|
|
|
/**
|
|
|
|
|
* testAlterSchemaIndexes method
|
2009-03-17 21:10:28 +00:00
|
|
|
|
*
|
2010-10-15 21:57:36 +00:00
|
|
|
|
* @group indices
|
2008-12-09 04:54:58 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
|
public function testAlterSchemaIndexes() {
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$this->Dbo->cacheSources = $this->Dbo->testing = false;
|
2011-02-24 04:14:48 +00:00
|
|
|
|
$table = $this->Dbo->fullTableName('altertest');
|
2008-12-09 04:54:58 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$schema1 = new CakeSchema(array(
|
2008-12-09 04:54:58 +00:00
|
|
|
|
'name' => 'AlterTest1',
|
2010-09-20 02:58:30 +00:00
|
|
|
|
'connection' => 'test',
|
2008-12-09 04:54:58 +00:00
|
|
|
|
'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-10-15 23:11:17 +00:00
|
|
|
|
$result = $this->Dbo->createSchema($schema1);
|
|
|
|
|
$this->assertContains('`id` int(11) DEFAULT 0 NOT NULL,', $result);
|
|
|
|
|
$this->assertContains('`name` varchar(50) NOT NULL,', $result);
|
|
|
|
|
$this->assertContains('`group1` int(11) DEFAULT NULL', $result);
|
|
|
|
|
$this->assertContains('`group2` int(11) DEFAULT NULL', $result);
|
|
|
|
|
|
|
|
|
|
//Test that the string is syntactically correct
|
|
|
|
|
$query = $this->Dbo->getConnection()->prepare($result);
|
|
|
|
|
$this->assertEquals($result, $query->queryString);
|
2008-12-09 04:54:58 +00:00
|
|
|
|
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$schema2 = new CakeSchema(array(
|
2008-12-09 04:54:58 +00:00
|
|
|
|
'name' => 'AlterTest2',
|
2010-09-20 02:58:30 +00:00
|
|
|
|
'connection' => 'test',
|
2008-12-09 04:54:58 +00:00
|
|
|
|
'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))
|
|
|
|
|
)));
|
2009-03-17 21:10:28 +00:00
|
|
|
|
|
2010-10-15 23:11:17 +00:00
|
|
|
|
$result = $this->Dbo->alterSchema($schema2->compare($schema1));
|
2011-02-24 04:14:48 +00:00
|
|
|
|
$this->assertContains("ALTER TABLE $table", $result);
|
2010-10-15 23:11:17 +00:00
|
|
|
|
$this->assertContains('ADD KEY name_idx (`name`),', $result);
|
|
|
|
|
$this->assertContains('ADD KEY group_idx (`group1`),', $result);
|
|
|
|
|
$this->assertContains('ADD KEY compound_idx (`group1`, `group2`),', $result);
|
|
|
|
|
$this->assertContains('ADD PRIMARY KEY (`id`);', $result);
|
|
|
|
|
|
|
|
|
|
//Test that the string is syntactically correct
|
|
|
|
|
$query = $this->Dbo->getConnection()->prepare($result);
|
|
|
|
|
$this->assertEquals($result, $query->queryString);
|
2009-03-17 21:10:28 +00:00
|
|
|
|
|
2008-12-09 04:54:58 +00:00
|
|
|
|
// Change three indexes, delete one and add another one
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$schema3 = new CakeSchema(array(
|
2008-12-09 04:54:58 +00:00
|
|
|
|
'name' => 'AlterTest3',
|
2010-09-20 02:58:30 +00:00
|
|
|
|
'connection' => 'test',
|
2008-12-09 04:54:58 +00:00
|
|
|
|
'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(
|
2009-03-17 21:10:28 +00:00
|
|
|
|
'name_idx' => array('column' => 'name', 'unique' => 1),
|
2008-12-09 04:54:58 +00:00
|
|
|
|
'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-10-15 23:11:17 +00:00
|
|
|
|
$result = $this->Dbo->alterSchema($schema3->compare($schema2));
|
2011-02-24 04:14:48 +00:00
|
|
|
|
$this->assertContains("ALTER TABLE $table", $result);
|
2010-10-15 23:11:17 +00:00
|
|
|
|
$this->assertContains('DROP PRIMARY KEY,', $result);
|
|
|
|
|
$this->assertContains('DROP KEY name_idx,', $result);
|
|
|
|
|
$this->assertContains('DROP KEY group_idx,', $result);
|
|
|
|
|
$this->assertContains('DROP KEY compound_idx,', $result);
|
|
|
|
|
$this->assertContains('ADD KEY id_name_idx (`id`, `name`),', $result);
|
|
|
|
|
$this->assertContains('ADD UNIQUE KEY name_idx (`name`),', $result);
|
|
|
|
|
$this->assertContains('ADD KEY group_idx (`group2`),', $result);
|
|
|
|
|
$this->assertContains('ADD KEY compound_idx (`group2`, `group1`);', $result);
|
2008-12-09 04:54:58 +00:00
|
|
|
|
|
2010-10-15 23:11:17 +00:00
|
|
|
|
$query = $this->Dbo->getConnection()->prepare($result);
|
|
|
|
|
$this->assertEquals($result, $query->queryString);
|
2008-12-09 04:54:58 +00:00
|
|
|
|
|
|
|
|
|
// Compare us to ourself.
|
|
|
|
|
$this->assertEqual($schema3->compare($schema3), array());
|
|
|
|
|
|
|
|
|
|
// Drop the indexes
|
2010-10-15 23:11:17 +00:00
|
|
|
|
$result = $this->Dbo->alterSchema($schema1->compare($schema3));
|
2008-12-09 04:54:58 +00:00
|
|
|
|
|
2011-02-24 04:14:48 +00:00
|
|
|
|
$this->assertContains("ALTER TABLE $table", $result);
|
2010-10-15 23:11:17 +00:00
|
|
|
|
$this->assertContains('DROP KEY name_idx,', $result);
|
|
|
|
|
$this->assertContains('DROP KEY group_idx,', $result);
|
|
|
|
|
$this->assertContains('DROP KEY compound_idx,', $result);
|
|
|
|
|
$this->assertContains('DROP KEY id_name_idx;', $result);
|
2008-12-09 04:54:58 +00:00
|
|
|
|
|
2010-10-15 23:11:17 +00:00
|
|
|
|
$query = $this->Dbo->getConnection()->prepare($result);
|
|
|
|
|
$this->assertEquals($result, $query->queryString);
|
2009-10-05 01:48:20 +00:00
|
|
|
|
}
|
2010-10-15 23:11:17 +00:00
|
|
|
|
|
2009-10-24 16:53:22 +00:00
|
|
|
|
/**
|
|
|
|
|
* test saving and retrieval of blobs
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
|
public function testBlobSaving() {
|
2010-10-16 14:53:13 +00:00
|
|
|
|
$this->loadFixtures('BinaryTest');
|
2010-06-10 01:33:46 +00:00
|
|
|
|
$this->Dbo->cacheSources = false;
|
2009-10-24 16:53:22 +00:00
|
|
|
|
$data = "GIF87ab |