PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/phpunit/WebservicesTest.php

https://github.com/asterix14/dolibarr
PHP | 172 lines | 98 code | 23 blank | 51 comment | 6 complexity | 7cd8e1a3b43981e0a641f63f61fdacd0 MD5 | raw file
Possible License(s): LGPL-2.0
  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 2 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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /**
  19. * \file test/phpunit/WebservicesTest.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 WebservicesTest extends PHPUnit_Framework_TestCase
  45. {
  46. protected $savconf;
  47. protected $savuser;
  48. protected $savlangs;
  49. protected $savdb;
  50. /**
  51. * Constructor
  52. * We save global variables into local variables
  53. *
  54. * @return DateLibTest
  55. */
  56. function WebservicesTest()
  57. {
  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. // Static methods
  69. public static function setUpBeforeClass()
  70. {
  71. global $conf,$user,$langs,$db;
  72. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  73. print __METHOD__."\n";
  74. }
  75. public static function tearDownAfterClass()
  76. {
  77. global $conf,$user,$langs,$db;
  78. $db->rollback();
  79. print __METHOD__."\n";
  80. }
  81. /**
  82. */
  83. protected function setUp()
  84. {
  85. global $conf,$user,$langs,$db;
  86. $conf=$this->savconf;
  87. $user=$this->savuser;
  88. $langs=$this->savlangs;
  89. $db=$this->savdb;
  90. print __METHOD__."\n";
  91. }
  92. /**
  93. */
  94. protected function tearDown()
  95. {
  96. print __METHOD__."\n";
  97. }
  98. /**
  99. */
  100. public function testWSVersion()
  101. {
  102. global $conf,$user,$langs,$db;
  103. $conf=$this->savconf;
  104. $user=$this->savuser;
  105. $langs=$this->savlangs;
  106. $db=$this->savdb;
  107. $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_other.php';
  108. //$WS_DOL_URL = 'http://localhost:8080/'; // If not a page, should end with /
  109. $WS_METHOD = 'getVersions';
  110. $ns='http://www.dolibarr.org/ns/';
  111. // Set the WebService URL
  112. print __METHOD__."Create nusoap_client for URL=".$WS_DOL_URL."\n";
  113. $soapclient = new nusoap_client($WS_DOL_URL);
  114. if ($soapclient)
  115. {
  116. $soapclient->soap_defencoding='UTF-8';
  117. $soapclient->decodeUTF8(false);
  118. }
  119. // Call the WebService method and store its result in $result.
  120. $authentication=array(
  121. 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY,
  122. 'sourceapplication'=>'DEMO',
  123. 'login'=>'admin',
  124. 'password'=>'admin',
  125. 'entity'=>'');
  126. // Test URL
  127. if ($WS_METHOD)
  128. {
  129. $parameters = array('authentication'=>$authentication);
  130. print __METHOD__."Call method ".$WS_METHOD."\n";
  131. $result = $soapclient->call($WS_METHOD,$parameters,$ns,'');
  132. if (! $result)
  133. {
  134. //var_dump($soapclient);
  135. //print_r($soapclient);
  136. print $soapclient->error_str;
  137. print "<br>\n\n";
  138. print $soapclient->request;
  139. print "<br>\n\n";
  140. print $soapclient->response;
  141. exit;
  142. }
  143. }
  144. print __METHOD__." result=".$result."\n";
  145. $this->assertEquals('OK',$result['result']['result_code']);
  146. return $result;
  147. }
  148. }
  149. ?>