~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * DboMysqliTest file
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
8
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://cakephp.org CakePHP(tm) Project
 
15
 * @package       cake
 
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)
 
19
 */
 
20
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
 
21
        define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
 
22
}
 
23
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysqli'));
 
24
 
 
25
/**
 
26
 * DboMysqliTestDb class
 
27
 *
 
28
 * @package       cake
 
29
 * @subpackage    cake.tests.cases.libs.model.datasources
 
30
 */
 
31
class DboMysqliTestDb extends DboMysqli {
 
32
 
 
33
/**
 
34
 * simulated property
 
35
 *
 
36
 * @var array
 
37
 * @access public
 
38
 */
 
39
        var $simulated = array();
 
40
 
 
41
/**
 
42
 * testing property
 
43
 *
 
44
 * @var bool true
 
45
 * @access public
 
46
 */
 
47
        var $testing = true;
 
48
 
 
49
/**
 
50
 * execute method
 
51
 *
 
52
 * @param mixed $sql
 
53
 * @access protected
 
54
 * @return void
 
55
 */
 
56
        function _execute($sql) {
 
57
                if ($this->testing) {
 
58
                        $this->simulated[] = $sql;
 
59
                        return null;
 
60
                }
 
61
                return parent::_execute($sql);
 
62
        }
 
63
 
 
64
/**
 
65
 * getLastQuery method
 
66
 *
 
67
 * @access public
 
68
 * @return void
 
69
 */
 
70
        function getLastQuery() {
 
71
                return $this->simulated[count($this->simulated) - 1];
 
72
        }
 
73
}
 
74
 
 
75
/**
 
76
 * MysqliTestModel class
 
77
 *
 
78
 * @package       cake
 
79
 * @subpackage    cake.tests.cases.libs.model.datasources
 
80
 */
 
81
class MysqliTestModel extends Model {
 
82
 
 
83
/**
 
84
 * name property
 
85
 *
 
86
 * @var string 'MysqlTestModel'
 
87
 * @access public
 
88
 */
 
89
        var $name = 'MysqliTestModel';
 
90
 
 
91
/**
 
92
 * useTable property
 
93
 *
 
94
 * @var bool false
 
95
 * @access public
 
96
 */
 
97
        var $useTable = false;
 
98
 
 
99
/**
 
100
 * schema method
 
101
 *
 
102
 * @access public
 
103
 * @return void
 
104
 */
 
105
        function schema() {
 
106
                return array(
 
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)
 
125
                );
 
126
        }
 
127
}
 
128
 
 
129
/**
 
130
 * DboMysqliTest class
 
131
 *
 
132
 * @package       cake
 
133
 * @subpackage    cake.tests.cases.libs.model.datasources.dbo
 
134
 */
 
135
class DboMysqliTest extends CakeTestCase {
 
136
        var $fixtures = array('core.datatype');
 
137
/**
 
138
 * The Dbo instance to be tested
 
139
 *
 
140
 * @var DboSource
 
141
 * @access public
 
142
 */
 
143
        var $Db = null;
 
144
 
 
145
/**
 
146
 * Skip if cannot connect to mysqli
 
147
 *
 
148
 * @access public
 
149
 */
 
150
        function skip() {
 
151
                $this->_initDb();
 
152
                $this->skipUnless($this->db->config['driver'] == 'mysqli', '%s MySQLi connection not available');
 
153
        }
 
154
 
 
155
/**
 
156
 * Sets up a Dbo class instance for testing
 
157
 *
 
158
 * @access public
 
159
 */
 
160
        function setUp() {
 
161
                $this->model = new MysqliTestModel();
 
162
        }
 
163
 
 
164
/**
 
165
 * Sets up a Dbo class instance for testing
 
166
 *
 
167
 * @access public
 
168
 */
 
169
        function tearDown() {
 
170
                unset($this->model);
 
171
                ClassRegistry::flush();
 
172
        }
 
173
 
 
174
/**
 
175
 * testIndexDetection method
 
176
 *
 
177
 * @return void
 
178
 * @access public
 
179
 */
 
180
        function testIndexDetection() {
 
181
                $this->db->cacheSources = false;
 
182
 
 
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);
 
189
 
 
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` ));');
 
192
                $expected = array(
 
193
                        'PRIMARY' => array('column' => 'id', 'unique' => 1),
 
194
                        'pointless_bool' => array('column' => 'bool', 'unique' => 0),
 
195
                );
 
196
                $result = $this->db->index($name, false);
 
197
                $this->assertEqual($expected, $result);
 
198
                $this->db->query('DROP TABLE ' . $name);
 
199
 
 
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` ));');
 
