DboSourceTest.php 43 KB

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