ClientTest.php 21 KB

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