ResponseTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. public function testLoadXmlWithInvalidValue()
  120. {
  121. $this->assertFalse($this->_response->loadXml(new stdClass()));
  122. $this->assertTrue($this->_response->isFault());
  123. $this->assertSame(650, $this->_response->getFault()->getCode());
  124. }
  125. /**
  126. * @group ZF-5404
  127. */
  128. public function testNilResponseFromXmlRpcServer()
  129. {
  130. $rawResponse = <<<EOD
  131. <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>
  132. EOD;
  133. try {
  134. $response = new Zend_XmlRpc_Response();
  135. $ret = $response->loadXml($rawResponse);
  136. } catch(Exception $e) {
  137. $this->fail("Parsing the response should not throw an exception.");
  138. }
  139. $this->assertTrue($ret);
  140. $this->assertEquals(array(
  141. 0 => array(
  142. 'id' => 1,
  143. 'name' => 'birdy num num!',
  144. 'description' => null,
  145. )
  146. ), $response->getReturnValue());
  147. }
  148. /**
  149. * helper for saveXML() and __toString() tests
  150. *
  151. * @param string $xml
  152. * @return void
  153. */
  154. protected function _testXmlResponse($xml)
  155. {
  156. try {
  157. $sx = new SimpleXMLElement($xml);
  158. } catch (Exception $e) {
  159. $this->fail('Invalid XML returned');
  160. }
  161. $this->assertTrue($sx->params ? true : false);
  162. $this->assertTrue($sx->params->param ? true : false);
  163. $this->assertTrue($sx->params->param->value ? true : false);
  164. $this->assertTrue($sx->params->param->value->string ? true : false);
  165. $this->assertEquals('return value', (string) $sx->params->param->value->string);
  166. }
  167. /**
  168. * saveXML() test
  169. */
  170. public function testSaveXML()
  171. {
  172. $this->_response->setReturnValue('return value');
  173. $xml = $this->_response->saveXML();
  174. $this->_testXmlResponse($xml);
  175. }
  176. /**
  177. * __toString() test
  178. */
  179. public function test__toString()
  180. {
  181. $this->_response->setReturnValue('return value');
  182. $xml = $this->_response->__toString();
  183. $this->_testXmlResponse($xml);
  184. }
  185. /**
  186. * Test encoding settings
  187. */
  188. public function testSetGetEncoding()
  189. {
  190. $this->assertEquals('UTF-8', $this->_response->getEncoding());
  191. $this->_response->setEncoding('ISO-8859-1');
  192. $this->assertEquals('ISO-8859-1', $this->_response->getEncoding());
  193. }
  194. public function testLoadXmlThrowsExceptionWithMissingNodes()
  195. {
  196. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params><param>foo</param></params></methodResponse>');
  197. $this->_loadXml($sxl->asXML());
  198. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><params>foo</params></methodResponse>');
  199. $this->_loadXml($sxl->asXML());
  200. $sxl = new SimpleXMLElement('<?xml version="1.0"?><methodResponse><bar>foo</bar></methodResponse>');
  201. $this->_loadXml($sxl->asXML());
  202. }
  203. protected function _loadXml($xml)
  204. {
  205. try {
  206. $this->_response->loadXml($xml);
  207. $this->fail('Invalid XML-RPC response should raise an exception');
  208. } catch (Exception $e) {
  209. }
  210. }
  211. }