ServerTest.php 32 KB

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