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