DboSourceTest.php 43 KB

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