ClientTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. /**
  138. * @group ZF-8053
  139. */
  140. public function testGetAndSetUserAgentOption()
  141. {
  142. $client = new Zend_Soap_Client();
  143. $this->assertNull($client->getUserAgent());
  144. $client->setUserAgent('agent1');
  145. $this->assertEquals('agent1', $client->getUserAgent());
  146. $client->setOptions(array(
  147. 'user_agent' => 'agent2'
  148. ));
  149. $this->assertEquals('agent2', $client->getUserAgent());
  150. $client->setOptions(array(
  151. 'useragent' => 'agent3'
  152. ));
  153. $this->assertEquals('agent3', $client->getUserAgent());
  154. $client->setOptions(array(
  155. 'userAgent' => 'agent4'
  156. ));
  157. $this->assertEquals('agent4', $client->getUserAgent());
  158. $options = $client->getOptions();
  159. $this->assertEquals('agent4', $options['user_agent']);
  160. }
  161. public function testGetFunctions()
  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. $this->assertTrue($client->getFunctions() == array('string testFunc1()',
  167. 'string testFunc2(string $who)',
  168. 'string testFunc3(string $who, int $when)',
  169. 'string testFunc4()'));
  170. }
  171. /**
  172. * @todo Implement testGetTypes().
  173. */
  174. public function testGetTypes()
  175. {
  176. // Remove the following line when you implement this test.
  177. $this->markTestIncomplete(
  178. "This test has not been implemented yet."
  179. );
  180. }
  181. public function testGetLastRequest()
  182. {
  183. if (headers_sent()) {
  184. $this->markTestSkipped('Cannot run testGetLastRequest() 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. $expectedRequest = '<?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>'
  198. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  199. . '<who xsi:type="xsd:string">World</who>'
  200. . '</env:testFunc2>'
  201. . '</env:Body>'
  202. . '</env:Envelope>' . "\n";
  203. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  204. }
  205. public function testGetLastResponse()
  206. {
  207. if (headers_sent()) {
  208. $this->markTestSkipped('Cannot run testGetLastResponse() when headers have already been sent; enable output buffering to run this test');
  209. return;
  210. }
  211. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  212. $server->setClass('Zend_Soap_Client_TestClass');
  213. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  214. // Perform request
  215. $client->testFunc2('World');
  216. $expectedResponse = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  217. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  218. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  219. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  220. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  221. . '<env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">'
  222. . '<env:testFunc2Response env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  223. . '<rpc:result>testFunc2Return</rpc:result>'
  224. . '<testFunc2Return xsi:type="xsd:string">Hello World!</testFunc2Return>'
  225. . '</env:testFunc2Response>'
  226. . '</env:Body>'
  227. . '</env:Envelope>' . "\n";
  228. $this->assertEquals($client->getLastResponse(), $expectedResponse);
  229. }
  230. public function testCallInvoke()
  231. {
  232. if (headers_sent()) {
  233. $this->markTestSkipped('Cannot run testCallInvoke() when headers have already been sent; enable output buffering to run this test');
  234. return;
  235. }
  236. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  237. $server->setClass('Zend_Soap_Client_TestClass');
  238. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  239. $this->assertEquals($client->testFunc2('World'), 'Hello World!');
  240. }
  241. public function testSetOptionsWithZendConfig()
  242. {
  243. $ctx = stream_context_create();
  244. $nonWsdlOptions = array('soap_version' => SOAP_1_1,
  245. 'classmap' => array('TestData1' => 'Zend_Soap_Client_TestData1',
  246. 'TestData2' => 'Zend_Soap_Client_TestData2',),
  247. 'encoding' => 'ISO-8859-1',
  248. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  249. 'location' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  250. 'use' => SOAP_ENCODED,
  251. 'style' => SOAP_RPC,
  252. 'login' => 'http_login',
  253. 'password' => 'http_password',
  254. 'proxy_host' => 'proxy.somehost.com',
  255. 'proxy_port' => 8080,
  256. 'proxy_login' => 'proxy_login',
  257. 'proxy_password' => 'proxy_password',
  258. 'local_cert' => dirname(__FILE__).'/_files/cert_file',
  259. 'passphrase' => 'some pass phrase',
  260. 'stream_context' => $ctx,
  261. 'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 5
  262. );
  263. $config = new Zend_Config($nonWsdlOptions);
  264. $client = new Zend_Soap_Client(null, $config);
  265. $this->assertEquals($nonWsdlOptions, $client->getOptions());
  266. }
  267. public function testSetInputHeaders()
  268. {
  269. if (headers_sent()) {
  270. $this->markTestSkipped('Cannot run testSetInputHeaders() when headers have already been sent; enable output buffering to run this test');
  271. return;
  272. }
  273. $server = new Zend_Soap_Server(dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  274. $server->setClass('Zend_Soap_Client_TestClass');
  275. $client = new Zend_Soap_Client_Local($server, dirname(__FILE__) . '/_files/wsdl_example.wsdl');
  276. // Add request header
  277. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader1', 'SOAP header content 1'));
  278. // Add permanent request header
  279. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader2', 'SOAP header content 2'), true);
  280. // Perform request
  281. $client->testFunc2('World');
  282. $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  283. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  284. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  285. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  286. . 'xmlns:ns1="http://www.example.com/namespace" '
  287. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  288. . '<env:Header>'
  289. . '<ns1:MyHeader2>SOAP header content 2</ns1:MyHeader2>'
  290. . '<ns1:MyHeader1>SOAP header content 1</ns1:MyHeader1>'
  291. . '</env:Header>'
  292. . '<env:Body>'
  293. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  294. . '<who xsi:type="xsd:string">World</who>'
  295. . '</env:testFunc2>'
  296. . '</env:Body>'
  297. . '</env:Envelope>' . "\n";
  298. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  299. // Add request header
  300. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader3', 'SOAP header content 3'));
  301. // Perform request
  302. $client->testFunc2('World');
  303. $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  304. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  305. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  306. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  307. . 'xmlns:ns1="http://www.example.com/namespace" '
  308. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  309. . '<env:Header>'
  310. . '<ns1:MyHeader2>SOAP header content 2</ns1:MyHeader2>'
  311. . '<ns1:MyHeader3>SOAP header content 3</ns1:MyHeader3>'
  312. . '</env:Header>'
  313. . '<env:Body>'
  314. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  315. . '<who xsi:type="xsd:string">World</who>'
  316. . '</env:testFunc2>'
  317. . '</env:Body>'
  318. . '</env:Envelope>' . "\n";
  319. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  320. $client->resetSoapInputHeaders();
  321. // Add request header
  322. $client->addSoapInputHeader(new SoapHeader('http://www.example.com/namespace', 'MyHeader4', 'SOAP header content 4'));
  323. // Perform request
  324. $client->testFunc2('World');
  325. $expectedRequest = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  326. . '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" '
  327. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  328. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  329. . 'xmlns:ns1="http://www.example.com/namespace" '
  330. . 'xmlns:enc="http://www.w3.org/2003/05/soap-encoding">'
  331. . '<env:Header>'
  332. . '<ns1:MyHeader4>SOAP header content 4</ns1:MyHeader4>'
  333. . '</env:Header>'
  334. . '<env:Body>'
  335. . '<env:testFunc2 env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">'
  336. . '<who xsi:type="xsd:string">World</who>'
  337. . '</env:testFunc2>'
  338. . '</env:Body>'
  339. . '</env:Envelope>' . "\n";
  340. $this->assertEquals($client->getLastRequest(), $expectedRequest);
  341. }
  342. /**
  343. * @group ZF-6955
  344. */
  345. public function testSetCookieIsDelegatedToSoapClient()
  346. {
  347. $fixtureCookieKey = "foo";
  348. $fixtureCookieValue = "bar";
  349. $clientMock = $this->getMock('SoapClient', array('__setCookie'), array(null, array('uri' => 'http://www.zend.com', 'location' => 'http://www.zend.com')));
  350. $clientMock->expects($this->once())
  351. ->method('__setCookie')
  352. ->with($fixtureCookieKey, $fixtureCookieValue);
  353. $soap = new Zend_Soap_Client();
  354. $soap->setSoapClient($clientMock);
  355. $soap->setCookie($fixtureCookieKey, $fixtureCookieValue);
  356. }
  357. public function testSetSoapClient()
  358. {
  359. $clientMock = $this->getMock('SoapClient', array('__setCookie'), array(null, array('uri' => 'http://www.zend.com', 'location' => 'http://www.zend.com')));
  360. $soap = new Zend_Soap_Client();
  361. $soap->setSoapClient($clientMock);
  362. $this->assertSame($clientMock, $soap->getSoapClient());
  363. }
  364. }
  365. /** Test Class */
  366. class Zend_Soap_Client_TestClass {
  367. /**
  368. * Test Function 1
  369. *
  370. * @return string
  371. */
  372. function testFunc1()
  373. {
  374. return "Hello World";
  375. }
  376. /**
  377. * Test Function 2
  378. *
  379. * @param string $who Some Arg
  380. * @return string
  381. */
  382. function testFunc2($who)
  383. {
  384. return "Hello $who!";
  385. }
  386. /**
  387. * Test Function 3
  388. *
  389. * @param string $who Some Arg
  390. * @param int $when Some
  391. * @return string
  392. */
  393. function testFunc3($who, $when)
  394. {
  395. return "Hello $who, How are you $when";
  396. }
  397. /**
  398. * Test Function 4
  399. *
  400. * @return string
  401. */
  402. static function testFunc4()
  403. {
  404. return "I'm Static!";
  405. }
  406. }
  407. /** Test class 2 */
  408. class Zend_Soap_Client_TestData1 {
  409. /**
  410. * Property1
  411. *
  412. * @var string
  413. */
  414. public $property1;
  415. /**
  416. * Property2
  417. *
  418. * @var float
  419. */
  420. public $property2;
  421. }
  422. /** Test class 2 */
  423. class Zend_Soap_Client_TestData2 {
  424. /**
  425. * Property1
  426. *
  427. * @var integer
  428. */
  429. public $property1;
  430. /**
  431. * Property1
  432. *
  433. * @var float
  434. */
  435. public $property2;
  436. }
  437. /* Test Functions */
  438. /**
  439. * Test Function
  440. *
  441. * @param string $arg
  442. * @return string
  443. */
  444. function Zend_Soap_Client_TestFunc1($who)
  445. {
  446. return "Hello $who";
  447. }
  448. /**
  449. * Test Function 2
  450. */
  451. function Zend_Soap_Client_TestFunc2()
  452. {
  453. return "Hello World";
  454. }
  455. /**
  456. * Return false
  457. *
  458. * @return bool
  459. */
  460. function Zend_Soap_Client_TestFunc3()
  461. {
  462. return false;
  463. }
  464. /**
  465. * Return true
  466. *
  467. * @return bool
  468. */
  469. function Zend_Soap_Client_TestFunc4()
  470. {
  471. return true;
  472. }
  473. /**
  474. * Return integer
  475. *
  476. * @return int
  477. */
  478. function Zend_Soap_Client_TestFunc5()
  479. {
  480. return 123;
  481. }
  482. /**
  483. * Return string
  484. *
  485. * @return string
  486. */
  487. function Zend_Soap_Client_TestFunc6()
  488. {
  489. return "string";
  490. }
  491. if (PHPUnit_MAIN_METHOD == 'Zend_Soap_ClientTest::main') {
  492. Zend_Soap_ClientTest::main();
  493. }