ServerTest.php 33 KB

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