WebservicesInvoicesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/WebservicesInvoicesTest.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/core/lib/date.lib.php';
  29. require_once(NUSOAP_PATH.'/nusoap.php'); // Include SOAP
  30. if (empty($user->id))
  31. {
  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. $conf->global->MAIN_UMASK='0666';
  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 WebservicesInvoicesTest extends PHPUnit_Framework_TestCase
  46. {
  47. protected $savconf;
  48. protected $savuser;
  49. protected $savlangs;
  50. protected $savdb;
  51. protected $soapclient;
  52. private static $socid;
  53. protected $ns = 'http://www.dolibarr.org/ns/';
  54. /**
  55. * Constructor
  56. * We save global variables into local variables
  57. *
  58. * @return DateLibTest
  59. */
  60. function __construct()
  61. {
  62. parent::__construct();
  63. //$this->sharedFixture
  64. global $conf,$user,$langs,$db;
  65. $this->savconf=$conf;
  66. $this->savuser=$user;
  67. $this->savlangs=$langs;
  68. $this->savdb=$db;
  69. // Set the WebService URL
  70. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_invoice.php';
  71. print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n";
  72. $this->soapclient = new nusoap_client($WS_DOL_URL);
  73. if ($this->soapclient)
  74. {
  75. $this->soapclient->soap_defencoding='UTF-8';
  76. $this->soapclient->decodeUTF8(false);
  77. }
  78. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  79. //print " - db ".$db->db;
  80. print "\n";
  81. }
  82. public static function setUpBeforeClass()
  83. {
  84. global $conf,$user,$langs,$db;
  85. // create a third_party, needed to create an invoice
  86. //
  87. // The third party is created in setUpBeforeClass() and not in the
  88. // constructor to avoid creating several objects (the constructor is
  89. // called for each test).
  90. //
  91. // The third party must be created before beginning the DB transaction
  92. // because there is a foreign key constraint between invoices and third
  93. // parties (tables: lx_facture and llx_societe) and with MySQL,
  94. // constraints are checked immediately, they are not deferred to
  95. // transaction commit. So if the invoice is created in the same
  96. // transaction than the third party, the FK constraint fails.
  97. // See this post for more detail: http://stackoverflow.com/a/5014744/5187108
  98. $societe=new Societe($db);
  99. $societe->ref='';
  100. $societe->name='name';
  101. $societe->ref_ext='ref-phpunit';
  102. $societe->status=1;
  103. $societe->client=1;
  104. $societe->fournisseur=0;
  105. $societe->date_creation=$now;
  106. $societe->tva_assuj=0;
  107. $societe->particulier=0;
  108. $societe->create($user);
  109. self::$socid = $societe->id;
  110. print __METHOD__." societe created id=".$societe->id."\n";
  111. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  112. print __METHOD__."\n";
  113. }
  114. public static function tearDownAfterClass()
  115. {
  116. global $conf,$user,$langs,$db;
  117. $db->rollback();
  118. print __METHOD__."\n";
  119. }
  120. /**
  121. * Init phpunit tests
  122. *
  123. * @return void
  124. */
  125. protected function setUp()
  126. {
  127. global $conf,$user,$langs,$db;
  128. $conf=$this->savconf;
  129. $user=$this->savuser;
  130. $langs=$this->savlangs;
  131. $db=$this->savdb;
  132. print __METHOD__."\n";
  133. }
  134. /**
  135. * End phpunit tests
  136. *
  137. * @return void
  138. */
  139. protected function tearDown()
  140. {
  141. print __METHOD__."\n";
  142. }
  143. /**
  144. * testWSInvoicesCreateInvoice
  145. *
  146. * @return int invoice created
  147. */
  148. public function testWSInvoicesCreateInvoice()
  149. {
  150. global $conf,$user,$langs,$db;
  151. $conf=$this->savconf;
  152. $user=$this->savuser;
  153. $langs=$this->savlangs;
  154. $db=$this->savdb;
  155. $WS_METHOD = 'createInvoice';
  156. $body = array (
  157. "id" => NULL,
  158. "ref" => NULL,
  159. "ref_ext" => "ref-phpunit-2",
  160. "thirdparty_id" => self::$socid,
  161. "fk_user_author" => NULL,
  162. "fk_user_valid" => NULL,
  163. "date" => "2015-04-19 20:16:53",
  164. "date_due" => "",
  165. "date_creation" => "",
  166. "date_validation" => "",
  167. "date_modification" => "",
  168. "type" => "",
  169. "total_net" => "36.30",
  170. "total_vat" => "6.00",
  171. "total" => "42.30",
  172. "payment_mode_id" => 50,
  173. "note_private" => "Synchronised from Prestashop",
  174. "note_public" => "",
  175. "status" => "1",
  176. "close_code" => NULL ,
  177. "close_note" => NULL,
  178. "project_id" => NULL,
  179. "lines" => array(
  180. array("id" => NULL,
  181. "type" => 0,
  182. "desc" => "Horloge Vinyle Serge",
  183. "vat_rate" => 20,
  184. "qty" => 1,
  185. "unitprice" => "30.000000",
  186. "total_net" => "30.000000",
  187. "total_vat" => "6.00",
  188. "total" => "36.000000",
  189. "date_start" => "",
  190. "date_end" => "",
  191. "payment_mode_id" => "",
  192. "product_id" => "",
  193. "product_ref" => "",
  194. "product_label" => "",
  195. "product_desc" => "" ))
  196. );
  197. // Call the WebService method and store its result in $result.
  198. $authentication=array(
  199. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  200. 'sourceapplication'=>'DEMO',
  201. 'login'=>'admin',
  202. 'password'=>'admin',
  203. 'entity'=>'');
  204. // Test URL
  205. $result='';
  206. $parameters = array('authentication'=>$authentication,'invoice'=>$body);
  207. print __METHOD__." call method ".$WS_METHOD."\n";
  208. try {
  209. $result = $this->soapclient->call($WS_METHOD,$parameters,$this->ns,'');
  210. }
  211. catch(SoapFault $exception)
  212. {
  213. echo $exception;
  214. $result=0;
  215. }
  216. if (! $result || ! empty($result['faultstring']))
  217. {
  218. //var_dump($soapclient);
  219. print $this->soapclient->error_str;
  220. print "\n<br>\n";
  221. print $this->soapclient->request;
  222. print "\n<br>\n";
  223. print $this->soapclient->response;
  224. print "\n";
  225. }
  226. print __METHOD__." result=".$result['result']['result_code']."\n";
  227. $this->assertEquals('OK',$result['result']['result_code']);
  228. $this->assertEquals('ref-phpunit-2', $result['ref_ext']);
  229. return $result;
  230. }
  231. /**
  232. * testWSInvoicesGetInvoiceByRefExt
  233. *
  234. * Retrieve an invoice using ref_ext
  235. * @depends testWSInvoicesCreateInvoice
  236. *
  237. * @param array $result Invoice created by create method
  238. * @return array Invoice
  239. */
  240. public function testWSInvoicesGetInvoiceByRefExt($result)
  241. {
  242. global $conf,$user,$langs,$db;
  243. $conf=$this->savconf;
  244. $user=$this->savuser;
  245. $langs=$this->savlangs;
  246. $db=$this->savdb;
  247. $WS_METHOD = 'getInvoice';
  248. // Call the WebService method and store its result in $result.
  249. $authentication=array(
  250. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  251. 'sourceapplication'=>'DEMO',
  252. 'login'=>'admin',
  253. 'password'=>'admin',
  254. 'entity'=>'');
  255. // Test URL
  256. $result='';
  257. $parameters = array('authentication'=>$authentication,'id'=>NULL,'ref'=>NULL,'ref_ext'=>'ref-phpunit-2');
  258. print __METHOD__." call method ".$WS_METHOD."\n";
  259. try {
  260. $result = $this->soapclient->call($WS_METHOD,$parameters,$this->ns,'');
  261. }
  262. catch(SoapFault $exception)
  263. {
  264. echo $exception;
  265. $result=0;
  266. }
  267. if (! $result || ! empty($result['faultstring']))
  268. {
  269. print $this->soapclient->error_str;
  270. print "\n<br>\n";
  271. print $this->soapclient->request;
  272. print "\n<br>\n";
  273. print $this->soapclient->response;
  274. print "\n";
  275. }
  276. print __METHOD__." result=".$result['result']['result_code']."\n";
  277. $this->assertEquals('OK',$result['result']['result_code']);
  278. $this->assertEquals('ref-phpunit-2', $result['invoice']['ref_ext']);
  279. return $result;
  280. }
  281. /**
  282. * testWSInvoicesUpdateInvoiceByRefExt
  283. *
  284. * Update an invoice using ref_ext
  285. * @depends testWSInvoicesCreateInvoice
  286. *
  287. * @param array $result invoice created by create method
  288. * @return array Invoice
  289. */
  290. public function testWSInvoicesUpdateInvoiceByRefExt($result)
  291. {
  292. global $conf,$user,$langs,$db;
  293. $conf=$this->savconf;
  294. $user=$this->savuser;
  295. $langs=$this->savlangs;
  296. $db=$this->savdb;
  297. $WS_METHOD = 'updateInvoice';
  298. // update status to 2
  299. $body = array (
  300. "id" => NULL,
  301. "ref" => NULL,
  302. "ref_ext" => "ref-phpunit-2",
  303. "thirdparty_id" => self::$socid,
  304. "fk_user_author" => NULL,
  305. "fk_user_valid" => NULL,
  306. "date" => "2015-04-19 20:16:53",
  307. "date_due" => "",
  308. "date_creation" => "",
  309. "date_validation" => "",
  310. "date_modification" => "",
  311. "type" => "",
  312. "total_net" => "36.30",
  313. "total_vat" => "6.00",
  314. "total" => "42.30",
  315. "payment_mode_id" => 50,
  316. "note_private" => "Synchronised from Prestashop",
  317. "note_public" => "",
  318. "status" => "2",
  319. "close_code" => NULL ,
  320. "close_note" => NULL,
  321. "project_id" => NULL,
  322. "lines" => array(
  323. array(
  324. "id" => NULL,
  325. "type" => 0,
  326. "desc" => "Horloge Vinyle Serge",
  327. "vat_rate" => 20,
  328. "qty" => "1",
  329. "unitprice" => "30.000000",
  330. "total_net" => "30.000000",
  331. "total_vat" => "6.00",
  332. "total" => "36.000000",
  333. "date_start" => "",
  334. "date_end" => "",
  335. "payment_mode_id" => "",
  336. "product_id" => "",
  337. "product_ref" => "",
  338. "product_label" => "",
  339. "product_desc" => "" ))
  340. );
  341. // Call the WebService method and store its result in $result.
  342. $authentication=array(
  343. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  344. 'sourceapplication'=>'DEMO',
  345. 'login'=>'admin',
  346. 'password'=>'admin',
  347. 'entity'=>'');
  348. // Test URL
  349. $result='';
  350. $parameters = array('authentication'=>$authentication,'invoice'=>$body);
  351. print __METHOD__." call method ".$WS_METHOD."\n";
  352. try {
  353. $result = $this->soapclient->call($WS_METHOD,$parameters,$this->ns,'');
  354. }
  355. catch(SoapFault $exception)
  356. {
  357. echo $exception;
  358. $result=0;
  359. }
  360. if (! $result || ! empty($result['faultstring']))
  361. {
  362. print $this->soapclient->error_str;
  363. print "\n<br>\n";
  364. print $this->soapclient->request;
  365. print "\n<br>\n";
  366. print $this->soapclient->response;
  367. print "\n";
  368. }
  369. print __METHOD__." result=".$result['result']['result_code'].$result['result']['result_label']."\n";
  370. $this->assertEquals('OK',$result['result']['result_code']);
  371. $this->assertEquals('ref-phpunit-2', $result['ref_ext']);
  372. return $result;
  373. }
  374. }