InventoryTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  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/InventoryTest.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/inventory/class/inventory.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 InventoryTest 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 InventoryTest
  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. }
  104. /**
  105. * End phpunit tests
  106. *
  107. * @return void
  108. */
  109. protected function tearDown()
  110. {
  111. print __METHOD__."\n";
  112. }
  113. /**
  114. * testInventoryCreate
  115. *
  116. * @return int
  117. */
  118. public function testInventoryCreate()
  119. {
  120. global $conf,$user,$langs,$db;
  121. $conf=$this->savconf;
  122. $user=$this->savuser;
  123. $langs=$this->savlangs;
  124. $db=$this->savdb;
  125. $localobject=new Inventory($db);
  126. $localobject->initAsSpecimen();
  127. $result=$localobject->create($user);
  128. $this->assertLessThan($result, 0);
  129. print __METHOD__." result=".$result."\n";
  130. return $result;
  131. }
  132. /**
  133. * testInventoryFetch
  134. *
  135. * @param int $id Id invoice
  136. * @return int
  137. *
  138. * @depends testInventoryCreate
  139. * The depends says test is run only if previous is ok
  140. */
  141. public function testInventoryFetch($id)
  142. {
  143. global $conf,$user,$langs,$db;
  144. $conf=$this->savconf;
  145. $user=$this->savuser;
  146. $langs=$this->savlangs;
  147. $db=$this->savdb;
  148. $localobject=new Inventory($this->savdb);
  149. $result=$localobject->fetch($id);
  150. $this->assertLessThan($result, 0);
  151. print __METHOD__." id=".$id." result=".$result."\n";
  152. return $localobject;
  153. }
  154. /**
  155. * testInventoryUpdate
  156. *
  157. * @param Inventory $localobject Invoice
  158. * @return int
  159. *
  160. * @depends testInventoryFetch
  161. * The depends says test is run only if previous is ok
  162. */
  163. public function testInventoryUpdate($localobject)
  164. {
  165. global $conf,$user,$langs,$db;
  166. $conf=$this->savconf;
  167. $user=$this->savuser;
  168. $langs=$this->savlangs;
  169. $db=$this->savdb;
  170. $localobject->status = 9;
  171. $localobject->title = 'test';
  172. $result=$localobject->update($user, $user);
  173. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  174. $this->assertLessThan($result, 0);
  175. return $localobject;
  176. }
  177. /**
  178. * testInventoryValidate
  179. *
  180. * @param Inventory $localobject Invoice
  181. * @return void
  182. *
  183. * @depends testInventoryUpdate
  184. * The depends says test is run only if previous is ok
  185. */
  186. public function testInventoryValidate($localobject)
  187. {
  188. global $conf,$user,$langs,$db;
  189. $conf=$this->savconf;
  190. $user=$this->savuser;
  191. $langs=$this->savlangs;
  192. $db=$this->savdb;
  193. $result=$localobject->validate($user);
  194. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  195. $this->assertLessThan($result, 0);
  196. $this->assertEquals($localobject->status, '1');
  197. return $localobject;
  198. }
  199. /**
  200. * testInventorySetDraft
  201. *
  202. * @param Inventory $localobject Invoice
  203. * @return void
  204. *
  205. * @depends testInventoryValidate
  206. * The depends says test is run only if previous is ok
  207. */
  208. public function testInventorySetDraft($localobject)
  209. {
  210. global $conf,$user,$langs,$db;
  211. $conf=$this->savconf;
  212. $user=$this->savuser;
  213. $langs=$this->savlangs;
  214. $db=$this->savdb;
  215. $result=$localobject->setDraft($user);
  216. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  217. $this->assertLessThan($result, 0);
  218. $this->assertEquals($localobject->status, '0');
  219. return $localobject;
  220. }
  221. /**
  222. * testInventorySetRecorded
  223. *
  224. * @param Inventory $localobject Invoice
  225. * @return void
  226. *
  227. * @depends testInventorySetDraft
  228. * The depends says test is run only if previous is ok
  229. */
  230. public function testInventorySetRecorded($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->setRecorded($user);
  238. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  239. $this->assertLessThan($result, 0);
  240. $this->assertEquals($localobject->status, '2');
  241. return $localobject;
  242. }
  243. /**
  244. * testInventorySetCanceled
  245. *
  246. * @param Inventory $localobject Invoice
  247. * @return void
  248. *
  249. * @depends testInventorySetRecorded
  250. * The depends says test is run only if previous is ok
  251. */
  252. public function testInventorySetCanceled($localobject)
  253. {
  254. global $conf,$user,$langs,$db;
  255. $conf=$this->savconf;
  256. $user=$this->savuser;
  257. $langs=$this->savlangs;
  258. $db=$this->savdb;
  259. $result=$localobject->setCanceled($user);
  260. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  261. $this->assertLessThan($result, 0);
  262. $this->assertEquals($localobject->status, '9');
  263. return $localobject;
  264. }
  265. /**
  266. * testInventoryOther
  267. *
  268. * @param Inventory $localobject Invoice
  269. * @return int
  270. * @depends testInventorySetRecorded
  271. * The depends says test is run only if previous is ok
  272. */
  273. public function testInventoryOther($localobject)
  274. {
  275. global $conf,$user,$langs,$db;
  276. $conf=$this->savconf;
  277. $user=$this->savuser;
  278. $langs=$this->savlangs;
  279. $db=$this->savdb;
  280. $localobject->info($localobject->id);
  281. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  282. $this->assertNotEquals($localobject->date_creation, '');
  283. return $localobject->id;
  284. }
  285. /**
  286. * testInventoryDelete
  287. *
  288. * @param int $id Id of invoice
  289. * @return int
  290. * @depends testInventoryOther
  291. * The depends says test is run only if previous is ok
  292. */
  293. public function testInventoryDelete($id)
  294. {
  295. global $conf,$user,$langs,$db;
  296. $conf=$this->savconf;
  297. $user=$this->savuser;
  298. $langs=$this->savlangs;
  299. $db=$this->savdb;
  300. $localobject=new Inventory($this->savdb);
  301. $result=$localobject->fetch($id);
  302. $result=$localobject->delete($user);
  303. print __METHOD__." id=".$id." result=".$result."\n";
  304. $this->assertLessThan($result, 0);
  305. return $result;
  306. }
  307. /**
  308. * Compare all public properties values of 2 objects
  309. *
  310. * @param Object $oA Object operand 1
  311. * @param Object $oB Object operand 2
  312. * @param boolean $ignoretype False will not report diff if type of value differs
  313. * @param array $fieldstoignorearray Array of fields to ignore in diff
  314. * @return array Array with differences
  315. */
  316. public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id'))
  317. {
  318. $retAr=array();
  319. if (get_class($oA) !== get_class($oB)) {
  320. $retAr[]="Supplied objects are not of same class.";
  321. } else {
  322. $oVarsA=get_object_vars($oA);
  323. $oVarsB=get_object_vars($oB);
  324. $aKeys=array_keys($oVarsA);
  325. foreach ($aKeys as $sKey) {
  326. if (in_array($sKey, $fieldstoignorearray)) {
  327. continue;
  328. }
  329. if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) {
  330. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  331. }
  332. if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) {
  333. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  334. }
  335. }
  336. }
  337. return $retAr;
  338. }
  339. }