ResponseTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-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. 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-2009 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. * Setup environment
  45. */
  46. public function setUp()
  47. {
  48. $this->_response = new Zend_XmlRpc_Response();
  49. }
  50. /**
  51. * Teardown environment
  52. */
  53. public function tearDown()
  54. {
  55. unset($this->_response);
  56. }
  57. /**
  58. * __construct() test
  59. */
  60. public function test__construct()
  61. {
  62. $this->assertTrue($this->_response instanceof Zend_XmlRpc_Response);
  63. }
  64. /**
  65. * get/setReturnValue() test
  66. */
  67. public function testReturnValue()
  68. {
  69. $this->_response->setReturnValue('string');
  70. $this->assertEquals('string', $this->_response->getReturnValue());
  71. $this->_response->setReturnValue(array('one', 'two'));
  72. $this->assertSame(array('one', 'two'), $this->_response->getReturnValue());
  73. }
  74. /**
  75. * isFault() test
  76. *
  77. * Call as method call
  78. *
  79. * Returns: boolean
  80. */
  81. public function testIsFault()
  82. {
  83. $this->assertFalse($this->_response->isFault());
  84. $this->_response->loadXml('foo');
  85. $this->assertTrue($this->_response->isFault());
  86. }
  87. /**
  88. * Tests getFault() returns NULL (no fault) or the fault object
  89. */
  90. public function testGetFault()
  91. {
  92. $this->assertNull($this->_response->getFault());
  93. $this->_response->loadXml('foo');
  94. $this->assertType('Zend_XmlRpc_Fault', $this->_response->getFault());
  95. }
  96. /**
  97. * loadXml() test
  98. *
  99. * Call as method call
  100. *
  101. * Expects:
  102. * - response:
  103. *
  104. * Returns: boolean
  105. */
  106. public function testLoadXml()
  107. {
  108. $dom = new DOMDocument('1.0', 'UTF-8');
  109. $response = $dom->appendChild($dom->createElement('methodResponse'));
  110. $params = $response->appendChild($dom->createElement('params'));
  111. $param = $params->appendChild($dom->createElement('param'));
  112. $value = $param->appendChild($dom->createElement('value'));
  113. $value->appendChild($dom->createElement('string', 'Return value'));
  114. $xml = $dom->saveXML();
  115. $parsed = $this->_response->loadXml($xml);
  116. $this->assertTrue($parsed, $xml);
  117. $this->assertEquals('Return value', $this->_response->getReturnValue());
  118. }
  119. /**
  120. * @group ZF-5404
  121. */
  122. public function testNilResponseFromXmlRpcServer()
  123. {
  124. $rawResponse = <<<EOD
  125. <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>
  126. EOD;
  127. try {
  128. $response = new Zend_XmlRpc_Response();
  129. $ret = $response->loadXml($rawResponse);
  130. } catch(Exception $e) {
  131. $this->fail("Parsing the response should not throw an exception.");
  132. }
  133. $this->assertTrue($ret);
  134. $this->assertEquals(array(
  135. 0 => array(
  136. 'id' => 1,
  137. 'name' => 'birdy num num!',
  138. 'description' => null,
  139. )
  140. ), $response->getReturnValue());
  141. }
  142. /**
  143. * helper for saveXML() and __toString() tests
  144. *
  145. * @param string $xml
  146. * @return void
  147. */
  148. protected function _testXmlResponse($xml)
  149. {
  150. try {
  151. $sx = new SimpleXMLElement($xml);
  152. } catch (Exception $e) {
  153. $this->fail('Invalid XML returned');
  154. }
  155. $this->assertTrue($sx->params ? true : false);
  156. $this->assertTrue($sx->params->param ? true : false);
  157. $this->assertTrue($sx->params->param->value ? true : false);
  158. $this->assertTrue($sx->params->param->value->string ? true : false);
  159. $this->assertEquals('return value', (string) $sx->params->param->value->string);
  160. }
  161. /**
  162. * saveXML() test
  163. */
  164. public function testSaveXML()
  165. {
  166. $this->_response->setReturnValue('return value');
  167. $xml = $this->_response->saveXML();
  168. $this->_testXmlResponse($xml);
  169. }
  170. /**
  171. * __toString() test
  172. */
  173. public function test__toString()
  174. {
  175. $this->_response->setReturnValue('return value');
  176. $xml = $this->_response->__toString();
  177. $this->_testXmlResponse($xml);
  178. }
  179. /**
  180. * Test encoding settings
  181. */
  182. public function testSetGetEncoding()
  183. {
  184. $this->assertEquals('UTF-8', $this->_response->getEncoding());
  185. $this->_response->setEncoding('ISO-8859-1');
  186. $this->assertEquals('ISO-8859-1', $this->_response->getEncoding());
  187. }
  188. public function testLoadXmlThrowsExceptionWithMissingNodes()
  189. {
  190. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params><param>foo</param></params></methodResponse>');
  191. $this->_loadXml($sxl->asXML());
  192. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params>foo</params></methodResponse>');
  193. $this->_loadXml($sxl->asXML());
  194. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><bar>foo</bar></methodResponse>');
  195. $this->_loadXml($sxl->asXML());
  196. }
  197. protected function _loadXml($xml)
  198. {
  199. try {
  200. $this->_response->loadXml($xml);
  201. $this->fail('Invalid XML-RPC response should raise an exception');
  202. } catch (Exception $e) {
  203. }
  204. }
  205. }