ValueTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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-3947
  275. */
  276. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys()
  277. {
  278. $native = array('foo' => '');
  279. $xml = '<value><struct><member><name>foo</name>'
  280. . '<value/></member></struct></value>';
  281. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  282. Zend_XmlRpc_Value::XML_STRING);
  283. $this->assertXmlRpcType('struct', $val);
  284. $this->assertEquals('struct', $val->getType());
  285. $this->assertSame($native, $val->getValue());
  286. $this->assertType('DomElement', $val->getAsDOM());
  287. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  288. }
  289. // DateTime
  290. public function testMarshalDateTimeFromNativeString()
  291. {
  292. $native = '1997-07-16T19:20+01:00';
  293. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  294. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  295. $this->assertXmlRpcType('dateTime', $val);
  296. $expected = '1997-07-16T19:20+01:00';
  297. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  298. }
  299. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  300. {
  301. $native = '1997-07-16T19:20+01:00';
  302. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  303. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  304. $this->assertXmlRpcType('dateTime', $val);
  305. $expected = date('c', strtotime($native));
  306. $expected = substr($expected, 0, strlen($expected) - 6);
  307. $expected = str_replace('-', '', $expected);
  308. $received = $val->getValue();
  309. $this->assertEquals($expected, $received);
  310. }
  311. public function testMarshalDateTimeFromNativeInteger()
  312. {
  313. $native = strtotime('1997-07-16T19:20+01:00');
  314. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  315. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  316. $this->assertXmlRpcType('dateTime', $val);
  317. $this->assertSame($native, strtotime($val->getValue()));
  318. }
  319. public function testMarshalDateTimeFromXmlRpc()
  320. {
  321. $iso8601 = '1997-07-16T19:20+01:00';
  322. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  323. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  324. Zend_XmlRpc_Value::XML_STRING);
  325. $this->assertXmlRpcType('dateTime', $val);
  326. $this->assertEquals('dateTime.iso8601', $val->getType());
  327. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  328. $this->assertType('DomElement', $val->getAsDOM());
  329. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  330. }
  331. // Base64
  332. public function testMarshalBase64FromString()
  333. {
  334. $native = 'foo';
  335. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  336. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  337. $this->assertXmlRpcType('base64', $val);
  338. $this->assertSame($native, $val->getValue());
  339. }
  340. public function testMarshalBase64FromXmlRpc()
  341. {
  342. $native = 'foo';
  343. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  344. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  345. Zend_XmlRpc_Value::XML_STRING);
  346. $this->assertXmlRpcType('base64', $val);
  347. $this->assertEquals('base64', $val->getType());
  348. $this->assertSame($native, $val->getValue());
  349. $this->assertType('DomElement', $val->getAsDOM());
  350. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  351. }
  352. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  353. {
  354. $native = 'foo';
  355. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  356. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  357. $this->assertXmlRpcType('base64', $val);
  358. $xml = $val->saveXML();
  359. $encoded = base64_encode($native);
  360. $this->assertContains($encoded, $xml);
  361. }
  362. // Exceptions
  363. public function testFactoryThrowsWhenInvalidTypeSpecified()
  364. {
  365. try {
  366. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  367. $this->fail();
  368. } catch (Exception $e) {
  369. $this->assertRegexp('/given type is not/i', $e->getMessage());
  370. }
  371. }
  372. // Custom Assertions and Helper Methods
  373. public function assertXmlRpcType($type, $object)
  374. {
  375. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  376. $this->assertType($type, $object);
  377. }
  378. public function wrapXml($xml)
  379. {
  380. return "<?xml version=\"1.0\"?>\n$xml\n";
  381. }
  382. }
  383. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  384. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  385. Zend_XmlRpc_ValueTest::main();
  386. }