MouvementStockTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/MouvementStockTest.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/product/stock/class/mouvementstock.class.php';
  29. require_once dirname(__FILE__).'/../../htdocs/product/stock/class/entrepot.class.php';
  30. require_once dirname(__FILE__).'/../../htdocs/product/class/product.class.php';
  31. if (empty($user->id)) {
  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 MouvementStockTest 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 ContratTest
  55. */
  56. public function __construct()
  57. {
  58. parent::__construct();
  59. //$this->sharedFixture
  60. global $conf,$user,$langs,$db;
  61. $this->savconf=$conf;
  62. $this->savuser=$user;
  63. $this->savlangs=$langs;
  64. $this->savdb=$db;
  65. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  66. //print " - db ".$db->db;
  67. print "\n";
  68. }
  69. /**
  70. * setUpBeforeClass
  71. *
  72. * @return void
  73. */
  74. public static function setUpBeforeClass()
  75. {
  76. global $conf,$user,$langs,$db;
  77. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  78. print __METHOD__."\n";
  79. }
  80. /**
  81. * tearDownAfterClass
  82. *
  83. * @return void
  84. */
  85. public static function tearDownAfterClass()
  86. {
  87. global $conf,$user,$langs,$db;
  88. $db->rollback();
  89. print __METHOD__."\n";
  90. }
  91. /**
  92. * Init phpunit tests
  93. *
  94. * @return void
  95. */
  96. protected function setUp()
  97. {
  98. global $conf,$user,$langs,$db;
  99. $conf=$this->savconf;
  100. $user=$this->savuser;
  101. $langs=$this->savlangs;
  102. $db=$this->savdb;
  103. if (empty($conf->productbatch->enabled)) {
  104. print "\n".__METHOD__." module Lot/Serial must be enabled.\n"; die(1);
  105. }
  106. print __METHOD__."\n";
  107. }
  108. /**
  109. * End phpunit tests
  110. *
  111. * @return void
  112. */
  113. protected function tearDown()
  114. {
  115. print __METHOD__."\n";
  116. }
  117. /**
  118. * testMouvementCreate
  119. *
  120. * @return MouvementStock
  121. */
  122. public function testMouvementCreate()
  123. {
  124. global $conf,$user,$langs,$db;
  125. $conf=$this->savconf;
  126. $user=$this->savuser;
  127. $langs=$this->savlangs;
  128. $db=$this->savdb;
  129. // We create a product for tests
  130. $product0=new Product($db);
  131. $product0->initAsSpecimen();
  132. $product0->ref.=' 0';
  133. $product0->label.=' 0';
  134. $product0->status_batch = 1;
  135. $product0id=$product0->create($user);
  136. $product1=new Product($db);
  137. $product1->initAsSpecimen();
  138. $product1->ref.=' 1';
  139. $product1->label.=' 1';
  140. $product1id=$product1->create($user);
  141. $product2=new Product($db);
  142. $product2->initAsSpecimen();
  143. $product2->ref.=' 2';
  144. $product2->label.=' 2';
  145. $product2id=$product2->create($user);
  146. // We create a product for tests
  147. $warehouse0=new Entrepot($db);
  148. $warehouse0->initAsSpecimen();
  149. $warehouse0->label.=' 0';
  150. $warehouse0->description.=' 0';
  151. $warehouse0->statut = 0;
  152. $warehouse0id=$warehouse0->create($user);
  153. $warehouse1=new Entrepot($db);
  154. $warehouse1->initAsSpecimen();
  155. $warehouse1->label.=' 1';
  156. $warehouse1->description.=' 1';
  157. $warehouse1id=$warehouse1->create($user);
  158. $warehouse2=new Entrepot($db);
  159. $warehouse2->initAsSpecimen();
  160. $warehouse2->label.=' 2';
  161. $warehouse2->description.=' 2';
  162. $warehouse2id=$warehouse2->create($user);
  163. $localobject=new MouvementStock($this->savdb);
  164. $datetest1 = dol_mktime(0, 0, 0, 1, 1, 2000);
  165. $datetest2 = dol_mktime(0, 0, 0, 1, 2, 2000);
  166. // Create an input movement movement (type = 3) with value for eatby date and a lot
  167. $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest1, $datetest1, 'anotyetuselotnumberA', '', 0, 'Inventory Code Test with batch');
  168. print __METHOD__." result=".$result."\n";
  169. $this->assertGreaterThan(0, $result, 'Failed to create a movement with a lot number of product with status_batch=1');
  170. $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest1, $datetest1, 'anotyetuselotnumberB', '', 0, 'Inventory Code Test with batch');
  171. print __METHOD__." result=".$result."\n";
  172. $this->assertGreaterThan(0, $result, 'Test to check we can create a movement a eatby dare different when lot number is different');
  173. // Create same input movement movement (type = 3) with same lot but a different value of eatby date
  174. $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest2, $datetest1, 'anotyetuselotnumberA', '', 0, 'Inventory Code Test with batch');
  175. print __METHOD__." result=".$result."\n";
  176. $this->assertEquals(-3, $result, 'Test to check we can t create a movement for a lot with a different eatby date');
  177. // Do a list of movement into warehouse 1
  178. // Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9
  179. $result=$localobject->reception($user, $product1id, $warehouse1id, 10, 9.9, 'Movement for unit test 1', '', '', '', '', 0, 'Inventory Code Test');
  180. print __METHOD__." result=".$result."\n";
  181. $this->assertGreaterThan(0, $result, 'Return code of 0 was expected for the reception test 1');
  182. // Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  183. $result=$localobject->reception($user, $product1id, $warehouse1id, 10, 9.7, 'Movement for unit test 2', '', '', '', '', 0, 'Inventory Code Test');
  184. print __METHOD__." result=".$result."\n";
  185. $this->assertGreaterThan(0, $result);
  186. // Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  187. $result=$localobject->livraison($user, $product1id, $warehouse1id, 5, 999, 'Movement for unit test 3', '', '', '', '', 0, 'Inventory Code Test');
  188. print __METHOD__." result=".$result."\n";
  189. $this->assertGreaterThan(0, $result);
  190. // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  191. $result=$localobject->_create($user, $product1id, $warehouse1id, 1, 0, 0, 'Input from transfer', 'Transfert X');
  192. print __METHOD__." result=".$result."\n";
  193. $this->assertGreaterThan(0, $result);
  194. // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  195. $result=$localobject->_create($user, $product1id, $warehouse1id, -2, 1, 0, 'Output from transfer', 'Transfert Y');
  196. print __METHOD__." result=".$result."\n";
  197. $this->assertGreaterThan(0, $result);
  198. // Do same but into warehouse 2
  199. // Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9
  200. $result=$localobject->reception($user, $product1id, $warehouse2id, 10, 9.9, 'Movement for unit test 1 wh 2', '', '', '', '', 0, 'Inventory Code Test 2');
  201. print __METHOD__." result=".$result."\n";
  202. $this->assertGreaterThan(0, $result);
  203. // Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  204. $result=$localobject->reception($user, $product1id, $warehouse2id, 10, 9.7, 'Movement for unit test 2 wh 2', '', '', '', '', 0, 'Inventory Code Test 2');
  205. print __METHOD__." result=".$result."\n";
  206. $this->assertGreaterThan(0, $result);
  207. // Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  208. $result=$localobject->livraison($user, $product1id, $warehouse2id, 5, 999, 'Movement for unit test 3 wh 2', '', '', '', '', 0, 'Inventory Code Test 2');
  209. print __METHOD__." result=".$result."\n";
  210. $this->assertGreaterThan(0, $result);
  211. // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  212. $result=$localobject->_create($user, $product1id, $warehouse2id, 1, 0, 0, 'Input from transfer wh 2', 'Transfert X 2');
  213. print __METHOD__." result=".$result."\n";
  214. $this->assertGreaterThan(0, $result);
  215. // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  216. $result=$localobject->_create($user, $product1id, $warehouse2id, -2, 1, 0, 'Output from transfer wh 2', 'Transfert Y 2');
  217. print __METHOD__." result=".$result."\n";
  218. $this->assertGreaterThan(0, $result);
  219. return $localobject;
  220. }
  221. /**
  222. * testMouvementCheck
  223. *
  224. * @param MouvementStock $localobject Movement object we created
  225. * @return MouvementStock
  226. *
  227. * @depends testMouvementCreate
  228. * The depends says test is run only if previous is ok
  229. */
  230. public function testMouvementCheck($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. $productid = $localobject->product_id;
  238. $warehouseid = $localobject->entrepot_id;
  239. print __METHOD__." productid=".$productid."\n";
  240. $producttotest = new Product($db);
  241. $producttotest->fetch($productid);
  242. print __METHOD__." producttotest->stock_reel=".$producttotest->stock_reel."\n";
  243. $this->assertEquals($producttotest->stock_reel, 28); // 28 is result of stock movement defined into testMouvementCreate
  244. print __METHOD__." producttotest->pmp=".$producttotest->pmp."\n";
  245. $this->assertEquals($producttotest->pmp, 9.8);
  246. return $localobject;
  247. }
  248. }