CommandeFournisseurTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. // Delete existing supplier prices
  129. // TODO
  130. // Create 1 supplier price with min qty = 10;
  131. $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
  132. $this->assertGreaterThanOrEqual(1, $result);
  133. $result=$product->update_buyprice($quantity, 20, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0);
  134. $this->assertGreaterThanOrEqual(0, $result);
  135. // Create supplier order with a too low quantity and option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is on
  136. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 1;
  137. $localobject=new CommandeFournisseur($db);
  138. $localobject->initAsSpecimen();
  139. $localobject->lines=array(); // Overwrite lines of order
  140. $line=new CommandeFournisseurLigne($db);
  141. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  142. $line->qty=1; // So lower than $quantity
  143. $line->subprice=100;
  144. $line->fk_product=$product->id;
  145. $line->ref_fourn=$ref_fourn;
  146. $localobject->lines[]=$line;
  147. $result=$localobject->create($user);
  148. print __METHOD__." result=".$result."\n";
  149. $this->assertEquals(-1, $result, 'Creation of too low quantity'); // must be -1 because quantity is lower than minimum of supplier price
  150. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''";
  151. $db->query($sql);
  152. // Create supplier order
  153. $localobject2=new CommandeFournisseur($db);
  154. $localobject2->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  155. $localobject2->lines=array(); // Overwrite lines of order
  156. $line=new CommandeFournisseurLigne($db);
  157. $line->desc=$langs->trans("Description")." specimen line ok";
  158. $line->qty=10; // So enough quantity
  159. $line->subprice=100;
  160. $line->fk_product=$product->id;
  161. $line->ref_fourn=$ref_fourn;
  162. $localobject2->lines[]=$line;
  163. $result=$localobject2->create($user);
  164. print __METHOD__." result=".$result."\n";
  165. $this->assertGreaterThan(0, $result);
  166. // Create supplier order with a too low quantity but option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is off
  167. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 0;
  168. $localobject3=new CommandeFournisseur($db);
  169. $localobject3->initAsSpecimen();
  170. $localobject3->lines=array(); // Overwrite lines of order
  171. $line=new CommandeFournisseurLigne($db);
  172. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  173. $line->qty=1; // So lower than $quantity
  174. $line->subprice=100;
  175. $line->fk_product=$product->id;
  176. $line->ref_fourn=$ref_fourn;
  177. $localobject3->lines[]=$line;
  178. $result=$localobject3->create($user);
  179. print __METHOD__." result=".$result."\n";
  180. $this->assertGreaterThan(0, $result, 'Creation of too low quantity should be ok'); // must be id of line because there is no test on minimum quantity
  181. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''";
  182. $db->query($sql);
  183. // Create supplier order
  184. $localobject4=new CommandeFournisseur($db);
  185. $localobject4->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  186. $localobject4->lines=array(); // Overwrite lines of order
  187. $line=new CommandeFournisseurLigne($db);
  188. $line->desc=$langs->trans("Description")." specimen line ok";
  189. $line->qty=10; // So enough quantity
  190. $line->subprice=100;
  191. $line->fk_product=$product->id;
  192. $line->ref_fourn=$ref_fourn;
  193. $localobject4->lines[]=$line;
  194. $result=$localobject4->create($user);
  195. print __METHOD__." result=".$result."\n";
  196. $this->assertGreaterThan(0, $result);
  197. return $result;
  198. }
  199. /**
  200. * testCommandeFournisseurFetch
  201. *
  202. * @param int $id Id of supplier order
  203. * @return void
  204. *
  205. * @depends testCommandeFournisseurCreate
  206. * The depends says test is run only if previous is ok
  207. */
  208. public function testCommandeFournisseurFetch($id)
  209. {
  210. global $conf,$user,$langs,$db;
  211. $conf=$this->savconf;
  212. $user=$this->savuser;
  213. $langs=$this->savlangs;
  214. $db=$this->savdb;
  215. $localobject=new CommandeFournisseur($this->savdb);
  216. $result=$localobject->fetch($id);
  217. print __METHOD__." id=".$id." result=".$result."\n";
  218. $this->assertLessThan($result, 0, 'Failed to fetch supplier order with id '.$id);
  219. return $localobject;
  220. }
  221. /**
  222. * testCommandeFournisseurValid
  223. *
  224. * @param Object $localobject Supplier order
  225. * @return void
  226. *
  227. * @depends testCommandeFournisseurFetch
  228. * The depends says test is run only if previous is ok
  229. */
  230. public function testCommandeFournisseurValid($localobject)
  231. {
  232. global $conf,$user,$langs,$db;
  233. $conf=$this->savconf;
  234. $user=$this->savuser;
  235. $langs=$this->savlangs;
  236. $db=$this->savdb;
  237. $result=$localobject->valid($user);
  238. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  239. $this->assertLessThan($result, 0);
  240. return $localobject;
  241. }
  242. /**
  243. * testCommandeFournisseurApprove
  244. *
  245. * @param Object $localobject Supplier order
  246. * @return void
  247. *
  248. * @depends testCommandeFournisseurValid
  249. * The depends says test is run only if previous is ok
  250. */
  251. public function testCommandeFournisseurApprove($localobject)
  252. {
  253. global $conf,$user,$langs,$db;
  254. $conf=$this->savconf;
  255. $user=$this->savuser;
  256. $langs=$this->savlangs;
  257. $db=$this->savdb;
  258. $result=$localobject->approve($user);
  259. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  260. $this->assertLessThan($result, 0);
  261. return $localobject;
  262. }
  263. /**
  264. * testCommandeFournisseurCancel
  265. *
  266. * @param Object $localobject Supplier order
  267. * @return void
  268. *
  269. * @depends testCommandeFournisseurApprove
  270. * The depends says test is run only if previous is ok
  271. */
  272. public function testCommandeFournisseurCancel($localobject)
  273. {
  274. global $conf,$user,$langs,$db;
  275. $conf=$this->savconf;
  276. $user=$this->savuser;
  277. $langs=$this->savlangs;
  278. $db=$this->savdb;
  279. $result=$localobject->cancel($user);
  280. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  281. $this->assertLessThan($result, 0);
  282. return $localobject;
  283. }
  284. /**
  285. * testCommandeFournisseurOther
  286. *
  287. * @param Object $localobject Supplier order
  288. * @return void
  289. *
  290. * @depends testCommandeFournisseurCancel
  291. * The depends says test is run only if previous is ok
  292. */
  293. public function testCommandeFournisseurOther($localobject)
  294. {
  295. global $conf,$user,$langs,$db;
  296. $conf=$this->savconf;
  297. $user=$this->savuser;
  298. $langs=$this->savlangs;
  299. $db=$this->savdb;
  300. /*$result=$localobject->setstatus(0);
  301. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  302. $this->assertLessThan($result, 0);
  303. */
  304. /*$localobject->info($localobject->id);
  305. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  306. $this->assertNotEquals($localobject->date_creation, '');
  307. */
  308. return $localobject->id;
  309. }
  310. /**
  311. * testCommandeFournisseurDelete
  312. *
  313. * @param int $id Id of order
  314. * @return void
  315. *
  316. * @depends testCommandeFournisseurOther
  317. * The depends says test is run only if previous is ok
  318. */
  319. public function testCommandeFournisseurDelete($id)
  320. {
  321. global $conf,$user,$langs,$db;
  322. $conf=$this->savconf;
  323. $user=$this->savuser;
  324. $langs=$this->savlangs;
  325. $db=$this->savdb;
  326. $localobject=new CommandeFournisseur($this->savdb);
  327. $result=$localobject->fetch($id);
  328. $result=$localobject->delete($user);
  329. print __METHOD__." id=".$id." result=".$result."\n";
  330. $this->assertLessThan($result, 0);
  331. return $result;
  332. }
  333. }