CommandeFournisseurTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <https://www.gnu.org/licenses/>.
  16. * or see https://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. public 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. /**
  69. * setUpBeforeClass
  70. *
  71. * @return void
  72. */
  73. public static function setUpBeforeClass()
  74. {
  75. global $conf,$user,$langs,$db;
  76. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  77. print __METHOD__."\n";
  78. }
  79. /**
  80. * tearDownAfterClass
  81. *
  82. * @return void
  83. */
  84. public static function tearDownAfterClass()
  85. {
  86. global $conf,$user,$langs,$db;
  87. $db->rollback();
  88. print __METHOD__."\n";
  89. }
  90. /**
  91. * Init phpunit tests
  92. *
  93. * @return void
  94. */
  95. protected function setUp()
  96. {
  97. global $conf,$user,$langs,$db;
  98. $conf=$this->savconf;
  99. $user=$this->savuser;
  100. $langs=$this->savlangs;
  101. $db=$this->savdb;
  102. print __METHOD__."\n";
  103. //print $db->getVersion()."\n";
  104. }
  105. /**
  106. * End phpunit tests
  107. *
  108. * @return void
  109. */
  110. protected function tearDown()
  111. {
  112. print __METHOD__."\n";
  113. }
  114. /**
  115. * testCommandeFournisseurCreate
  116. *
  117. * @return int ID of purchase order
  118. */
  119. public function testCommandeFournisseurCreate()
  120. {
  121. global $conf,$user,$langs,$db;
  122. $conf=$this->savconf;
  123. $user=$this->savuser;
  124. $langs=$this->savlangs;
  125. $db=$this->savdb;
  126. // Set supplier and product to use
  127. $socid=1;
  128. $societe=new Societe($db);
  129. $societe->fetch($socid);
  130. $product=new ProductFournisseur($db);
  131. $product->fetch(0, 'PINKDRESS');
  132. if ($product->id <= 0) {
  133. print "\n".__METHOD__." A product with ref PINKDRESS must exists into database"; die(1);
  134. }
  135. $quantity=10;
  136. $ref_fourn='SUPPLIER_REF_PHPUNIT';
  137. $tva_tx=19.6;
  138. // Delete existing supplier prices
  139. // TODO
  140. // Create 1 supplier price with min qty = 10;
  141. $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
  142. print __METHOD__." add_fournisseur result=".$result."\n";
  143. $this->assertGreaterThanOrEqual(0, $result, 'Create 1 supplier price with min qty = 10 if not exists');
  144. $result=$product->update_buyprice($quantity, 20, $user, 'HT', $societe, '', $ref_fourn, $tva_tx, 0, 0);
  145. print __METHOD__." update_buyprice result=".$result."\n";
  146. $this->assertGreaterThanOrEqual(0, $result, 'Update buyprice');
  147. // Create purchase order with a too low quantity and option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is on
  148. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 1;
  149. $localobject=new CommandeFournisseur($db);
  150. $localobject->initAsSpecimen();
  151. $localobject->lines=array(); // Overwrite lines of order
  152. $line=new CommandeFournisseurLigne($db);
  153. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  154. $line->qty=1; // So lower than $quantity
  155. $line->subprice=100;
  156. $line->fk_product=$product->id;
  157. $line->ref_fourn=$ref_fourn;
  158. $localobject->lines[]=$line;
  159. $result=$localobject->create($user);
  160. print __METHOD__." result=".$result."\n";
  161. $this->assertEquals(-1, $result, 'Creation of too low quantity'); // must be -1 because quantity is lower than minimum of supplier price
  162. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref IN ('', '(PROV)')";
  163. $db->query($sql);
  164. // Create purchase order
  165. $localobject2=new CommandeFournisseur($db);
  166. $localobject2->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  167. $localobject2->lines=array(); // Overwrite lines of order
  168. $line=new CommandeFournisseurLigne($db);
  169. $line->desc=$langs->trans("Description")." specimen line ok";
  170. $line->qty=10; // So enough quantity
  171. $line->subprice=100;
  172. $line->fk_product=$product->id;
  173. $line->ref_fourn=$ref_fourn;
  174. $localobject2->lines[]=$line;
  175. $result=$localobject2->create($user);
  176. print __METHOD__." result=".$result."\n";
  177. $this->assertGreaterThan(0, $result);
  178. // Create purchase order with a too low quantity but option SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY is off
  179. $conf->global->SUPPLIER_ORDER_WITH_PREDEFINED_PRICES_ONLY = 0;
  180. $localobject3=new CommandeFournisseur($db);
  181. $localobject3->initAsSpecimen();
  182. $localobject3->lines=array(); // Overwrite lines of order
  183. $line=new CommandeFournisseurLigne($db);
  184. $line->desc=$langs->trans("Description")." specimen line with qty too low";
  185. $line->qty=1; // So lower than $quantity
  186. $line->subprice=100;
  187. $line->fk_product=$product->id;
  188. $line->ref_fourn=$ref_fourn;
  189. $localobject3->lines[]=$line;
  190. $result=$localobject3->create($user);
  191. print __METHOD__." result=".$result."\n";
  192. $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
  193. $sql="DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur where ref=''";
  194. $db->query($sql);
  195. // Create purchase order
  196. $localobject4=new CommandeFournisseur($db);
  197. $localobject4->initAsSpecimen(); // This create 5 lines of first product found for socid 1
  198. $localobject4->lines=array(); // Overwrite lines of order
  199. $line=new CommandeFournisseurLigne($db);
  200. $line->desc=$langs->trans("Description")." specimen line ok";
  201. $line->qty=10; // So enough quantity
  202. $line->subprice=100;
  203. $line->fk_product=$product->id;
  204. $line->ref_fourn=$ref_fourn;
  205. $localobject4->lines[]=$line;
  206. $result=$localobject4->create($user);
  207. print __METHOD__." id for purchase order created by testCommandeFournisseurCreate = ".$result."\n";
  208. $this->assertGreaterThan(0, $result, 'Test to create a purchase order by testCommandeFournisseurCreate');
  209. return $result;
  210. }
  211. /**
  212. * testCommandeFournisseurFetch
  213. *
  214. * @param int $id Id of purchase order
  215. * @return CommandeFournisseur Purchase order
  216. *
  217. * @depends testCommandeFournisseurCreate
  218. * The depends says test is run only if previous is ok
  219. */
  220. public function testCommandeFournisseurFetch($id)
  221. {
  222. global $conf,$user,$langs,$db;
  223. $conf=$this->savconf;
  224. $user=$this->savuser;
  225. $langs=$this->savlangs;
  226. $db=$this->savdb;
  227. $localobject=new CommandeFournisseur($this->savdb);
  228. $result=$localobject->fetch($id);
  229. print __METHOD__." id=".$id." result=".$result."\n";
  230. $this->assertLessThan($result, 0, 'Failed to fetch supplier order with id '.$id);
  231. return $localobject;
  232. }
  233. /**
  234. * testCommandeFournisseurValid
  235. *
  236. * @param CommandeFournisseur $localobject Supplier order
  237. * @return CommandeFournisseur Supplier order
  238. *
  239. * @depends testCommandeFournisseurFetch
  240. * The depends says test is run only if previous is ok
  241. */
  242. public function testCommandeFournisseurValid($localobject)
  243. {
  244. global $conf,$user,$langs,$db;
  245. $conf=$this->savconf;
  246. $user=$this->savuser;
  247. $langs=$this->savlangs;
  248. $db=$this->savdb;
  249. $result=$localobject->valid($user);
  250. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  251. $this->assertLessThan($result, 0);
  252. return $localobject;
  253. }
  254. /**
  255. * testCommandeFournisseurApprove
  256. *
  257. * @param CommandeFournisseur $localobject Supplier order
  258. * @return CommandeFournisseur Supplier order
  259. *
  260. * @depends testCommandeFournisseurValid
  261. * The depends says test is run only if previous is ok
  262. */
  263. public function testCommandeFournisseurApprove($localobject)
  264. {
  265. global $conf,$user,$langs,$db;
  266. $conf=$this->savconf;
  267. $user=$this->savuser;
  268. $langs=$this->savlangs;
  269. $db=$this->savdb;
  270. $result=$localobject->approve($user);
  271. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  272. $this->assertLessThan($result, 0);
  273. return $localobject;
  274. }
  275. /**
  276. * testCommandeFournisseurCancel
  277. *
  278. * @param CommandeFournisseur $localobject Supplier order
  279. * @return CommandeFournisseur Supplier order
  280. *
  281. * @depends testCommandeFournisseurApprove
  282. * The depends says test is run only if previous is ok
  283. */
  284. public function testCommandeFournisseurCancel($localobject)
  285. {
  286. global $conf,$user,$langs,$db;
  287. $conf=$this->savconf;
  288. $user=$this->savuser;
  289. $langs=$this->savlangs;
  290. $db=$this->savdb;
  291. $result=$localobject->cancel($user);
  292. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  293. $this->assertLessThan($result, 0);
  294. return $localobject;
  295. }
  296. /**
  297. * testCommandeFournisseurOther
  298. *
  299. * @param CommandeFournisseur $localobject Supplier order
  300. * @return int Id of purchase order
  301. *
  302. * @depends testCommandeFournisseurCancel
  303. * The depends says test is run only if previous is ok
  304. */
  305. public function testCommandeFournisseurOther($localobject)
  306. {
  307. global $conf,$user,$langs,$db;
  308. $conf=$this->savconf;
  309. $user=$this->savuser;
  310. $langs=$this->savlangs;
  311. $db=$this->savdb;
  312. /*$result=$localobject->setstatus(0);
  313. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  314. $this->assertLessThan($result, 0);
  315. */
  316. /*$localobject->info($localobject->id);
  317. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  318. $this->assertNotEquals($localobject->date_creation, '');
  319. */
  320. $this->assertEquals(1, 1);
  321. return $localobject->id;
  322. }
  323. /**
  324. * testCommandeFournisseurDelete
  325. *
  326. * @param int $id Id of order
  327. * @return int Result of delete
  328. *
  329. * @depends testCommandeFournisseurOther
  330. * The depends says test is run only if previous is ok
  331. */
  332. public function testCommandeFournisseurDelete($id)
  333. {
  334. global $conf,$user,$langs,$db;
  335. $conf=$this->savconf;
  336. $user=$this->savuser;
  337. $langs=$this->savlangs;
  338. $db=$this->savdb;
  339. $localobject=new CommandeFournisseur($this->savdb);
  340. $result=$localobject->fetch($id);
  341. $result=$localobject->delete($user);
  342. print __METHOD__." id=".$id." result=".$result."\n";
  343. $this->assertLessThan($result, 0);
  344. return $result;
  345. }
  346. }