ResponseTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_XmlRpc
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once dirname(__FILE__)."/../../TestHelper.php";
  23. require_once 'Zend/XmlRpc/Response.php';
  24. require_once 'PHPUnit/Framework/TestCase.php';
  25. require_once 'PHPUnit/Framework/IncompleteTestError.php';
  26. /**
  27. * Test case for Zend_XmlRpc_Response
  28. *
  29. * @category Zend
  30. * @package Zend_XmlRpc
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_XmlRpc
  35. */
  36. class Zend_XmlRpc_ResponseTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Zend_XmlRpc_Response object
  40. * @var Zend_XmlRpc_Response
  41. */
  42. protected $_response;
  43. /**
  44. * @var bool
  45. */
  46. protected $_errorOccured = false;
  47. /**
  48. * Setup environment
  49. */
  50. public function setUp()
  51. {
  52. $this->_response = new Zend_XmlRpc_Response();
  53. }
  54. /**
  55. * Teardown environment
  56. */
  57. public function tearDown()
  58. {
  59. unset($this->_response);
  60. }
  61. /**
  62. * __construct() test
  63. */
  64. public function test__construct()
  65. {
  66. $this->assertTrue($this->_response instanceof Zend_XmlRpc_Response);
  67. }
  68. /**
  69. * get/setReturnValue() test
  70. */
  71. public function testReturnValue()
  72. {
  73. $this->_response->setReturnValue('string');
  74. $this->assertEquals('string', $this->_response->getReturnValue());
  75. $this->_response->setReturnValue(array('one', 'two'));
  76. $this->assertSame(array('one', 'two'), $this->_response->getReturnValue());
  77. }
  78. /**
  79. * isFault() test
  80. *
  81. * Call as method call
  82. *
  83. * Returns: boolean
  84. */
  85. public function testIsFault()
  86. {
  87. $this->assertFalse($this->_response->isFault());
  88. $this->_response->loadXml('foo');
  89. $this->assertTrue($this->_response->isFault());
  90. }
  91. /**
  92. * Tests getFault() returns NULL (no fault) or the fault object
  93. */
  94. public function testGetFault()
  95. {
  96. $this->assertNull($this->_response->getFault());
  97. $this->_response->loadXml('foo');
  98. $this->assertType('Zend_XmlRpc_Fault', $this->_response->getFault());
  99. }
  100. /**
  101. * loadXml() test
  102. *
  103. * Call as method call
  104. *
  105. * Expects:
  106. * - response:
  107. *
  108. * Returns: boolean
  109. */
  110. public function testLoadXml()
  111. {
  112. $dom = new DOMDocument('1.0', 'UTF-8');
  113. $response = $dom->appendChild($dom->createElement('methodResponse'));
  114. $params = $response->appendChild($dom->createElement('params'));
  115. $param = $params->appendChild($dom->createElement('param'));
  116. $value = $param->appendChild($dom->createElement('value'));
  117. $value->appendChild($dom->createElement('string', 'Return value'));
  118. $xml = $dom->saveXml();
  119. $parsed = $this->_response->loadXml($xml);
  120. $this->assertTrue($parsed, $xml);
  121. $this->assertEquals('Return value', $this->_response->getReturnValue());
  122. }
  123. public function testLoadXmlWithInvalidValue()
  124. {
  125. $this->assertFalse($this->_response->loadXml(new stdClass()));
  126. $this->assertTrue($this->_response->isFault());
  127. $this->assertSame(650, $this->_response->getFault()->getCode());
  128. }
  129. /**
  130. * @group ZF-9039
  131. */
  132. public function testExceptionIsThrownWhenInvalidXmlIsReturnedByServer()
  133. {
  134. set_error_handler(array($this, 'trackError'));
  135. $invalidResponse = 'foo';
  136. $response = new Zend_XmlRpc_Response();
  137. $this->assertFalse($this->_errorOccured);
  138. $this->assertFalse($response->loadXml($invalidResponse));
  139. $this->assertFalse($this->_errorOccured);
  140. }
  141. /**
  142. * @group ZF-5404
  143. */
  144. public function testNilResponseFromXmlRpcServer()
  145. {
  146. $rawResponse = <<<EOD
  147. <methodResponse><params><param><value><array><data><value><struct><member><name>id</name><value><string>1</string></value></member><member><name>name</name><value><string>birdy num num!</string></value></member><member><name>description</name><value><nil/></value></member></struct></value></data></array></value></param></params></methodResponse>
  148. EOD;
  149. try {
  150. $response = new Zend_XmlRpc_Response();
  151. $ret = $response->loadXml($rawResponse);
  152. } catch(Exception $e) {
  153. $this->fail("Parsing the response should not throw an exception.");
  154. }
  155. $this->assertTrue($ret);
  156. $this->assertEquals(array(
  157. 0 => array(
  158. 'id' => 1,
  159. 'name' => 'birdy num num!',
  160. 'description' => null,
  161. )
  162. ), $response->getReturnValue());
  163. }
  164. /**
  165. * helper for saveXml() and __toString() tests
  166. *
  167. * @param string $xml
  168. * @return void
  169. */
  170. protected function _testXmlResponse($xml)
  171. {
  172. try {
  173. $sx = new SimpleXMLElement($xml);
  174. } catch (Exception $e) {
  175. $this->fail('Invalid XML returned');
  176. }
  177. $this->assertTrue($sx->params ? true : false);
  178. $this->assertTrue($sx->params->param ? true : false);
  179. $this->assertTrue($sx->params->param->value ? true : false);
  180. $this->assertTrue($sx->params->param->value->string ? true : false);
  181. $this->assertEquals('return value', (string) $sx->params->param->value->string);
  182. }
  183. /**
  184. * saveXml() test
  185. */
  186. public function testSaveXML()
  187. {
  188. $this->_response->setReturnValue('return value');
  189. $xml = $this->_response->saveXml();
  190. $this->_testXmlResponse($xml);
  191. }
  192. /**
  193. * __toString() test
  194. */
  195. public function test__toString()
  196. {
  197. $this->_response->setReturnValue('return value');
  198. $xml = $this->_response->__toString();
  199. $this->_testXmlResponse($xml);
  200. }
  201. /**
  202. * Test encoding settings
  203. */
  204. public function testSetGetEncoding()
  205. {
  206. $this->assertEquals('UTF-8', $this->_response->getEncoding());
  207. $this->assertEquals('UTF-8', Zend_XmlRpc_Value::getGenerator()->getEncoding());
  208. $this->assertSame($this->_response, $this->_response->setEncoding('ISO-8859-1'));
  209. $this->assertEquals('ISO-8859-1', $this->_response->getEncoding());
  210. $this->assertEquals('ISO-8859-1', Zend_XmlRpc_Value::getGenerator()->getEncoding());
  211. }
  212. public function testLoadXmlThrowsExceptionWithMissingNodes()
  213. {
  214. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params><param>foo</param></params></methodResponse>');
  215. $this->_loadXml($sxl->asXML());
  216. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params>foo</params></methodResponse>');
  217. $this->_loadXml($sxl->asXML());
  218. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><bar>foo</bar></methodResponse>');
  219. $this->_loadXml($sxl->asXML());
  220. }
  221. protected function _loadXml($xml)
  222. {
  223. try {
  224. $this->_response->loadXml($xml);
  225. $this->fail('Invalid XML-RPC response should raise an exception');
  226. } catch (Exception $e) {
  227. }
  228. }
  229. public function trackError($error)
  230. {
  231. $this->_errorOccured = true;
  232. }
  233. }