DboSourceTest.php 28 KB

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