/test/phpunit/CategorieTest.php

https://github.com/asterix14/dolibarr · PHP · 299 lines · 164 code · 43 blank · 92 comment · 2 complexity · 9bd05ae41a3285c1635b6a0235d78905 MD5 · raw file

  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /**
  19. * \file test/phpunit/CategorieTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/categories/class/categorie.class.php';
  29. require_once dirname(__FILE__).'/../../htdocs/product/class/product.class.php';
  30. if (empty($user->id))
  31. {
  32. print "Load permissions for admin user nb 1\n";
  33. $user->fetch(1);
  34. $user->getrights();
  35. }
  36. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  37. /**
  38. * Class for PHPUnit tests
  39. *
  40. * @backupGlobals disabled
  41. * @backupStaticAttributes enabled
  42. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  43. */
  44. class CategorieTest extends PHPUnit_Framework_TestCase
  45. {
  46. protected $savconf;
  47. protected $savuser;
  48. protected $savlangs;
  49. protected $savdb;
  50. /**
  51. * Constructor
  52. * We save global variables into local variables
  53. *
  54. * @return CategorieTest
  55. */
  56. function CategorieTest()
  57. {
  58. //$this->sharedFixture
  59. global $conf,$user,$langs,$db;
  60. $this->savconf=$conf;
  61. $this->savuser=$user;
  62. $this->savlangs=$langs;
  63. $this->savdb=$db;
  64. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  65. //print " - db ".$db->db;
  66. print "\n";
  67. }
  68. // Static methods
  69. public static function setUpBeforeClass()
  70. {
  71. global $conf,$user,$langs,$db;
  72. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  73. print __METHOD__."\n";
  74. }
  75. public static function tearDownAfterClass()
  76. {
  77. global $conf,$user,$langs,$db;
  78. $db->rollback();
  79. print __METHOD__."\n";
  80. }
  81. /**
  82. */
  83. protected function setUp()
  84. {
  85. global $conf,$user,$langs,$db;
  86. $conf=$this->savconf;
  87. $user=$this->savuser;
  88. $langs=$this->savlangs;
  89. $db=$this->savdb;
  90. print __METHOD__."\n";
  91. }
  92. /**
  93. */
  94. protected function tearDown()
  95. {
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. */
  100. public function testCategorieCreate()
  101. {
  102. global $conf,$user,$langs,$db;
  103. $conf=$this->savconf;
  104. $user=$this->savuser;
  105. $langs=$this->savlangs;
  106. $db=$this->savdb;
  107. $localobject=new Categorie($this->savdb);
  108. $localobject->initAsSpecimen();
  109. $result=$localobject->create($user);
  110. $this->assertLessThan($result, 0);
  111. print __METHOD__." result=".$result."\n";
  112. // TODO Add test on error when creating duplicate
  113. return $result;
  114. }
  115. /**
  116. * @depends testCategorieCreate
  117. * The depends says test is run only if previous is ok
  118. */
  119. public function testCategorieProduct($id)
  120. {
  121. global $conf,$user,$langs,$db;
  122. $conf=$this->savconf;
  123. $user=$this->savuser;
  124. $langs=$this->savlangs;
  125. $db=$this->savdb;
  126. $localobjecttmp=new Categorie($this->savdb);
  127. $localobjecttmp->initAsSpecimen();
  128. $localobjecttmp->label='Specimen Category for product';
  129. $localobjecttmp->type=0; // product category
  130. $catid=$localobjecttmp->create($user);
  131. print __METHOD__." catid=".$catid."\n";
  132. $this->assertLessThanOrEqual($catid, 0);
  133. // Category
  134. $localobject2=new Product($this->savdb);
  135. $localobject2->initAsSpecimen();
  136. $localobject2->ref.='-CATEG';
  137. $localobject2->tva_npr=1;
  138. $localobject2->catid=$catid;
  139. $result=$localobject2->create($user);
  140. print __METHOD__." result=".$result."\n";
  141. $this->assertLessThanOrEqual($result, 0);
  142. return $id;
  143. }
  144. /**
  145. * @depends testCategorieProduct
  146. * The depends says test is run only if previous is ok
  147. */
  148. public function testCategorieFetch($id)
  149. {
  150. global $conf,$user,$langs,$db;
  151. $conf=$this->savconf;
  152. $user=$this->savuser;
  153. $langs=$this->savlangs;
  154. $db=$this->savdb;
  155. $localobject=new Categorie($this->savdb);
  156. $result=$localobject->fetch($id);
  157. $this->assertLessThan($result, 0);
  158. print __METHOD__." id=".$id." result=".$result."\n";
  159. return $localobject;
  160. }
  161. /**
  162. * @depends testCategorieFetch
  163. * The depends says test is run only if previous is ok
  164. */
  165. public function testCategorieUpdate($localobject)
  166. {
  167. global $conf,$user,$langs,$db;
  168. $conf=$this->savconf;
  169. $user=$this->savuser;
  170. $langs=$this->savlangs;
  171. $db=$this->savdb;
  172. $localobject->note='New note after update';
  173. $result=$localobject->update($user);
  174. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  175. $this->assertLessThan($result, 0);
  176. return $localobject;
  177. }
  178. /**
  179. * @depends testCategorieUpdate
  180. * The depends says test is run only if previous is ok
  181. */
  182. /*public function testCategorieXXX($localobject)
  183. {
  184. global $conf,$user,$langs,$db;
  185. $conf=$this->savconf;
  186. $user=$this->savuser;
  187. $langs=$this->savlangs;
  188. $db=$this->savdb;
  189. $result=$localobject->delete(0);
  190. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  191. $this->assertLessThan($result, 0);
  192. return $localobject;
  193. }*/
  194. /**
  195. * @depends testCategorieUpdate
  196. * The depends says test is run only if previous is ok
  197. */
  198. public function testCategorieOther($localobject)
  199. {
  200. global $conf,$user,$langs,$db;
  201. $conf=$this->savconf;
  202. $user=$this->savuser;
  203. $langs=$this->savlangs;
  204. $db=$this->savdb;
  205. /*$result=$localobject->setstatus(0);
  206. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  207. $this->assertLessThan($result, 0);
  208. */
  209. $localobject2=new Categorie($db);
  210. $localobject2->initAsSpecimen();
  211. $retarray=$localobject->liste_photos('/');
  212. print __METHOD__." retarry size=".count($rearray)."\n";
  213. $this->assertTrue(is_array($retarray));
  214. $ret=$localobject->is_fille($localobject2);
  215. print __METHOD__." retarry size=".count($rearray)."\n";
  216. $this->assertFalse($ret);
  217. return $localobject->id;
  218. }
  219. /**
  220. * @depends testCategorieOther
  221. * The depends says test is run only if previous is ok
  222. */
  223. public function testCategorieDelete($id)
  224. {
  225. global $conf,$user,$langs,$db;
  226. $conf=$this->savconf;
  227. $user=$this->savuser;
  228. $langs=$this->savlangs;
  229. $db=$this->savdb;
  230. $localobject=new Categorie($this->savdb);
  231. $result=$localobject->fetch($id);
  232. $result=$localobject->delete($id);
  233. print __METHOD__." id=".$id." result=".$result."\n";
  234. $this->assertLessThan($result, 0);
  235. return $result;
  236. }
  237. /**
  238. * @depends testCategorieDelete
  239. */
  240. public function testCategorieStatic()
  241. {
  242. global $conf,$user,$langs,$db;
  243. $conf=$this->savconf;
  244. $user=$this->savuser;
  245. $langs=$this->savlangs;
  246. $db=$this->savdb;
  247. $localobject=new Categorie($this->savdb);
  248. $retarray=$localobject->get_full_arbo(3);
  249. print __METHOD__." retarray size=".count($retarray)."\n";
  250. $this->assertTrue(is_array($retarray));
  251. return $result;
  252. }
  253. }
  254. ?>