BankAccountTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/BankAccounrTest.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/compta/bank/class/account.class.php';
  30. require_once dirname(__FILE__).'/../../htdocs/core/lib/bank.lib.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. $langs->load("main");
  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 BankAccountTest 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 BankAccountTest
  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. print __METHOD__."\n";
  106. }
  107. /**
  108. * End phpunit tests
  109. *
  110. * @return void
  111. */
  112. protected function tearDown(): void
  113. {
  114. print __METHOD__."\n";
  115. }
  116. /**
  117. * testBankAccountCreate
  118. *
  119. * @return int
  120. */
  121. public function testBankAccountCreate()
  122. {
  123. global $conf,$user,$langs,$db;
  124. $conf=$this->savconf;
  125. $user=$this->savuser;
  126. $langs=$this->savlangs;
  127. $db=$this->savdb;
  128. $localobject = new Account($db);
  129. $localobject->initAsSpecimen();
  130. $localobject->date_solde = dol_now();
  131. $result=$localobject->create($user);
  132. print __METHOD__." result=".$result."\n";
  133. $this->assertLessThan($result, 0);
  134. return $result;
  135. }
  136. /**
  137. * testBankAccountFetch
  138. *
  139. * @param int $id Id of contract
  140. * @return int
  141. *
  142. * @depends testBankAccountCreate
  143. * The depends says test is run only if previous is ok
  144. */
  145. public function testBankAccountFetch($id)
  146. {
  147. global $conf,$user,$langs,$db;
  148. $conf=$this->savconf;
  149. $user=$this->savuser;
  150. $langs=$this->savlangs;
  151. $db=$this->savdb;
  152. $localobject=new Account($db);
  153. $result=$localobject->fetch($id);
  154. print __METHOD__." id=".$id." result=".$result."\n";
  155. $this->assertLessThan($result, 0);
  156. return $localobject;
  157. }
  158. /**
  159. * testBankAccountOther
  160. *
  161. * @param Account $localobject Account
  162. * @return int
  163. *
  164. * @depends testBankAccountFetch
  165. * The depends says test is run only if previous is ok
  166. */
  167. public function testBankAccountOther($localobject)
  168. {
  169. global $conf,$user,$langs,$db;
  170. $conf=$this->savconf;
  171. $user=$this->savuser;
  172. $langs=$this->savlangs;
  173. $db=$this->savdb;
  174. /*$result=$localobject->setstatus(0);
  175. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  176. $this->assertLessThan($result, 0);
  177. */
  178. $localobject->info($localobject->id);
  179. $result = $localobject->needIBAN();
  180. //print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  181. $this->assertEquals(1, $result);
  182. // Test checkIbanForAccount for FR account
  183. $result = checkIbanForAccount($localobject);
  184. print __METHOD__." checkIbanForAccount(".$localobject->iban.") = ".$result."\n";
  185. $this->assertTrue($result);
  186. // Test checkIbanForAccount for CI account
  187. $localobject2=new Account($db);
  188. $localobject2->country = 'CI';
  189. $localobject2->iban = 'CI77A12312341234123412341234';
  190. $result = checkIbanForAccount($localobject2);
  191. print __METHOD__." checkIbanForAccount(".$localobject2->iban.") = ".$result."\n";
  192. $this->assertTrue($result);
  193. return $localobject->id;
  194. }
  195. /**
  196. * testBankAccountDelete
  197. *
  198. * @param int $id Id of contract
  199. * @return int
  200. *
  201. * @depends testBankAccountOther
  202. * The depends says test is run only if previous is ok
  203. */
  204. public function testBankAccountDelete($id)
  205. {
  206. global $conf,$user,$langs,$db;
  207. $conf=$this->savconf;
  208. $user=$this->savuser;
  209. $langs=$this->savlangs;
  210. $db=$this->savdb;
  211. $localobject=new Account($db);
  212. $result=$localobject->fetch($id);
  213. $result=$localobject->delete($user);
  214. print __METHOD__." id=".$id." result=".$result."\n";
  215. $this->assertLessThan($result, 0);
  216. return $result;
  217. }
  218. }