PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Soap/ServerTest.php

https://github.com/eiwaen/zf2
PHP | 993 lines | 719 code | 189 blank | 85 comment | 20 complexity | b98ce9db6a282a19e7c384b26207daa4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Soap;
  10. require_once __DIR__ . '/TestAsset/commontypes.php';
  11. use Zend\Soap\AutoDiscover;
  12. use Zend\Soap\Server;
  13. /**
  14. * Zend_Soap_Server
  15. *
  16. * @group Zend_Soap
  17. * @group Zend_Soap_Server
  18. */
  19. class ServerTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function setUp()
  22. {
  23. if (!extension_loaded('soap')) {
  24. $this->markTestSkipped('SOAP Extension is not loaded');
  25. }
  26. }
  27. public function testSetOptions()
  28. {
  29. $server = new Server();
  30. $this->assertTrue($server->getOptions() == array('soap_version' => SOAP_1_2));
  31. $options = array('soap_version' => SOAP_1_1,
  32. 'actor' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  33. 'classmap' => array('TestData1' => '\ZendTest\Soap\TestAsset\TestData1',
  34. 'TestData2' => '\ZendTest\Soap\TestAsset\TestData2',),
  35. 'encoding' => 'ISO-8859-1',
  36. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php'
  37. );
  38. $server->setOptions($options);
  39. $this->assertTrue($server->getOptions() == $options);
  40. }
  41. public function testSetOptionsViaSecondConstructorArgument()
  42. {
  43. $options = array(
  44. 'soap_version' => SOAP_1_1,
  45. 'actor' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  46. 'classmap' => array(
  47. 'TestData1' => '\ZendTest\Soap\TestAsset\TestData1',
  48. 'TestData2' => '\ZendTest\Soap\TestAsset\TestData2',
  49. ),
  50. 'encoding' => 'ISO-8859-1',
  51. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  52. );
  53. $server = new Server(null, $options);
  54. $this->assertTrue($server->getOptions() == $options);
  55. }
  56. /**
  57. * @group ZF-9816
  58. */
  59. public function testSetOptionsWithFeaturesOption()
  60. {
  61. $server = new Server(null, array(
  62. 'features' => SOAP_SINGLE_ELEMENT_ARRAYS
  63. ));
  64. $this->assertEquals(
  65. SOAP_SINGLE_ELEMENT_ARRAYS,
  66. $server->getSoapFeatures()
  67. );
  68. }
  69. public function testSetWsdlViaOptionsArrayIsPossible()
  70. {
  71. $server = new Server();
  72. $server->setOptions(array('wsdl' => 'http://www.example.com/test.wsdl'));
  73. $this->assertEquals('http://www.example.com/test.wsdl', $server->getWSDL());
  74. }
  75. public function testGetOptions()
  76. {
  77. $server = new Server();
  78. $this->assertTrue($server->getOptions() == array('soap_version' => SOAP_1_2));
  79. $options = array('soap_version' => SOAP_1_1,
  80. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php'
  81. );
  82. $server->setOptions($options);
  83. $this->assertTrue($server->getOptions() == $options);
  84. }
  85. public function testEncoding()
  86. {
  87. $server = new Server();
  88. $this->assertNull($server->getEncoding());
  89. $server->setEncoding('ISO-8859-1');
  90. $this->assertEquals('ISO-8859-1', $server->getEncoding());
  91. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid encoding specified');
  92. $server->setEncoding(array('UTF-8'));
  93. }
  94. public function testSoapVersion()
  95. {
  96. $server = new Server();
  97. $this->assertEquals(SOAP_1_2, $server->getSoapVersion());
  98. $server->setSoapVersion(SOAP_1_1);
  99. $this->assertEquals(SOAP_1_1, $server->getSoapVersion());
  100. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid soap version specified');
  101. $server->setSoapVersion('bogus');
  102. }
  103. public function testValidateUrn()
  104. {
  105. $server = new Server();
  106. $this->assertTrue($server->validateUrn('http://framework.zend.com/'));
  107. $this->assertTrue($server->validateUrn('urn:soapHandler/GetOpt'));
  108. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid URN');
  109. $server->validateUrn('bogosity');
  110. }
  111. public function testSetActor()
  112. {
  113. $server = new Server();
  114. $this->assertNull($server->getActor());
  115. $server->setActor('http://framework.zend.com/');
  116. $this->assertEquals('http://framework.zend.com/', $server->getActor());
  117. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid URN');
  118. $server->setActor('bogus');
  119. }
  120. public function testGetActor()
  121. {
  122. $server = new Server();
  123. $this->assertNull($server->getActor());
  124. $server->setActor('http://framework.zend.com/');
  125. $this->assertEquals('http://framework.zend.com/', $server->getActor());
  126. }
  127. public function testSetUri()
  128. {
  129. $server = new Server();
  130. $this->assertNull($server->getUri());
  131. $server->setUri('http://framework.zend.com/');
  132. $this->assertEquals('http://framework.zend.com/', $server->getUri());
  133. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid URN');
  134. $server->setUri('bogus');
  135. }
  136. public function testGetUri()
  137. {
  138. $server = new Server();
  139. $this->assertNull($server->getUri());
  140. $server->setUri('http://framework.zend.com/');
  141. $this->assertEquals('http://framework.zend.com/', $server->getUri());
  142. }
  143. public function testSetClassmap()
  144. {
  145. $server = new Server();
  146. $classmap = array('TestData1' => '\ZendTest\Soap\TestAsset\TestData1',
  147. 'TestData2' => '\ZendTest\Soap\TestAsset\TestData2');
  148. $this->assertNull($server->getClassmap());
  149. $server->setClassmap($classmap);
  150. $this->assertTrue($classmap == $server->getClassmap());
  151. }
  152. public function testSetClassmapThrowsExceptionOnBogusStringParameter()
  153. {
  154. $server = new Server();
  155. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Classmap must be an array');
  156. $server->setClassmap('bogus');
  157. }
  158. public function testSetClassmapThrowsExceptionOnBogusArrayParameter()
  159. {
  160. $server = new Server();
  161. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid class in class map');
  162. $server->setClassmap(array('soapTypeName', 'bogusClassName'));
  163. }
  164. public function testGetClassmap()
  165. {
  166. $server = new Server();
  167. $classmap = array('TestData1' => '\ZendTest\Soap\TestAsset\TestData1',
  168. 'TestData2' => '\ZendTest\Soap\TestAsset\TestData2');
  169. $this->assertNull($server->getClassmap());
  170. $server->setClassmap($classmap);
  171. $this->assertTrue($classmap == $server->getClassmap());
  172. }
  173. public function testSetWsdl()
  174. {
  175. $server = new Server();
  176. $this->assertNull($server->getWSDL());
  177. $server->setWSDL(__DIR__.'/_files/wsdl_example.wsdl');
  178. $this->assertEquals(__DIR__.'/_files/wsdl_example.wsdl', $server->getWSDL());
  179. //$this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'foo');
  180. $server->setWSDL(__DIR__.'/_files/bogus.wsdl');
  181. }
  182. public function testGetWsdl()
  183. {
  184. $server = new Server();
  185. $this->assertNull($server->getWSDL());
  186. $server->setWSDL(__DIR__.'/_files/wsdl_example.wsdl');
  187. $this->assertEquals(__DIR__.'/_files/wsdl_example.wsdl', $server->getWSDL());
  188. }
  189. public function testAddFunction()
  190. {
  191. $server = new Server();
  192. // Correct function should pass
  193. $server->addFunction('\ZendTest\Soap\TestAsset\TestFunc');
  194. // Array of correct functions should pass
  195. $functions = array('\ZendTest\Soap\TestAsset\TestFunc2',
  196. '\ZendTest\Soap\TestAsset\TestFunc3',
  197. '\ZendTest\Soap\TestAsset\TestFunc4');
  198. $server->addFunction($functions);
  199. $this->assertEquals(
  200. array_merge(array('\ZendTest\Soap\TestAsset\TestFunc'), $functions),
  201. $server->getFunctions()
  202. );
  203. }
  204. public function testAddBogusFunctionAsInteger()
  205. {
  206. $server = new Server();
  207. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid function specified');
  208. $server->addFunction(126);
  209. }
  210. public function testAddBogusFunctionsAsString()
  211. {
  212. $server = new Server();
  213. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid function specified');
  214. $server->addFunction('bogus_function');
  215. }
  216. public function testAddBogusFunctionsAsArray()
  217. {
  218. $server = new Server();
  219. $functions = array('\ZendTest\Soap\TestAsset\TestFunc5',
  220. 'bogus_function',
  221. '\ZendTest\Soap\TestAsset\TestFunc6');
  222. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'One or more invalid functions specified in array');
  223. $server->addFunction($functions);
  224. }
  225. public function testAddAllFunctionsSoapConstant()
  226. {
  227. $server = new Server();
  228. // SOAP_FUNCTIONS_ALL as a value should pass
  229. $server->addFunction(SOAP_FUNCTIONS_ALL);
  230. $server->addFunction('substr');
  231. $this->assertEquals(array(SOAP_FUNCTIONS_ALL), $server->getFunctions());
  232. }
  233. public function testSetClass()
  234. {
  235. $server = new Server();
  236. // Correct class name should pass
  237. $r = $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  238. $this->assertSame($server, $r);
  239. }
  240. /**
  241. * @group PR-706
  242. */
  243. public function testSetClassWithObject()
  244. {
  245. $server = new Server();
  246. // Correct class name should pass
  247. $object = new \ZendTest\Soap\TestAsset\ServerTestClass();
  248. $r = $server->setClass($object);
  249. $this->assertSame($server, $r);
  250. }
  251. public function testSetClassTwiceThrowsException()
  252. {
  253. $server = new Server();
  254. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  255. $this->setExpectedException(
  256. 'Zend\Soap\Exception\InvalidArgumentException',
  257. 'A class has already been registered with this soap server instance'
  258. );
  259. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  260. }
  261. public function testSetClassWithArguments()
  262. {
  263. $server = new Server();
  264. // Correct class name should pass
  265. $r = $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass', null, 1, 2, 3, 4);
  266. $this->assertSame($server, $r);
  267. }
  268. public function testSetBogusClassWithIntegerName()
  269. {
  270. $server = new Server();
  271. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid class argument (integer)');
  272. $server->setClass(465);
  273. }
  274. public function testSetBogusClassWithUnknownClassName()
  275. {
  276. $server = new Server();
  277. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Class "Zend_Soap_Server_Test_BogusClass" does not exist');
  278. $server->setClass('Zend_Soap_Server_Test_BogusClass');
  279. }
  280. /**
  281. * @group ZF-4366
  282. */
  283. public function testSetObject()
  284. {
  285. $server = new Server();
  286. // Correct class name should pass
  287. $r = $server->setObject(new TestAsset\ServerTestClass());
  288. $this->assertSame($server, $r);
  289. }
  290. /**
  291. * @group ZF-4366
  292. */
  293. public function testSetObjectThrowsExceptionWithBadInput1()
  294. {
  295. $server = new Server();
  296. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid object argument (integer)');
  297. $server->setObject(465);
  298. }
  299. /**
  300. * @group ZF-4366
  301. */
  302. public function testSetObjectThrowsExceptionWithBadInput2()
  303. {
  304. $server = new Server();
  305. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid object argument (integer)');
  306. $int = 1;
  307. $server->setObject($int);
  308. }
  309. /**
  310. * @group ZF-4366
  311. */
  312. public function testSetObjectThrowsExceptionWithBadInput3()
  313. {
  314. $server = new Server();
  315. //$this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'foo');
  316. $server->setObject(new TestAsset\ServerTestClass());
  317. }
  318. public function testGetFunctions()
  319. {
  320. $server = new Server();
  321. $server->addFunction('\ZendTest\Soap\TestAsset\TestFunc');
  322. $functions = array('\ZendTest\Soap\TestAsset\TestFunc2',
  323. '\ZendTest\Soap\TestAsset\TestFunc3',
  324. '\ZendTest\Soap\TestAsset\TestFunc4');
  325. $server->addFunction($functions);
  326. $functions = array('\ZendTest\Soap\TestAsset\TestFunc3',
  327. '\ZendTest\Soap\TestAsset\TestFunc5',
  328. '\ZendTest\Soap\TestAsset\TestFunc6');
  329. $server->addFunction($functions);
  330. $allAddedFunctions = array(
  331. '\ZendTest\Soap\TestAsset\TestFunc',
  332. '\ZendTest\Soap\TestAsset\TestFunc2',
  333. '\ZendTest\Soap\TestAsset\TestFunc3',
  334. '\ZendTest\Soap\TestAsset\TestFunc4',
  335. '\ZendTest\Soap\TestAsset\TestFunc5',
  336. '\ZendTest\Soap\TestAsset\TestFunc6'
  337. );
  338. $this->assertTrue($server->getFunctions() == $allAddedFunctions);
  339. }
  340. public function testGetFunctionsWithClassAttached()
  341. {
  342. $server = new Server();
  343. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  344. $this->assertEquals(
  345. array('testFunc1', 'testFunc2', 'testFunc3', 'testFunc4', 'testFunc5'),
  346. $server->getFunctions()
  347. );
  348. }
  349. public function testGetFunctionsWithObjectAttached()
  350. {
  351. $server = new Server();
  352. $server->setObject(new TestAsset\ServerTestClass());
  353. $this->assertEquals(
  354. array('testFunc1', 'testFunc2', 'testFunc3', 'testFunc4', 'testFunc5'),
  355. $server->getFunctions()
  356. );
  357. }
  358. public function testSetPersistence()
  359. {
  360. $server = new Server();
  361. $this->assertNull($server->getPersistence());
  362. $server->setPersistence(SOAP_PERSISTENCE_SESSION);
  363. $this->assertEquals(SOAP_PERSISTENCE_SESSION, $server->getPersistence());
  364. $server->setPersistence(SOAP_PERSISTENCE_REQUEST);
  365. $this->assertEquals(SOAP_PERSISTENCE_REQUEST, $server->getPersistence());
  366. }
  367. public function testSetUnknownPersistenceStateThrowsException()
  368. {
  369. $server = new Server();
  370. $this->setExpectedException('Zend\Soap\Exception\InvalidArgumentException', 'Invalid persistence mode specified');
  371. $server->setPersistence('bogus');
  372. }
  373. public function testGetPersistence()
  374. {
  375. $server = new Server();
  376. $this->assertNull($server->getPersistence());
  377. $server->setPersistence(SOAP_PERSISTENCE_SESSION);
  378. $this->assertEquals(SOAP_PERSISTENCE_SESSION, $server->getPersistence());
  379. }
  380. public function testGetLastRequest()
  381. {
  382. if (headers_sent()) {
  383. $this->markTestSkipped('Cannot run testGetLastRequest() when headers have already been sent; enable output buffering to run this test');
  384. return;
  385. }
  386. $server = new Server();
  387. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  388. $server->setReturnResponse(true);
  389. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  390. $request =
  391. '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  392. . 'xmlns:ns1="http://framework.zend.com" '
  393. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  394. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  395. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  396. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  397. . '<SOAP-ENV:Body>'
  398. . '<ns1:testFunc2>'
  399. . '<param0 xsi:type="xsd:string">World</param0>'
  400. . '</ns1:testFunc2>'
  401. . '</SOAP-ENV:Body>'
  402. . '</SOAP-ENV:Envelope>';
  403. $response = $server->handle($request);
  404. $this->assertEquals($request, $server->getLastRequest());
  405. }
  406. public function testSetReturnResponse()
  407. {
  408. $server = new Server();
  409. $this->assertFalse($server->getReturnResponse());
  410. $server->setReturnResponse(true);
  411. $this->assertTrue($server->getReturnResponse());
  412. $server->setReturnResponse(false);
  413. $this->assertFalse($server->getReturnResponse());
  414. }
  415. public function testGetReturnResponse()
  416. {
  417. $server = new Server();
  418. $this->assertFalse($server->getReturnResponse());
  419. $server->setReturnResponse(true);
  420. $this->assertTrue($server->getReturnResponse());
  421. }
  422. public function testGetLastResponse()
  423. {
  424. if (headers_sent()) {
  425. $this->markTestSkipped('Cannot run testGetLastResponse() when headers have already been sent; enable output buffering to run this test');
  426. return;
  427. }
  428. $server = new Server();
  429. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  430. $server->setReturnResponse(true);
  431. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  432. $request =
  433. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  434. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  435. . 'xmlns:ns1="http://framework.zend.com" '
  436. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  437. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  438. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  439. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  440. . '<SOAP-ENV:Body>'
  441. . '<ns1:testFunc2>'
  442. . '<param0 xsi:type="xsd:string">World</param0>'
  443. . '</ns1:testFunc2>'
  444. . '</SOAP-ENV:Body>'
  445. . '</SOAP-ENV:Envelope>' . "\n";
  446. $expectedResponse =
  447. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  448. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  449. . 'xmlns:ns1="http://framework.zend.com" '
  450. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  451. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  452. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  453. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  454. . '<SOAP-ENV:Body>'
  455. . '<ns1:testFunc2Response>'
  456. . '<return xsi:type="xsd:string">Hello World!</return>'
  457. . '</ns1:testFunc2Response>'
  458. . '</SOAP-ENV:Body>'
  459. . '</SOAP-ENV:Envelope>' . "\n";
  460. $server->handle($request);
  461. $this->assertEquals($expectedResponse, $server->getResponse());
  462. }
  463. public function testHandle()
  464. {
  465. if (!extension_loaded('soap')) {
  466. $this->markTestSkipped('Soap extension not loaded');
  467. }
  468. if (headers_sent()) {
  469. $this->markTestSkipped('Cannot run testHandle() when headers have already been sent; enable output buffering to run this test');
  470. return;
  471. }
  472. $server = new Server();
  473. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  474. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  475. $localClient = new TestAsset\TestLocalSoapClient($server,
  476. null,
  477. array('location'=>'test://',
  478. 'uri'=>'http://framework.zend.com'));
  479. // Local SOAP client call automatically invokes handle method of the provided SOAP server
  480. $this->assertEquals('Hello World!', $localClient->testFunc2('World'));
  481. $request =
  482. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  483. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  484. . 'xmlns:ns1="http://framework.zend.com" '
  485. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  486. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  487. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  488. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  489. . '<SOAP-ENV:Body>'
  490. . '<ns1:testFunc2>'
  491. . '<param0 xsi:type="xsd:string">World</param0>'
  492. . '</ns1:testFunc2>'
  493. . '</SOAP-ENV:Body>'
  494. . '</SOAP-ENV:Envelope>' . "\n";
  495. $expectedResponse =
  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:testFunc2Response>'
  505. . '<return xsi:type="xsd:string">Hello World!</return>'
  506. . '</ns1:testFunc2Response>'
  507. . '</SOAP-ENV:Body>'
  508. . '</SOAP-ENV:Envelope>' . "\n";
  509. $server1 = new Server();
  510. $server1->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  511. $server1->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  512. $server1->setReturnResponse(true);
  513. $this->assertEquals($expectedResponse, $server1->handle($request));
  514. }
  515. /**
  516. * @dataProvider dataProviderForRegisterFaultException
  517. *
  518. * @param string|array $exception
  519. */
  520. public function testRegisterFaultException($exception)
  521. {
  522. $server = new Server();
  523. $server->registerFaultException($exception);
  524. if (!is_array($exception)) {
  525. $this->assertContains($exception, $server->getFaultExceptions());
  526. } else {
  527. foreach ($exception as $row) {
  528. $this->assertContains($row, $server->getFaultExceptions());
  529. }
  530. }
  531. }
  532. /**
  533. * @dataProvider dataProviderForRegisterFaultException
  534. *
  535. * @param string|array $exception
  536. */
  537. public function testDeregisterFaultException($exception)
  538. {
  539. $server = new Server();
  540. $server->registerFaultException($exception);
  541. if (is_array($exception)) {
  542. $exception = array_shift($exception);
  543. }
  544. $this->assertTrue($server->deregisterFaultException($exception));
  545. $this->assertNotContains($exception, $server->getFaultExceptions());
  546. }
  547. /**
  548. * @dataProvider dataProviderForRegisterFaultException
  549. *
  550. * @param string|array $exception
  551. */
  552. public function testIsRegisteredAsFaultException($exception)
  553. {
  554. $server = new Server();
  555. $server->registerFaultException($exception);
  556. if (!is_array($exception)) {
  557. $this->assertTrue($server->isRegisteredAsFaultException($exception));
  558. } else {
  559. foreach ($exception as $row) {
  560. $this->assertTrue($server->isRegisteredAsFaultException($row));
  561. }
  562. }
  563. }
  564. /**
  565. * @return array
  566. */
  567. public function dataProviderForRegisterFaultException()
  568. {
  569. return array(
  570. array('Exception'),
  571. array('Zend\Soap\Exception\InvalidArgumentException'),
  572. array('InvalidArgumentException'),
  573. array('Zend\Server\Exception\RuntimeException'),
  574. array(array('Zend\Server\Exception\RuntimeException')),
  575. array(array('Zend\Server\Exception\RuntimeException', 'InvalidArgumentException')),
  576. );
  577. }
  578. public function testFaultWithTextMessage()
  579. {
  580. $server = new Server();
  581. $fault = $server->fault('FaultMessage!');
  582. $this->assertTrue($fault instanceof \SoapFault);
  583. $this->assertContains('FaultMessage!', $fault->getMessage());
  584. }
  585. public function testFaultWithUnregisteredException()
  586. {
  587. $server = new Server();
  588. $fault = $server->fault(new \Exception('MyException'));
  589. $this->assertTrue($fault instanceof \SoapFault);
  590. $this->assertContains('Unknown error', $fault->getMessage());
  591. $this->assertNotContains('MyException', $fault->getMessage());
  592. }
  593. public function testFaultWithRegisteredException()
  594. {
  595. $server = new Server();
  596. $server->registerFaultException('\Zend\Soap\Exception\RuntimeException');
  597. $server->registerFaultException('\Zend\Soap\Exception\InvalidArgumentException');
  598. $fault = $server->fault(new \Zend\Soap\Exception\RuntimeException('MyException'));
  599. $this->assertTrue($fault instanceof \SoapFault);
  600. $this->assertNotContains('Unknown error', $fault->getMessage());
  601. $this->assertContains('MyException', $fault->getMessage());
  602. }
  603. public function testFaultWithBogusInput()
  604. {
  605. $server = new Server();
  606. $fault = $server->fault(array('Here', 'There', 'Bogus'));
  607. $this->assertContains('Unknown error', $fault->getMessage());
  608. }
  609. /**
  610. * @group ZF-3958
  611. */
  612. public function testFaultWithIntegerFailureCodeDoesNotBreakClassSoapFault()
  613. {
  614. $server = new Server();
  615. $fault = $server->fault("FaultMessage!", 5000);
  616. $this->assertTrue($fault instanceof \SoapFault);
  617. }
  618. /**
  619. * @expectedException \SoapFault
  620. */
  621. public function testHandlePhpErrors()
  622. {
  623. if (headers_sent()) {
  624. $this->markTestSkipped('Cannot run ' . __METHOD__ . '() when headers have already been sent; enable output buffering to run this test');
  625. return;
  626. }
  627. $wsdlFilename = __DIR__ . '/TestAsset/testHandlePhpErrors.wsdl';
  628. $autodiscover = new AutoDiscover();
  629. $autodiscover->setOperationBodyStyle(array(
  630. 'use' => 'literal',
  631. ));
  632. $autodiscover->setBindingStyle(array(
  633. 'style' => 'document',
  634. 'transport' => 'http://schemas.xmlsoap.org/soap/http'
  635. ));
  636. $autodiscover->setServiceName('ExampleService');
  637. $autodiscover->setUri('http://example.com');
  638. $autodiscover->setClass('\ZendTest\Soap\TestAsset\errorClass');
  639. $wsdl = $autodiscover->generate();
  640. $wsdl->dump($wsdlFilename);
  641. $server = new Server($wsdlFilename);
  642. $server->setClass('\ZendTest\Soap\TestAsset\errorClass');
  643. $client = new \Zend\Soap\Client\Local($server, $wsdlFilename);
  644. $client->triggerError();
  645. unlink($wsdlFilename);
  646. }
  647. public function testLoadFunctionsIsNotImplemented()
  648. {
  649. $server = new Server();
  650. $this->setExpectedException('Zend\Soap\Exception\RuntimeException', 'Unimplemented method');
  651. $server->loadFunctions("bogus");
  652. }
  653. public function testErrorHandlingOfSoapServerChangesToThrowingSoapFaultWhenInHandleMode()
  654. {
  655. if (headers_sent()) {
  656. $this->markTestSkipped('Cannot run ' . __METHOD__ . '() when headers have already been sent; enable output buffering to run this test');
  657. return;
  658. }
  659. $server = new Server();
  660. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  661. $server->setReturnResponse(true);
  662. // Requesting Method with enforced parameter without it.
  663. $request =
  664. '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
  665. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  666. . 'xmlns:ns1="http://framework.zend.com" '
  667. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  668. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  669. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  670. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  671. . '<SOAP-ENV:Body>'
  672. . '<ns1:testFunc5 />'
  673. . '</SOAP-ENV:Body>'
  674. . '</SOAP-ENV:Envelope>' . "\n";
  675. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  676. $response = $server->handle($request);
  677. $this->assertContains(
  678. '<SOAP-ENV:Fault><faultcode>Receiver</faultcode><faultstring>Test Message</faultstring></SOAP-ENV:Fault>',
  679. $response
  680. );
  681. }
  682. /**
  683. * @group ZF-5597
  684. */
  685. public function testServerAcceptsZendConfigObject()
  686. {
  687. $options = array('soap_version' => SOAP_1_1,
  688. 'actor' => 'http://framework.zend.com/Zend_Soap_ServerTest.php',
  689. 'classmap' => array('TestData1' => '\ZendTest\Soap\TestAsset\TestData1',
  690. 'TestData2' => '\ZendTest\Soap\TestAsset\TestData2',),
  691. 'encoding' => 'ISO-8859-1',
  692. 'uri' => 'http://framework.zend.com/Zend_Soap_ServerTest.php'
  693. );
  694. $config = new \Zend\Config\Config($options);
  695. $server = new Server();
  696. $server->setOptions($config);
  697. $this->assertEquals($options, $server->getOptions());
  698. }
  699. /**
  700. * @group ZF-5300
  701. */
  702. public function testSetAndGetFeatures()
  703. {
  704. $server = new Server();
  705. $this->assertNull($server->getSoapFeatures());
  706. $server->setSoapFeatures(100);
  707. $this->assertEquals(100, $server->getSoapFeatures());
  708. $options = $server->getOptions();
  709. $this->assertTrue(isset($options['features']));
  710. $this->assertEquals(100, $options['features']);
  711. }
  712. /**
  713. * @group ZF-5300
  714. */
  715. public function testSetAndGetWSDLCache()
  716. {
  717. $server = new Server();
  718. $this->assertNull($server->getWSDLCache());
  719. $server->setWSDLCache(100);
  720. $this->assertEquals(100, $server->getWSDLCache());
  721. $options = $server->getOptions();
  722. $this->assertTrue(isset($options['cache_wsdl']));
  723. $this->assertEquals(100, $options['cache_wsdl']);
  724. }
  725. /**
  726. * @group ZF-11411
  727. */
  728. public function testHandleUsesProperRequestParameter()
  729. {
  730. $server = new \ZendTest\Soap\TestAsset\MockServer();
  731. $r = $server->handle(new \DOMDocument('1.0', 'UTF-8'));
  732. $this->assertTrue(is_string($server->mockSoapServer->handle[0]));
  733. }
  734. /**
  735. * @runInSeparateProcess
  736. */
  737. public function testShouldThrowExceptionIfHandledRequestContainsDoctype()
  738. {
  739. $server = new Server();
  740. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  741. $server->setReturnResponse(true);
  742. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  743. $request =
  744. '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<!DOCTYPE foo>' . "\n"
  745. . '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
  746. . 'xmlns:ns1="http://framework.zend.com" '
  747. . 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '
  748. . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
  749. . 'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
  750. . 'SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
  751. . '<SOAP-ENV:Body>'
  752. . '<ns1:testFunc2>'
  753. . '<param0 xsi:type="xsd:string">World</param0>'
  754. . '</ns1:testFunc2>'
  755. . '</SOAP-ENV:Body>'
  756. . '</SOAP-ENV:Envelope>' . "\n";
  757. $response = $server->handle($request);
  758. $this->assertContains('Invalid XML', $response->getMessage());
  759. }
  760. public function testDebugMode()
  761. {
  762. $server = new Server();
  763. $beforeDebug = $server->fault(new \Exception('test'));
  764. $server->setDebugMode(true);
  765. $afterDebug = $server->fault(new \Exception('test'));
  766. $this->assertEquals('Unknown error', $beforeDebug->getMessage());
  767. $this->assertEquals('test', $afterDebug->getMessage());
  768. }
  769. public function testGetOriginalCaughtException()
  770. {
  771. $server = new Server();
  772. $fault = $server->fault(new \Exception('test'));
  773. $exception = $server->getException();
  774. $this->assertInstanceOf('\Exception', $exception);
  775. $this->assertEquals('test', $exception->getMessage());
  776. $this->assertInstanceOf('\SoapFault', $fault);
  777. $this->assertEquals('Unknown error', $fault->getMessage());
  778. }
  779. public function testGetSoapInternalInstance()
  780. {
  781. $server = new Server();
  782. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  783. $internalServer = $server->getSoap();
  784. $this->assertInstanceOf('\SoapServer', $internalServer);
  785. $this->assertSame($internalServer, $server->getSoap());
  786. }
  787. public function testDisableEntityLoaderAfterException()
  788. {
  789. $server = new Server();
  790. $server->setOptions(array('location'=>'test://', 'uri'=>'http://framework.zend.com'));
  791. $server->setReturnResponse(true);
  792. $server->setClass('\ZendTest\Soap\TestAsset\ServerTestClass');
  793. $loadEntities = libxml_disable_entity_loader(false);
  794. // Doing a request that is guaranteed to cause an exception in Server::_setRequest():
  795. $invalidRequest = '---';
  796. $response = @$server->handle($invalidRequest);
  797. // Sanity check; making sure that an exception has been triggered:
  798. $this->assertInstanceOf('\SoapFault', $response);
  799. // The "disable entity loader" setting should be restored to "false" after the exception is raised:
  800. $this->assertFalse(libxml_disable_entity_loader());
  801. // Cleanup; restoring original setting:
  802. libxml_disable_entity_loader($loadEntities);
  803. }
  804. }