ValueTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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/BigInteger.php';
  32. require_once 'Zend/XmlRpc/Value/Collection.php';
  33. require_once 'Zend/XmlRpc/Value/Array.php';
  34. require_once 'Zend/XmlRpc/Value/Base64.php';
  35. require_once 'Zend/XmlRpc/Value/Boolean.php';
  36. require_once 'Zend/XmlRpc/Value/DateTime.php';
  37. require_once 'Zend/XmlRpc/Value/Double.php';
  38. require_once 'Zend/XmlRpc/Value/Integer.php';
  39. require_once 'Zend/XmlRpc/Value/String.php';
  40. require_once 'Zend/XmlRpc/Value/Nil.php';
  41. require_once 'Zend/XmlRpc/Value/Struct.php';
  42. require_once 'Zend/Crypt/Math/BigInteger.php';
  43. /**
  44. * Test case for Zend_XmlRpc_Value
  45. *
  46. * @category Zend
  47. * @package Zend_XmlRpc
  48. * @subpackage UnitTests
  49. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  50. * @license http://framework.zend.com/license/new-bsd New BSD License
  51. * @group Zend_XmlRpc
  52. */
  53. class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
  54. {
  55. /**
  56. * Runs the test methods of this class.
  57. *
  58. * @return void
  59. */
  60. public static function main()
  61. {
  62. require_once "PHPUnit/TextUI/TestRunner.php";
  63. $suite = new PHPUnit_Framework_TestSuite("Zend_XmlRpc_ValueTest");
  64. $result = PHPUnit_TextUI_TestRunner::run($suite);
  65. }
  66. // Boolean
  67. public function testFactoryAutodetectsBoolean()
  68. {
  69. foreach (array(true, false) as $native) {
  70. $val = Zend_XmlRpc_Value::getXmlRpcValue($native);
  71. $this->assertXmlRpcType('boolean', $val);
  72. }
  73. }
  74. public function testMarshalBooleanFromNative()
  75. {
  76. $native = true;
  77. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  78. Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN);
  79. $this->assertXmlRpcType('boolean', $val);
  80. $this->assertSame($native, $val->getValue());
  81. }
  82. public function testMarshalBooleanFromXmlRpc()
  83. {
  84. $xml = '<value><boolean>1</boolean></value>';
  85. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  86. Zend_XmlRpc_Value::XML_STRING);
  87. $this->assertXmlRpcType('boolean', $val);
  88. $this->assertEquals('boolean', $val->getType());
  89. $this->assertSame(true, $val->getValue());
  90. $this->assertType('DomElement', $val->getAsDOM());
  91. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  92. }
  93. // Integer
  94. public function testFactoryAutodetectsInteger()
  95. {
  96. $val = Zend_XmlRpc_Value::getXmlRpcValue(1);
  97. $this->assertXmlRpcType('integer', $val);
  98. }
  99. public function testMarshalIntegerFromNative()
  100. {
  101. $native = 1;
  102. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_I4,
  103. Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER);
  104. foreach ($types as $type) {
  105. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  106. $this->assertXmlRpcType('integer', $val);
  107. $this->assertSame($native, $val->getValue());
  108. }
  109. }
  110. public function testMarshalIntegerFromXmlRpc()
  111. {
  112. $native = 1;
  113. $xmls = array("<value><int>$native</int></value>",
  114. "<value><i4>$native</i4></value>");
  115. foreach ($xmls as $xml) {
  116. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  117. Zend_XmlRpc_Value::XML_STRING);
  118. $this->assertXmlRpcType('integer', $val);
  119. $this->assertEquals('int', $val->getType());
  120. $this->assertSame($native, $val->getValue());
  121. $this->assertType('DomElement', $val->getAsDOM());
  122. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  123. }
  124. }
  125. /**
  126. * @group ZF-3310
  127. */
  128. public function testMarshalI4FromOverlongNativeThrowsException()
  129. {
  130. $this->setExpectedException('Zend_XmlRpc_Value_Exception', 'Overlong integer given');
  131. Zend_XmlRpc_Value::getXmlRpcValue(PHP_INT_MAX + 1, Zend_XmlRpc_Value::XMLRPC_TYPE_I4);
  132. }
  133. /**
  134. * @group ZF-3310
  135. */
  136. public function testMarshalIntegerFromOverlongNativeThrowsException()
  137. {
  138. $this->setExpectedException('Zend_XmlRpc_Value_Exception', 'Overlong integer given');
  139. Zend_XmlRpc_Value::getXmlRpcValue(PHP_INT_MAX + 1, Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER);
  140. }
  141. // BigInteger
  142. /**
  143. * @group ZF-6445
  144. */
  145. public function testMarshalBigIntegerFromFromXmlRpc()
  146. {
  147. $bigInt = (string)(PHP_INT_MAX + 1);
  148. $native = new Zend_Crypt_Math_BigInteger();
  149. $native->init($bigInt);
  150. $xmlStrings = array("<value><i8>$bigInt</i8></value>",
  151. "<value><ex:i8 xmlns:ex=\"http://ws.apache.org/xmlrpc/namespaces/extensions\">$bigInt</ex:i8></value>");
  152. foreach ($xmlStrings as $xml) {
  153. $value = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  154. $this->assertEquals($native, $value->getValue());
  155. $this->assertEquals('i8', $value->getType());
  156. $this->assertType('DOMElement', $value->getAsDOM());
  157. $this->assertEquals($this->wrapXml($xml), $value->saveXml());
  158. }
  159. }
  160. /**
  161. * @group ZF-6445
  162. */
  163. public function testMarshalBigIntegerFromNative()
  164. {
  165. $native = (string)(PHP_INT_MAX + 1);
  166. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_APACHEI8,
  167. Zend_XmlRpc_Value::XMLRPC_TYPE_I8);
  168. $bigInt = new Zend_Crypt_Math_BigInteger();
  169. $bigInt->init($native);
  170. foreach ($types as $type) {
  171. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  172. $this->assertSame('i8', $value->getType());
  173. $this->assertEquals($bigInt, $value->getValue());
  174. }
  175. }
  176. // Double
  177. public function testFactoryAutodetectsFloat()
  178. {
  179. $val = Zend_XmlRpc_Value::getXmlRpcValue((float)1);
  180. $this->assertXmlRpcType('double', $val);
  181. }
  182. public function testMarshalDoubleFromNative()
  183. {
  184. $native = 1.1;
  185. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  186. Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
  187. $this->assertXmlRpcType('double', $val);
  188. $this->assertSame($native, $val->getValue());
  189. }
  190. public function testMarshalDoubleFromXmlRpc()
  191. {
  192. $native = 1.1;
  193. $xml = "<value><double>$native</double></value>";
  194. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  195. Zend_XmlRpc_Value::XML_STRING);
  196. $this->assertXmlRpcType('double', $val);
  197. $this->assertEquals('double', $val->getType());
  198. $this->assertSame($native, $val->getValue());
  199. $this->assertType('DomElement', $val->getAsDOM());
  200. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  201. }
  202. /**
  203. * @group ZF-7712
  204. */
  205. public function testMarshallingDoubleWithHigherPrecisionFromNative()
  206. {
  207. if (ini_get('precision') < 7) {
  208. $this->markTestSkipped('precision is too low');
  209. }
  210. $native = 0.1234567;
  211. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
  212. $this->assertXmlRpcType('double', $value);
  213. $this->assertSame($native, $value->getValue());
  214. }
  215. // String
  216. public function testFactoryAutodetectsString()
  217. {
  218. $val = Zend_XmlRpc_Value::getXmlRpcValue('');
  219. $this->assertXmlRpcType('string', $val);
  220. }
  221. public function testMarshalStringFromNative()
  222. {
  223. $native = 'foo';
  224. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  225. Zend_XmlRpc_Value::XMLRPC_TYPE_STRING);
  226. $this->assertXmlRpcType('string', $val);
  227. $this->assertSame($native, $val->getValue());
  228. }
  229. public function testMarshalStringFromXmlRpc()
  230. {
  231. $native = 'foo';
  232. $xml = "<value><string>$native</string></value>";
  233. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  234. Zend_XmlRpc_Value::XML_STRING);
  235. $this->assertXmlRpcType('string', $val);
  236. $this->assertEquals('string', $val->getType());
  237. $this->assertSame($native, $val->getValue());
  238. $this->assertType('DomElement', $val->getAsDOM());
  239. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  240. }
  241. public function testMarshalStringFromDefault()
  242. {
  243. $native = 'foo';
  244. $xml = "<string>$native</string>";
  245. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  246. Zend_XmlRpc_Value::XML_STRING);
  247. $this->assertXmlRpcType('string', $val);
  248. $this->assertEquals('string', $val->getType());
  249. $this->assertSame($native, $val->getValue());
  250. $this->assertType('DomElement', $val->getAsDOM());
  251. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  252. }
  253. //Nil
  254. public function testFactoryAutodetectsNil()
  255. {
  256. $val = Zend_XmlRpc_Value::getXmlRpcValue(NULL);
  257. $this->assertXmlRpcType('nil', $val);
  258. }
  259. public function testMarshalNilFromNative()
  260. {
  261. $native = NULL;
  262. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_NIL,
  263. Zend_XmlRpc_Value::XMLRPC_TYPE_APACHENIL);
  264. foreach ($types as $type) {
  265. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  266. $this->assertXmlRpcType('nil', $value);
  267. $this->assertSame($native, $value->getValue());
  268. }
  269. }
  270. public function testMarshalNilFromXmlRpc()
  271. {
  272. $xmls = array('<value><nil/></value>',
  273. '<value><ex:nil xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions"/></value>');
  274. foreach ($xmls as $xml) {
  275. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  276. Zend_XmlRpc_Value::XML_STRING);
  277. $this->assertXmlRpcType('nil', $val);
  278. $this->assertEquals('nil', $val->getType());
  279. $this->assertSame(NULL, $val->getValue());
  280. $this->assertType('DomElement', $val->getAsDOM());
  281. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  282. }
  283. }
  284. // Array
  285. public function testFactoryAutodetectsArray()
  286. {
  287. $val = Zend_XmlRpc_Value::getXmlRpcValue(array(0, 'foo'));
  288. $this->assertXmlRpcType('array', $val);
  289. }
  290. public function testMarshalArrayFromNative()
  291. {
  292. $native = array(0,1);
  293. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  294. Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY);
  295. $this->assertXmlRpcType('array', $val);
  296. $this->assertSame($native, $val->getValue());
  297. }
  298. public function testMarshalArrayFromXmlRpc()
  299. {
  300. $native = array(0,1);
  301. $xml = '<value><array><data><value><int>0</int></value>'
  302. . '<value><int>1</int></value></data></array></value>';
  303. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  304. Zend_XmlRpc_Value::XML_STRING);
  305. $this->assertXmlRpcType('array', $val);
  306. $this->assertEquals('array', $val->getType());
  307. $this->assertSame($native, $val->getValue());
  308. $this->assertType('DomElement', $val->getAsDOM());
  309. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  310. }
  311. public function testEmptyXmlRpcArrayResultsInEmptyArray()
  312. {
  313. $native = array();
  314. $xml = '<value><array><data/></array></value>';
  315. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  316. Zend_XmlRpc_Value::XML_STRING);
  317. $this->assertXmlRpcType('array', $val);
  318. $this->assertEquals('array', $val->getType());
  319. $this->assertSame($native, $val->getValue());
  320. $value = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  321. $this->assertXmlRpcType('array', $value);
  322. $this->assertEquals('array', $value->getType());
  323. $this->assertSame($native, $value->getValue());
  324. }
  325. public function testArrayMustContainDataElement()
  326. {
  327. $native = array();
  328. $xml = '<value><array/></value>';
  329. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  330. 'Invalid XML for XML-RPC native array type: ARRAY tag must contain DATA tag');
  331. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  332. Zend_XmlRpc_Value::XML_STRING);
  333. }
  334. /**
  335. * @group ZF-5405
  336. */
  337. public function testMarshalNilInStructWrappedInArray()
  338. {
  339. $expected = array(array('id' => '1', 'name' => 'vertebra, caudal', 'description' => null));
  340. $xml = '<value>'
  341. . '<array><data><value><struct><member><name>id</name><value><string>1</string></value></member>'
  342. . '<member><name>name</name><value><string>vertebra, caudal</string></value></member>'
  343. . '<member><name>description</name><value><nil/></value></member></struct></value></data></array>'
  344. . '</value>';
  345. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  346. $this->assertSame($expected, $val->getValue());
  347. }
  348. // Struct
  349. public function testFactoryAutodetectsStruct()
  350. {
  351. $val = Zend_XmlRpc_Value::getXmlRpcValue(array('foo' => 0));
  352. $this->assertXmlRpcType('struct', $val);
  353. }
  354. public function testFactoryAutodetectsStructFromObject()
  355. {
  356. $val = Zend_XmlRpc_Value::getXmlRpcValue((object)array('foo' => 0));
  357. $this->assertXmlRpcType('struct', $val);
  358. }
  359. public function testMarshalStructFromNative()
  360. {
  361. $native = array('foo' => 0);
  362. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  363. Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  364. $this->assertXmlRpcType('struct', $val);
  365. $this->assertSame($native, $val->getValue());
  366. }
  367. public function testMarshalStructFromXmlRpc()
  368. {
  369. $native = array('foo' => 0);
  370. $xml = '<value><struct><member><name>foo</name><value><int>0</int>'
  371. . '</value></member></struct></value>';
  372. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  373. Zend_XmlRpc_Value::XML_STRING);
  374. $this->assertXmlRpcType('struct', $val);
  375. $this->assertEquals('struct', $val->getType());
  376. $this->assertSame($native, $val->getValue());
  377. $this->assertType('DomElement', $val->getAsDOM());
  378. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  379. }
  380. public function testMarshallingStructWithMemberWithoutValue()
  381. {
  382. $native = array('foo' => 0, 'bar' => 1);
  383. $xml = '<value><struct>'
  384. . '<member><name>foo</name><value><int>0</int></value></member>'
  385. . '<member><name>foo</name><bar/></member>'
  386. . '<member><name>bar</name><value><int>1</int></value></member>'
  387. . '</struct></value>';
  388. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  389. Zend_XmlRpc_Value::XML_STRING);
  390. $this->assertXmlRpcType('struct', $val);
  391. $this->assertEquals('struct', $val->getType());
  392. $this->assertSame($native, $val->getValue());
  393. $this->assertType('DomElement', $val->getAsDOM());
  394. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  395. }
  396. public function testMarshallingStructWithMemberWithoutName()
  397. {
  398. $native = array('foo' => 0, 'bar' => 1);
  399. $xml = '<value><struct>'
  400. . '<member><name>foo</name><value><int>0</int></value></member>'
  401. . '<member><value><string>foo</string></value></member>'
  402. . '<member><name>bar</name><value><int>1</int></value></member>'
  403. . '</struct></value>';
  404. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  405. Zend_XmlRpc_Value::XML_STRING);
  406. $this->assertXmlRpcType('struct', $val);
  407. $this->assertEquals('struct', $val->getType());
  408. $this->assertSame($native, $val->getValue());
  409. $this->assertType('DomElement', $val->getAsDOM());
  410. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  411. }
  412. /**
  413. * @group ZF-7639
  414. */
  415. public function testMarshalStructFromXmlRpcWithEntities()
  416. {
  417. $native = array('&nbsp;' => 0);
  418. $xml = '<value><struct><member><name>&amp;nbsp;</name><value><int>0</int>'
  419. . '</value></member></struct></value>';
  420. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  421. $this->assertXmlRpcType('struct', $val);
  422. $this->assertSame($native, $val->getValue());
  423. $this->assertSame($this->wrapXml($xml), $val->saveXML());
  424. }
  425. /**
  426. * @group ZF-3947
  427. */
  428. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys()
  429. {
  430. $native = array('foo' => '');
  431. $xml = '<value><struct><member><name>foo</name>'
  432. . '<value/></member></struct></value>';
  433. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  434. Zend_XmlRpc_Value::XML_STRING);
  435. $this->assertXmlRpcType('struct', $val);
  436. $this->assertEquals('struct', $val->getType());
  437. $this->assertSame($native, $val->getValue());
  438. $this->assertType('DomElement', $val->getAsDOM());
  439. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  440. }
  441. public function testMarshallingStructWithMultibyteValueFromXmlRpcRetainsMultibyteValue()
  442. {
  443. $native = array('foo' => 'ß');
  444. $xml = '<value><struct><member><name>foo</name>'
  445. . '<value><string>&#xDF;</string></value></member></struct></value>';
  446. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  447. Zend_XmlRpc_Value::XML_STRING);
  448. $this->assertXmlRpcType('struct', $val);
  449. $this->assertEquals('struct', $val->getType());
  450. $this->assertSame($native, $val->getValue());
  451. $this->assertType('DomElement', $val->getAsDOM());
  452. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  453. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  454. $this->assertSame($native, $val->getValue());
  455. $this->assertSame($xml . "\n", $val->saveXML());
  456. }
  457. // DateTime
  458. public function testMarshalDateTimeFromNativeString()
  459. {
  460. $native = '1997-07-16T19:20+01:00';
  461. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  462. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  463. $this->assertXmlRpcType('dateTime', $val);
  464. $expected = '1997-07-16T19:20+01:00';
  465. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  466. }
  467. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  468. {
  469. $native = '1997-07-16T19:20+01:00';
  470. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  471. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  472. $this->assertXmlRpcType('dateTime', $val);
  473. $expected = date('c', strtotime($native));
  474. $expected = substr($expected, 0, strlen($expected) - 6);
  475. $expected = str_replace('-', '', $expected);
  476. $received = $val->getValue();
  477. $this->assertEquals($expected, $received);
  478. }
  479. public function testMarshalDateTimeFromInvalidString()
  480. {
  481. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  482. "Cannot convert given value 'foobarbaz' to a timestamp");
  483. Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  484. }
  485. public function testMarshalDateTimeFromNativeInteger()
  486. {
  487. $native = strtotime('1997-07-16T19:20+01:00');
  488. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  489. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  490. $this->assertXmlRpcType('dateTime', $val);
  491. $this->assertSame($native, strtotime($val->getValue()));
  492. }
  493. public function testMarshalDateTimeFromXmlRpc()
  494. {
  495. $iso8601 = '1997-07-16T19:20+01:00';
  496. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  497. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  498. Zend_XmlRpc_Value::XML_STRING);
  499. $this->assertXmlRpcType('dateTime', $val);
  500. $this->assertEquals('dateTime.iso8601', $val->getType());
  501. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  502. $this->assertType('DomElement', $val->getAsDOM());
  503. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  504. }
  505. // Base64
  506. public function testMarshalBase64FromString()
  507. {
  508. $native = 'foo';
  509. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  510. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  511. $this->assertXmlRpcType('base64', $val);
  512. $this->assertSame($native, $val->getValue());
  513. }
  514. public function testMarshalBase64FromXmlRpc()
  515. {
  516. $native = 'foo';
  517. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  518. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  519. Zend_XmlRpc_Value::XML_STRING);
  520. $this->assertXmlRpcType('base64', $val);
  521. $this->assertEquals('base64', $val->getType());
  522. $this->assertSame($native, $val->getValue());
  523. $this->assertType('DomElement', $val->getAsDOM());
  524. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  525. }
  526. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  527. {
  528. $native = 'foo';
  529. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  530. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  531. $this->assertXmlRpcType('base64', $val);
  532. $xml = $val->saveXML();
  533. $encoded = base64_encode($native);
  534. $this->assertContains($encoded, $xml);
  535. }
  536. /**
  537. * @group ZF-3862
  538. */
  539. public function testMarshalSerializedObjectAsBase64()
  540. {
  541. $o = new Zend_XmlRpc_SerializableTestClass();
  542. $o->setProperty('foobar');
  543. $serialized = serialize($o);
  544. $val = Zend_XmlRpc_Value::getXmlRpcValue($serialized,
  545. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  546. $this->assertXmlRpcType('base64', $val);
  547. $o2 = unserialize($val->getValue());
  548. $this->assertSame('foobar', $o2->getProperty());
  549. }
  550. // Exceptions
  551. public function testFactoryThrowsWhenInvalidTypeSpecified()
  552. {
  553. try {
  554. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  555. $this->fail();
  556. } catch (Exception $e) {
  557. $this->assertRegexp('/given type is not/i', $e->getMessage());
  558. }
  559. }
  560. // Custom Assertions and Helper Methods
  561. public function assertXmlRpcType($type, $object)
  562. {
  563. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  564. $this->assertType($type, $object);
  565. }
  566. public function wrapXml($xml)
  567. {
  568. return "<?xml version=\"1.0\"?>\n$xml\n";
  569. }
  570. }
  571. class Zend_XmlRpc_SerializableTestClass
  572. {
  573. protected $_property;
  574. public function setProperty($property)
  575. {
  576. $this->_property = $property;
  577. }
  578. public function getProperty()
  579. {
  580. return $this->_property;
  581. }
  582. }
  583. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  584. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  585. Zend_XmlRpc_ValueTest::main();
  586. }