ValueTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. require_once dirname(__FILE__) . '/../../TestHelper.php';
  5. define("PHPUnit_MAIN_METHOD", "Zend_XmlRpc_ValueTest::main");
  6. }
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/XmlRpc/Value.php';
  10. require_once 'Zend/XmlRpc/Value/Scalar.php';
  11. require_once 'Zend/XmlRpc/Value/Collection.php';
  12. require_once 'Zend/XmlRpc/Value/Array.php';
  13. require_once 'Zend/XmlRpc/Value/Base64.php';
  14. require_once 'Zend/XmlRpc/Value/Boolean.php';
  15. require_once 'Zend/XmlRpc/Value/DateTime.php';
  16. require_once 'Zend/XmlRpc/Value/Double.php';
  17. require_once 'Zend/XmlRpc/Value/Integer.php';
  18. require_once 'Zend/XmlRpc/Value/String.php';
  19. require_once 'Zend/XmlRpc/Value/Nil.php';
  20. require_once 'Zend/XmlRpc/Value/Struct.php';
  21. /**
  22. * Test case for Zend_XmlRpc_Value
  23. *
  24. * @package Zend_XmlRpc
  25. * @subpackage UnitTests
  26. * @version $Id$
  27. */
  28. class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
  29. {
  30. /**
  31. * Runs the test methods of this class.
  32. *
  33. * @return void
  34. */
  35. public static function main()
  36. {
  37. require_once "PHPUnit/TextUI/TestRunner.php";
  38. $suite = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_ValueTest");
  39. $result = PHPUnit_TextUI_TestRunner::run($suite);
  40. }
  41. // Boolean
  42. public function testFactoryAutodetectsBoolean()
  43. {
  44. foreach (array(true, false) as $native) {
  45. $val = Zend_XmlRpc_Value::getXmlRpcValue($native);
  46. $this->assertXmlRpcType('boolean', $val);
  47. }
  48. }
  49. public function testMarshalBooleanFromNative()
  50. {
  51. $native = true;
  52. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  53. Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN);
  54. $this->assertXmlRpcType('boolean', $val);
  55. $this->assertSame($native, $val->getValue());
  56. }
  57. public function testMarshalBooleanFromXmlRpc()
  58. {
  59. $xml = '<value><boolean>1</boolean></value>';
  60. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  61. Zend_XmlRpc_Value::XML_STRING);
  62. $this->assertXmlRpcType('boolean', $val);
  63. $this->assertEquals('boolean', $val->getType());
  64. $this->assertSame(true, $val->getValue());
  65. $this->assertType('DomElement', $val->getAsDOM());
  66. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  67. }
  68. // Integer
  69. public function testFactoryAutodetectsInteger()
  70. {
  71. $val = Zend_XmlRpc_Value::getXmlRpcValue(1);
  72. $this->assertXmlRpcType('integer', $val);
  73. }
  74. public function testMarshalIntegerFromNative()
  75. {
  76. $native = 1;
  77. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_I4,
  78. Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER);
  79. foreach ($types as $type) {
  80. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  81. $this->assertXmlRpcType('integer', $val);
  82. $this->assertSame($native, $val->getValue());
  83. }
  84. }
  85. public function testMarshalIntegerFromXmlRpc()
  86. {
  87. $native = 1;
  88. $xmls = array("<value><int>$native</int></value>",
  89. "<value><i4>$native</i4></value>");
  90. foreach ($xmls as $xml) {
  91. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  92. Zend_XmlRpc_Value::XML_STRING);
  93. $this->assertXmlRpcType('integer', $val);
  94. $this->assertEquals('int', $val->getType());
  95. $this->assertSame($native, $val->getValue());
  96. $this->assertType('DomElement', $val->getAsDOM());
  97. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  98. }
  99. }
  100. // Double
  101. public function testFactoryAutodetectsFloat()
  102. {
  103. $val = Zend_XmlRpc_Value::getXmlRpcValue((float)1);
  104. $this->assertXmlRpcType('double', $val);
  105. }
  106. public function testMarshalDoubleFromNative()
  107. {
  108. $native = 1.1;
  109. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  110. Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
  111. $this->assertXmlRpcType('double', $val);
  112. $this->assertSame($native, $val->getValue());
  113. }
  114. public function testMarshalDoubleFromXmlRpc()
  115. {
  116. $native = 1.1;
  117. $xml = "<value><double>$native</double></value>";
  118. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  119. Zend_XmlRpc_Value::XML_STRING);
  120. $this->assertXmlRpcType('double', $val);
  121. $this->assertEquals('double', $val->getType());
  122. $this->assertSame($native, $val->getValue());
  123. $this->assertType('DomElement', $val->getAsDOM());
  124. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  125. }
  126. // String
  127. public function testFactoryAutodetectsString()
  128. {
  129. $val = Zend_XmlRpc_Value::getXmlRpcValue('');
  130. $this->assertXmlRpcType('string', $val);
  131. }
  132. public function testMarshalStringFromNative()
  133. {
  134. $native = 'foo';
  135. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  136. Zend_XmlRpc_Value::XMLRPC_TYPE_STRING);
  137. $this->assertXmlRpcType('string', $val);
  138. $this->assertSame($native, $val->getValue());
  139. }
  140. public function testMarshalStringFromXmlRpc()
  141. {
  142. $native = 'foo';
  143. $xml = "<value><string>$native</string></value>";
  144. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  145. Zend_XmlRpc_Value::XML_STRING);
  146. $this->assertXmlRpcType('string', $val);
  147. $this->assertEquals('string', $val->getType());
  148. $this->assertSame($native, $val->getValue());
  149. $this->assertType('DomElement', $val->getAsDOM());
  150. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  151. }
  152. //Nil
  153. public function testFactoryAutodetectsNil()
  154. {
  155. $val = Zend_XmlRpc_Value::getXmlRpcValue(NULL);
  156. $this->assertXmlRpcType('nil', $val);
  157. }
  158. public function testMarshalNilFromNative()
  159. {
  160. $native = NULL;
  161. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  162. Zend_XmlRpc_Value::XMLRPC_TYPE_NIL);
  163. $this->assertXmlRpcType('nil', $val);
  164. $this->assertSame($native, $val->getValue());
  165. }
  166. public function testMarshalNilFromXmlRpc()
  167. {
  168. $xml = '<value><nil/></value>';
  169. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  170. Zend_XmlRpc_Value::XML_STRING);
  171. $this->assertXmlRpcType('nil', $val);
  172. $this->assertEquals('nil', $val->getType());
  173. $this->assertSame(NULL, $val->getValue());
  174. $this->assertType('DomElement', $val->getAsDOM());
  175. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  176. }
  177. // Array
  178. public function testFactoryAutodetectsArray()
  179. {
  180. $val = Zend_XmlRpc_Value::getXmlRpcValue(array(0, 'foo'));
  181. $this->assertXmlRpcType('array', $val);
  182. }
  183. public function testMarshalArrayFromNative()
  184. {
  185. $native = array(0,1);
  186. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  187. Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY);
  188. $this->assertXmlRpcType('array', $val);
  189. $this->assertSame($native, $val->getValue());
  190. }
  191. public function testMarshalArrayFromXmlRpc()
  192. {
  193. $native = array(0,1);
  194. $xml = '<value><array><data><value><int>0</int></value>'
  195. . '<value><int>1</int></value></data></array></value>';
  196. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  197. Zend_XmlRpc_Value::XML_STRING);
  198. $this->assertXmlRpcType('array', $val);
  199. $this->assertEquals('array', $val->getType());
  200. $this->assertSame($native, $val->getValue());
  201. $this->assertType('DomElement', $val->getAsDOM());
  202. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  203. }
  204. public function testEmptyXmlRpcArrayResultsInEmptyArray()
  205. {
  206. $native = array();
  207. $xml = '<value><array><data/></array></value>';
  208. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  209. Zend_XmlRpc_Value::XML_STRING);
  210. $this->assertXmlRpcType('array', $val);
  211. $this->assertEquals('array', $val->getType());
  212. $this->assertSame($native, $val->getValue());
  213. $value = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  214. $this->assertXmlRpcType('array', $value);
  215. $this->assertEquals('array', $value->getType());
  216. $this->assertSame($native, $value->getValue());
  217. }
  218. // Struct
  219. public function testFactoryAutodetectsStruct()
  220. {
  221. $val = Zend_XmlRpc_Value::getXmlRpcValue(array('foo' => 0));
  222. $this->assertXmlRpcType('struct', $val);
  223. }
  224. public function testFactoryAutodetectsStructFromObject()
  225. {
  226. $val = Zend_XmlRpc_Value::getXmlRpcValue((object)array('foo' => 0));
  227. $this->assertXmlRpcType('struct', $val);
  228. }
  229. public function testMarshalStructFromNative()
  230. {
  231. $native = array('foo' => 0);
  232. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  233. Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  234. $this->assertXmlRpcType('struct', $val);
  235. $this->assertSame($native, $val->getValue());
  236. }
  237. public function testMarshalStructFromXmlRpc()
  238. {
  239. $native = array('foo' => 0);
  240. $xml = '<value><struct><member><name>foo</name><value><int>0</int>'
  241. . '</value></member></struct></value>';
  242. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  243. Zend_XmlRpc_Value::XML_STRING);
  244. $this->assertXmlRpcType('struct', $val);
  245. $this->assertEquals('struct', $val->getType());
  246. $this->assertSame($native, $val->getValue());
  247. $this->assertType('DomElement', $val->getAsDOM());
  248. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  249. }
  250. /**
  251. * @group ZF-3947
  252. */
  253. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys()
  254. {
  255. $native = array('foo' => '');
  256. $xml = '<value><struct><member><name>foo</name>'
  257. . '<value/></member></struct></value>';
  258. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  259. Zend_XmlRpc_Value::XML_STRING);
  260. $this->assertXmlRpcType('struct', $val);
  261. $this->assertEquals('struct', $val->getType());
  262. $this->assertSame($native, $val->getValue());
  263. $this->assertType('DomElement', $val->getAsDOM());
  264. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  265. }
  266. // DateTime
  267. public function testMarshalDateTimeFromNativeString()
  268. {
  269. $native = '1997-07-16T19:20+01:00';
  270. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  271. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  272. $this->assertXmlRpcType('dateTime', $val);
  273. $expected = '1997-07-16T19:20+01:00';
  274. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  275. }
  276. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  277. {
  278. $native = '1997-07-16T19:20+01:00';
  279. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  280. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  281. $this->assertXmlRpcType('dateTime', $val);
  282. $expected = date('c', strtotime($native));
  283. $expected = substr($expected, 0, strlen($expected) - 6);
  284. $expected = str_replace('-', '', $expected);
  285. $received = $val->getValue();
  286. $this->assertEquals($expected, $received);
  287. }
  288. public function testMarshalDateTimeFromNativeInteger()
  289. {
  290. $native = strtotime('1997-07-16T19:20+01:00');
  291. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  292. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  293. $this->assertXmlRpcType('dateTime', $val);
  294. $this->assertSame($native, strtotime($val->getValue()));
  295. }
  296. public function testMarshalDateTimeFromXmlRpc()
  297. {
  298. $iso8601 = '1997-07-16T19:20+01:00';
  299. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  300. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  301. Zend_XmlRpc_Value::XML_STRING);
  302. $this->assertXmlRpcType('dateTime', $val);
  303. $this->assertEquals('dateTime.iso8601', $val->getType());
  304. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  305. $this->assertType('DomElement', $val->getAsDOM());
  306. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  307. }
  308. // Base64
  309. public function testMarshalBase64FromString()
  310. {
  311. $native = 'foo';
  312. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  313. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  314. $this->assertXmlRpcType('base64', $val);
  315. $this->assertSame($native, $val->getValue());
  316. }
  317. public function testMarshalBase64FromXmlRpc()
  318. {
  319. $native = 'foo';
  320. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  321. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  322. Zend_XmlRpc_Value::XML_STRING);
  323. $this->assertXmlRpcType('base64', $val);
  324. $this->assertEquals('base64', $val->getType());
  325. $this->assertSame($native, $val->getValue());
  326. $this->assertType('DomElement', $val->getAsDOM());
  327. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  328. }
  329. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  330. {
  331. $native = 'foo';
  332. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  333. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  334. $this->assertXmlRpcType('base64', $val);
  335. $xml = $val->saveXML();
  336. $encoded = base64_encode($native);
  337. $this->assertContains($encoded, $xml);
  338. }
  339. // Exceptions
  340. public function testFactoryThrowsWhenInvalidTypeSpecified()
  341. {
  342. try {
  343. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  344. $this->fail();
  345. } catch (Exception $e) {
  346. $this->assertRegexp('/given type is not/i', $e->getMessage());
  347. }
  348. }
  349. // Custom Assertions and Helper Methods
  350. public function assertXmlRpcType($type, $object)
  351. {
  352. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  353. $this->assertType($type, $object);
  354. }
  355. public function wrapXml($xml)
  356. {
  357. return "<?xml version=\"1.0\"?>\n$xml\n";
  358. }
  359. }
  360. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  361. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  362. Zend_XmlRpc_ValueTest::main();
  363. }