7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
16
* @subpackage cake.cake.libs
17
* @since CakePHP(tm) v 1.2.0
18
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
21
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
23
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysqli'));
26
* DboMysqliTestDb class
29
* @subpackage cake.tests.cases.libs.model.datasources
31
class DboMysqliTestDb extends DboMysqli {
39
var $simulated = array();
56
function _execute($sql) {
58
$this->simulated[] = $sql;
61
return parent::_execute($sql);
70
function getLastQuery() {
71
return $this->simulated[count($this->simulated) - 1];
76
* MysqliTestModel class
79
* @subpackage cake.tests.cases.libs.model.datasources
81
class MysqliTestModel extends Model {
86
* @var string 'MysqlTestModel'
89
var $name = 'MysqliTestModel';
97
var $useTable = false;
107
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
108
'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
109
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
110
'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
111
'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
112
'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
113
'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
114
'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
115
'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
116
'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
117
'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
118
'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
119
'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
120
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
121
'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
122
'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
123
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
124
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
130
* DboMysqliTest class
133
* @subpackage cake.tests.cases.libs.model.datasources.dbo
135
class DboMysqliTest extends CakeTestCase {
136
var $fixtures = array('core.datatype');
138
* The Dbo instance to be tested
146
* Skip if cannot connect to mysqli
152
$this->skipUnless($this->db->config['driver'] == 'mysqli', '%s MySQLi connection not available');
156
* Sets up a Dbo class instance for testing
161
$this->model = new MysqliTestModel();
165
* Sets up a Dbo class instance for testing
169
function tearDown() {
171
ClassRegistry::flush();
175
* testIndexDetection method
180
function testIndexDetection() {
181
$this->db->cacheSources = false;
183
$name = $this->db->fullTableName('simple');
184
$this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
185
$expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1));
186
$result = $this->db->index($name, false);
187
$this->assertEqual($expected, $result);
188
$this->db->query('DROP TABLE ' . $name);
190
$name = $this->db->fullTableName('with_a_key');
191
$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` ));');
193
'PRIMARY' => array('column' => 'id', 'unique' => 1),
194
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
196
$result = $this->db->index($name, false);
197
$this->assertEqual($expected, $result);
198
$this->db->query('DROP TABLE ' . $name);
200
$name = $this->db->fullTableName('with_two_keys');
201
$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` ));');
203
'PRIMARY' => array('column' => 'id', 'unique' => 1),
204
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
205
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
207
$result = $this->db->index($name, false);
208
$this->assertEqual($expected, $result);
209
$this->db->query('DROP TABLE ' . $name);
211
$name = $this->db->fullTableName('with_compound_keys');
212
$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` ));');
214
'PRIMARY' => array('column' => 'id', 'unique' => 1),
215
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
216
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
217
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
219
$result = $this->db->index($name, false);
220
$this->assertEqual($expected, $result);
221
$this->db->query('DROP TABLE ' . $name);
223
$name = $this->db->fullTableName('with_multiple_compound_keys');
224
$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` ));');
226
'PRIMARY' => array('column' => 'id', 'unique' => 1),
227
'pointless_bool' => array('column' => 'bool', 'unique' => 0),
228
'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
229
'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
230
'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
232
$result = $this->db->index($name, false);
233
$this->assertEqual($expected, $result);
234
$this->db->query('DROP TABLE ' . $name);
243
function testColumn() {
244
$result = $this->db->column('varchar(50)');
245
$expected = 'string';
246
$this->assertEqual($result, $expected);
248
$result = $this->db->column('text');
250
$this->assertEqual($result, $expected);
252
$result = $this->db->column('int(11)');
253
$expected = 'integer';
254
$this->assertEqual($result, $expected);
256
$result = $this->db->column('int(11) unsigned');
257
$expected = 'integer';
258
$this->assertEqual($result, $expected);
260
$result = $this->db->column('tinyint(1)');
261
$expected = 'boolean';
262
$this->assertEqual($result, $expected);
264
$result = $this->db->column('boolean');
265
$expected = 'boolean';
266
$this->assertEqual($result, $expected);
268
$result = $this->db->column('float');
270
$this->assertEqual($result, $expected);
272
$result = $this->db->column('float unsigned');
274
$this->assertEqual($result, $expected);
276
$result = $this->db->column('double unsigned');
278
$this->assertEqual($result, $expected);
280
$result = $this->db->column('decimal(14,7) unsigned');
282
$this->assertEqual($result, $expected);
286
* test transaction commands.
291
function testTransactions() {
292
$this->db->testing = false;
293
$result = $this->db->begin($this->model);
294
$this->assertTrue($result);
296
$beginSqlCalls = Set::extract('/.[query=START TRANSACTION]', $this->db->_queriesLog);
297
$this->assertEqual(1, count($beginSqlCalls));
299
$result = $this->db->commit($this->model);
300
$this->assertTrue($result);
303
* test that float values are correctly identified
307
function testFloatParsing() {
308
$model =& new Model(array('ds' => 'test_suite', 'table' => 'datatypes', 'name' => 'Datatype'));
309
$result = $this->db->describe($model);
310
$this->assertEqual((string)$result['float_field']['length'], '5,2');
314
* test that tableParameters like collation, charset and engine are functioning.
319
function testReadTableParameters() {
320
$this->db->cacheSources = $this->db->testing = false;
321
$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;');
322
$result = $this->db->readTableParameters('tinyint');
325
'collate' => 'utf8_unicode_ci',
326
'engine' => 'InnoDB');
327
$this->assertEqual($result, $expected);
329
$this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint'));
330
$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;');
331
$result = $this->db->readTableParameters('tinyint');
333
'charset' => 'cp1250',
334
'collate' => 'cp1250_general_ci',
335
'engine' => 'MyISAM');
336
$this->assertEqual($result, $expected);
337
$this->db->query('DROP TABLE ' . $this->db->fullTableName('tinyint'));