ServerTest.php 32 KB

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