RequestTest.php 12 KB

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