PageRenderTime 134ms CodeModel.GetById 54ms app.highlight 68ms RepoModel.GetById 1ms app.codeStats 0ms

/cakephp/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php

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