202
                $expected = array(
 
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),
 
206
                );
 
207
                $result = $this->db->index($name, false);
 
208
                $this->assertEqual($expected, $result);
 
209
                $this->db->query('DROP TABLE ' . $name);
 
210
 
 
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` ));');
 
213
                $expected = array(
 
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),
 
218
                );
 
219
                $result = $this->db->index($name, false);
 
220
                $this->assertEqual($expected, $result);
 
221
                $this->db->query('DROP TABLE ' . $name);
 
222
 
 
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` ));');
 
225
                $expected = array(
 
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),
 
231
                );
 
232
                $result = $this->db->index($name, false);
 
233
                $this->assertEqual($expected, $result);
 
234
                $this->db->query('DROP TABLE ' . $name);
 
235
        }
 
236
 
 
237
/**
 
238
 * testColumn method
 
239
 *
 
240
 * @return void
 
241
 * @access public
 
242
 */
 
243
        function testColumn() {
 
244
                $result = $this->db->column('varchar(50)');
 
245
                $expected = 'string';
 
246
                $this->assertEqual($result, $expected);
 
247
 
 
248
                $result = $this->db->column('text');
 
249
                $expected = 'text';
 
250
                $this->assertEqual($result, $expected);
 
251
 
 
252
                $result = $this->db->column('int(11)');
 
253
                $expected = 'integer';
 
254
                $this->assertEqual($result, $expected);
 
255
 
 
256
                $result = $this->db->column('int(11) unsigned');
 
257
                $expected = 'integer';
 
258
                $this->assertEqual($result, $expected);
 
259
 
 
260
                $result = $this->db->column('tinyint(1)');
 
261
                $expected = 'boolean';
 
262
                $this->assertEqual($result, $expected);
 
263
 
 
264
                $result = $this->db->column('boolean');
 
265
                $expected = 'boolean';
 
266
                $this->assertEqual($result, $expected);
 
267
 
 
268
                $result = $this->db->column('float');
 
269
                $expected = 'float';
 
270
                $this->assertEqual($result, $expected);
 
271
 
 
272
                $result = $this->db->column('float unsigned');
 
273
                $expected = 'float';
 
274
                $this->assertEqual($result, $expected);
 
275
 
 
276
                $result = $this->db->column('double unsigned');
 
277
                $expected = 'float';
 
278
                $this->assertEqual($result, $expected);
 
279
 
 
280
                $result = $this->db->column('decimal(14,7) unsigned');
 
281
                $expected = 'float';
 
282
                $this->assertEqual($result, $expected);
 
283
        }
 
284
 
 
285
/**
 
286
 * test transaction commands.
 
287
 *
 
288
 * @return void
 
289
 * @access public
 
290
 */
 
291
        function testTransactions() {
 
292
                $this->db->testing = false;
 
293
                $result = $this->db->begin($this->model);
 
294
                $this->assertTrue($result);
 
295
 
 
296
                $beginSqlCalls = Set::extract('/.[query=START TRANSACTION]', $this->db->_queriesLog);
 
297
                $this->assertEqual(1, count($beginSqlCalls));
 
298
 
 
299
                $result = $this->db->commit($this->model);
 
300
                $this->assertTrue($result);
 
301
        }
 
302
/**
 
303
 * test that float values are correctly identified
 
304
 *
 
305
 * @return void
 
306
 */
 
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');
 
311
        }
 
312
 
 
313
/**
 
314
 * test that tableParameters like collation, charset and engine are functioning.
 
315
 *
 
316
 * @access public
 
317
 * @return void
 
318
 */
 
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');
 
323
                $expected = array(
 
324
                        'charset' => 'utf8',
 
325
                        'collate' => 'utf8_unicode_ci',
 
326
                        'engine' => 'InnoDB');
 
327
                $this->assertEqual($result, $expected);
 
328
 
 
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');
 
332
                $expected = array(
 
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'));
 
338
        }
 
339
}