ServerTest.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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-2014 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-2014 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. * @runInSeparateProcess
  824. * @preserveGlobalState disabled
  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. if (extension_loaded('soap')) {
  852. /** Local SOAP client */
  853. class Zend_Soap_Server_TestLocalSoapClient extends SoapClient
  854. {
  855. /**
  856. * Server object
  857. *
  858. * @var Zend_Soap_Server
  859. */
  860. public $server;
  861. /**
  862. * Local client constructor
  863. *
  864. * @param Zend_Soap_Server $server
  865. * @param string $wsdl
  866. * @param array $options
  867. */
  868. function __construct(Zend_Soap_Server $server, $wsdl, $options)
  869. {
  870. $this->server = $server;
  871. parent::__construct($wsdl, $options);
  872. }
  873. function __doRequest($request, $location, $action, $version, $one_way = 0)
  874. {
  875. ob_start();
  876. $this->server->handle($request);
  877. $response = ob_get_clean();
  878. return $response;
  879. }
  880. }
  881. }
  882. class MockSoapServer {
  883. public $handle = null;
  884. public function handle()
  885. {
  886. $this->handle = func_get_args();
  887. }
  888. public function __call($name, $args) {}
  889. }
  890. class Zend_Soap_MockServer extends Zend_Soap_Server {
  891. public $mockSoapServer = null;
  892. protected function _getSoap() {
  893. $this->mockSoapServer = new MockSoapServer();
  894. return $this->mockSoapServer;
  895. }
  896. }
  897. /** Test Class */
  898. class Zend_Soap_Server_TestClass {
  899. /**
  900. * Test Function 1
  901. *
  902. * @return string
  903. */
  904. function testFunc1()
  905. {
  906. return "Hello World";
  907. }
  908. /**
  909. * Test Function 2
  910. *
  911. * @param string $who Some Arg
  912. * @return string
  913. */
  914. function testFunc2($who)
  915. {
  916. return "Hello $who!";
  917. }
  918. /**
  919. * Test Function 3
  920. *
  921. * @param string $who Some Arg
  922. * @param int $when Some
  923. * @return string
  924. */
  925. function testFunc3($who, $when)
  926. {
  927. return "Hello $who, How are you $when";
  928. }
  929. /**
  930. * Test Function 4
  931. *
  932. * @return string
  933. */
  934. static function testFunc4()
  935. {
  936. return "I'm Static!";
  937. }
  938. /**
  939. * Test Function 5 raises a user error
  940. *
  941. * @return void
  942. */
  943. function testFunc5()
  944. {
  945. trigger_error("Test Message", E_USER_ERROR);
  946. }
  947. }
  948. /** Test class 2 */
  949. class Zend_Soap_Server_TestData1 {
  950. /**
  951. * Property1
  952. *
  953. * @var string
  954. */
  955. public $property1;
  956. /**
  957. * Property2
  958. *
  959. * @var float
  960. */
  961. public $property2;
  962. }
  963. /** Test class 2 */
  964. class Zend_Soap_Server_TestData2 {
  965. /**
  966. * Property1
  967. *
  968. * @var integer
  969. */
  970. public $property1;
  971. /**
  972. * Property1
  973. *
  974. * @var float
  975. */
  976. public $property2;
  977. }
  978. /* Test Functions */
  979. /**
  980. * Test Function
  981. *
  982. * @param string $arg
  983. * @return string
  984. */
  985. function Zend_Soap_Server_TestFunc1($who)
  986. {
  987. return "Hello $who";
  988. }
  989. /**
  990. * Test Function 2
  991. */
  992. function Zend_Soap_Server_TestFunc2()
  993. {
  994. return "Hello World";
  995. }
  996. /**
  997. * Return false
  998. *
  999. * @return bool
  1000. */
  1001. function Zend_Soap_Server_TestFunc3()
  1002. {
  1003. return false;
  1004. }
  1005. /**
  1006. * Return true
  1007. *
  1008. * @return bool
  1009. */
  1010. function Zend_Soap_Server_TestFunc4()
  1011. {
  1012. return true;
  1013. }
  1014. /**
  1015. * Return integer
  1016. *
  1017. * @return int
  1018. */
  1019. function Zend_Soap_Server_TestFunc5()
  1020. {
  1021. return 123;
  1022. }
  1023. /**
  1024. * Return string
  1025. *
  1026. * @return string
  1027. */
  1028. function Zend_Soap_Server_TestFunc6()
  1029. {
  1030. return "string";
  1031. }