ServerTest.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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-2012 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. /** Zend_Soap_Server */
  23. require_once 'Zend/Soap/Server.php';
  24. require_once 'Zend/Soap/Server/Exception.php';
  25. require_once "Zend/Config.php";
  26. /**
  27. * Zend_Soap_Server
  28. *
  29. * @category Zend
  30. * @package Zend_Soap
  31. * @subpackage UnitTests
  32. * @uses Zend_Server_Interface
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Soap
  36. * @group Zend_Soap_Server
  37. */
  38. class Zend_Soap_ServerTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function setUp()
  41. {
  42. if (!extension_loaded('soap')) {
  43. $this->markTestSkipped('SOAP Extension is not loaded');
  44. }
  45. }
  46. public function testSetOptions()
  47. {
  48. $server = new Zend_Soap_Server();
  49. $this->assertTrue($server->getOptions() == array('soap_version' => SOAP_1_2));
  50. $options = array('soap_version' => SOAP_1_1,
  51. 'actor' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  52. 'classmap' => array('TestData1' => 'Zend_Soap_Server_TestData1',
  53. 'TestData2' => 'Zend_Soap_Server_TestData2',),
  54. 'encoding' => 'ISO-8859-1',
  55. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php'
  56. );
  57. $server->setOptions($options);
  58. $this->assertTrue($server->getOptions() == $options);
  59. }
  60. public function testSetOptionsViaSecondConstructorArgument()
  61. {
  62. $options = array(
  63. 'soap_version' => SOAP_1_1,
  64. 'actor' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  65. 'classmap' => array(
  66. 'TestData1' => 'Zend_Soap_Server_TestData1',
  67. 'TestData2' => 'Zend_Soap_Server_TestData2',
  68. ),
  69. 'encoding' => 'ISO-8859-1',
  70. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  71. );
  72. $server = new Zend_Soap_Server(null, $options);
  73. $this->assertTrue($server->getOptions() == $options);
  74. }
  75. public function testSetWsiCompliantViaConstructor()
  76. {
  77. $options = array(
  78. 'wsi_compliant' => true
  79. );
  80. $server = new Zend_Soap_Server(null, $options);
  81. $this->assertTrue($server->getWsiCompliant());
  82. }
  83. public function testSetWsiCompliant()
  84. {
  85. $server = new Zend_Soap_Server();
  86. $server->setWsiCompliant(true);
  87. $this->assertTrue($server->getWsiCompliant());
  88. $server->setWsiCompliant(false);
  89. $this->assertFalse($server->getWsiCompliant());
  90. }
  91. /**
  92. * @group ZF-9816
  93. */
  94. public function testSetOptionsWithFeaturesOption()
  95. {
  96. $server = new Zend_Soap_Server(null, array(
  97. 'features' => SOAP_SINGLE_ELEMENT_ARRAYS
  98. ));
  99. $this->assertEquals(
  100. SOAP_SINGLE_ELEMENT_ARRAYS,
  101. $server->getSoapFeatures()
  102. );
  103. }
  104. public function testSetWsdlViaOptionsArrayIsPossible()
  105. {
  106. $server = new Zend_Soap_Server();
  107. $server->setOptions(array('wsdl' => 'http://www.example.com/test.wsdl'));
  108. $this->assertEquals('http://www.example.com/test.wsdl', $server->getWsdl());
  109. }
  110. public function testGetOptions()
  111. {
  112. $server = new Zend_Soap_Server();
  113. $this->assertTrue($server->getOptions() == array('soap_version' => SOAP_1_2));
  114. $options = array('soap_version' => SOAP_1_1,
  115. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php'
  116. );
  117. $server->setOptions($options);
  118. $this->assertTrue($server->getOptions() == $options);
  119. }
  120. public function testEncoding()
  121. {
  122. $server = new Zend_Soap_Server();
  123. $this->assertNull($server->getEncoding());
  124. $server->setEncoding('ISO-8859-1');
  125. $this->assertEquals('ISO-8859-1', $server->getEncoding());
  126. try {
  127. $server->setEncoding(array('UTF-8'));
  128. $this->fail('Non-string encoding values should fail');
  129. } catch (Exception $e) {
  130. // success
  131. }
  132. }
  133. public function testSoapVersion()
  134. {
  135. $server = new Zend_Soap_Server();
  136. $this->assertEquals(SOAP_1_2, $server->getSoapVersion());
  137. $server->setSoapVersion(SOAP_1_1);
  138. $this->assertEquals(SOAP_1_1, $server->getSoapVersion());
  139. try {
  140. $server->setSoapVersion('bogus');
  141. $this->fail('Invalid soap versions should fail');
  142. } catch (Exception $e) {
  143. // success
  144. }
  145. }
  146. public function testValidateUrn()
  147. {
  148. $server = new Zend_Soap_Server();
  149. try {
  150. $server->validateUrn('bogosity');
  151. $this->fail('URNs without schemes should fail');
  152. } catch (Exception $e) {
  153. // success
  154. }
  155. $this->assertTrue($server->validateUrn('http://framework.zend.com/'));
  156. $this->assertTrue($server->validateUrn('urn:soapHandler/GetOpt'));
  157. }
  158. public function testSetActor()
  159. {
  160. $server = new Zend_Soap_Server();
  161. $this->assertNull($server->getActor());
  162. $server->setActor('http://framework.zend.com/');
  163. $this->assertEquals('http://framework.zend.com/', $server->getActor());
  164. try {
  165. $server->setActor('bogus');
  166. $this->fail('Invalid actor should fail');
  167. } catch (Exception $e) {
  168. // success
  169. }
  170. }
  171. public function testGetActor()
  172. {
  173. $server = new Zend_Soap_Server();
  174. $this->assertNull($server->getActor());
  175. $server->setActor('http://framework.zend.com/');
  176. $this->assertEquals('http://framework.zend.com/', $server->getActor());
  177. }
  178. public function testSetUri()
  179. {
  180. $server = new Zend_Soap_Server();
  181. $this->assertNull($server->getUri());
  182. $server->setUri('http://framework.zend.com/');
  183. $this->assertEquals('http://framework.zend.com/', $server->getUri());
  184. try {
  185. $server->setUri('bogus');
  186. $this->fail('Invalid URI should fail');
  187. } catch (Exception $e) {
  188. // success
  189. }
  190. }
  191. public function testGetUri()
  192. {
  193. $server = new Zend_Soap_Server();
  194. $this->assertNull($server->getUri());
  195. $server->setUri('http://framework.zend.com/');
  196. $this->assertEquals('http://framework.zend.com/', $server->getUri());
  197. }
  198. public function testSetClassmap()
  199. {
  200. $server = new Zend_Soap_Server();
  201. $classmap = array('TestData1' => 'Zend_Soap_Server_TestData1',
  202. 'TestData2' => 'Zend_Soap_Server_TestData2');
  203. $this->assertNull($server->getClassmap());
  204. $server->setClassmap($classmap);
  205. $this->assertTrue($classmap == $server->getClassmap());
  206. try {
  207. $server->setClassmap('bogus');
  208. $this->fail('Classmap which is not an array should fail');
  209. } catch (Exception $e) {
  210. // success
  211. }
  212. try {
  213. $server->setClassmap(array('soapTypeName', 'bogusClassName'));
  214. $this->fail('Invalid class within classmap should fail');
  215. } catch (Exception $e) {
  216. // success
  217. }
  218. }
  219. public function testGetClassmap()
  220. {
  221. $server = new Zend_Soap_Server();
  222. $classmap = array('TestData1' => 'Zend_Soap_Server_TestData1',
  223. 'TestData2' => 'Zend_Soap_Server_TestData2');
  224. $this->assertNull($server->getClassmap());
  225. $server->setClassmap($classmap);
  226. $this->assertTrue($classmap == $server->getClassmap());
  227. }
  228. public function testSetWsdl()
  229. {
  230. $server = new Zend_Soap_Server();
  231. $this->assertNull($server->getWsdl());
  232. $server->setWsdl(dirname(__FILE__).'/_files/wsdl_example.wsdl');
  233. $this->assertEquals(dirname(__FILE__).'/_files/wsdl_example.wsdl', $server->getWsdl());
  234. try {
  235. $server->setWsdl(dirname(__FILE__).'/_files/bogus.wsdl');
  236. $this->fail('Invalid WSDL URI or PATH should fail');
  237. } catch (Exception $e) {
  238. // success
  239. }
  240. }
  241. public function testGetWsdl()
  242. {
  243. $server = new Zend_Soap_Server();
  244. $this->assertNull($server->getWsdl());
  245. $server->setWsdl(dirname(__FILE__).'/_files/wsdl_example.wsdl');
  246. $this->assertEquals(dirname(__FILE__).'/_files/wsdl_example.wsdl', $server->getWsdl());
  247. }
  248. public function testAddFunction()
  249. {
  250. $server = new Zend_Soap_Server();
  251. // Correct function should pass
  252. $server->addFunction('Zend_Soap_Server_TestFunc1');
  253. // Array of correct functions should pass
  254. $functions = array('Zend_Soap_Server_TestFunc2',
  255. 'Zend_Soap_Server_TestFunc3',
  256. 'Zend_Soap_Server_TestFunc4');
  257. $server->addFunction($functions);
  258. $this->assertEquals(
  259. array_merge(array('Zend_Soap_Server_TestFunc1'), $functions),
  260. $server->getFunctions()
  261. );
  262. }
  263. public function testAddBogusFunctionAsInteger()
  264. {
  265. $server = new Zend_Soap_Server();
  266. try {
  267. $server->addFunction(126);
  268. $this->fail('Invalid value should fail');
  269. } catch (Zend_Soap_Server_Exception $e) {
  270. // success
  271. }
  272. }
  273. public function testAddBogusFunctionsAsString()
  274. {
  275. $server = new Zend_Soap_Server();
  276. try {
  277. $server->addFunction('bogus_function');
  278. $this->fail('Invalid function should fail.');
  279. } catch (Zend_Soap_Server_Exception $e) {
  280. // success
  281. }
  282. }
  283. public function testAddBogusFunctionsAsArray()
  284. {
  285. $server = new Zend_Soap_Server();
  286. try {
  287. $functions = array('Zend_Soap_Server_TestFunc5',
  288. 'bogus_function',
  289. 'Zend_Soap_Server_TestFunc6');
  290. $server->addFunction($functions);
  291. $this->fail('Invalid function within a set of functions should fail');
  292. } catch (Zend_Soap_Server_Exception $e) {
  293. // success
  294. }
  295. }
  296. public function testAddAllFunctionsSoapConstant()
  297. {
  298. $server = new Zend_Soap_Server();
  299. // SOAP_FUNCTIONS_ALL as a value should pass
  300. $server->addFunction(SOAP_FUNCTIONS_ALL);
  301. $server->addFunction('substr');
  302. $this->assertEquals(array(SOAP_FUNCTIONS_ALL), $server->getFunctions());
  303. }
  304. public function testSetClass()
  305. {
  306. $server = new Zend_Soap_Server();
  307. // Correct class name should pass
  308. try {
  309. $server->setClass('Zend_Soap_Server_TestClass');
  310. } catch(Exception $e) {
  311. $this->fail("Setting a correct class name should not fail setClass()");
  312. }
  313. }
  314. public function testSetClassTwiceThrowsException()
  315. {
  316. $server = new Zend_Soap_Server();
  317. // Correct class name should pass
  318. try {
  319. $server->setClass('Zend_Soap_Server_TestClass');
  320. $server->setClass('Zend_Soap_Server_TestClass');
  321. $this->fail();
  322. } catch(Zend_Soap_Server_Exception $e) {
  323. $this->assertEquals('A class has already been registered with this soap server instance', $e->getMessage());
  324. }
  325. }
  326. public function testSetClassWithArguments()
  327. {
  328. $server = new Zend_Soap_Server();
  329. // Correct class name should pass
  330. try {
  331. $server->setClass('Zend_Soap_Server_TestClass', 1, 2, 3, 4);
  332. } catch(Exception $e) {
  333. $this->fail("Setting a correct class name should not fail setClass()");
  334. }
  335. }
  336. public function testSetBogusClassWithIntegerName()
  337. {
  338. $server = new Zend_Soap_Server();
  339. try {
  340. $server->setClass(465);
  341. $this->fail('Non-string value should fail');
  342. } catch (Exception $e) {
  343. // success
  344. }
  345. }
  346. public function testSetBogusClassWithUnknownClassName()
  347. {
  348. $server = new Zend_Soap_Server();
  349. try {
  350. $server->setClass('Zend_Soap_Server_Test_BogusClass');
  351. $this->fail('Invalid class should fail');
  352. } catch (Exception $e) {
  353. // success
  354. }
  355. }
  356. /**
  357. * @group ZF-4366
  358. */
  359. public function testSetObject()
  360. {
  361. $server = new Zend_Soap_Server();
  362. try {
  363. $server->setObject(465);
  364. $this->fail('Non-object value should fail');
  365. } catch (Exception $e) {
  366. // success
  367. }
  368. try {
  369. $int = 1;
  370. $server->setObject($int);
  371. $this->fail('Invalid argument should fail');
  372. } catch (Exception $e) {
  373. // success
  374. }
  375. // Correct class name should pass
  376. $server->setObject(new Zend_Soap_Server_TestClass());
  377. try {
  378. $server->setObject(new Zend_Soap_Server_TestClass());
  379. $this->fail('setClass() should pass only once');
  380. } catch (Exception $e) {
  381. // success
  382. }
  383. }
  384. public function testGetFunctions()
  385. {
  386. $server = new Zend_Soap_Server();
  387. $server->addFunction('Zend_Soap_Server_TestFunc1');
  388. $functions = array('Zend_Soap_Server_TestFunc2',
  389. 'Zend_Soap_Server_TestFunc3',
  390. 'Zend_Soap_Server_TestFunc4');
  391. $server->addFunction($functions);
  392. $functions = array('Zend_Soap_Server_TestFunc3',
  393. 'Zend_Soap_Server_TestFunc5',
  394. 'Zend_Soap_Server_TestFunc6');
  395. $server->addFunction($functions);
  396. $allAddedFunctions = array(
  397. 'Zend_Soap_Server_TestFunc1',
  398. 'Zend_Soap_Server_TestFunc2',
  399. 'Zend_Soap_Server_TestFunc3',
  400. 'Zend_Soap_Server_TestFunc4',
  401. 'Zend_Soap_Server_TestFunc5',
  402. 'Zend_Soap_Server_TestFunc6'
  403. );
  404. $this->assertTrue($server->getFunctions() == $allAddedFunctions);
  405. }
  406. public function testGetFunctionsWithClassAttached()
  407. {
  408. $server = new Zend_Soap_Server();
  409. $server->setClass('Zend_Soap_Server_TestClass');
  410. $this->assertEquals(
  411. array('testFunc1', 'testFunc2', 'testFunc3', 'testFunc4', 'testFunc5'),
  412. $server->getFunctions()
  413. );
  414. }
  415. public function testGetFunctionsWithObjectAttached()
  416. {
  417. $server = new Zend_Soap_Server();
  418. $server->setObject(new Zend_Soap_Server_TestClass());
  419. $this->assertEquals(
  420. array('testFunc1', 'testFunc2', 'testFunc3', 'testFunc4', 'testFunc5'),
  421. $server->getFunctions()
  422. );
  423. }
  424. public function testSetPersistence()
  425. {
  426. $server = new Zend_Soap_Server();
  427. $this->assertNull($server->getPersistence());
  428. $server->setPersistence(SOAP_PERSISTENCE_SESSION);
  429. $this->assertEquals(SOAP_PERSISTENCE_SESSION, $server->getPersistence());
  430. try {
  431. $server->setSoapVersion('bogus');
  432. $this->fail('Invalid soap versions should fail');
  433. } catch (Exception $e) {
  434. // success
  435. }
  436. $server->setPersistence(SOAP_PERSISTENCE_REQUEST);
  437. $this->assertEquals(SOAP_PERSISTENCE_REQUEST, $server->getPersistence());
  438. }
  439. public function testSetUnknownPersistenceStateThrowsException()
  440. {
  441. $server = new Zend_Soap_Server();
  442. try {
  443. $server->setPersistence('bogus');
  444. $this->fail();
  445. } catch(Zend_Soap_Server_Exception $e) {
  446. }
  447. }
  448. public function testGetPersistence()
  449. {
  450. $server = new Zend_Soap_Server();
  451. $this->assertNull($server->getPersistence());
  452. $server->setPersistence(SOAP_PERSISTENCE_SESSION);
  453. $this->assertEquals(SOAP_PERSISTENCE_SESSION, $server->getPersistence());
  454. }
  455. public function testGetLastRequest()
  456. {
  457. if (headers_sent()) {
  458. $this->markTestSkipped('Cannot run testGetLastRequest() when headers have already been sent; enable output buffering to run this test');
  459. return;
  460. }
  461. $server = new Zend_Soap_Server();
  462. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  463. $server->setReturnResponse(true);
  464. $server->setClass('Zend_Soap_Server_TestClass');
  465. $request =
  466. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  467. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  468. . 'xmlns:ns1="http://framework.zend.com" '
  469. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  470. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  471. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  472. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  473. . '<SOAP-ENV:Body>'
  474. . '<ns1:testFunc2>'
  475. . '<param0 xsi:type="xsd:string">World</param0>'
  476. . '</ns1:testFunc2>'
  477. . '</SOAP-ENV:Body>'
  478. . '</SOAP-ENV:Envelope>' . "\n";
  479. $response = $server->handle($request);
  480. $this->assertEquals($request, $server->getLastRequest());
  481. }
  482. public function testWsiCompliant()
  483. {
  484. $server = new Zend_Soap_Server(null, array('wsi_compliant' => true));
  485. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  486. $server->setReturnResponse(true);
  487. $server->setClass('Zend_Soap_Server_TestClass');
  488. $request =
  489. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  490. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  491. . 'xmlns:ns1="http://framework.zend.com" '
  492. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  493. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  494. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  495. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  496. . '<SOAP-ENV:Body>'
  497. . '<ns1:testFunc2>'
  498. . '<who>World</who>'
  499. . '</ns1:testFunc2>'
  500. . '</SOAP-ENV:Body>'
  501. . '</SOAP-ENV:Envelope>' . "\n";
  502. $expectedResult =
  503. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  504. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  505. . 'xmlns:ns1="http://framework.zend.com" '
  506. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  507. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  508. . 'xmlns:ns2="http://xml.apache.org/xml-soap" '
  509. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  510. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  511. . '<SOAP-ENV:Body>'
  512. . '<ns1:testFunc2Response>'
  513. . '<return xsi:type="ns2:Map">'
  514. . '<item>'
  515. . '<key xsi:type="xsd:string">testFunc2Result</key>'
  516. . '<value xsi:type="xsd:string">Hello World!</value>'
  517. . '</item>'
  518. . '</return>'
  519. . '</ns1:testFunc2Response>'
  520. . '</SOAP-ENV:Body>'
  521. . '</SOAP-ENV:Envelope>' . "\n";
  522. $response = $server->handle($request);
  523. $this->assertEquals($response, $expectedResult);
  524. }
  525. public function testSetReturnResponse()
  526. {
  527. $server = new Zend_Soap_Server();
  528. $this->assertFalse($server->getReturnResponse());
  529. $server->setReturnResponse(true);
  530. $this->assertTrue($server->getReturnResponse());
  531. $server->setReturnResponse(false);
  532. $this->assertFalse($server->getReturnResponse());
  533. }
  534. public function testGetReturnResponse()
  535. {
  536. $server = new Zend_Soap_Server();
  537. $this->assertFalse($server->getReturnResponse());
  538. $server->setReturnResponse(true);
  539. $this->assertTrue($server->getReturnResponse());
  540. }
  541. public function testGetLastResponse()
  542. {
  543. if (headers_sent()) {
  544. $this->markTestSkipped('Cannot run testGetLastResponse() when headers have already been sent; enable output buffering to run this test');
  545. return;
  546. }
  547. $server = new Zend_Soap_Server();
  548. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  549. $server->setReturnResponse(true);
  550. $server->setClass('Zend_Soap_Server_TestClass');
  551. $request =
  552. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  553. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  554. . 'xmlns:ns1="http://framework.zend.com" '
  555. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  556. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  557. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  558. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  559. . '<SOAP-ENV:Body>'
  560. . '<ns1:testFunc2>'
  561. . '<param0 xsi:type="xsd:string">World</param0>'
  562. . '</ns1:testFunc2>'
  563. . '</SOAP-ENV:Body>'
  564. . '</SOAP-ENV:Envelope>' . "\n";
  565. $expectedResponse =
  566. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  567. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  568. . 'xmlns:ns1="http://framework.zend.com" '
  569. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  570. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  571. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  572. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  573. . '<SOAP-ENV:Body>'
  574. . '<ns1:testFunc2Response>'
  575. . '<return xsi:type="xsd:string">Hello World!</return>'
  576. . '</ns1:testFunc2Response>'
  577. . '</SOAP-ENV:Body>'
  578. . '</SOAP-ENV:Envelope>' . "\n";
  579. $server->handle($request);
  580. $this->assertEquals($expectedResponse, $server->getLastResponse());
  581. }
  582. public function testHandle()
  583. {
  584. if (headers_sent()) {
  585. $this->markTestSkipped('Cannot run testHandle() when headers have already been sent; enable output buffering to run this test');
  586. return;
  587. }
  588. $server = new Zend_Soap_Server();
  589. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  590. $server->setClass('Zend_Soap_Server_TestClass');
  591. $localClient = new Zend_Soap_Server_TestLocalSoapClient($server,
  592. null,
  593. array('location'=>'test://',
  594. 'uri'=>'http://framework.zend.com'));
  595. // Local SOAP client call automatically invokes handle method of the provided SOAP server
  596. $this->assertEquals('Hello World!', $localClient->testFunc2('World'));
  597. $request =
  598. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  599. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  600. . 'xmlns:ns1="http://framework.zend.com" '
  601. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  602. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  603. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  604. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  605. . '<SOAP-ENV:Body>'
  606. . '<ns1:testFunc2>'
  607. . '<param0 xsi:type="xsd:string">World</param0>'
  608. . '</ns1:testFunc2>'
  609. . '</SOAP-ENV:Body>'
  610. . '</SOAP-ENV:Envelope>' . "\n";
  611. $expectedResponse =
  612. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  613. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  614. . 'xmlns:ns1="http://framework.zend.com" '
  615. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  616. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  617. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  618. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  619. . '<SOAP-ENV:Body>'
  620. . '<ns1:testFunc2Response>'
  621. . '<return xsi:type="xsd:string">Hello World!</return>'
  622. . '</ns1:testFunc2Response>'
  623. . '</SOAP-ENV:Body>'
  624. . '</SOAP-ENV:Envelope>' . "\n";
  625. $server1 = new Zend_Soap_Server();
  626. $server1->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  627. $server1->setClass('Zend_Soap_Server_TestClass');
  628. $server1->setReturnResponse(true);
  629. $this->assertEquals($expectedResponse, $server1->handle($request));
  630. }
  631. /**
  632. * @todo Implement testRegisterFaultException().
  633. */
  634. public function testRegisterFaultException()
  635. {
  636. $server = new Zend_Soap_Server();
  637. $server->registerFaultException("Zend_Soap_Server_Exception");
  638. $server->registerFaultException(array("OutOfBoundsException", "BogusException"));
  639. $this->assertEquals(array(
  640. 'Zend_Soap_Server_Exception',
  641. 'OutOfBoundsException',
  642. 'BogusException',
  643. ), $server->getFaultExceptions());
  644. }
  645. /**
  646. * @todo Implement testDeregisterFaultException().
  647. */
  648. public function testDeregisterFaultException()
  649. {
  650. $server = new Zend_Soap_Server();
  651. $server->registerFaultException(array("OutOfBoundsException", "BogusException"));
  652. $ret = $server->deregisterFaultException("BogusException");
  653. $this->assertTrue($ret);
  654. $this->assertEquals(array(
  655. 'OutOfBoundsException',
  656. ), $server->getFaultExceptions());
  657. $ret = $server->deregisterFaultException("NonRegisteredException");
  658. $this->assertFalse($ret);
  659. }
  660. /**
  661. * @todo Implement testGetFaultExceptions().
  662. */
  663. public function testGetFaultExceptions()
  664. {
  665. $server = new Zend_Soap_Server();
  666. $this->assertEquals(array(), $server->getFaultExceptions());
  667. $server->registerFaultException("Exception");
  668. $this->assertEquals(array("Exception"), $server->getFaultExceptions());
  669. }
  670. public function testFaultWithTextMessage()
  671. {
  672. $server = new Zend_Soap_Server();
  673. $fault = $server->fault("Faultmessage!");
  674. $this->assertTrue($fault instanceof SOAPFault);
  675. $this->assertContains("Faultmessage!", $fault->getMessage());
  676. }
  677. public function testFaultWithUnregisteredException()
  678. {
  679. $server = new Zend_Soap_Server();
  680. $fault = $server->fault(new Exception("MyException"));
  681. $this->assertTrue($fault instanceof SOAPFault);
  682. $this->assertContains("Unknown error", $fault->getMessage());
  683. $this->assertNotContains("MyException", $fault->getMessage());
  684. }
  685. public function testFaultWithRegisteredException()
  686. {
  687. $server = new Zend_Soap_Server();
  688. $server->registerFaultException("Exception");
  689. $fault = $server->fault(new Exception("MyException"));
  690. $this->assertTrue($fault instanceof SOAPFault);
  691. $this->assertNotContains("Unknown error", $fault->getMessage());
  692. $this->assertContains("MyException", $fault->getMessage());
  693. }
  694. public function testFautlWithBogusInput()
  695. {
  696. $server = new Zend_Soap_Server();
  697. $fault = $server->fault(array("Here", "There", "Bogus"));
  698. $this->assertContains("Unknown error", $fault->getMessage());
  699. }
  700. /**
  701. * @group ZF-3958
  702. */
  703. public function testFaultWithIntegerFailureCodeDoesNotBreakClassSoapFault()
  704. {
  705. $server = new Zend_Soap_Server();
  706. $fault = $server->fault("Faultmessage!", 5000);
  707. $this->assertTrue($fault instanceof SOAPFault);
  708. }
  709. /**
  710. * @todo Implement testHandlePhpErrors().
  711. */
  712. public function testHandlePhpErrors()
  713. {
  714. $server = new Zend_Soap_Server();
  715. // Remove the following line when you implement this test.
  716. $this->markTestIncomplete(
  717. "This test has not been implemented yet."
  718. );
  719. }
  720. public function testLoadFunctionsIsNotImplemented()
  721. {
  722. $server = new Zend_Soap_Server();
  723. try {
  724. $server->loadFunctions("bogus");
  725. $this->fail();
  726. } catch(Zend_Soap_Server_Exception $e) {
  727. }
  728. }
  729. public function testErrorHandlingOfSoapServerChangesToThrowingSoapFaultWhenInHandleMode()
  730. {
  731. if (headers_sent()) {
  732. $this->markTestSkipped('Cannot run ' . __METHOD__ . '() when headers have already been sent; enable output buffering to run this test');
  733. return;
  734. }
  735. $server = new Zend_Soap_Server();
  736. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  737. $server->setReturnResponse(true);
  738. // Requesting Method with enforced parameter without it.
  739. $request =
  740. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  741. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  742. . 'xmlns:ns1="http://framework.zend.com" '
  743. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  744. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  745. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  746. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  747. . '<SOAP-ENV:Body>'
  748. . '<ns1:testFunc5 />'
  749. . '</SOAP-ENV:Body>'
  750. . '</SOAP-ENV:Envelope>' . "\n";
  751. $server->setClass('Zend_Soap_Server_TestClass');
  752. $response = $server->handle($request);
  753. $this->assertContains(
  754. '<SOAP-ENV:Fault><faultcode>Receiver</faultcode><faultstring>Test Message</faultstring></SOAP-ENV:Fault>',
  755. $response
  756. );
  757. }
  758. /**
  759. * @group ZF-5597
  760. */
  761. public function testServerAcceptsZendConfigObject()
  762. {
  763. $options = array('soap_version' => SOAP_1_1,
  764. 'actor' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  765. 'classmap' => array('TestData1' => 'Zend_Soap_Server_TestData1',
  766. 'TestData2' => 'Zend_Soap_Server_TestData2',),
  767. 'encoding' => 'ISO-8859-1',
  768. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php'
  769. );
  770. $config = new Zend_Config($options);
  771. $server = new Zend_Soap_Server();
  772. $server->setOptions($config);
  773. $this->assertEquals($options, $server->getOptions());
  774. }
  775. /**
  776. * @group ZF-5300
  777. */
  778. public function testSetAndGetFeatures()
  779. {
  780. $server = new Zend_Soap_Server();
  781. $this->assertNull($server->getSoapFeatures());
  782. $server->setSoapFeatures(100);
  783. $this->assertEquals(100, $server->getSoapFeatures());
  784. $options = $server->getOptions();
  785. $this->assertTrue(isset($options['features']));
  786. $this->assertEquals(100, $options['features']);
  787. }
  788. /**
  789. * @group ZF-5300
  790. */
  791. public function testSetAndGetWsdlCache()
  792. {
  793. $server = new Zend_Soap_Server();
  794. $this->assertNull($server->getWsdlCache());
  795. $server->setWsdlCache(100);
  796. $this->assertEquals(100, $server->getWsdlCache());
  797. $options = $server->getOptions();
  798. $this->assertTrue(isset($options['cache_wsdl']));
  799. $this->assertEquals(100, $options['cache_wsdl']);
  800. }
  801. /**
  802. * @group ZF-11411
  803. */
  804. public function testHandleUsesProperRequestParameter()
  805. {
  806. $server = new Zend_Soap_MockServer();
  807. $r = $server->handle(new DomDocument('1.0', 'UTF-8'));
  808. $this->assertTrue(is_string($server->mockSoapServer->handle[0]));
  809. }
  810. }
  811. if (extension_loaded('soap')) {
  812. /** Local SOAP client */
  813. class Zend_Soap_Server_TestLocalSoapClient extends SoapClient
  814. {
  815. /**
  816. * Server object
  817. *
  818. * @var Zend_Soap_Server
  819. */
  820. public $server;
  821. /**
  822. * Local client constructor
  823. *
  824. * @param Zend_Soap_Server $server
  825. * @param string $wsdl
  826. * @param array $options
  827. */
  828. function __construct(Zend_Soap_Server $server, $wsdl, $options)
  829. {
  830. $this->server = $server;
  831. parent::__construct($wsdl, $options);
  832. }
  833. function __doRequest($request, $location, $action, $version, $one_way = 0)
  834. {
  835. ob_start();
  836. $this->server->handle($request);
  837. $response = ob_get_clean();
  838. return $response;
  839. }
  840. }
  841. }
  842. class MockSoapServer {
  843. public $handle = null;
  844. public function handle()
  845. {
  846. $this->handle = func_get_args();
  847. }
  848. public function __call($name, $args) {}
  849. }
  850. class Zend_Soap_MockServer extends Zend_Soap_Server {
  851. public $mockSoapServer = null;
  852. protected function _getSoap() {
  853. $this->mockSoapServer = new MockSoapServer();
  854. return $this->mockSoapServer;
  855. }
  856. }
  857. /** Test Class */
  858. class Zend_Soap_Server_TestClass {
  859. /**
  860. * Test Function 1
  861. *
  862. * @return string
  863. */
  864. function testFunc1()
  865. {
  866. return "Hello World";
  867. }
  868. /**
  869. * Test Function 2
  870. *
  871. * @param string $who Some Arg
  872. * @return string
  873. */
  874. function testFunc2($who)
  875. {
  876. return "Hello $who!";
  877. }
  878. /**
  879. * Test Function 3
  880. *
  881. * @param string $who Some Arg
  882. * @param int $when Some
  883. * @return string
  884. */
  885. function testFunc3($who, $when)
  886. {
  887. return "Hello $who, How are you $when";
  888. }
  889. /**
  890. * Test Function 4
  891. *
  892. * @return string
  893. */
  894. static function testFunc4()
  895. {
  896. return "I'm Static!";
  897. }
  898. /**
  899. * Test Function 5 raises a user error
  900. *
  901. * @return void
  902. */
  903. function testFunc5()
  904. {
  905. trigger_error("Test Message", E_USER_ERROR);
  906. }
  907. }
  908. /** Test class 2 */
  909. class Zend_Soap_Server_TestData1 {
  910. /**
  911. * Property1
  912. *
  913. * @var string
  914. */
  915. public $property1;
  916. /**
  917. * Property2
  918. *
  919. * @var float
  920. */
  921. public $property2;
  922. }
  923. /** Test class 2 */
  924. class Zend_Soap_Server_TestData2 {
  925. /**
  926. * Property1
  927. *
  928. * @var integer
  929. */
  930. public $property1;
  931. /**
  932. * Property1
  933. *
  934. * @var float
  935. */
  936. public $property2;
  937. }
  938. /* Test Functions */
  939. /**
  940. * Test Function
  941. *
  942. * @param string $arg
  943. * @return string
  944. */
  945. function Zend_Soap_Server_TestFunc1($who)
  946. {
  947. return "Hello $who";
  948. }
  949. /**
  950. * Test Function 2
  951. */
  952. function Zend_Soap_Server_TestFunc2()
  953. {
  954. return "Hello World";
  955. }
  956. /**
  957. * Return false
  958. *
  959. * @return bool
  960. */
  961. function Zend_Soap_Server_TestFunc3()
  962. {
  963. return false;
  964. }
  965. /**
  966. * Return true
  967. *
  968. * @return bool
  969. */
  970. function Zend_Soap_Server_TestFunc4()
  971. {
  972. return true;
  973. }
  974. /**
  975. * Return integer
  976. *
  977. * @return int
  978. */
  979. function Zend_Soap_Server_TestFunc5()
  980. {
  981. return 123;
  982. }
  983. /**
  984. * Return string
  985. *
  986. * @return string
  987. */
  988. function Zend_Soap_Server_TestFunc6()
  989. {
  990. return "string";
  991. }