ResponseTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. require_once dirname(__FILE__)."/../../TestHelper.php";
  3. require_once 'Zend/XmlRpc/Response.php';
  4. require_once 'PHPUnit/Framework/TestCase.php';
  5. require_once 'PHPUnit/Framework/IncompleteTestError.php';
  6. /**
  7. * Test case for Zend_XmlRpc_Response
  8. *
  9. * @package Zend_XmlRpc
  10. * @subpackage UnitTests
  11. * @version $Id$
  12. */
  13. class Zend_XmlRpc_ResponseTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * Zend_XmlRpc_Response object
  17. * @var Zend_XmlRpc_Response
  18. */
  19. protected $_response;
  20. /**
  21. * Setup environment
  22. */
  23. public function setUp()
  24. {
  25. $this->_response = new Zend_XmlRpc_Response();
  26. }
  27. /**
  28. * Teardown environment
  29. */
  30. public function tearDown()
  31. {
  32. unset($this->_response);
  33. }
  34. /**
  35. * __construct() test
  36. */
  37. public function test__construct()
  38. {
  39. $this->assertTrue($this->_response instanceof Zend_XmlRpc_Response);
  40. }
  41. /**
  42. * get/setReturnValue() test
  43. */
  44. public function testReturnValue()
  45. {
  46. $this->_response->setReturnValue('string');
  47. $this->assertEquals('string', $this->_response->getReturnValue());
  48. $this->_response->setReturnValue(array('one', 'two'));
  49. $this->assertSame(array('one', 'two'), $this->_response->getReturnValue());
  50. }
  51. /**
  52. * isFault() test
  53. *
  54. * Call as method call
  55. *
  56. * Returns: boolean
  57. */
  58. public function testIsFault()
  59. {
  60. $this->assertFalse($this->_response->isFault());
  61. $this->_response->loadXml('foo');
  62. $this->assertTrue($this->_response->isFault());
  63. }
  64. /**
  65. * Tests getFault() returns NULL (no fault) or the fault object
  66. */
  67. public function testGetFault()
  68. {
  69. $this->assertNull($this->_response->getFault());
  70. $this->_response->loadXml('foo');
  71. $this->assertType('Zend_XmlRpc_Fault', $this->_response->getFault());
  72. }
  73. /**
  74. * loadXml() test
  75. *
  76. * Call as method call
  77. *
  78. * Expects:
  79. * - response:
  80. *
  81. * Returns: boolean
  82. */
  83. public function testLoadXml()
  84. {
  85. $dom = new DOMDocument('1.0', 'UTF-8');
  86. $response = $dom->appendChild($dom->createElement('methodResponse'));
  87. $params = $response->appendChild($dom->createElement('params'));
  88. $param = $params->appendChild($dom->createElement('param'));
  89. $value = $param->appendChild($dom->createElement('value'));
  90. $value->appendChild($dom->createElement('string', 'Return value'));
  91. $xml = $dom->saveXML();
  92. $parsed = $this->_response->loadXml($xml);
  93. $this->assertTrue($parsed, $xml);
  94. $this->assertEquals('Return value', $this->_response->getReturnValue());
  95. }
  96. /**
  97. * @group ZF-5404
  98. */
  99. public function testNilResponseFromXmlRpcServer()
  100. {
  101. $rawResponse = <<<EOD
  102. <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>
  103. EOD;
  104. try {
  105. $response = new Zend_XmlRpc_Response();
  106. $ret = $response->loadXml($rawResponse);
  107. } catch(Exception $e) {
  108. $this->fail("Parsing the response should not throw an exception.");
  109. }
  110. $this->assertTrue($ret);
  111. $this->assertEquals(array(
  112. 0 => array(
  113. 'id' => 1,
  114. 'name' => 'birdy num num!',
  115. 'description' => null,
  116. )
  117. ), $response->getReturnValue());
  118. }
  119. /**
  120. * helper for saveXML() and __toString() tests
  121. *
  122. * @param string $xml
  123. * @return void
  124. */
  125. protected function _testXmlResponse($xml)
  126. {
  127. try {
  128. $sx = new SimpleXMLElement($xml);
  129. } catch (Exception $e) {
  130. $this->fail('Invalid XML returned');
  131. }
  132. $this->assertTrue($sx->params ? true : false);
  133. $this->assertTrue($sx->params->param ? true : false);
  134. $this->assertTrue($sx->params->param->value ? true : false);
  135. $this->assertTrue($sx->params->param->value->string ? true : false);
  136. $this->assertEquals('return value', (string) $sx->params->param->value->string);
  137. }
  138. /**
  139. * saveXML() test
  140. */
  141. public function testSaveXML()
  142. {
  143. $this->_response->setReturnValue('return value');
  144. $xml = $this->_response->saveXML();
  145. $this->_testXmlResponse($xml);
  146. }
  147. /**
  148. * __toString() test
  149. */
  150. public function test__toString()
  151. {
  152. $this->_response->setReturnValue('return value');
  153. $xml = $this->_response->__toString();
  154. $this->_testXmlResponse($xml);
  155. }
  156. /**
  157. * Test encoding settings
  158. */
  159. public function testSetGetEncoding()
  160. {
  161. $this->assertEquals('UTF-8', $this->_response->getEncoding());
  162. $this->_response->setEncoding('ISO-8859-1');
  163. $this->assertEquals('ISO-8859-1', $this->_response->getEncoding());
  164. }
  165. public function testLoadXmlThrowsExceptionWithMissingNodes()
  166. {
  167. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params><param>foo</param></params></methodResponse>');
  168. $this->_loadXml($sxl->asXML());
  169. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params>foo</params></methodResponse>');
  170. $this->_loadXml($sxl->asXML());
  171. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><bar>foo</bar></methodResponse>');
  172. $this->_loadXml($sxl->asXML());
  173. }
  174. protected function _loadXml($xml)
  175. {
  176. try {
  177. $this->_response->loadXml($xml);
  178. $this->fail('Invalid XML-RPC response should raise an exception');
  179. } catch (Exception $e) {
  180. }
  181. }
  182. }