RequestTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 'Zend/XmlRpc/Request.php';
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. require_once 'PHPUnit/Framework/IncompleteTestError.php';
  25. /**
  26. * Test case for Zend_XmlRpc_Request
  27. *
  28. * @category Zend
  29. * @package Zend_XmlRpc
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_XmlRpc
  34. */
  35. class Zend_XmlRpc_RequestTest extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * Zend_XmlRpc_Request object
  39. * @var Zend_XmlRpc_Request
  40. */
  41. protected $_request;
  42. /**
  43. * Setup environment
  44. */
  45. public function setUp()
  46. {
  47. $this->_request = new Zend_XmlRpc_Request();
  48. }
  49. /**
  50. * Teardown environment
  51. */
  52. public function tearDown()
  53. {
  54. unset($this->_request);
  55. }
  56. /**
  57. * get/setMethod() test
  58. */
  59. public function testMethod()
  60. {
  61. $this->assertTrue($this->_request->setMethod('testMethod'));
  62. $this->assertTrue($this->_request->setMethod('testMethod9'));
  63. $this->assertTrue($this->_request->setMethod('test.Method'));
  64. $this->assertTrue($this->_request->setMethod('test_method'));
  65. $this->assertTrue($this->_request->setMethod('test:method'));
  66. $this->assertTrue($this->_request->setMethod('test/method'));
  67. $this->assertFalse($this->_request->setMethod('testMethod-bogus'));
  68. $this->assertEquals('test/method', $this->_request->getMethod());
  69. }
  70. /**
  71. * __construct() test
  72. */
  73. public function testConstructorOptionallySetsMethodAndParams()
  74. {
  75. $r = new Zend_XmlRpc_Request();
  76. $this->assertEquals('', $r->getMethod());
  77. $this->assertEquals(array(), $r->getParams());
  78. $method = 'foo.bar';
  79. $params = array('baz', 1, array('foo' => 'bar'));
  80. $r = new Zend_XmlRpc_Request($method, $params);
  81. $this->assertEquals($method, $r->getMethod());
  82. $this->assertEquals($params, $r->getParams());
  83. }
  84. /**
  85. * addParam()/getParams() test
  86. */
  87. public function testAddParam()
  88. {
  89. $this->_request->addParam('string1');
  90. $params = $this->_request->getParams();
  91. $this->assertEquals(1, count($params));
  92. $this->assertEquals('string1', $params[0]);
  93. $this->_request->addParam('string2');
  94. $params = $this->_request->getParams();
  95. $this->assertEquals(2, count($params));
  96. $this->assertEquals('string1', $params[0]);
  97. $this->assertEquals('string2', $params[1]);
  98. }
  99. public function testAddDateParamGeneratesCorrectXml()
  100. {
  101. $time = time();
  102. $this->_request->addParam($time, Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  103. $this->_request->setMethod('foo.bar');
  104. $xml = $this->_request->saveXML();
  105. $sxl = new SimpleXMLElement($xml);
  106. $param = $sxl->params->param->value;
  107. $type = 'dateTime.iso8601';
  108. $this->assertTrue(isset($param->{$type}), var_export($param, 1));
  109. $this->assertEquals($time, strtotime((string) $param->{$type}));
  110. }
  111. /**
  112. * setParams()/getParams() test
  113. */
  114. public function testSetParams()
  115. {
  116. $params = array(
  117. 'string1',
  118. true,
  119. array('one', 'two')
  120. );
  121. $this->_request->setParams($params);
  122. $returned = $this->_request->getParams();
  123. $this->assertSame($params, $returned);
  124. $params = array(
  125. 'string2',
  126. array('two', 'one')
  127. );
  128. $this->_request->setParams($params);
  129. $returned = $this->_request->getParams();
  130. $this->assertSame($params, $returned);
  131. }
  132. /**
  133. * loadXml() test
  134. */
  135. public function testLoadXml()
  136. {
  137. $dom = new DOMDocument('1.0', 'UTF-8');
  138. $mCall = $dom->appendChild($dom->createElement('methodCall'));
  139. $mName = $mCall->appendChild($dom->createElement('methodName', 'do.Something'));
  140. $params = $mCall->appendChild($dom->createElement('params'));
  141. $param1 = $params->appendChild($dom->createElement('param'));
  142. $value1 = $param1->appendChild($dom->createElement('value'));
  143. $value1->appendChild($dom->createElement('string', 'string1'));
  144. $param2 = $params->appendChild($dom->createElement('param'));
  145. $value2 = $param2->appendChild($dom->createElement('value'));
  146. $value2->appendChild($dom->createElement('boolean', 1));
  147. $xml = $dom->saveXML();
  148. try {
  149. $parsed = $this->_request->loadXml($xml);
  150. } catch (Exception $e) {
  151. $this->fail('Failed to parse XML: ' . $e->getMessage());
  152. }
  153. $this->assertTrue($parsed, $xml);
  154. $this->assertEquals('do.Something', $this->_request->getMethod());
  155. $test = array('string1', true);
  156. $params = $this->_request->getParams();
  157. $this->assertSame($test, $params);
  158. try {
  159. $parsed = $this->_request->loadXml('foo');
  160. } catch (Exception $e) {
  161. $this->fail('Failed to parse XML: ' . $e->getMessage());
  162. }
  163. $this->assertFalse($parsed, 'Parsed non-XML string?');
  164. }
  165. /**
  166. * isFault() test
  167. */
  168. public function testIsFault()
  169. {
  170. $this->assertFalse($this->_request->isFault());
  171. $this->_request->loadXml('foo');
  172. $this->assertTrue($this->_request->isFault());
  173. }
  174. /**
  175. * getFault() test
  176. */
  177. public function testGetFault()
  178. {
  179. $fault = $this->_request->getFault();
  180. $this->assertTrue(null === $fault);
  181. $this->_request->loadXml('foo');
  182. $fault = $this->_request->getFault();
  183. $this->assertTrue($fault instanceof Zend_XmlRpc_Fault);
  184. }
  185. /**
  186. * helper for saveXML() and __toString() tests
  187. *
  188. * @param string $xml
  189. * @return void
  190. */
  191. protected function _testXmlRequest($xml, $argv)
  192. {
  193. try {
  194. $sx = new SimpleXMLElement($xml);
  195. } catch (Exception $e) {
  196. $this->fail('Invalid XML returned');
  197. }
  198. $result = $sx->xpath('//methodName');
  199. $count = 0;
  200. while (list( , $node) = each($result)) {
  201. ++$count;
  202. }
  203. $this->assertEquals(1, $count, $xml);
  204. $result = $sx->xpath('//params');
  205. $count = 0;
  206. while (list( , $node) = each($result)) {
  207. ++$count;
  208. }
  209. $this->assertEquals(1, $count, $xml);
  210. try {
  211. $methodName = (string) $sx->methodName;
  212. $params = array(
  213. (string) $sx->params->param[0]->value->string,
  214. (bool) $sx->params->param[1]->value->boolean
  215. );
  216. } catch (Exception $e) {
  217. $this->fail('One or more inconsistencies parsing generated XML: ' . $e->getMessage());
  218. }
  219. $this->assertEquals('do.Something', $methodName);
  220. $this->assertSame($argv, $params, $xml);
  221. }
  222. /**
  223. * testSaveXML() test
  224. */
  225. public function testSaveXML()
  226. {
  227. $argv = array('string', true);
  228. $this->_request->setMethod('do.Something');
  229. $this->_request->setParams($argv);
  230. $xml = $this->_request->saveXML();
  231. $this->_testXmlRequest($xml, $argv);
  232. }
  233. /**
  234. * __toString() test
  235. */
  236. public function test__toString()
  237. {
  238. $argv = array('string', true);
  239. $this->_request->setMethod('do.Something');
  240. $this->_request->setParams($argv);
  241. $xml = $this->_request->__toString();
  242. $this->_testXmlRequest($xml, $argv);
  243. }
  244. /**
  245. * Test encoding settings
  246. */
  247. public function testSetGetEncoding()
  248. {
  249. $this->assertEquals('UTF-8', $this->_request->getEncoding());
  250. $this->_request->setEncoding('ISO-8859-1');
  251. $this->assertEquals('ISO-8859-1', $this->_request->getEncoding());
  252. }
  253. }