MouvementStockTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file test/phpunit/MouvementStockTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  29. require_once dirname(__FILE__).'/../../htdocs/product/stock/class/mouvementstock.class.php';
  30. require_once dirname(__FILE__).'/../../htdocs/product/stock/class/entrepot.class.php';
  31. require_once dirname(__FILE__).'/../../htdocs/product/class/product.class.php';
  32. if (empty($user->id)) {
  33. print "Load permissions for admin user nb 1\n";
  34. $user->fetch(1);
  35. $user->getrights();
  36. }
  37. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  38. /**
  39. * Class for PHPUnit tests
  40. *
  41. * @backupGlobals disabled
  42. * @backupStaticAttributes enabled
  43. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  44. */
  45. class MouvementStockTest extends PHPUnit\Framework\TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. /**
  52. * Constructor
  53. * We save global variables into local variables
  54. *
  55. * @param string $name Name
  56. * @return ContratTest
  57. */
  58. public function __construct($name = '')
  59. {
  60. parent::__construct($name);
  61. //$this->sharedFixture
  62. global $conf,$user,$langs,$db;
  63. $this->savconf=$conf;
  64. $this->savuser=$user;
  65. $this->savlangs=$langs;
  66. $this->savdb=$db;
  67. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  68. //print " - db ".$db->db;
  69. print "\n";
  70. }
  71. /**
  72. * setUpBeforeClass
  73. *
  74. * @return void
  75. */
  76. public static function setUpBeforeClass(): void
  77. {
  78. global $conf,$user,$langs,$db;
  79. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  80. print __METHOD__."\n";
  81. }
  82. /**
  83. * tearDownAfterClass
  84. *
  85. * @return void
  86. */
  87. public static function tearDownAfterClass(): void
  88. {
  89. global $conf,$user,$langs,$db;
  90. $db->rollback();
  91. print __METHOD__."\n";
  92. }
  93. /**
  94. * Init phpunit tests
  95. *
  96. * @return void
  97. */
  98. protected function setUp(): void
  99. {
  100. global $conf,$user,$langs,$db;
  101. $conf=$this->savconf;
  102. $user=$this->savuser;
  103. $langs=$this->savlangs;
  104. $db=$this->savdb;
  105. if (!isModEnabled('productbatch')) {
  106. print "\n".__METHOD__." module Lot/Serial must be enabled.\n"; die(1);
  107. }
  108. print __METHOD__."\n";
  109. }
  110. /**
  111. * End phpunit tests
  112. *
  113. * @return void
  114. */
  115. protected function tearDown(): void
  116. {
  117. print __METHOD__."\n";
  118. }
  119. /**
  120. * testMouvementCreate
  121. *
  122. * @return MouvementStock
  123. */
  124. public function testMouvementCreate()
  125. {
  126. global $conf,$user,$langs,$db;
  127. $conf=$this->savconf;
  128. $user=$this->savuser;
  129. $langs=$this->savlangs;
  130. $db=$this->savdb;
  131. // We create a product for tests
  132. $product0=new Product($db);
  133. $product0->initAsSpecimen();
  134. $product0->ref.=' phpunit 0';
  135. $product0->label.=' phpunit 0';
  136. $product0->status_batch = 1;
  137. $product0id=$product0->create($user);
  138. print __METHOD__." product0id=".$product0id."\n";
  139. $this->assertGreaterThan(0, $product0id, 'Failed to create product');
  140. $product1=new Product($db);
  141. $product1->initAsSpecimen();
  142. $product1->ref.=' phpunit 1';
  143. $product1->label.=' phpunit 1';
  144. $product1id=$product1->create($user);
  145. $product2=new Product($db);
  146. $product2->initAsSpecimen();
  147. $product2->ref.=' phpunit 2';
  148. $product2->label.=' phpunit 2';
  149. $product2id=$product2->create($user);
  150. // We create a product for tests
  151. $warehouse0=new Entrepot($db);
  152. $warehouse0->initAsSpecimen();
  153. $warehouse0->label.=' phpunit 0';
  154. $warehouse0->description.=' phpunit 0';
  155. $warehouse0->statut = 0;
  156. $warehouse0id=$warehouse0->create($user);
  157. $warehouse1=new Entrepot($db);
  158. $warehouse1->initAsSpecimen();
  159. $warehouse1->label.=' phpunit 1';
  160. $warehouse1->description.=' phpunit 1';
  161. $warehouse1id=$warehouse1->create($user);
  162. $warehouse2=new Entrepot($db);
  163. $warehouse2->initAsSpecimen();
  164. $warehouse2->label.=' phpunit 2';
  165. $warehouse2->description.=' phpunit 2';
  166. $warehouse2id=$warehouse2->create($user);
  167. $localobject=new MouvementStock($db);
  168. $datetest1 = dol_mktime(0, 0, 0, 1, 1, 2000);
  169. $datetest2 = dol_mktime(0, 0, 0, 1, 2, 2000);
  170. // Create an input movement movement (type = 3) with value for eatby date and a lot $datetest1
  171. $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest1, $datetest1, 'anotyetuselotnumberA', '', 0, 'Inventory Code Test with batch');
  172. print __METHOD__." result=".$result."\n";
  173. $this->assertGreaterThan(0, $result, 'Failed to create a movement with a lot number '.$datetest1.' for product id='.$product0id.' with status_batch=1');
  174. $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest1, $datetest1, 'anotyetuselotnumberB', '', 0, 'Inventory Code Test with batch');
  175. print __METHOD__." result=".$result."\n";
  176. $this->assertGreaterThan(0, $result, 'Test to check we can create a movement a eatby dare different when lot number is different');
  177. // Create same input movement movement (type = 3) with same lot but a different value of eatby date
  178. $result=$localobject->reception($user, $product0id, $warehouse0id, 5, 999, 'Movement for unit test with batch', $datetest2, $datetest1, 'anotyetuselotnumberA', '', 0, 'Inventory Code Test with batch');
  179. print __METHOD__." result=".$result."\n";
  180. $this->assertEquals(-3, $result, 'Test to check we can t create a movement for a lot with a different eatby date');
  181. // Do a list of movement into warehouse 1
  182. // Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9
  183. $result=$localobject->reception($user, $product1id, $warehouse1id, 10, 9.9, 'Movement for unit test 1', '', '', '', '', 0, 'Inventory Code Test');
  184. print __METHOD__." result=".$result."\n";
  185. $this->assertGreaterThan(0, $result, 'Return code of 0 was expected for the reception test 1');
  186. // Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  187. $result=$localobject->reception($user, $product1id, $warehouse1id, 10, 9.7, 'Movement for unit test 2', '', '', '', '', 0, 'Inventory Code Test');
  188. print __METHOD__." result=".$result."\n";
  189. $this->assertGreaterThan(0, $result);
  190. // Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  191. $result=$localobject->livraison($user, $product1id, $warehouse1id, 5, 999, 'Movement for unit test 3', '', '', '', '', 0, 'Inventory Code Test');
  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, 1, 0, 0, 'Input from transfer', 'Transfert X');
  196. print __METHOD__." result=".$result."\n";
  197. $this->assertGreaterThan(0, $result);
  198. // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  199. $result=$localobject->_create($user, $product1id, $warehouse1id, -2, 1, 0, 'Output from transfer', 'Transfert Y');
  200. print __METHOD__." result=".$result."\n";
  201. $this->assertGreaterThan(0, $result);
  202. // Do same but into warehouse 2
  203. // Create an input movement (type = 3) of price 9.9 -> should update PMP to 9.9
  204. $result=$localobject->reception($user, $product1id, $warehouse2id, 10, 9.9, 'Movement for unit test 1 wh 2', '', '', '', '', 0, 'Inventory Code Test 2');
  205. print __METHOD__." result=".$result."\n";
  206. $this->assertGreaterThan(0, $result);
  207. // Create an input movement (type = 3) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  208. $result=$localobject->reception($user, $product1id, $warehouse2id, 10, 9.7, 'Movement for unit test 2 wh 2', '', '', '', '', 0, 'Inventory Code Test 2');
  209. print __METHOD__." result=".$result."\n";
  210. $this->assertGreaterThan(0, $result);
  211. // Create an output movement (type = 2) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  212. $result=$localobject->livraison($user, $product1id, $warehouse2id, 5, 999, 'Movement for unit test 3 wh 2', '', '', '', '', 0, 'Inventory Code Test 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, 1, 0, 0, 'Input from transfer wh 2', 'Transfert X 2');
  217. print __METHOD__." result=".$result."\n";
  218. $this->assertGreaterThan(0, $result, 'Test create A');
  219. // Create an output movement (type = 1) of price 9.7 -> should update PMP to 9.9/9.7 = 9.8
  220. $result=$localobject->_create($user, $product1id, $warehouse2id, -2, 1, 0, 'Output from transfer wh 2', 'Transfert Y 2');
  221. print __METHOD__." result=".$result."\n";
  222. $this->assertGreaterThan(0, $result, 'Test create B');
  223. return $localobject;
  224. }
  225. /**
  226. * testMouvementCheck
  227. *
  228. * @param MouvementStock $localobject Movement object we created
  229. * @return MouvementStock
  230. *
  231. * @depends testMouvementCreate
  232. * The depends says test is run only if previous is ok
  233. */
  234. public function testMouvementCheck($localobject)
  235. {
  236. global $conf,$user,$langs,$db;
  237. $conf=$this->savconf;
  238. $user=$this->savuser;
  239. $langs=$this->savlangs;
  240. $db=$this->savdb;
  241. $productid = $localobject->product_id;
  242. $warehouseid = $localobject->entrepot_id;
  243. print __METHOD__." productid=".$productid."\n";
  244. $producttotest = new Product($db);
  245. $producttotest->fetch($productid);
  246. print __METHOD__." producttotest->stock_reel=".$producttotest->stock_reel."\n";
  247. $this->assertEquals($producttotest->stock_reel, 28); // 28 is result of stock movement defined into testMouvementCreate
  248. print __METHOD__." producttotest->pmp=".$producttotest->pmp."\n";
  249. $this->assertEquals($producttotest->pmp, 9.8);
  250. return $localobject;
  251. }
  252. }