ServerTest.php 38 KB

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