PageRenderTime 76ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/protected/tests/unit/RedBeanTest.php

https://bitbucket.org/sanbrar/zurmo_invoice
PHP | 348 lines | 278 code | 32 blank | 38 comment | 11 complexity | 603b728268c2423eac29c592366d256b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, BSD-2-Clause, GPL-3.0
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. // HintManager copied from...
  27. // http://groups.google.com/group/redbeanorm/browse_thread/thread/7eb59797e8478a89/61eae7941cae1970
  28. // Used in testDateTimeHinting, and maybe other things, below. null test added.
  29. class HintManager implements RedBean_Observer { // Not Coding Standard
  30. public function __construct( $toolbox ) { // Not Coding Standard
  31. $this->dateOpt = new RedBean_Plugin_Optimizer_Datetime( $toolbox ); // Not Coding Standard
  32. } // Not Coding Standard
  33. // Not Coding Standard
  34. public function onEvent( $type, $info ) { // Not Coding Standard
  35. $hints = $info->getMeta("hint"); // Not Coding Standard
  36. if ($hints !== null) { // Not Coding Standard
  37. foreach ($hints as $k=>$v) { // Not Coding Standard
  38. if ($v=="date"){ //or select an optimizer based on value in $v // Not Coding Standard
  39. $this->dateOpt->setTable($info->getMeta("type")); // Not Coding Standard
  40. $this->dateOpt->setColumn($k); // Not Coding Standard
  41. $this->dateOpt->setValue($info->$k); // Not Coding Standard
  42. $this->dateOpt->optimize(); // Not Coding Standard
  43. } // Not Coding Standard
  44. } // Not Coding Standard
  45. }
  46. }
  47. }
  48. // This is for testing details of how RedBean works.
  49. class RedBeanTest extends BaseTest
  50. {
  51. public function testZeros()
  52. {
  53. $thing = R::dispense('thing');
  54. $thing->zero = 0;
  55. R::store($thing);
  56. $id = $thing->id;
  57. unset($thing);
  58. $thing = R::load('thing', $id);
  59. $this->assertEquals(0, $thing->zero);
  60. //Try saving a second thing.
  61. $thing = R::dispense('thing');
  62. $thing->zero = 2;
  63. R::store($thing);
  64. $id = $thing->id;
  65. unset($thing);
  66. $thing = R::load('thing', $id);
  67. $this->assertEquals(2, $thing->zero);
  68. }
  69. public function testNulls()
  70. {
  71. $thing = R::dispense('thing');
  72. $thing->zero = null;
  73. R::store($thing);
  74. $id = $thing->id;
  75. unset($thing);
  76. $thing = R::load('thing', $id);
  77. $this->assertEquals(null, $thing->zero);
  78. }
  79. public function testGetAllTableReturnsNullOn5_2()
  80. {
  81. $sql = 'select id from atableneverhere';
  82. $rows = R::getAll($sql);
  83. if (version_compare(PHP_VERSION, '5.3.0', '>'))
  84. {
  85. $this->assertTrue(is_array($rows));
  86. }
  87. else
  88. {
  89. $this->assertTrue($rows === null);
  90. }
  91. }
  92. public function testStringContainingOnlyNumbers()
  93. {
  94. $thing = R::dispense('thing');
  95. $thing->phoneNumberNumber = 5551234;
  96. $thing->phoneNumberString1 = '555-1234';
  97. $thing->phoneNumberString2 = '5551234';
  98. R::store($thing);
  99. $databaseType = R::$toolbox->getDatabaseAdapter()->getDatabase()->getDatabaseType();
  100. switch ($databaseType)
  101. {
  102. case 'mysql':
  103. $sql = 'desc thing;';
  104. $rows = R::getAll($sql);
  105. $this->assertEquals('phoneNumberNumber', $rows[2]['Field']);
  106. $this->assertEquals('int(11) unsigned', $rows[2]['Type']);
  107. $this->assertEquals('phoneNumberString1', $rows[3]['Field']);
  108. $this->assertEquals('varchar(255)', $rows[3]['Type']);
  109. $this->assertEquals('phoneNumberString2', $rows[4]['Field']);
  110. $this->assertEquals('int(11) unsigned', $rows[4]['Type']);
  111. break;
  112. case 'sqlite':
  113. $sql = 'pragma table_info(\'thing\');';
  114. $rows = R::getAll($sql);
  115. $this->assertEquals('phoneNumberNumber', $rows[2]['name']);
  116. $this->assertEquals('INTEGER', $rows[2]['type']);
  117. $this->assertEquals('phoneNumberString1', $rows[3]['name']);
  118. $this->assertEquals('TEXT', $rows[3]['type']);
  119. $this->assertEquals('phoneNumberString2', $rows[4]['name']);
  120. $this->assertEquals('INTEGER', $rows[4]['type']);
  121. break;
  122. case 'pgsql':
  123. $sql = 'select column_name, data_type from information_schema.columns where table_name = \'thing\' and column_name like \'phone%\' order by column_name;';
  124. $rows = R::getAll($sql);
  125. $this->assertEquals('phonenumbernumber', $rows[0]['column_name']);
  126. $this->assertEquals('integer', $rows[0]['data_type']);
  127. $this->assertEquals('phonenumberstring1', $rows[1]['column_name']);
  128. $this->assertEquals('text', $rows[1]['data_type']);
  129. $this->assertEquals('phonenumberstring2', $rows[2]['column_name']);
  130. $this->assertEquals('text', $rows[2]['data_type']);
  131. break;
  132. default:
  133. $this->fail('Test does not support database type: ' . $databaseType);
  134. }
  135. }
  136. public function testRedBeanTypesShowingPDODodginess()
  137. {
  138. $wukka = R::dispense('wukka');
  139. $wukka->integer = 69;
  140. $wukka->string = 'xxx';
  141. R::store($wukka);
  142. $this->assertEquals('integer', gettype($wukka->integer));
  143. $this->assertEquals('string', gettype($wukka->string));
  144. $this->assertTrue ($wukka->integer !== $wukka->string);
  145. $id = $wukka->id;
  146. unset($wukka);
  147. $databaseType = R::$toolbox->getDatabaseAdapter()->getDatabase()->getDatabaseType();
  148. switch ($databaseType)
  149. {
  150. case 'mysql':
  151. $sql = 'desc wukka;';
  152. $rows = R::getAll($sql);
  153. $this->assertEquals('integer', $rows[1]['Field']);
  154. $this->assertEquals('tinyint(3) unsigned', $rows[1]['Type']);
  155. $this->assertEquals('string', $rows[2]['Field']);
  156. $this->assertEquals('varchar(255)', $rows[2]['Type']);
  157. break;
  158. }
  159. $wukka = R::load('wukka', $id);
  160. $this->assertEquals('string', gettype($wukka->integer)); // Dodgy.
  161. $this->assertEquals('string', gettype($wukka->string));
  162. $this->assertTrue ($wukka->integer !== $wukka->string);
  163. }
  164. public function testGetBeanWhenThereIsNoneToGet()
  165. {
  166. $bean = R::dispense('a');
  167. $bean2 = R::getBean($bean, 'b');
  168. if (!method_exists('R', 'getVersion') ||
  169. substr(R::getVersion(), 0, 3) == '1.2')
  170. {
  171. $this->assertTrue($bean2 !== null);
  172. }
  173. else
  174. {
  175. $this->assertEquals('1.3', substr(R::getVersion(), 0, 3));
  176. $this->assertTrue($bean2 === null);
  177. }
  178. }
  179. public function testUniqueMeta()
  180. {
  181. $bean = R::dispense('hombre');
  182. if (!method_exists('R', 'getVersion') ||
  183. substr(R::getVersion(), 0, 3) == '1.2')
  184. {
  185. $bean->setMeta("buildcommand.unique.0", array( "nombre") );
  186. }
  187. else
  188. {
  189. $this->assertEquals('1.3', substr(R::getVersion(), 0, 3));
  190. $bean->setMeta("buildcommand.unique", array(array("nombre")));
  191. }
  192. $bean->nombre = 'Pablo';
  193. R::store($bean);
  194. $bean2 = R::dispense('hombre');
  195. $bean2->nombre = 'Pablo';
  196. try
  197. {
  198. R::store($bean2);
  199. $this->fail('Expected a RedBean_Exception_SQL: Integrity constraint violation');
  200. }
  201. catch (RedBean_Exception_SQL $e)
  202. {
  203. $message = "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Pablo'";
  204. $this->assertEquals($message, substr($e->getMessage(), 0, strlen($message)));
  205. }
  206. }
  207. public function testExampleStoredProcedure()
  208. {
  209. $wukka = R::dispense('wukka');
  210. $wukka->integer = 666;
  211. $wukka->string = 'yyy';
  212. R::store($wukka);
  213. try
  214. {
  215. R::exec("drop procedure get_wukka_integer");
  216. }
  217. catch (Exception $e)
  218. {
  219. }
  220. R::exec("
  221. create procedure get_wukka_integer(in the_string varchar(255), out the_integer int(11))
  222. begin
  223. select wukka.integer
  224. into the_integer
  225. from wukka
  226. where wukka.string = the_string;
  227. end
  228. ");
  229. R::exec("call get_wukka_integer('yyy', @the_integer)");
  230. $this->assertEquals(666, R::getCell("select @the_integer"));
  231. }
  232. /**
  233. * @depends testExampleStoredProcedure
  234. */
  235. public function testExampleStoredFunction()
  236. {
  237. try
  238. {
  239. R::exec("drop function get_wukka_integer2");
  240. }
  241. catch (Exception $e)
  242. {
  243. }
  244. R::exec("
  245. create function get_wukka_integer2(the_string varchar(255))
  246. returns int(11)
  247. begin
  248. declare the_integer int(11);
  249. select wukka.integer
  250. into the_integer
  251. from wukka
  252. where wukka.string = the_string;
  253. return the_integer;
  254. end
  255. ");
  256. $this->assertEquals(666, R::getCell("select get_wukka_integer2('yyy')"));
  257. }
  258. public function testCascadedDeleteDoesNotWorkForLinkedBeans()
  259. {
  260. $person = R::dispense('person');
  261. $person->name = 'bill';
  262. R::store($person);
  263. $phone = R::dispense('phone');
  264. $phone->number = '555-1234';
  265. R::store($phone);
  266. R::$linkManager->link($phone, $person);
  267. R::store($phone);
  268. // Either way this doesn't work.
  269. // RedBean_Plugin_Constraint::addConstraint($person, $phone);
  270. RedBean_Plugin_Constraint::addConstraint($phone, $person);
  271. $id = $phone->id;
  272. unset($phone);
  273. R::trash($person);
  274. unset($person);
  275. $phone = R::load('phone', $id);
  276. $this->assertNotNull($phone); // The phone is not deleted.
  277. }
  278. public function testDateTimeFields()
  279. {
  280. $toolbox = RedBean_Setup::kickstartDev(Yii::app()->db->connectionString,
  281. Yii::app()->db->username,
  282. Yii::app()->db->password);
  283. $optimizer = new RedBean_Plugin_Optimizer($toolbox);
  284. $optimizer->addOptimizer(new RedBean_Plugin_Optimizer_DateTime($toolbox));
  285. $redbean = $toolbox->getRedBean();
  286. $redbean->addEventListener('update', $optimizer);
  287. for ($i = 1; $i < 10; $i++)
  288. {
  289. $person = R::dispense("person");
  290. $person->name = "bill$i";
  291. $person->date1 = time();
  292. $person->date2 = date('Y-m-d H:i:s');
  293. $redbean->store($person);
  294. }
  295. // TODO: to be continued...
  296. }
  297. public function testDateTimeHinting()
  298. {
  299. $toolbox = RedBean_Setup::kickstartDev(Yii::app()->db->connectionString,
  300. Yii::app()->db->username,
  301. Yii::app()->db->password);
  302. // Copied directly from...
  303. // http://groups.google.com/group/redbeanorm/browse_thread/thread/7eb59797e8478a89/61eae7941cae1970
  304. $hint = new HintManager( R::$toolbox ); // Not Coding Standard
  305. R::$redbean->addEventListener( "after_update", $hint ); // Not Coding Standard
  306. R::exec("drop table if exists bean"); // Not Coding Standard
  307. $bean = R::dispense("bean"); // Not Coding Standard
  308. $bean->setMeta("hint",array("prop"=>"date")); // Not Coding Standard
  309. $bean->prop = "2010-01-01 10:00:00"; // Not Coding Standard
  310. R::store($bean); // Not Coding Standard
  311. $rows = R::getAll('desc bean');
  312. $this->assertEquals('prop', $rows[1]['Field']);
  313. $this->assertEquals('datetime', $rows[1]['Type']);
  314. }
  315. }
  316. ?>