DboSourceTest.php 35 KB

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