CommandeFournisseurTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 3 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, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/CommandeFournisseurTest.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/fourn/class/fournisseur.commande.class.php';
  29. require_once dirname(__FILE__).'/../../htdocs/fourn/class/fournisseur.product.class.php';
  30. if (empty($user->id)) {
  31. print "Load permissions for admin user nb 1\n";
  32. $user->fetch(1);
  33. $user->getrights();
  34. }
  35. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  36. /**
  37. * Class for PHPUnit tests
  38. *
  39. * @backupGlobals disabled
  40. * @backupStaticAttributes enabled
  41. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  42. */
  43. class CommandeFournisseurTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $savconf;
  46. protected $savuser;
  47. protected $savlangs;
  48. protected $savdb;
  49. /**
  50. * Constructor
  51. * We save global variables into local variables
  52. *
  53. * @return CommandeFournisseurTest
  54. */
  55. function __construct()
  56. {
  57. parent::__construct();
  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. // tear down after class
  76. public static function tearDownAfterClass()
  77. {
  78. global $conf,$user,$langs,$db;
  79. $db->rollback();
  80. print __METHOD__."\n";
  81. }
  82. /**
  83. * Init phpunit tests
  84. *
  85. * @return void
  86. */
  87. protected function setUp()
  88. {
  89. global $conf,$user,$langs,$db;
  90. $conf=$this->savconf;
  91. $user=$this->savuser;
  92. $langs=$this->savlangs;
  93. $db=$this->savdb;
  94. print __METHOD__."\n";
  95. //print $db->getVersion()."\n";
  96. }
  97. /**
  98. * End phpunit tests
  99. *
  100. * @return void
  101. */
  102. protected function tearDown()
  103. {
  104. print __METHOD__."\n";
  105. }
  106. /**
  107. * testCommandeFournisseurCreate
  108. *
  109. * @return void
  110. */
  111. public function testCommandeFournisseurCreate()
  112. {
  113. global $conf,$user,$langs,$db;
  114. $conf=$this->savconf;
  115. $user=$this->savuser;
  116. $langs=$this->savlangs;
  117. $db=$this->savdb;
  118. // Set supplier and product to use
  119. $socid=1;
  120. $societe=new Societe($db);
  121. $societe->fetch($socid);
  122. $product=new ProductFournisseur($db);
  123. $product->fetch(0,'PIDRESS');
  124. if ($product->id <= 0) { print "\n".__METHOD__." A product with ref PIDRESS must exists into database"; die(); }
  125. $quantity=10;
  126. $ref_fourn='SUPPLIER_REF_PHPUNIT';
  127. $tva_tx=19.6;
  128. // Create supplier price
  129. $result=$product->add_fournisseur($user, $societe->id, $ref_fourn, $quantity); // This insert record with no value for price. Values are update later with update_buyprice
  130. $this->assertGreaterThanOrEqual(1, $result);
  131. $result=$product->update_buyprice($quantity, 20, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0);
  132. $this->assertGreaterThanOrEqual(0, $result);
  133. // Create supplier order with a too low quantity
  134. $localobject=new CommandeFournisseur($db);
  135. $localobject->initAsSpecimen();
  136. $localobject->lines=array(); // Overwrite lines of order
  137. $line=new CommandeFournisseurLigne($db);
  138. $line->desc=$langs->trans("Description")." specimen line too low";
  139. $line->qty=1; // So lower than $quantity
  140. $line->fk_product=$product->id;
  141. $line->ref_fourn=$ref_fourn;
  142. $localobject->lines[]=$line;
  143. $result=$localobject->create($user);
  144. print __METHOD__." result=".$result."\n";
  145. $this->assertEquals(-1, $result); // must be -1 because quantity is lower than minimum of supplier price
  146. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''";
  147. $db->query($sql);
  148. // Create supplier order
  149. $localobject2=new CommandeFournisseur($db);
  150. $localobject2->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  151. $localobject2->lines=array(); // Overwrite lines of order
  152. $line=new CommandeFournisseurLigne($db);
  153. $line->desc=$langs->trans("Description")." specimen line ok";
  154. $line->qty=10; // So enough quantity
  155. $line->fk_product=$product->id;
  156. $line->ref_fourn=$ref_fourn;
  157. $localobject2->lines[]=$line;
  158. $result=$localobject2->create($user);
  159. print __METHOD__." result=".$result."\n";
  160. $this->assertGreaterThanOrEqual(0, $result);
  161. return $result;
  162. }
  163. /**
  164. * testCommandeFournisseurFetch
  165. *
  166. * @param int $id Id of supplier order
  167. * @return void
  168. *
  169. * @depends testCommandeFournisseurCreate
  170. * The depends says test is run only if previous is ok
  171. */
  172. public function testCommandeFournisseurFetch($id)
  173. {
  174. global $conf,$user,$langs,$db;
  175. $conf=$this->savconf;
  176. $user=$this->savuser;
  177. $langs=$this->savlangs;
  178. $db=$this->savdb;
  179. $localobject=new CommandeFournisseur($this->savdb);
  180. $result=$localobject->fetch($id);
  181. print __METHOD__." id=".$id." result=".$result."\n";
  182. $this->assertLessThan($result, 0);
  183. return $localobject;
  184. }
  185. /**
  186. * testCommandeFournisseurValid
  187. *
  188. * @param Object $localobject Supplier order
  189. * @return void
  190. *
  191. * @depends testCommandeFournisseurFetch
  192. * The depends says test is run only if previous is ok
  193. */
  194. public function testCommandeFournisseurValid($localobject)
  195. {
  196. global $conf,$user,$langs,$db;
  197. $conf=$this->savconf;
  198. $user=$this->savuser;
  199. $langs=$this->savlangs;
  200. $db=$this->savdb;
  201. $result=$localobject->valid($user);
  202. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  203. $this->assertLessThan($result, 0);
  204. return $localobject;
  205. }
  206. /**
  207. * testCommandeFournisseurApprove
  208. *
  209. * @param Object $localobject Supplier order
  210. * @return void
  211. *
  212. * @depends testCommandeFournisseurValid
  213. * The depends says test is run only if previous is ok
  214. */
  215. public function testCommandeFournisseurApprove($localobject)
  216. {
  217. global $conf,$user,$langs,$db;
  218. $conf=$this->savconf;
  219. $user=$this->savuser;
  220. $langs=$this->savlangs;
  221. $db=$this->savdb;
  222. $result=$localobject->approve($user);
  223. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  224. $this->assertLessThan($result, 0);
  225. return $localobject;
  226. }
  227. /**
  228. * testCommandeFournisseurCancel
  229. *
  230. * @param Object $localobject Supplier order
  231. * @return void
  232. *
  233. * @depends testCommandeFournisseurApprove
  234. * The depends says test is run only if previous is ok
  235. */
  236. public function testCommandeFournisseurCancel($localobject)
  237. {
  238. global $conf,$user,$langs,$db;
  239. $conf=$this->savconf;
  240. $user=$this->savuser;
  241. $langs=$this->savlangs;
  242. $db=$this->savdb;
  243. $result=$localobject->cancel($user);
  244. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  245. $this->assertLessThan($result, 0);
  246. return $localobject;
  247. }
  248. /**
  249. * testCommandeFournisseurOther
  250. *
  251. * @param Object $localobject Supplier order
  252. * @return void
  253. *
  254. * @depends testCommandeFournisseurCancel
  255. * The depends says test is run only if previous is ok
  256. */
  257. public function testCommandeFournisseurOther($localobject)
  258. {
  259. global $conf,$user,$langs,$db;
  260. $conf=$this->savconf;
  261. $user=$this->savuser;
  262. $langs=$this->savlangs;
  263. $db=$this->savdb;
  264. /*$result=$localobject->setstatus(0);
  265. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  266. $this->assertLessThan($result, 0);
  267. */
  268. /*$localobject->info($localobject->id);
  269. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  270. $this->assertNotEquals($localobject->date_creation, '');
  271. */
  272. return $localobject->id;
  273. }
  274. /**
  275. * testCommandeFournisseurDelete
  276. *
  277. * @param int $id Id of order
  278. * @return void
  279. *
  280. * @depends testCommandeFournisseurOther
  281. * The depends says test is run only if previous is ok
  282. */
  283. public function testCommandeFournisseurDelete($id)
  284. {
  285. global $conf,$user,$langs,$db;
  286. $conf=$this->savconf;
  287. $user=$this->savuser;
  288. $langs=$this->savlangs;
  289. $db=$this->savdb;
  290. $localobject=new CommandeFournisseur($this->savdb);
  291. $result=$localobject->fetch($id);
  292. $result=$localobject->delete($user);
  293. print __METHOD__." id=".$id." result=".$result."\n";
  294. $this->assertLessThan($result, 0);
  295. return $result;
  296. }
  297. }