ValueTest.php 17 KB

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