DboSourceTest.php 45 KB

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