ServerTest.php 38 KB

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