DboSourceTest.php 31 KB

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