PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 767 lines | 540 code | 91 blank | 136 comment | 0 complexity | 3051c2547b2117fbb40845ee061e997c MD5 | raw file
  1. <?php
  2. /**
  3. * DboSourceTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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 MockDataSource extends DataSource {
  25. }
  26. class DboTestSource extends DboSource {
  27. public function connect($config = array()) {
  28. $this->connected = true;
  29. }
  30. public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
  31. return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
  32. }
  33. public function setConfig($config = array()) {
  34. $this->config = $config;
  35. }
  36. public function setConnection($conn) {
  37. $this->_connection = $conn;
  38. }
  39. }
  40. /**
  41. * DboSourceTest class
  42. *
  43. * @package Cake.Test.Case.Model.Datasource
  44. */
  45. class DboSourceTest extends CakeTestCase {
  46. /**
  47. * debug property
  48. *
  49. * @var mixed null
  50. */
  51. public $debug = null;
  52. /**
  53. * autoFixtures property
  54. *
  55. * @var bool false
  56. */
  57. public $autoFixtures = false;
  58. /**
  59. * fixtures property
  60. *
  61. * @var array
  62. */
  63. public $fixtures = array(
  64. 'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
  65. 'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test'
  66. );
  67. /**
  68. * setUp method
  69. *
  70. * @return void
  71. */
  72. public function setUp() {
  73. parent::setUp();
  74. $this->__config = $this->db->config;
  75. $this->testDb = new DboTestSource();
  76. $this->testDb->cacheSources = false;
  77. $this->testDb->startQuote = '`';
  78. $this->testDb->endQuote = '`';
  79. $this->Model = new TestModel();
  80. }
  81. /**
  82. * endTest method
  83. *
  84. * @return void
  85. */
  86. public function tearDown() {
  87. parent::tearDown();
  88. unset($this->Model);
  89. }
  90. /**
  91. * test that booleans and null make logical condition strings.
  92. *
  93. * @return void
  94. */
  95. public function testBooleanNullConditionsParsing() {
  96. $result = $this->testDb->conditions(true);
  97. $this->assertEquals($result, ' WHERE 1 = 1', 'true conditions failed %s');
  98. $result = $this->testDb->conditions(false);
  99. $this->assertEquals($result, ' WHERE 0 = 1', 'false conditions failed %s');
  100. $result = $this->testDb->conditions(null);
  101. $this->assertEquals($result, ' WHERE 1 = 1', 'null conditions failed %s');
  102. $result = $this->testDb->conditions(array());
  103. $this->assertEquals($result, ' WHERE 1 = 1', 'array() conditions failed %s');
  104. $result = $this->testDb->conditions('');
  105. $this->assertEquals($result, ' WHERE 1 = 1', '"" conditions failed %s');
  106. $result = $this->testDb->conditions(' ', '" " conditions failed %s');
  107. $this->assertEquals($result, ' WHERE 1 = 1');
  108. }
  109. /**
  110. * test that order() will accept objects made from DboSource::expression
  111. *
  112. * @return void
  113. */
  114. public function testOrderWithExpression() {
  115. $expression = $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col");
  116. $result = $this->testDb->order($expression);
  117. $expected = " ORDER BY CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col";
  118. $this->assertEquals($expected, $result);
  119. }
  120. /**
  121. * testMergeAssociations method
  122. *
  123. * @return void
  124. */
  125. public function testMergeAssociations() {
  126. $data = array('Article2' => array(
  127. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  128. 'body' => 'First Article Body', 'published' => 'Y',
  129. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  130. ));
  131. $merge = array('Topic' => array(array(
  132. 'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
  133. 'updated' => '2007-03-17 01:18:31'
  134. )));
  135. $expected = array(
  136. 'Article2' => array(
  137. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  138. 'body' => 'First Article Body', 'published' => 'Y',
  139. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  140. ),
  141. 'Topic' => array(
  142. 'id' => '1', 'topic' => 'Topic', 'created' => '2007-03-17 01:16:23',
  143. 'updated' => '2007-03-17 01:18:31'
  144. )
  145. );
  146. $this->testDb->mergeAssociation($data, $merge, 'Topic', 'hasOne');
  147. $this->assertEquals($data, $expected);
  148. $data = array('Article2' => array(
  149. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  150. 'body' => 'First Article Body', 'published' => 'Y',
  151. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  152. ));
  153. $merge = array('User2' => array(array(
  154. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  155. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  156. )));
  157. $expected = array(
  158. 'Article2' => array(
  159. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  160. 'body' => 'First Article Body', 'published' => 'Y',
  161. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  162. ),
  163. 'User2' => array(
  164. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  165. )
  166. );
  167. $this->testDb->mergeAssociation($data, $merge, 'User2', 'belongsTo');
  168. $this->assertEquals($data, $expected);
  169. $data = array(
  170. 'Article2' => array(
  171. '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'
  172. )
  173. );
  174. $merge = array(array('Comment' => false));
  175. $expected = array(
  176. 'Article2' => array(
  177. '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'
  178. ),
  179. 'Comment' => array()
  180. );
  181. $this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
  182. $this->assertEquals($data, $expected);
  183. $data = array(
  184. 'Article' => array(
  185. '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'
  186. )
  187. );
  188. $merge = array(
  189. array(
  190. 'Comment' => array(
  191. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  192. )
  193. ),
  194. array(
  195. 'Comment' => array(
  196. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  197. )
  198. )
  199. );
  200. $expected = array(
  201. 'Article' => array(
  202. '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'
  203. ),
  204. 'Comment' => array(
  205. array(
  206. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  207. ),
  208. array(
  209. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  210. )
  211. )
  212. );
  213. $this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
  214. $this->assertEquals($data, $expected);
  215. $data = array(
  216. 'Article' => array(
  217. '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'
  218. )
  219. );
  220. $merge = array(
  221. array(
  222. 'Comment' => array(
  223. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  224. ),
  225. 'User2' => array(
  226. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  227. )
  228. ),
  229. array(
  230. 'Comment' => array(
  231. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  232. ),
  233. 'User2' => array(
  234. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  235. )
  236. )
  237. );
  238. $expected = array(
  239. 'Article' => array(
  240. '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'
  241. ),
  242. 'Comment' => array(
  243. array(
  244. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  245. 'User2' => array(
  246. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  247. )
  248. ),
  249. array(
  250. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  251. 'User2' => array(
  252. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  253. )
  254. )
  255. )
  256. );
  257. $this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
  258. $this->assertEquals($data, $expected);
  259. $data = array(
  260. 'Article' => array(
  261. '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'
  262. )
  263. );
  264. $merge = array(
  265. array(
  266. 'Comment' => array(
  267. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  268. ),
  269. 'User2' => array(
  270. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  271. ),
  272. 'Tag' => array(
  273. array('id' => 1, 'tag' => 'Tag 1'),
  274. array('id' => 2, 'tag' => 'Tag 2')
  275. )
  276. ),
  277. array(
  278. 'Comment' => array(
  279. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  280. ),
  281. 'User2' => array(
  282. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  283. ),
  284. 'Tag' => array()
  285. )
  286. );
  287. $expected = 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. 'Comment' => array(
  292. array(
  293. 'id' => '1', 'comment' => 'Comment 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  294. 'User2' => array(
  295. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  296. ),
  297. 'Tag' => array(
  298. array('id' => 1, 'tag' => 'Tag 1'),
  299. array('id' => 2, 'tag' => 'Tag 2')
  300. )
  301. ),
  302. array(
  303. 'id' => '2', 'comment' => 'Comment 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31',
  304. 'User2' => array(
  305. 'id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  306. ),
  307. 'Tag' => array()
  308. )
  309. )
  310. );
  311. $this->testDb->mergeAssociation($data, $merge, 'Comment', 'hasMany');
  312. $this->assertEquals($data, $expected);
  313. $data = array(
  314. 'Article' => array(
  315. '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'
  316. )
  317. );
  318. $merge = array(
  319. array(
  320. 'Tag' => array(
  321. 'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  322. )
  323. ),
  324. array(
  325. 'Tag' => array(
  326. 'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  327. )
  328. ),
  329. array(
  330. 'Tag' => array(
  331. 'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  332. )
  333. )
  334. );
  335. $expected = array(
  336. 'Article' => array(
  337. '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'
  338. ),
  339. 'Tag' => array(
  340. array(
  341. 'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  342. ),
  343. array(
  344. 'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  345. ),
  346. array(
  347. 'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  348. )
  349. )
  350. );
  351. $this->testDb->mergeAssociation($data, $merge, 'Tag', 'hasAndBelongsToMany');
  352. $this->assertEquals($data, $expected);
  353. $data = array(
  354. 'Article' => array(
  355. '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'
  356. )
  357. );
  358. $merge = array(
  359. array(
  360. 'Tag' => array(
  361. 'id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  362. )
  363. ),
  364. array(
  365. 'Tag' => array(
  366. 'id' => '2', 'tag' => 'Tag 2', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  367. )
  368. ),
  369. array(
  370. 'Tag' => array(
  371. 'id' => '3', 'tag' => 'Tag 3', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  372. )
  373. )
  374. );
  375. $expected = array(
  376. 'Article' => array(
  377. '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'
  378. ),
  379. 'Tag' => array('id' => '1', 'tag' => 'Tag 1', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31')
  380. );
  381. $this->testDb->mergeAssociation($data, $merge, 'Tag', 'hasOne');
  382. $this->assertEquals($data, $expected);
  383. }
  384. /**
  385. * testMagicMethodQuerying method
  386. *
  387. * @return void
  388. */
  389. public function testMagicMethodQuerying() {
  390. $result = $this->db->query('findByFieldName', array('value'), $this->Model);
  391. $expected = array('first', array(
  392. 'conditions' => array('TestModel.field_name' => 'value'),
  393. 'fields' => null, 'order' => null, 'recursive' => null
  394. ));
  395. $this->assertEquals($expected, $result);
  396. $result = $this->db->query('findByFindBy', array('value'), $this->Model);
  397. $expected = array('first', array(
  398. 'conditions' => array('TestModel.find_by' => 'value'),
  399. 'fields' => null, 'order' => null, 'recursive' => null
  400. ));
  401. $this->assertEquals($expected, $result);
  402. $result = $this->db->query('findAllByFieldName', array('value'), $this->Model);
  403. $expected = array('all', array(
  404. 'conditions' => array('TestModel.field_name' => 'value'),
  405. 'fields' => null, 'order' => null, 'limit' => null,
  406. 'page' => null, 'recursive' => null
  407. ));
  408. $this->assertEquals($expected, $result);
  409. $result = $this->db->query('findAllById', array('a'), $this->Model);
  410. $expected = array('all', array(
  411. 'conditions' => array('TestModel.id' => 'a'),
  412. 'fields' => null, 'order' => null, 'limit' => null,
  413. 'page' => null, 'recursive' => null
  414. ));
  415. $this->assertEquals($expected, $result);
  416. $result = $this->db->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
  417. $expected = array('first', array(
  418. 'conditions' => array('TestModel.field_name' => array('value1', 'value2', 'value3')),
  419. 'fields' => null, 'order' => null, 'recursive' => null
  420. ));
  421. $this->assertEquals($expected, $result);
  422. $result = $this->db->query('findByFieldName', array(null), $this->Model);
  423. $expected = array('first', array(
  424. 'conditions' => array('TestModel.field_name' => null),
  425. 'fields' => null, 'order' => null, 'recursive' => null
  426. ));
  427. $this->assertEquals($expected, $result);
  428. $result = $this->db->query('findByFieldName', array('= a'), $this->Model);
  429. $expected = array('first', array(
  430. 'conditions' => array('TestModel.field_name' => '= a'),
  431. 'fields' => null, 'order' => null, 'recursive' => null
  432. ));
  433. $this->assertEquals($expected, $result);
  434. $result = $this->db->query('findByFieldName', array(), $this->Model);
  435. $expected = false;
  436. $this->assertEquals($expected, $result);
  437. }
  438. /**
  439. *
  440. * @expectedException PDOException
  441. * @return void
  442. */
  443. public function testDirectCallThrowsException() {
  444. $result = $this->db->query('directCall', array(), $this->Model);
  445. }
  446. /**
  447. * testValue method
  448. *
  449. * @return void
  450. */
  451. public function testValue() {
  452. $result = $this->db->value('{$__cakeForeignKey__$}');
  453. $this->assertEquals($result, '{$__cakeForeignKey__$}');
  454. $result = $this->db->value(array('first', 2, 'third'));
  455. $expected = array('\'first\'', 2, '\'third\'');
  456. $this->assertEquals($expected, $result);
  457. }
  458. /**
  459. * testReconnect method
  460. *
  461. * @return void
  462. */
  463. public function testReconnect() {
  464. $this->testDb->reconnect(array('prefix' => 'foo'));
  465. $this->assertTrue($this->testDb->connected);
  466. $this->assertEquals($this->testDb->config['prefix'], 'foo');
  467. }
  468. /**
  469. * testName method
  470. *
  471. * @return void
  472. */
  473. public function testName() {
  474. $result = $this->testDb->name('name');
  475. $expected = '`name`';
  476. $this->assertEquals($expected, $result);
  477. $result = $this->testDb->name(array('name', 'Model.*'));
  478. $expected = array('`name`', '`Model`.*');
  479. $this->assertEquals($expected, $result);
  480. $result = $this->testDb->name('MTD()');
  481. $expected = 'MTD()';
  482. $this->assertEquals($expected, $result);
  483. $result = $this->testDb->name('(sm)');
  484. $expected = '(sm)';
  485. $this->assertEquals($expected, $result);
  486. $result = $this->testDb->name('name AS x');
  487. $expected = '`name` AS `x`';
  488. $this->assertEquals($expected, $result);
  489. $result = $this->testDb->name('Model.name AS x');
  490. $expected = '`Model`.`name` AS `x`';
  491. $this->assertEquals($expected, $result);
  492. $result = $this->testDb->name('Function(Something.foo)');
  493. $expected = 'Function(`Something`.`foo`)';
  494. $this->assertEquals($expected, $result);
  495. $result = $this->testDb->name('Function(SubFunction(Something.foo))');
  496. $expected = 'Function(SubFunction(`Something`.`foo`))';
  497. $this->assertEquals($expected, $result);
  498. $result = $this->testDb->name('Function(Something.foo) AS x');
  499. $expected = 'Function(`Something`.`foo`) AS `x`';
  500. $this->assertEquals($expected, $result);
  501. $result = $this->testDb->name('name-with-minus');
  502. $expected = '`name-with-minus`';
  503. $this->assertEquals($expected, $result);
  504. $result = $this->testDb->name(array('my-name', 'Foo-Model.*'));
  505. $expected = array('`my-name`', '`Foo-Model`.*');
  506. $this->assertEquals($expected, $result);
  507. $result = $this->testDb->name(array('Team.P%', 'Team.G/G'));
  508. $expected = array('`Team`.`P%`', '`Team`.`G/G`');
  509. $this->assertEquals($expected, $result);
  510. $result = $this->testDb->name('Model.name as y');
  511. $expected = '`Model`.`name` AS `y`';
  512. $this->assertEquals($expected, $result);
  513. }
  514. /**
  515. * test that cacheMethod works as exepected
  516. *
  517. * @return void
  518. */
  519. public function testCacheMethod() {
  520. $this->testDb->cacheMethods = true;
  521. $result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
  522. $this->assertEquals($result, 'stuff');
  523. $result = $this->testDb->cacheMethod('name', 'some-key');
  524. $this->assertEquals($result, 'stuff');
  525. $result = $this->testDb->cacheMethod('conditions', 'some-key');
  526. $this->assertNull($result);
  527. $result = $this->testDb->cacheMethod('name', 'other-key');
  528. $this->assertNull($result);
  529. $this->testDb->cacheMethods = false;
  530. $result = $this->testDb->cacheMethod('name', 'some-key', 'stuff');
  531. $this->assertEquals($result, 'stuff');
  532. $result = $this->testDb->cacheMethod('name', 'some-key');
  533. $this->assertNull($result);
  534. }
  535. /**
  536. * testLog method
  537. *
  538. * @outputBuffering enabled
  539. * @return void
  540. */
  541. public function testLog() {
  542. $this->testDb->logQuery('Query 1');
  543. $this->testDb->logQuery('Query 2');
  544. $log = $this->testDb->getLog(false, false);
  545. $result = Set::extract($log['log'], '/query');
  546. $expected = array('Query 1', 'Query 2');
  547. $this->assertEquals($expected, $result);
  548. $oldDebug = Configure::read('debug');
  549. Configure::write('debug', 2);
  550. ob_start();
  551. $this->testDb->showLog();
  552. $contents = ob_get_clean();
  553. $this->assertRegExp('/Query 1/s', $contents);
  554. $this->assertRegExp('/Query 2/s', $contents);
  555. ob_start();
  556. $this->testDb->showLog(true);
  557. $contents = ob_get_clean();
  558. $this->assertRegExp('/Query 1/s', $contents);
  559. $this->assertRegExp('/Query 2/s', $contents);
  560. Configure::write('debug', $oldDebug);
  561. }
  562. /**
  563. * test getting the query log as an array.
  564. *
  565. * @return void
  566. */
  567. public function testGetLog() {
  568. $this->testDb->logQuery('Query 1');
  569. $this->testDb->logQuery('Query 2');
  570. $log = $this->testDb->getLog();
  571. $expected = array('query' => 'Query 1', 'affected' => '', 'numRows' => '', 'took' => '');
  572. $this->assertEquals($log['log'][0], $expected);
  573. $expected = array('query' => 'Query 2', 'affected' => '', 'numRows' => '', 'took' => '');
  574. $this->assertEquals($log['log'][1], $expected);
  575. $expected = array('query' => 'Error 1', 'affected' => '', 'numRows' => '', 'took' => '');
  576. }
  577. /**
  578. * test that query() returns boolean values from operations like CREATE TABLE
  579. *
  580. * @return void
  581. */
  582. public function testFetchAllBooleanReturns() {
  583. $name = $this->db->fullTableName('test_query');
  584. $query = "CREATE TABLE {$name} (name varchar(10));";
  585. $result = $this->db->query($query);
  586. $this->assertTrue($result, 'Query did not return a boolean');
  587. $query = "DROP TABLE {$name};";
  588. $result = $this->db->query($query);
  589. $this->assertTrue($result, 'Query did not return a boolean');
  590. }
  591. /**
  592. * test order to generate query order clause for virtual fields
  593. *
  594. * @return void
  595. */
  596. public function testVirtualFieldsInOrder() {
  597. $Article = ClassRegistry::init('Article');
  598. $Article->virtualFields = array(
  599. 'this_moment' => 'NOW()',
  600. 'two' => '1 + 1',
  601. );
  602. $order = array('two', 'this_moment');
  603. $result = $this->db->order($order, 'ASC', $Article);
  604. $expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
  605. $this->assertEquals($expected, $result);
  606. $order = array('Article.two', 'Article.this_moment');
  607. $result = $this->db->order($order, 'ASC', $Article);
  608. $expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
  609. $this->assertEquals($expected, $result);
  610. }
  611. /**
  612. * test the permutations of fullTableName()
  613. *
  614. * @return void
  615. */
  616. public function testFullTablePermutations() {
  617. $Article = ClassRegistry::init('Article');
  618. $result = $this->testDb->fullTableName($Article, false);
  619. $this->assertEquals($result, 'articles');
  620. $Article->tablePrefix = 'tbl_';
  621. $result = $this->testDb->fullTableName($Article, false);
  622. $this->assertEquals($result, 'tbl_articles');
  623. $Article->useTable = $Article->table = 'with spaces';
  624. $Article->tablePrefix = '';
  625. $result = $this->testDb->fullTableName($Article);
  626. $this->assertEquals($result, '`with spaces`');
  627. }
  628. /**
  629. * test that read() only calls queryAssociation on db objects when the method is defined.
  630. *
  631. * @return void
  632. */
  633. public function testReadOnlyCallingQueryAssociationWhenDefined() {
  634. $this->loadFixtures('Article', 'User', 'ArticlesTag', 'Tag');
  635. ConnectionManager::create('test_no_queryAssociation', array(
  636. 'datasource' => 'MockDataSource'
  637. ));
  638. $Article = ClassRegistry::init('Article');
  639. $Article->Comment->useDbConfig = 'test_no_queryAssociation';
  640. $result = $Article->find('all');
  641. $this->assertTrue(is_array($result));
  642. }
  643. /**
  644. * test that fields() is using methodCache()
  645. *
  646. * @return void
  647. */
  648. public function testFieldsUsingMethodCache() {
  649. $this->testDb->cacheMethods = false;
  650. DboTestSource::$methodCache = array();
  651. $Article = ClassRegistry::init('Article');
  652. $this->testDb->fields($Article, null, array('title', 'body', 'published'));
  653. $this->assertTrue(empty(DboTestSource::$methodCache['fields']), 'Cache not empty');
  654. }
  655. /**
  656. * Test that group works without a model
  657. *
  658. * @return void
  659. */
  660. function testGroupNoModel() {
  661. $result = $this->db->group('created');
  662. $this->assertEquals(' GROUP BY created', $result);
  663. }
  664. /**
  665. * Test getting the last error.
  666. */
  667. function testLastError() {
  668. $stmt = $this->getMock('PDOStatement');
  669. $stmt->expects($this->any())
  670. ->method('errorInfo')
  671. ->will($this->returnValue(array('', 'something', 'bad')));
  672. $result = $this->db->lastError($stmt);
  673. $expected = 'something: bad';
  674. $this->assertEquals($expected, $result);
  675. }
  676. }