ClientTest.php 20 KB

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