WebservicesInvoicesTest.php 11 KB

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