MouvementStockTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/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. {
  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. * @return ContratTest
  56. */
  57. function __construct()
  58. {
  59. parent::__construct();
  60. //$this->sharedFixture
  61. global $conf,$user,$langs,$db;
  62. $this->savconf=$conf;
  63. $this->savuser=$user;
  64. $this->savlangs=$langs;
  65. $this->savdb=$db;
  66. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  67. //print " - db ".$db->db;
  68. print "\n";
  69. }
  70. // Static methods
  71. public static function setUpBeforeClass()
  72. {
  73. global $conf,$user,$langs,$db;
  74. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  75. print __METHOD__."\n";
  76. }
  77. // tear down after class
  78. public static function tearDownAfterClass()
  79. {
  80. global $conf,$user,$langs,$db;
  81. $db->rollback();
  82. print __METHOD__."\n";
  83. }
  84. /**
  85. * Init phpunit tests
  86. *
  87. * @return void
  88. */
  89. protected function setUp()
  90. {
  91. global $conf,$user,$langs,$db;
  92. $conf=$this->savconf;
  93. $user=$this->savuser;
  94. $langs=$this->savlangs;
  95. $db=$this->savdb;
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. * End phpunit tests
  100. *
  101. * @return void
  102. */
  103. protected function tearDown()
  104. {
  105. print __METHOD__."\n";
  106. }
  107. /**
  108. * testMouvementCreate
  109. *
  110. * @return int
  111. */
  112. public function testMouvementCreate()
  113. {
  114. global $conf,$user,$langs,$db;
  115. $conf=$this->savconf;
  116. $user=$this->savuser;
  117. $langs=$this->savlangs;
  118. $db=$this->savdb;
  119. // We create a product for tests
  120. $product1=new Product($db);
  121. $product1->initAsSpecimen();
  122. $product1->ref.=' 1';
  123. $product1->label.=' 1';
  124. $product1id=$product1->create($user);
  125. $product2=new Product($db);
  126. $product2->initAsSpecimen();
  127. $product2->ref.=' 2';
  128. $product2->label.=' 2';
  129. $product2id=$product2->create($user);
  130. // We create a product for tests
  131. $warehouse1=new Entrepot($db);
  132. $warehouse1->initAsSpecimen();
  133. $warehouse1->libelle.=' 1';
  134. $warehouse1->description.=' 1';
  135. $warehouse1id=$warehouse1->create($user);
  136. $warehouse2=new Entrepot($db);
  137. $warehouse2->initAsSpecimen();
  138. $warehouse2->libelle.=' 2';
  139. $warehouse2->description.=' 2';
  140. $warehouse2id=$warehouse2->create($user);
  141. $localobject=new MouvementStock($this->savdb);
  142. // Do a list of movement into warehouse 1
  143. // Create an input movement (type = 3) of price 9.9 -> shoul dupdate PMP to 9.9
  144. $result=$localobject->_create($user, $product1id, $warehouse1id, 10, 3, 9.9, 'Movement for unit test 1', 'Inventory Code Test');
  145. print __METHOD__." result=".$result."\n";
  146. $this->assertLessThan($result, 0);
  147. // Create an input movement (type = 3) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  148. $result=$localobject->_create($user, $product1id, $warehouse1id, 10, 3, 9.7, 'Movement for unit test 2', 'Inventory Code Test');
  149. print __METHOD__." result=".$result."\n";
  150. $this->assertLessThan($result, 0);
  151. // Create an output movement (type = 2) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  152. $result=$localobject->_create($user, $product1id, $warehouse1id, -5, 2, 999, 'Movement for unit test 3', 'Inventory Code Test');
  153. print __METHOD__." result=".$result."\n";
  154. $this->assertLessThan($result, 0);
  155. // Create an output movement (type = 1) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  156. $result=$localobject->_create($user, $product1id, $warehouse1id, 1, 0, 0, 'Input from transfer', 'Transfert X');
  157. print __METHOD__." result=".$result."\n";
  158. $this->assertLessThan($result, 0);
  159. // Create an output movement (type = 1) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  160. $result=$localobject->_create($user, $product1id, $warehouse1id, -2, 1, 0, 'Output from transfer', 'Transfert Y');
  161. print __METHOD__." result=".$result."\n";
  162. $this->assertLessThan($result, 0);
  163. // Do same but into warehouse 2
  164. // Create an input movement (type = 3) of price 9.9 -> shoul dupdate PMP to 9.9
  165. $result=$localobject->_create($user, $product1id, $warehouse2id, 10, 3, 9.9, 'Movement for unit test 1 wh 2', 'Inventory Code Test 2');
  166. print __METHOD__." result=".$result."\n";
  167. $this->assertLessThan($result, 0);
  168. // Create an input movement (type = 3) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  169. $result=$localobject->_create($user, $product1id, $warehouse2id, 10, 3, 9.7, 'Movement for unit test 2 wh 2', 'Inventory Code Test 2');
  170. print __METHOD__." result=".$result."\n";
  171. $this->assertLessThan($result, 0);
  172. // Create an output movement (type = 2) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  173. $result=$localobject->_create($user, $product1id, $warehouse2id, -5, 2, 999, 'Movement for unit test 3 wh 2', 'Inventory Code Test 2');
  174. print __METHOD__." result=".$result."\n";
  175. $this->assertLessThan($result, 0);
  176. // Create an output movement (type = 1) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  177. $result=$localobject->_create($user, $product1id, $warehouse2id, 1, 0, 0, 'Input from transfer wh 2', 'Transfert X 2');
  178. print __METHOD__." result=".$result."\n";
  179. $this->assertLessThan($result, 0);
  180. // Create an output movement (type = 1) of price 9.7 -> shoul dupdate PMP to 9.9/9.7 = 9.8
  181. $result=$localobject->_create($user, $product1id, $warehouse2id, -2, 1, 0, 'Output from transfer wh 2', 'Transfert Y 2');
  182. print __METHOD__." result=".$result."\n";
  183. $this->assertLessThan($result, 0);
  184. return $localobject;
  185. }
  186. /**
  187. * testMouvementCheck
  188. *
  189. * @param MouvementStock $localobject Movement object we created
  190. * @return int
  191. *
  192. * @depends testMouvementCreate
  193. * The depends says test is run only if previous is ok
  194. */
  195. public function testMouvementCheck($localobject)
  196. {
  197. global $conf,$user,$langs,$db;
  198. $conf=$this->savconf;
  199. $user=$this->savuser;
  200. $langs=$this->savlangs;
  201. $db=$this->savdb;
  202. $productid = $localobject->product_id;
  203. $warehouseid = $localobject->entrepot_id;
  204. print __METHOD__." productid=".$productid."\n";
  205. $producttotest = new Product($db);
  206. $producttotest->fetch($productid);
  207. print __METHOD__." producttotest->stock_reel=".$producttotest->stock_reel."\n";
  208. $this->assertEquals($producttotest->stock_reel, 28); // 28 is result of stock movement defined into testMouvementCreate
  209. print __METHOD__." producttotest->pmp=".$producttotest->pmp."\n";
  210. $this->assertEquals($producttotest->pmp, 9.8);
  211. return $localobject;
  212. }
  213. }