Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * DboSourceTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Model.Datasource
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Model', 'Model');
20
App::uses('AppModel', 'Model');
21
App::uses('DataSource', 'Model/Datasource');
22
App::uses('DboSource', 'Model/Datasource');
23
App::uses('DboTestSource', 'Model/Datasource');
24
App::uses('DboSecondTestSource', 'Model/Datasource');
25
App::uses('MockDataSource', 'Model/Datasource');
26
 
27
require_once dirname(dirname(__FILE__)) . DS . 'models.php';
28
 
29
/**
30
 * Class MockPDO
31
 *
32
 * @package       Cake.Test.Case.Model.Datasource
33
 */
34
class MockPDO extends PDO {
35
 
36
	public function __construct() {
37
	}
38
 
39
}
40
 
41
/**
42
 * Class MockDataSource
43
 *
44
 * @package       Cake.Test.Case.Model.Datasource
45
 */
46
class MockDataSource extends DataSource {
47
}
48
 
49
/**
50
 * Class DboTestSource
51
 *
52
 * @package       Cake.Test.Case.Model.Datasource
53
 */
54
class DboTestSource extends DboSource {
55
 
56
	public $nestedSupport = false;
57
 
58
	public function connect($config = array()) {
59
		$this->connected = true;
60
	}
61
 
62
	public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
63
		return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
64
	}
65
 
66
	public function setConfig($config = array()) {
67
		$this->config = $config;
68
	}
69
 
70
	public function setConnection($conn) {
71
		$this->_connection = $conn;
72
	}
73
 
74
	public function nestedTransactionSupported() {
75
		return $this->useNestedTransactions && $this->nestedSupport;
76
	}
77
 
78
}
79
 
80
/**
81
 * Class DboSecondTestSource
82
 *
83
 * @package       Cake.Test.Case.Model.Datasource
84
 */
85
class DboSecondTestSource extends DboSource {
86
 
87
	public $startQuote = '_';
88
 
89
	public $endQuote = '_';
90
 
91
	public function connect($config = array()) {
92
		$this->connected = true;
93
	}
94
 
95
	public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
96
		return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
97
	}
98
 
99
	public function setConfig($config = array()) {
100
		$this->config = $config;
101
	}
102
 
103
	public function setConnection($conn) {
104
		$this->_connection = $conn;
105
	}
106
 
107
}
108
 
109
/**
110
 * DboSourceTest class
111
 *
112
 * @package       Cake.Test.Case.Model.Datasource
113
 */
