ClientTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Soap
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Soap_ClientTest::main');
  24. }
  25. require_once dirname(__FILE__)."/../../TestHelper.php";
  26. /** PHPUnit Test Case */
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. /** Zend_Soap_Server */
  29. require_once 'Zend/Soap/Server.php';
  30. /** Zend_Soap_Client */
  31. require_once 'Zend/Soap/Client.php';
  32. require_once 'Zend/Config.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Soap
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Soap
  40. */
  41. class Zend_Soap_ClientTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs this test suite
  45. *
  46. * @return void
  47. */
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. public function setUp()
  54. {
  55. if (!extension_loaded('soap')) {
  56. $this->markTestSkipped('SOAP Extension is not loaded');
  57. }
  58. }
  59. public function testSetOptions()
  60. {
  61. /*************************************************************
  62. * ------ Test WSDL mode options -----------------------------
  63. *************************************************************/
  64. $client = new Zend_Soap_Client();
  65. $this->assertTrue($client->getOptions() == array('encoding' => 'UTF-8', 'soap_version' => SOAP_1_2));
  66. $ctx = stream_context_create();
  67. $nonWsdlOptions = array('soap_version' => SOAP_1_1,
  68. 'classmap' => array('TestData1' => 'Zend_Soap_Client_TestData1',
  69. 'TestData2' => 'Zend_Soap_Client_TestData2',),
  70. 'encoding' => 'ISO-8859-1',
  71. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  72. 'location' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  73. 'use' => SOAP_ENCODED,
  74. 'style' => SOAP_RPC,
  75. 'login' => 'http_login',
  76. 'password' => 'http_password',
  77. 'proxy_host' => 'proxy.somehost.com',
  78. 'proxy_port' => 8080,
  79. 'proxy_login' => 'proxy_login',
  80. 'proxy_password' => 'proxy_password',
  81. 'local_cert' => dirname(__FILE__).'/_files/cert_file',
  82. 'passphrase' => 'some pass phrase',
  83. 'stream_context' => $ctx,
  84. 'cache_wsdl' => 8,
  85. 'features' => 4,
  86. 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 5);
  87. $client->setOptions($nonWsdlOptions);
  88. $this->assertTrue($client->getOptions() == $nonWsdlOptions);
  89. /*************************************************************
  90. * ------ Test non-WSDL mode options -----------------------------
  91. *************************************************************/
  92. $client1 = new Zend_Soap_Client();
  93. $this->assertTrue($client1->getOptions() == array('encoding' => 'UTF-8', 'soap_version' => SOAP_1_2));
  94. $wsdlOptions = array('soap_version' => SOAP_1_1,
  95. 'wsdl' => dirname(__FILE__).'/_files/wsdl_example.wsdl',
  96. 'classmap' => array('TestData1' => 'Zend_Soap_Client_TestData1',
  97. 'TestData2' => 'Zend_Soap_Client_TestData2',),
  98. 'encoding' => 'ISO-8859-1',
  99. 'login' => 'http_login',
  100. 'password' => 'http_password',
  101. 'proxy_host' => 'proxy.somehost.com',
  102. 'proxy_port' => 8080,
  103. 'proxy_login' => 'proxy_login',
  104. 'proxy_password' => 'proxy_password',
  105. 'local_cert' => dirname(__FILE__).'/_files/cert_file',
  106. 'passphrase' => 'some pass phrase',
  107. 'stream_context' => $ctx,
  108. 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 5);
  109. $client1->setOptions($wsdlOptions);
  110. $this->assertTrue($client1->getOptions() == $wsdlOptions);
  111. }
  112. public function testGetOptions()
  113. {
  114. $client = new Zend_Soap_Client();
  115. $this->assertTrue($client->getOptions() == array('encoding' => 'UTF-8', 'soap_version' => SOAP_1_2));
  116. $options = array('soap_version' => SOAP_1_1,
  117. 'wsdl' => dirname(__FILE__).'/_files/wsdl_example.wsdl',
  118. 'classmap' => array('TestData1' => 'Zend_Soap_Client_TestData1',
  119. 'TestData2' => 'Zend_Soap_Client_TestData2',),
  120. 'encoding' => 'ISO-8859-1',
  121. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  122. 'location' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  123. 'use' => SOAP_ENCODED,
  124. 'style' => SOAP_RPC,
  125. 'login' => 'http_login',
  126. 'password' => 'http_password',
  127. 'proxy_host' => 'proxy.somehost.com',
  128. 'proxy_port' => 8080,
  129. 'proxy_login' => 'proxy_login',
  130. 'proxy_password' => 'proxy_password',
  131. 'local_cert' => dirname(__FILE__).'/_files/cert_file',
  132. 'passphrase' => 'some pass phrase',
  133. 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 5);
  134. $client->setOptions($options);
  135. $this->assertTrue($client->getOptions() == $options);
  136. }
  137. public function testGetFunctions()
  138. {
  139. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  140. $server->setClass('Zend_Soap_Client_TestClass');
  141. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  142. $this->assertTrue($client->getFunctions() == array('string testFunc1()',
  143. 'string testFunc2(string $who)',
  144. 'string testFunc3(string $who, int $when)',
  145. 'string testFunc4()'));
  146. }
  147. /**
  148. * @todo Implement testGetTypes().
  149. */
  150. public function testGetTypes()
  151. {
  152. // Remove the following line when you implement this test.
  153. $this->markTestIncomplete(
  154. "This test has not been implemented yet."
  155. );
  156. }
  157. public function testGetLastRequest()
  158. {
  159. if (headers_sent()) {
  160. $this->markTestSkipped('Cannot run testGetLastRequest() when headers have already been sent; enable output buffering to run this test');
  161. return;
  162. }
  163. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  164. $server->setClass('Zend_Soap_Client_TestClass');
  165. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  166. // Perform request
  167. $client->testFunc2('World');
  168. $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  169. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  170. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  171. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  172. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  173. . '<env:Body>'
  174. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  175. . '<who xsi:type="xsd:string">World</who>'
  176. . '</env:testFunc2>'
  177. . '</env:Body>'
  178. . '</env:Envelope>' . "\n";
  179. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  180. }
  181. public function testGetLastResponse()
  182. {
  183. if (headers_sent()) {
  184. $this->markTestSkipped('Cannot run testGetLastResponse() when headers have already been sent; enable output buffering to run this test');
  185. return;
  186. }
  187. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  188. $server->setClass('Zend_Soap_Client_TestClass');
  189. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  190. // Perform request
  191. $client->testFunc2('World');
  192. $expectedResponse = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  193. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  194. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  195. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  196. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  197. . '<env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">'
  198. . '<env:testFunc2Response env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  199. . '<rpc:result>testFunc2Return</rpc:result>'
  200. . '<testFunc2Return xsi:type="xsd:string">Hello World!</testFunc2Return>'
  201. . '</env:testFunc2Response>'
  202. . '</env:Body>'
  203. . '</env:Envelope>' . "\n";
  204. $this->assertEquals($client->getLastResponse(), $expectedResponse);
  205. }
  206. public function testCallInvoke()
  207. {
  208. if (headers_sent()) {
  209. $this->markTestSkipped('Cannot run testCallInvoke() when headers have already been sent; enable output buffering to run this test');
  210. return;
  211. }
  212. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  213. $server->setClass('Zend_Soap_Client_TestClass');
  214. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  215. $this->assertEquals($client->testFunc2('World'), 'Hello World!');
  216. }
  217. public function testSetOptionsWithZendConfig()
  218. {
  219. $ctx = stream_context_create();
  220. $nonWsdlOptions = array('soap_version' => SOAP_1_1,
  221. 'classmap' => array('TestData1' => 'Zend_Soap_Client_TestData1',
  222. 'TestData2' => 'Zend_Soap_Client_TestData2',),
  223. 'encoding' => 'ISO-8859-1',
  224. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  225. 'location' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  226. 'use' => SOAP_ENCODED,
  227. 'style' => SOAP_RPC,
  228. 'login' => 'http_login',
  229. 'password' => 'http_password',
  230. 'proxy_host' => 'proxy.somehost.com',
  231. 'proxy_port' => 8080,
  232. 'proxy_login' => 'proxy_login',
  233. 'proxy_password' => 'proxy_password',
  234. 'local_cert' => dirname(__FILE__).'/_files/cert_file',
  235. 'passphrase' => 'some pass phrase',
  236. 'stream_context' => $ctx,
  237. 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 5
  238. );
  239. $config = new Zend_Config($nonWsdlOptions);
  240. $client = new Zend_Soap_Client(null, $config);
  241. $this->assertEquals($nonWsdlOptions, $client->getOptions());
  242. }
  243. public function testSetInputHeaders()
  244. {
  245. if (headers_sent()) {
  246. $this->markTestSkipped('Cannot run testSetInputHeaders() when headers have already been sent; enable output buffering to run this test');
  247. return;
  248. }
  249. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  250. $server->setClass('Zend_Soap_Client_TestClass');
  251. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  252. // Add request header
  253. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader1', 'SOAP header content 1'));
  254. // Add permanent request header
  255. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader2', 'SOAP header content 2'), true);
  256. // Perform request
  257. $client->testFunc2('World');
  258. $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  259. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  260. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  261. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  262. . 'xmlns:ns1="http://www.example.com/namespace" '
  263. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  264. . '<env:Header>'
  265. . '<ns1:MyHeader2>SOAP header content 2</ns1:MyHeader2>'
  266. . '<ns1:MyHeader1>SOAP header content 1</ns1:MyHeader1>'
  267. . '</env:Header>'
  268. . '<env:Body>'
  269. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  270. . '<who xsi:type="xsd:string">World</who>'
  271. . '</env:testFunc2>'
  272. . '</env:Body>'
  273. . '</env:Envelope>' . "\n";
  274. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  275. // Add request header
  276. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader3', 'SOAP header content 3'));
  277. // Perform request
  278. $client->testFunc2('World');
  279. $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  280. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  281. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  282. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  283. . 'xmlns:ns1="http://www.example.com/namespace" '
  284. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  285. . '<env:Header>'
  286. . '<ns1:MyHeader2>SOAP header content 2</ns1:MyHeader2>'
  287. . '<ns1:MyHeader3>SOAP header content 3</ns1:MyHeader3>'
  288. . '</env:Header>'
  289. . '<env:Body>'
  290. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  291. . '<who xsi:type="xsd:string">World</who>'
  292. . '</env:testFunc2>'
  293. . '</env:Body>'
  294. . '</env:Envelope>' . "\n";
  295. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  296. $client->resetSoapInputHeaders();
  297. // Add request header
  298. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader4', 'SOAP header content 4'));
  299. // Perform request
  300. $client->testFunc2('World');
  301. $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  302. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  303. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  304. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  305. . 'xmlns:ns1="http://www.example.com/namespace" '
  306. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  307. . '<env:Header>'
  308. . '<ns1:MyHeader4>SOAP header content 4</ns1:MyHeader4>'
  309. . '</env:Header>'
  310. . '<env:Body>'
  311. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  312. . '<who xsi:type="xsd:string">World</who>'
  313. . '</env:testFunc2>'
  314. . '</env:Body>'
  315. . '</env:Envelope>' . "\n";
  316. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  317. }
  318. /**
  319. * @group ZF-6955
  320. */
  321. public function testSetCookieIsDelegatedToSoapClient()
  322. {
  323. $fixtureCookieKey = "foo";
  324. $fixtureCookieValue = "bar";
  325. $clientMock = $this->getMock('SoapClient', array('__setCookie'), array(null, array('uri' => 'http://www.zend.com', 'location' => 'http://www.zend.com')));
  326. $clientMock->expects($this->once())
  327. ->method('__setCookie')
  328. ->with($fixtureCookieKey, $fixtureCookieValue);
  329. $soap = new Zend_Soap_Client();
  330. $soap->setSoapClient($clientMock);
  331. $soap->setCookie($fixtureCookieKey, $fixtureCookieValue);
  332. }
  333. public function testSetSoapClient()
  334. {
  335. $clientMock = $this->getMock('SoapClient', array('__setCookie'), array(null, array('uri' => 'http://www.zend.com', 'location' => 'http://www.zend.com')));
  336. $soap = new Zend_Soap_Client();
  337. $soap->setSoapClient($clientMock);
  338. $this->assertSame($clientMock, $soap->getSoapClient());
  339. }
  340. }
  341. /** Test Class */
  342. class Zend_Soap_Client_TestClass {
  343. /**
  344. * Test Function 1
  345. *
  346. * @return string
  347. */
  348. function testFunc1()
  349. {
  350. return "Hello World";
  351. }
  352. /**
  353. * Test Function 2
  354. *
  355. * @param string $who Some Arg
  356. * @return string
  357. */
  358. function testFunc2($who)
  359. {
  360. return "Hello $who!";
  361. }
  362. /**
  363. * Test Function 3
  364. *
  365. * @param string $who Some Arg
  366. * @param int $when Some
  367. * @return string
  368. */
  369. function testFunc3($who, $when)
  370. {
  371. return "Hello $who, How are you $when";
  372. }
  373. /**
  374. * Test Function 4
  375. *
  376. * @return string
  377. */
  378. static function testFunc4()
  379. {
  380. return "I'm Static!";
  381. }
  382. }
  383. /** Test class 2 */
  384. class Zend_Soap_Client_TestData1 {
  385. /**
  386. * Property1
  387. *
  388. * @var string
  389. */
  390. public $property1;
  391. /**
  392. * Property2
  393. *
  394. * @var float
  395. */
  396. public $property2;
  397. }
  398. /** Test class 2 */
  399. class Zend_Soap_Client_TestData2 {
  400. /**
  401. * Property1
  402. *
  403. * @var integer
  404. */
  405. public $property1;
  406. /**
  407. * Property1
  408. *
  409. * @var float
  410. */
  411. public $property2;
  412. }
  413. /* Test Functions */
  414. /**
  415. * Test Function
  416. *
  417. * @param string $arg
  418. * @return string
  419. */
  420. function Zend_Soap_Client_TestFunc1($who)
  421. {
  422. return "Hello $who";
  423. }
  424. /**
  425. * Test Function 2
  426. */
  427. function Zend_Soap_Client_TestFunc2()
  428. {
  429. return "Hello World";
  430. }
  431. /**
  432. * Return false
  433. *
  434. * @return bool
  435. */
  436. function Zend_Soap_Client_TestFunc3()
  437. {
  438. return false;
  439. }
  440. /**
  441. * Return true
  442. *
  443. * @return bool
  444. */
  445. function Zend_Soap_Client_TestFunc4()
  446. {
  447. return true;
  448. }
  449. /**
  450. * Return integer
  451. *
  452. * @return int
  453. */
  454. function Zend_Soap_Client_TestFunc5()
  455. {
  456. return 123;
  457. }
  458. /**
  459. * Return string
  460. *
  461. * @return string
  462. */
  463. function Zend_Soap_Client_TestFunc6()
  464. {
  465. return "string";
  466. }
  467. if (PHPUnit_MAIN_METHOD == 'Zend_Soap_ClientTest::main') {
  468. Zend_Soap_ClientTest::main();
  469. }