114
class DboSourceTest extends CakeTestCase {
115
 
116
/**
117
 * autoFixtures property
118
 *
119
 * @var boolean
120
 */
121
	public $autoFixtures = false;
122
 
123
/**
124
 * fixtures property
125
 *
126
 * @var array
127
 */
128
	public $fixtures = array(
129
		'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
130
		'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test'
131
	);
132
 
133
/**
134
 * setUp method
135
 *
136
 * @return void
137
 */
138
	public function setUp() {
139
		parent::setUp();
140
		$this->__config = $this->db->config;
141
 
142
		$this->testDb = new DboTestSource();
143
		$this->testDb->cacheSources = false;
144
		$this->testDb->startQuote = '`';
145
		$this->testDb->endQuote = '`';
146
 
147
		$this->Model = new TestModel();
148
	}
149
 
150
/**
151
 * tearDown method
152
 *
153
 * @return void
154
 */
155
	public function tearDown() {
156
		parent::tearDown();
157
		unset($this->Model);
158
	}
159
 
160
/**
161
 * test that booleans and null make logical condition strings.
162
 *
163
 * @return void
164
 */
165
	public function testBooleanNullConditionsParsing() {
166
		$result = $this->testDb->conditions(true);
167
		$this->assertEquals(' WHERE 1 = 1', $result, 'true conditions failed %s');
168
 
169
		$result = $this->testDb->conditions(false);
170
		$this->assertEquals(' WHERE 0 = 1', $result, 'false conditions failed %s');
171
 
172
		$result = $this->testDb->conditions(null);
173
		$this->assertEquals(' WHERE 1 = 1', $result, 'null conditions failed %s');
174
 
175
		$result = $this->testDb->conditions(array());
176
		$this->assertEquals(' WHERE 1 = 1', $result, 'array() conditions failed %s');
177
 
178
		$result = $this->testDb->conditions('');
179
		$this->assertEquals(' WHERE 1 = 1', $result, '"" conditions failed %s');
180
 
181
		$result = $this->testDb->conditions(' ', '"  " conditions failed %s');
182
		$this->assertEquals(' WHERE 1 = 1', $result);
183
	}
184
 
185
/**
186
 * test that booleans work on empty set.
187
 *
188
 * @return void
189
 */
190
	public function testBooleanEmptyConditionsParsing() {
191
		$result = $this->testDb->conditions(array('OR' => array()));
192
		$this->assertEquals(' WHERE  1 = 1', $result, 'empty conditions failed');
193
 
194
		$result = $this->testDb->conditions(array('OR' => array('OR' => array())));
195
		$this->assertEquals(' WHERE  1 = 1', $result, 'nested empty conditions failed');
196
	}
197
 
198
/**
199
 * test that order() will accept objects made from DboSource::expression
200
 *
201
 * @return void
202
 */
203
	public function testOrderWithExpression() {
204
		$expression = $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col");
205
		$result = $this->testDb->order($expression);
206
		$expected = " ORDER BY CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col";
207
		$this->assertEquals($expected, $result);
208
	}
209
 
210
/**
211
 * testMergeAssociations method
212
 *
213
 * @return void
214
 */
215
	public function testMergeAssociations() {
216
		$data = array('Article2' => array(
217
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
218
				'body' => 'First Article Body', 'published' => 'Y',
219
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
220
		));
221
		$merge = array('Topic' => array(array(
222
			'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
223
			'updated' => '2007-03-17 01:18:31'
224
		)));
225
		$expected = array(
226
			'Article2' => array(
227
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
228
				'body' => 'First Article Body', 'published' => 'Y',
229
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
230
			),
231
			'Topic' => array(
232
				'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
233
				'updated' => '2007-03-17 01:18:31'
234
			)
235
		);
236
		$this->testDb->mergeAssociation($data, $merge, 'Topic', 'hasOne');
237
		$this->assertEquals($expected, $data);
238
 
239
		$data = array('Article2' => array(
240
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
241
				'body' => 'First Article Body', 'published' => 'Y',
242
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
243
		));
244
		$merge = array('User2' => array(array(
245
			'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
246
			'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
247
		)));
248
 
249
		$expected = array(
250
			'Article2' => array(
251
				'id' => '1', 'user_id' => '1', 'title' => 'First Article',
252
				'body' => 'First Article Body', 'published' => 'Y',
253
				'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
254
			),
255
			'User2' => array(
256
				'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
257
			)
258
		);
259
		$this->testDb->mergeAssociation($data, $merge, 'User2', 'belongsTo');
260
		$this->assertEquals($expected, $data);
261
 
262
		$data = array(
263
			'Article2' => array(
264
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
265
			)
266
		);
267
		$merge = array(array('Comment' => false));
268
		$expected = array(
269
			'Article2' => array(
270
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
271
			),
272
			'Comment' => array()
273
		);
274
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
275
		$this->assertEquals($expected, $data);
276
 
277
		$data = array(
278
			'Article' => array(
279
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
280
			)
281
		);
282
		$merge = array(
283
			array(
284
				'Comment' => array(
285
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
286
				)
287
			),
288
			array(
289
				'Comment' => array(
290
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
291
				)
292
			)
293
		);
294
		$expected = array(
295
			'Article' => array(
296
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
297
			),
298
			'Comment' => array(
299
				array(
300
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
301
				),
302
				array(
303
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
304
				)
305
			)
306
		);
307
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
308
		$this->assertEquals($expected, $data);
309
 
310
		$data = array(
311
			'Article' => array(
312
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
313
			)
314
		);
315
		$merge = array(
316
			array(
317
				'Comment' => array(
318
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
319
				),
320
				'User2' => array(
321
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
322
				)
323
			),
324
			array(
325
				'Comment' => array(
326
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
327
				),
328
				'User2' => array(
329
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
330
				)
331
			)
332
		);
333
		$expected = array(
334
			'Article' => array(
335
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
336
			),
337
			'Comment' => array(
338
				array(
339
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
340
					'User2' => array(
341
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
342
					)
343
				),
344
				array(
345
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
346
					'User2' => array(
347
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
348
					)
349
				)
350
			)
351
		);
352
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
353
		$this->assertEquals($expected, $data);
354
 
355
		$data = array(
356
			'Article' => array(
357
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
358
			)
359
		);
360
		$merge = array(
361
			array(
362
				'Comment' => array(
363
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
364
				),
365
				'User2' => array(
366
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
367
				),
368
				'Tag' => array(
369
					array('id' => 1, 'tag' => 'Tag 1'),
370
					array('id' => 2, 'tag' => 'Tag 2')
371
				)
372
			),
373
			array(
374
				'Comment' => array(
375
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
376
				),
377
				'User2' => array(
378
					'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
379
				),
380
				'Tag' => array()
381
			)
382
		);
383
		$expected = array(
384
			'Article' => array(
385
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
386
			),
387
			'Comment' => array(
388
				array(
389
					'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
390
					'User2' => array(
391
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
392
					),
393
					'Tag' => array(
394
						array('id' => 1, 'tag' => 'Tag 1'),
395
						array('id' => 2, 'tag' => 'Tag 2')
396
					)
397
				),
398
				array(
399
					'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
400
					'User2' => array(
401
						'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
402
					),
403
					'Tag' => array()
404
				)
405
			)
406
		);
407
		$this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
408
		$this->assertEquals($expected, $data);
409
 
410
		$data = array(
411
			'Article' => array(
412
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
413
			)
414
		);
415
		$merge = array(
416
			array(
417
				'Tag' => array(
418
					'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
419
				)
420
			),
421
			array(
422
				'Tag' => array(
423
					'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
424
				)
425
			),
426
			array(
427
				'Tag' => array(
428
					'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
429
				)
430
			)
431
		);
432
		$expected = array(
433
			'Article' => array(
434
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
435
			),
436
			'Tag' => array(
437
				array(
438
					'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
439
				),
440
				array(
441
					'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
442
				),
443
				array(
444
					'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
445
				)
446
			)
447
		);
448
		$this->testDb->mergeAssociation($data, $merge, 'Tag', 'hasAndBelongsToMany');
449
		$this->assertEquals($expected, $data);
450
 
451
		$data = array(
452
			'Article' => array(
453
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
454
			)
455
		);
456
		$merge = array(
457
			array(
458
				'Tag' => array(
459
					'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
460
				)
461
			),
462
			array(
463
				'Tag' => array(
464
					'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
465
				)
466
			),
467
			array(
468
				'Tag' => array(
469
					'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
470
				)
471
			)
472
		);
473
		$expected = array(
474
			'Article' => array(
475
				'id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
476
			),
477
			'Tag' => array('id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')
478
		);
479
		$this->testDb->mergeAssociation($data, $merge, 'Tag', 'hasOne');
480
		$this->assertEquals($expected, $data);
481
	}
482
 
483
/**
484
 * testMagicMethodQuerying method
485
 *
486
 * @return void
487
 */
488
	public function testMagicMethodQuerying() {
489
		$result = $this->db->query('findByFieldName', array('value'), $this->Model);
490
		$expected = array('first', array(
491
			'conditions' => array('TestModel.field_name' => 'value'),
492
			'fields' => null, 'order' => null, 'recursive' => null
493
		));
494
		$this->assertEquals($expected, $result);
495
 
496
		$result = $this->db->query('findByFindBy', array('value'), $this->Model);
497
		$expected = array('first', array(
498
			'conditions' => array('TestModel.find_by' => 'value'),
499
			'fields' => null, 'order' => null, 'recursive' => null
500
		));
501
		$this->assertEquals($expected, $result);
502
 
503
		$result = $this->db->query('findAllByFieldName', array('value'), $this->Model);
504
		$expected = array('all', array(
505
			'conditions' => array('TestModel.field_name' => 'value'),
506
			'fields' => null, 'order' => null, 'limit' => null,
507
			'page' => null, 'recursive' => null
508
		));
509
		$this->assertEquals($expected, $result);
510
 
511
		$result = $this->db->query('findAllById', array('a'), $this->Model);
512
		$expected = array('all', array(
513
			'conditions' => array('TestModel.id' => 'a'),
514
			'fields' => null, 'order' => null, 'limit' => null,
515
			'page' => null, 'recursive' => null
516
		));
517
		$this->assertEquals($expected, $result);
518
 
519
		$result = $this->db->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
520
		$expected = array('first', array(
521
			'conditions' => array('TestModel.field_name' => array('value1', 'value2', 'value3')),
522
			'fields' => null, 'order' => null, 'recursive' => null
523
		));
524
		$this->assertEquals($expected, $result);
525
 
526
		$result = $this->db->query('findByFieldName', array(null), $this->Model);
527
		$expected = array('first', array(
528
			'conditions' => array('TestModel.field_name' => null),
529
			'fields' => null, 'order' => null, 'recursive' => null
530
		));
531
		$this->assertEquals($expected, $result);
532
 
533
		$result = $this->db->query('findByFieldName', array('= a'), $this->Model);
534
		$expected = array('first', array(
535
			'conditions' => array('TestModel.field_name' => '= a'),
536
			'fields' => null, 'order' => null, 'recursive' => null
537
		));
538
		$this->assertEquals($expected, $result);
539
 
540
		$result = $this->db->query('findByFieldName', array(), $this->Model);
541
		$expected = false;
542
		$this->assertEquals($expected, $result);
543
	}
544
 
545
/**
546
 *
547
 * @expectedException PDOException
548
 * @return void
549
 */
550
	public function testDirectCallThrowsException() {
551
		$this->db->query('directCall', array(), $this->Model);
552
	}
553
 
554
/**
555
 * testValue method
556
 *
557
 * @return void
558
 */
559
	public function testValue() {
560
		if ($this->db instanceof Sqlserver) {
561
			$this->markTestSkipped('Cannot run this test with SqlServer');
562
		}
563
		$result = $this->db->value('{$__cakeForeignKey__$}');
564
		$this->assertEquals('{$__cakeForeignKey__$}', $result);
565
 
566
		$result = $this->db->value(array('first', 2, 'third'));
567
		$expected = array('\'first\'', 2, '\'third\'');
568
		$this->assertEquals($expected, $result);
569
	}
570
 
571
/**
572
 * Tests if the connection can be re-established and that the new (optional) config is set.
573
 *
574
 * @return void
575
 */
576
	public function testReconnect() {
577
		$this->testDb->reconnect(array('prefix' => 'foo'));
578
		$this->assertTrue($this->testDb->connected);
579
		$this->assertEquals('foo', $this->testDb->config['prefix']);
580
	}
581
 
582
/**
583
 * testName method
584
 *
585
 * @return void
586
 */
587
	public function testName() {
588
		$result = $this->testDb->name('name');
589
		$expected = '`name`';
590
		$this->assertEquals($expected, $result);
591
 
592
		$result = $this->testDb->name(array('name', 'Model.*'));
593
		$expected = array('`name`', '`Model`.*');
594
		$this->assertEquals($expected, $result);
595
 
596
		$result = $this->testDb->name('MTD()');
597
		$expected = 'MTD()';
598
		$this->assertEquals($expected, $result);
599
 
600
		$result = $this->testDb->name('(sm)');
601
		$expected = '(sm)';
602
		$this->assertEquals($expected, $result);
603
 
604
		$result = $this->testDb->name('name AS x');
605
		$expected = '`name` AS `x`';
606
		$this->assertEquals($expected, $result);
607
 
608
		$result = $this->testDb->name('Model.name AS x');
609
		$expected = '`Model`.`name` AS `x`';
610
		$this->assertEquals($expected, $result);
611
 
612
		$result = $this->testDb->name('Function(Something.foo)');
613
		$expected = 'Function(`Something`.`foo`)';
614
		$this->assertEquals($expected, $result);
615
 
616
		$result = $this->testDb->name('Function(SubFunction(Something.foo))');
617
		$expected = 'Function(SubFunction(`Something`.`foo`))';
618
		$this->assertEquals($expected, $result);
619
 
620
		$result = $this->testDb->name('Function(Something.foo) AS x');
621
		$expected = 'Function(`Something`.`foo`) AS `x`';
622
		$this->assertEquals($expected, $result);
623
 
624
		$result = $this->testDb->name('I18n__title__pt-br.locale');
625
		$expected = '`I18n__title__pt-br`.`locale`';
626
		$this->assertEquals($expected, $result);
627
 
628
		$result = $this->testDb->name('name-with-minus');
629
		$expected = '`name-with-minus`';
630
		$this->assertEquals($expected, $result);
631
 
632
		$result = $this->testDb->name(array('my-name', 'Foo-Model.*'));
633
		$expected = array('`my-name`', '`Foo-Model`.*');
634
		$this->assertEquals($expected, $result);
635
 
636
		$result = $this->testDb->name(array('Team.P%', 'Team.G/G'));
637
		$expected = array('`Team`.`P%`', '`Team`.`G/G`');
638
		$this->assertEquals($expected, $result);
639
 
640
		$result = $this->testDb->name('Model.name as y');
641
		$expected = '`Model`.`name` AS `y`';
642
		$this->assertEquals($expected, $result);
643
	}
644
 
645
/**
646
 * test that cacheMethod works as expected
647
 *
648
 * @return void
649
 */
650
	public function testCacheMethod() {
651
		$this->testDb->cacheMethods = true;
652
		$result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
653
		$this->assertEquals('stuff', $result);
654
 
655
		$result = $this->testDb->cacheMethod('name', 'some-key');
656
		$this->assertEquals('stuff', $result);
657
 
658
		$result = $this->testDb->cacheMethod('conditions', 'some-key');
659
		$this->assertNull($result);
660
 
661
		$result = $this->testDb->cacheMethod('name', 'other-key');
662
		$this->assertNull($result);
663
 
664
		$this->testDb->cacheMethods = false;
665
		$result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
666
		$this->assertEquals('stuff', $result);
667
 
668
		$result = $this->testDb->cacheMethod('name', 'some-key');
669
		$this->assertNull($result);
670
	}
671
 
672
/**
673
 * Test that rare collisions do not happen with method caching
674
 *
675
 * @return void
676
 */
677
	public function testNameMethodCacheCollisions() {
678
		$this->testDb->cacheMethods = true;
679
		$this->testDb->flushMethodCache();
680
		$this->testDb->name('Model.fieldlbqndkezcoapfgirmjsh');
681
		$result = $this->testDb->name('Model.fieldkhdfjmelarbqnzsogcpi');
682
		$expected = '`Model`.`fieldkhdfjmelarbqnzsogcpi`';
683
		$this->assertEquals($expected, $result);
684
	}
685
 
686
/**
687
 * Test that flushMethodCache works as expected
688
 *
689
 * @return void
690
 */
691
	public function testFlushMethodCache() {
692
		$this->testDb->cacheMethods = true;
693
		$this->testDb->cacheMethod('name', 'some-key', 'stuff');
694
 
695
		Cache::write('method_cache', DboTestSource::$methodCache, '_cake_core_');
696
 
697
		$this->testDb->flushMethodCache();
698
		$result = $this->testDb->cacheMethod('name', 'some-key');
699
		$this->assertNull($result);
700
	}
701
 
702
/**
703
 * testLog method
704
 *
705
 * @outputBuffering enabled
706
 * @return void
707
 */
708
	public function testLog() {
709
		$this->testDb->logQuery('Query 1');
710
		$this->testDb->logQuery('Query 2');
711
 
712
		$log = $this->testDb->getLog(false, false);
713
		$result = Hash::extract($log['log'], '{n}.query');
714
		$expected = array('Query 1', 'Query 2');
715
		$this->assertEquals($expected, $result);
716
 
717
		$oldDebug = Configure::read('debug');
718
		Configure::write('debug', 2);
719
		ob_start();
720
		$this->testDb->showLog();
721
		$contents = ob_get_clean();
722
 
723
		$this->assertRegExp('/Query 1/s', $contents);
724
		$this->assertRegExp('/Query 2/s', $contents);
725
 
726
		ob_start();
727
		$this->testDb->showLog(true);
728
		$contents = ob_get_clean();
729
 
730
		$this->assertRegExp('/Query 1/s', $contents);
731
		$this->assertRegExp('/Query 2/s', $contents);
732
 
733
		Configure::write('debug', $oldDebug);
734
	}
735
 
736
/**
737
 * test getting the query log as an array.
738
 *
739
 * @return void
740
 */
741
	public function testGetLog() {
742
		$this->testDb->logQuery('Query 1');
743
		$this->testDb->logQuery('Query 2');
744
 
745
		$log = $this->testDb->getLog();
746
		$expected = array('query' => 'Query 1', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
747
 
748
		$this->assertEquals($expected, $log['log'][0]);
749
		$expected = array('query' => 'Query 2', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
750
		$this->assertEquals($expected, $log['log'][1]);
751
		$expected = array('query' => 'Error 1', 'affected' => '', 'numRows' => '', 'took' => '');
752
	}
753
 
754
/**
755
 * test getting the query log as an array, setting bind params.
756
 *
757
 * @return void
758
 */
759
	public function testGetLogParams() {
760
		$this->testDb->logQuery('Query 1', array(1, 2, 'abc'));
761
		$this->testDb->logQuery('Query 2', array('field1' => 1, 'field2' => 'abc'));
762
 
763
		$log = $this->testDb->getLog();
764
		$expected = array('query' => 'Query 1', 'params' => array(1, 2, 'abc'), 'affected' => '', 'numRows' => '', 'took' => '');
765
		$this->assertEquals($expected, $log['log'][0]);
766
		$expected = array('query' => 'Query 2', 'params' => array('field1' => 1, 'field2' => 'abc'), 'affected' => '', 'numRows' => '', 'took' => '');
767
		$this->assertEquals($expected, $log['log'][1]);
768
	}
769
 
770
/**
771
 * test that query() returns boolean values from operations like CREATE TABLE
772
 *
773
 * @return void
774
 */
775
	public function testFetchAllBooleanReturns() {
776
		$name = $this->db->fullTableName('test_query');
777
		$query = "CREATE TABLE {$name} (name varchar(10));";
778
		$result = $this->db->query($query);
779
		$this->assertTrue($result, 'Query did not return a boolean');
780
 
781
		$query = "DROP TABLE {$name};";
782
		$result = $this->db->query($query);
783
		$this->assertTrue($result, 'Query did not return a boolean');
784
	}
785
 
786
/**
787
 * test order to generate query order clause for virtual fields
788
 *
789
 * @return void
790
 */
791
	public function testVirtualFieldsInOrder() {
792
		$Article = ClassRegistry::init('Article');
793
		$Article->virtualFields = array(
794
			'this_moment' => 'NOW()',
795
			'two' => '1 + 1',
796
		);
797
		$order = array('two', 'this_moment');
798
		$result = $this->db->order($order, 'ASC', $Article);
799
		$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
800
		$this->assertEquals($expected, $result);
801
 
802
		$order = array('Article.two', 'Article.this_moment');
803
		$result = $this->db->order($order, 'ASC', $Article);
804
		$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
805
		$this->assertEquals($expected, $result);
806
	}
807
 
808
/**
809
 * test the permutations of fullTableName()
810
 *
811
 * @return void
812
 */
813
	public function testFullTablePermutations() {
814
		$Article = ClassRegistry::init('Article');
815
		$result = $this->testDb->fullTableName($Article, false, false);
816
		$this->assertEquals('articles', $result);
817
 
818
		$Article->tablePrefix = 'tbl_';
819
		$result = $this->testDb->fullTableName($Article, false, false);
820
		$this->assertEquals('tbl_articles', $result);
821
 
822
		$Article->useTable = $Article->table = 'with spaces';
823
		$Article->tablePrefix = '';
824
		$result = $this->testDb->fullTableName($Article, true, false);
825
		$this->assertEquals('`with spaces`', $result);
826
 
827
		$this->loadFixtures('Article');
828
		$Article->useTable = $Article->table = 'articles';
829
		$Article->setDataSource('test');
830
		$testdb = $Article->getDataSource();
831
		$result = $testdb->fullTableName($Article, false, true);
832
		$this->assertEquals($testdb->getSchemaName() . '.articles', $result);
833
 
834
		// tests for empty schemaName
835
		$noschema = ConnectionManager::create('noschema', array(
836
			'datasource' => 'DboTestSource'
837
			));
838
		$Article->setDataSource('noschema');
839
		$Article->schemaName = null;
840
		$result = $noschema->fullTableName($Article, false, true);
841
		$this->assertEquals('articles', $result);
842
 
843
		$this->testDb->config['prefix'] = 't_';
844
		$result = $this->testDb->fullTableName('post_tag', false, false);
845
		$this->assertEquals('t_post_tag', $result);
846
	}
847
 
848
/**
849
 * test that read() only calls queryAssociation on db objects when the method is defined.
850
 *
851
 * @return void
852
 */
853
	public function testReadOnlyCallingQueryAssociationWhenDefined() {
854
		$this->loadFixtures('Article', 'User', 'ArticlesTag', 'Tag');
855
		ConnectionManager::create('test_no_queryAssociation', array(
856
			'datasource' => 'MockDataSource'
857
		));
858
		$Article = ClassRegistry::init('Article');
859
		$Article->Comment->useDbConfig = 'test_no_queryAssociation';
860
		$result = $Article->find('all');
861
		$this->assertTrue(is_array($result));
862
	}
863
 
864
/**
865
 * test that queryAssociation() reuse already joined data for 'belongsTo' and 'hasOne' associations
866
 * instead of running unneeded queries for each record
867
 *
868
 * @return void
869
 */
870
	public function testQueryAssociationUnneededQueries() {
871
		$this->loadFixtures('Article', 'User', 'Comment', 'Attachment', 'Tag', 'ArticlesTag');
872
		$Comment = new Comment;
873
 
874
		$fullDebug = $this->db->fullDebug;
875
		$this->db->fullDebug = true;
876
 
877
		$Comment->find('all', array('recursive' => 2)); // ensure Model descriptions are saved
878
		$this->db->getLog();
879
 
880
		// case: Comment belongsTo User and Article
881
		$Comment->unbindModel(array(
882
			'hasOne' => array('Attachment')
883
		));
884
		$Comment->Article->unbindModel(array(
885
			'belongsTo' => array('User'),
886
			'hasMany' => array('Comment'),
887
			'hasAndBelongsToMany' => array('Tag')
888
		));
889
		$Comment->find('all', array('recursive' => 2));
890
		$log = $this->db->getLog();
891
		$this->assertEquals(1, count($log['log']));
892
 
893
		// case: Comment belongsTo Article, Article belongsTo User
894
		$Comment->unbindModel(array(
895
			'belongsTo' => array('User'),
896
			'hasOne' => array('Attachment')
897
		));
898
		$Comment->Article->unbindModel(array(
899
			'hasMany' => array('Comment'),
900
			'hasAndBelongsToMany' => array('Tag'),
901
		));
902
		$Comment->find('all', array('recursive' => 2));
903
		$log = $this->db->getLog();
904
		$this->assertEquals(7, count($log['log']));
905
 
906
		// case: Comment hasOne Attachment
907
		$Comment->unbindModel(array(
908
			'belongsTo' => array('Article', 'User'),
909
		));
910
		$Comment->Attachment->unbindModel(array(
911
			'belongsTo' => array('Comment'),
912
		));
913
		$Comment->find('all', array('recursive' => 2));
914
		$log = $this->db->getLog();
915
		$this->assertEquals(1, count($log['log']));
916
 
917
		$this->db->fullDebug = $fullDebug;
918
	}
919
 
920
/**
921
 * test that fields() is using methodCache()
922
 *
923
 * @return void
924
 */
925
	public function testFieldsUsingMethodCache() {
926
		$this->testDb->cacheMethods = false;
927
		DboTestSource::$methodCache = array();
928
 
929
		$Article = ClassRegistry::init('Article');
930
		$this->testDb->fields($Article, null, array('title', 'body', 'published'));
931
		$this->assertTrue(empty(DboTestSource::$methodCache['fields']), 'Cache not empty');
932
	}
933
 
934
/**
935
 * test that fields() method cache detects datasource changes
936
 *
937
 * @return void
938
 */
939
	public function testFieldsCacheKeyWithDatasourceChange() {
940
		ConnectionManager::create('firstschema', array(
941
			'datasource' => 'DboTestSource'
942
		));
943
		ConnectionManager::create('secondschema', array(
944
			'datasource' => 'DboSecondTestSource'
945
		));
946
		Cache::delete('method_cache', '_cake_core_');
947
		DboTestSource::$methodCache = array();
948
		$Article = ClassRegistry::init('Article');
949
 
950
		$Article->setDataSource('firstschema');
951
		$ds = $Article->getDataSource();
952
		$ds->cacheMethods = true;
953
		$first = $ds->fields($Article, null, array('title', 'body', 'published'));
954
 
955
		$Article->setDataSource('secondschema');
956
		$ds = $Article->getDataSource();
957
		$ds->cacheMethods = true;
958
		$second = $ds->fields($Article, null, array('title', 'body', 'published'));
959
 
960
		$this->assertNotEquals($first, $second);
961
		$this->assertEquals(2, count(DboTestSource::$methodCache['fields']));
962
	}
963
 
964
/**
965
 * test that fields() method cache detects schema name changes
966
 *
967
 * @return void
968
 */
969
	public function testFieldsCacheKeyWithSchemanameChange() {
970
		if ($this->db instanceof Postgres || $this->db instanceof Sqlserver) {
971
			$this->markTestSkipped('Cannot run this test with SqlServer or Postgres');
972
		}
973
		Cache::delete('method_cache', '_cake_core_');
974
		DboSource::$methodCache = array();
975
		$Article = ClassRegistry::init('Article');
976
 
977
		$ds = $Article->getDataSource();
978
		$ds->cacheMethods = true;
979
		$first = $ds->fields($Article);
980
 
981
		$Article->schemaName = 'secondSchema';
982
		$ds = $Article->getDataSource();
983
		$ds->cacheMethods = true;
984
		$second = $ds->fields($Article);
985
 
986
		$this->assertEquals(2, count(DboSource::$methodCache['fields']));
987
	}
988
 
989
/**
990
 * Test that group works without a model
991
 *
992
 * @return void
993
 */
994
	public function testGroupNoModel() {
995
		$result = $this->db->group('created');
996
		$this->assertEquals(' GROUP BY created', $result);
997
	}
998
 
999
/**
1000
 * Test getting the last error.
1001
 */
1002
	public function testLastError() {
1003
		$stmt = $this->getMock('PDOStatement');
1004
		$stmt->expects($this->any())
1005
			->method('errorInfo')
1006
			->will($this->returnValue(array('', 'something', 'bad')));
1007
 
1008
		$result = $this->db->lastError($stmt);
1009
		$expected = 'something: bad';
1010
		$this->assertEquals($expected, $result);
1011
	}
1012
 
1013
/**
1014
 * Tests that transaction commands are logged
1015
 *
1016
 * @return void
1017
 */
1018
	public function testTransactionLogging() {
1019
		$conn = $this->getMock('MockPDO');
1020
		$db = new DboTestSource;
1021
		$db->setConnection($conn);
1022
		$conn->expects($this->exactly(2))->method('beginTransaction')
1023
			->will($this->returnValue(true));
1024
		$conn->expects($this->once())->method('commit')->will($this->returnValue(true));
1025
		$conn->expects($this->once())->method('rollback')->will($this->returnValue(true));
1026
 
1027
		$db->begin();
1028
		$log = $db->getLog();
1029
		$expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1030
		$this->assertEquals($expected, $log['log'][0]);
1031
 
1032
		$db->commit();
1033
		$expected = array('query' => 'COMMIT', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1034
		$log = $db->getLog();
1035
		$this->assertEquals($expected, $log['log'][0]);
1036
 
1037
		$db->begin();
1038
		$expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1039
		$log = $db->getLog();
1040
		$this->assertEquals($expected, $log['log'][0]);
1041
 
1042
		$db->rollback();
1043
		$expected = array('query' => 'ROLLBACK', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
1044
		$log = $db->getLog();
1045
		$this->assertEquals($expected, $log['log'][0]);
1046
	}
1047
 
1048
/**
1049
 * Test nested transaction calls
1050
 *
1051
 * @return void
1052
 */
1053
	public function testTransactionNested() {
1054
		$conn = $this->getMock('MockPDO');
1055
		$db = new DboTestSource();
1056
		$db->setConnection($conn);
1057
		$db->useNestedTransactions = true;
1058
		$db->nestedSupport = true;
1059
 
1060
		$conn->expects($this->at(0))->method('beginTransaction')->will($this->returnValue(true));
1061
		$conn->expects($this->at(1))->method('exec')->with($this->equalTo('SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1062
		$conn->expects($this->at(2))->method('exec')->with($this->equalTo('RELEASE SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1063
		$conn->expects($this->at(3))->method('exec')->with($this->equalTo('SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1064
		$conn->expects($this->at(4))->method('exec')->with($this->equalTo('ROLLBACK TO SAVEPOINT LEVEL1'))->will($this->returnValue(true));
1065
		$conn->expects($this->at(5))->method('commit')->will($this->returnValue(true));
1066
 
1067
		$this->_runTransactions($db);
1068
	}
1069
 
1070
/**
1071
 * Test nested transaction calls without support
1072
 *
1073
 * @return void
1074
 */
1075
	public function testTransactionNestedWithoutSupport() {
1076
		$conn = $this->getMock('MockPDO');
1077
		$db = new DboTestSource();
1078
		$db->setConnection($conn);
1079
		$db->useNestedTransactions = true;
1080
		$db->nestedSupport = false;
1081
 
1082
		$conn->expects($this->once())->method('beginTransaction')->will($this->returnValue(true));
1083
		$conn->expects($this->never())->method('exec');
1084
		$conn->expects($this->once())->method('commit')->will($this->returnValue(true));
1085
 
1086
		$this->_runTransactions($db);
1087
	}
1088
 
1089
/**
1090
 * Test nested transaction disabled
1091
 *
1092
 * @return void
1093
 */
1094
	public function testTransactionNestedDisabled() {
1095
		$conn = $this->getMock('MockPDO');
1096
		$db = new DboTestSource();
1097
		$db->setConnection($conn);
1098
		$db->useNestedTransactions = false;
1099
		$db->nestedSupport = true;
1100
 
1101
		$conn->expects($this->once())->method('beginTransaction')->will($this->returnValue(true));
1102
		$conn->expects($this->never())->method('exec');
1103
		$conn->expects($this->once())->method('commit')->will($this->returnValue(true));
1104
 
1105
		$this->_runTransactions($db);
1106
	}
1107
 
1108
/**
1109
 * Nested transaction calls
1110
 *
1111
 * @param DboTestSource $db
1112
 * @return void
1113
 */
1114
	protected function _runTransactions($db) {
1115
		$db->begin();
1116
		$db->begin();
1117
		$db->commit();
1118
		$db->begin();
1119
		$db->rollback();
1120
		$db->commit();
1121
	}
1122
 
1123
/**
1124
 * Test build statement with some fields missing
1125
 *
1126
 * @return void
1127
 */
1128
	public function testBuildStatementDefaults() {
1129
		$conn = $this->getMock('MockPDO', array('quote'));
1130
		$conn->expects($this->at(0))
1131
			->method('quote')
1132
			->will($this->returnValue('foo bar'));
1133
		$db = new DboTestSource;
1134
		$db->setConnection($conn);
1135
		$subQuery = $db->buildStatement(
1136
			array(
1137
				'fields' => array('DISTINCT(AssetsTag.asset_id)'),
1138
				'table' => "assets_tags",
1139
				'alias' => "AssetsTag",
1140
				'conditions' => array("Tag.name" => 'foo bar'),
1141
				'limit' => null,
1142
				'group' => "AssetsTag.asset_id"
1143
			),
1144
			$this->Model
1145
		);
1146
		$expected = 'SELECT DISTINCT(AssetsTag.asset_id) FROM assets_tags AS AssetsTag   WHERE Tag.name = foo bar  GROUP BY AssetsTag.asset_id';
1147
		$this->assertEquals($expected, $subQuery);
1148
	}
1149
 
1150
/**
1151
 * data provider for testBuildJoinStatement
1152
 *
1153
 * @return array
1154
 */
1155
	public static function joinStatements() {
1156
		return array(
1157
			array(array(
1158
				'type' => 'CROSS',
1159
				'alias' => 'PostsTag',
1160
				'table' => 'posts_tags',
1161
				'conditions' => array('1 = 1')
1162
			), 'CROSS JOIN cakephp.posts_tags AS PostsTag'),
1163
			array(array(
1164
				'type' => 'LEFT',
1165
				'alias' => 'PostsTag',
1166
				'table' => 'posts_tags',
1167
				'conditions' => array('PostsTag.post_id = Post.id')
1168
			), 'LEFT JOIN cakephp.posts_tags AS PostsTag ON (PostsTag.post_id = Post.id)'),
1169
			array(array(
1170
				'type' => 'LEFT',
1171
				'alias' => 'Stock',
1172
				'table' => '(SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id)',
1173
				'conditions' => 'Stock.article_id = Article.id'
1174
			), 'LEFT JOIN (SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id) AS Stock ON (Stock.article_id = Article.id)')
1175
		);
1176
	}
1177
 
1178
/**
1179
 * Test buildJoinStatement()
1180
 * ensure that schemaName is not added when table value is a subquery
1181
 *
1182
 * @dataProvider joinStatements
1183
 * @return void
1184
 */
1185
	public function testBuildJoinStatement($join, $expected) {
1186
		$db = $this->getMock('DboTestSource', array('getSchemaName'));
1187
		$db->expects($this->any())
1188
			->method('getSchemaName')
1189
			->will($this->returnValue('cakephp'));
1190
		$result = $db->buildJoinStatement($join);
1191
		$this->assertEquals($expected, $result);
1192
	}
1193
 
1194
/**
1195
 * data provider for testBuildJoinStatementWithTablePrefix
1196
 *
1197
 * @return array
1198
 */
1199
	public static function joinStatementsWithPrefix($schema) {
1200
		return array(
1201
			array(array(
1202
				'type' => 'LEFT',
1203
				'alias' => 'PostsTag',
1204
				'table' => 'posts_tags',
1205
				'conditions' => array('PostsTag.post_id = Post.id')
1206
			), 'LEFT JOIN pre_posts_tags AS PostsTag ON (PostsTag.post_id = Post.id)'),
1207
				array(array(
1208
					'type' => 'LEFT',
1209
					'alias' => 'Stock',
1210
					'table' => '(SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id)',
1211
					'conditions' => 'Stock.article_id = Article.id'
1212
				), 'LEFT JOIN (SELECT Stock.article_id, sum(quantite) quantite FROM stocks AS Stock GROUP BY Stock.article_id) AS Stock ON (Stock.article_id = Article.id)')
1213
			);
1214
	}
1215
 
1216
/**
1217
 * Test buildJoinStatement()
1218
 * ensure that prefix is not added when table value is a subquery
1219
 *
1220
 * @dataProvider joinStatementsWithPrefix
1221
 * @return void
1222
 */
1223
	public function testBuildJoinStatementWithTablePrefix($join, $expected) {
1224
		$db = new DboTestSource;
1225
		$db->config['prefix'] = 'pre_';
1226
		$result = $db->buildJoinStatement($join);
1227
		$this->assertEquals($expected, $result);
1228
	}
1229
 
1230
/**
1231
 * Test conditionKeysToString()
1232
 *
1233
 * @return void
1234
 */
1235
	public function testConditionKeysToString() {
1236
		$Article = ClassRegistry::init('Article');
1237
		$conn = $this->getMock('MockPDO', array('quote'));
1238
		$db = new DboTestSource;
1239
		$db->setConnection($conn);
1240
 
1241
		$conn->expects($this->at(0))
1242
			->method('quote')
1243
			->will($this->returnValue('just text'));
1244
 
1245
		$conditions = array('Article.name' => 'just text');
1246
		$result = $db->conditionKeysToString($conditions, true, $Article);
1247
		$expected = "Article.name = just text";
1248
		$this->assertEquals($expected, $result[0]);
1249
 
1250
		$conn->expects($this->at(0))
1251
			->method('quote')
1252
			->will($this->returnValue('just text'));
1253
		$conn->expects($this->at(1))
1254
			->method('quote')
1255
			->will($this->returnValue('other text'));
1256
 
1257
		$conditions = array('Article.name' => array('just text', 'other text'));
1258
		$result = $db->conditionKeysToString($conditions, true, $Article);
1259
		$expected = "Article.name IN (just text, other text)";
1260
		$this->assertEquals($expected, $result[0]);
1261
	}
1262
 
1263
/**
1264
 * Test conditionKeysToString() with virtual field
1265
 *
1266
 * @return void
1267
 */
1268
	public function testConditionKeysToStringVirtualField() {
1269
		$Article = ClassRegistry::init('Article');
1270
		$Article->virtualFields = array(
1271
			'extra' => 'something virtual'
1272
		);
1273
		$conn = $this->getMock('MockPDO', array('quote'));
1274
		$db = new DboTestSource;
1275
		$db->setConnection($conn);
1276
 
1277
		$conn->expects($this->at(0))
1278
			->method('quote')
1279
			->will($this->returnValue('just text'));
1280
 
1281
		$conditions = array('Article.extra' => 'just text');
1282
		$result = $db->conditionKeysToString($conditions, true, $Article);
1283
		$expected = "(" . $Article->virtualFields['extra'] . ") = just text";
1284
		$this->assertEquals($expected, $result[0]);
1285
 
1286
		$conn->expects($this->at(0))
1287
			->method('quote')
1288
			->will($this->returnValue('just text'));
1289
		$conn->expects($this->at(1))
1290
			->method('quote')
1291
			->will($this->returnValue('other text'));
1292
 
1293
		$conditions = array('Article.extra' => array('just text', 'other text'));
1294
		$result = $db->conditionKeysToString($conditions, true, $Article);
1295
		$expected = "(" . $Article->virtualFields['extra'] . ") IN (just text, other text)";
1296
		$this->assertEquals($expected, $result[0]);
1297
	}
1298
 
1299
/**
1300
 * Test the limit function.
1301
 *
1302
 * @return void
1303
 */
1304
	public function testLimit() {
1305
		$db = new DboTestSource;
1306
 
1307
		$result = $db->limit('0');
1308
		$this->assertNull($result);
1309
 
1310
		$result = $db->limit('10');
1311
		$this->assertEquals(' LIMIT 10', $result);
1312
 
1313
		$result = $db->limit('FARTS', 'BOOGERS');
1314
		$this->assertEquals(' LIMIT 0, 0', $result);
1315
 
1316
		$result = $db->limit(20, 10);
1317
		$this->assertEquals(' LIMIT 10, 20', $result);
1318
 
1319
		$result = $db->limit(10, 300000000000000000000000000000);
1320
		$scientificNotation = sprintf('%.1E', 300000000000000000000000000000);
1321
		$this->assertNotContains($scientificNotation, $result);
1322
	}
1323
 
1324
/**
1325
 * Test insertMulti with id position.
1326
 *
1327
 * @return void
1328
 */
1329
	public function testInsertMultiId() {
1330
		$this->loadFixtures('Article');
1331
		$Article = ClassRegistry::init('Article');
1332
		$db = $Article->getDatasource();
1333
		$datetime = date('Y-m-d H:i:s');
1334
		$data = array(
1335
			array(
1336
				'user_id' => 1,
1337
				'title' => 'test',
1338
				'body' => 'test',
1339
				'published' => 'N',
1340
				'created' => $datetime,
1341
				'updated' => $datetime,
1342
				'id' => 100,
1343
			),
1344
			array(
1345
				'user_id' => 1,
1346
				'title' => 'test 101',
1347
				'body' => 'test 101',
1348
				'published' => 'N',
1349
				'created' => $datetime,
1350
				'updated' => $datetime,
1351
				'id' => 101,
1352
			)
1353
		);
1354
		$result = $db->insertMulti('articles', array_keys($data[0]), $data);
1355
		$this->assertTrue($result, 'Data was saved');
1356
 
1357
		$data = array(
1358
			array(
1359
				'id' => 102,
1360
				'user_id' => 1,
1361
				'title' => 'test',
1362
				'body' => 'test',
1363
				'published' => 'N',
1364
				'created' => $datetime,
1365
				'updated' => $datetime,
1366
			),
1367
			array(
1368
				'id' => 103,
1369
				'user_id' => 1,
1370
				'title' => 'test 101',
1371
				'body' => 'test 101',
1372
				'published' => 'N',
1373
				'created' => $datetime,
1374
				'updated' => $datetime,
1375
			)
1376
		);
1377
 
1378
		$result = $db->insertMulti('articles', array_keys($data[0]), $data);
1379
		$this->assertTrue($result, 'Data was saved');
1380
	}
1381
}