ValueTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  263. Zend_XmlRpc_Value::XMLRPC_TYPE_NIL);
  264. $this->assertXmlRpcType('nil', $val);
  265. $this->assertSame($native, $val->getValue());
  266. }
  267. public function testMarshalNilFromXmlRpc()
  268. {
  269. $xml = '<value><nil/></value>';
  270. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  271. Zend_XmlRpc_Value::XML_STRING);
  272. $this->assertXmlRpcType('nil', $val);
  273. $this->assertEquals('nil', $val->getType());
  274. $this->assertSame(NULL, $val->getValue());
  275. $this->assertType('DomElement', $val->getAsDOM());
  276. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  277. }
  278. // Array
  279. public function testFactoryAutodetectsArray()
  280. {
  281. $val = Zend_XmlRpc_Value::getXmlRpcValue(array(0, 'foo'));
  282. $this->assertXmlRpcType('array', $val);
  283. }
  284. public function testMarshalArrayFromNative()
  285. {
  286. $native = array(0,1);
  287. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  288. Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY);
  289. $this->assertXmlRpcType('array', $val);
  290. $this->assertSame($native, $val->getValue());
  291. }
  292. public function testMarshalArrayFromXmlRpc()
  293. {
  294. $native = array(0,1);
  295. $xml = '<value><array><data><value><int>0</int></value>'
  296. . '<value><int>1</int></value></data></array></value>';
  297. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  298. Zend_XmlRpc_Value::XML_STRING);
  299. $this->assertXmlRpcType('array', $val);
  300. $this->assertEquals('array', $val->getType());
  301. $this->assertSame($native, $val->getValue());
  302. $this->assertType('DomElement', $val->getAsDOM());
  303. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  304. }
  305. public function testEmptyXmlRpcArrayResultsInEmptyArray()
  306. {
  307. $native = array();
  308. $xml = '<value><array><data/></array></value>';
  309. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  310. Zend_XmlRpc_Value::XML_STRING);
  311. $this->assertXmlRpcType('array', $val);
  312. $this->assertEquals('array', $val->getType());
  313. $this->assertSame($native, $val->getValue());
  314. $value = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  315. $this->assertXmlRpcType('array', $value);
  316. $this->assertEquals('array', $value->getType());
  317. $this->assertSame($native, $value->getValue());
  318. }
  319. public function testArrayMustContainDataElement()
  320. {
  321. $native = array();
  322. $xml = '<value><array/></value>';
  323. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  324. 'Invalid XML for XML-RPC native array type: ARRAY tag must contain DATA tag');
  325. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  326. Zend_XmlRpc_Value::XML_STRING);
  327. }
  328. /**
  329. * @group ZF-5405
  330. */
  331. public function testMarshalNilInStructWrappedInArray()
  332. {
  333. $expected = array(array('id' => '1', 'name' => 'vertebra, caudal', 'description' => null));
  334. $xml = '<value>'
  335. . '<array><data><value><struct><member><name>id</name><value><string>1</string></value></member>'
  336. . '<member><name>name</name><value><string>vertebra, caudal</string></value></member>'
  337. . '<member><name>description</name><value><nil/></value></member></struct></value></data></array>'
  338. . '</value>';
  339. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  340. $this->assertSame($expected, $val->getValue());
  341. }
  342. // Struct
  343. public function testFactoryAutodetectsStruct()
  344. {
  345. $val = Zend_XmlRpc_Value::getXmlRpcValue(array('foo' => 0));
  346. $this->assertXmlRpcType('struct', $val);
  347. }
  348. public function testFactoryAutodetectsStructFromObject()
  349. {
  350. $val = Zend_XmlRpc_Value::getXmlRpcValue((object)array('foo' => 0));
  351. $this->assertXmlRpcType('struct', $val);
  352. }
  353. public function testMarshalStructFromNative()
  354. {
  355. $native = array('foo' => 0);
  356. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  357. Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  358. $this->assertXmlRpcType('struct', $val);
  359. $this->assertSame($native, $val->getValue());
  360. }
  361. public function testMarshalStructFromXmlRpc()
  362. {
  363. $native = array('foo' => 0);
  364. $xml = '<value><struct><member><name>foo</name><value><int>0</int>'
  365. . '</value></member></struct></value>';
  366. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  367. Zend_XmlRpc_Value::XML_STRING);
  368. $this->assertXmlRpcType('struct', $val);
  369. $this->assertEquals('struct', $val->getType());
  370. $this->assertSame($native, $val->getValue());
  371. $this->assertType('DomElement', $val->getAsDOM());
  372. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  373. }
  374. public function testMarshallingStructWithMemberWithoutValue()
  375. {
  376. $native = array('foo' => 0, 'bar' => 1);
  377. $xml = '<value><struct>'
  378. . '<member><name>foo</name><value><int>0</int></value></member>'
  379. . '<member><name>foo</name><bar/></member>'
  380. . '<member><name>bar</name><value><int>1</int></value></member>'
  381. . '</struct></value>';
  382. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  383. Zend_XmlRpc_Value::XML_STRING);
  384. $this->assertXmlRpcType('struct', $val);
  385. $this->assertEquals('struct', $val->getType());
  386. $this->assertSame($native, $val->getValue());
  387. $this->assertType('DomElement', $val->getAsDOM());
  388. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  389. }
  390. public function testMarshallingStructWithMemberWithoutName()
  391. {
  392. $native = array('foo' => 0, 'bar' => 1);
  393. $xml = '<value><struct>'
  394. . '<member><name>foo</name><value><int>0</int></value></member>'
  395. . '<member><value><string>foo</string></value></member>'
  396. . '<member><name>bar</name><value><int>1</int></value></member>'
  397. . '</struct></value>';
  398. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  399. Zend_XmlRpc_Value::XML_STRING);
  400. $this->assertXmlRpcType('struct', $val);
  401. $this->assertEquals('struct', $val->getType());
  402. $this->assertSame($native, $val->getValue());
  403. $this->assertType('DomElement', $val->getAsDOM());
  404. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  405. }
  406. /**
  407. * @group ZF-7639
  408. */
  409. public function testMarshalStructFromXmlRpcWithEntities()
  410. {
  411. $native = array('&nbsp;' => 0);
  412. $xml = '<value><struct><member><name>&amp;nbsp;</name><value><int>0</int>'
  413. . '</value></member></struct></value>';
  414. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  415. $this->assertXmlRpcType('struct', $val);
  416. $this->assertSame($native, $val->getValue());
  417. $this->assertSame($this->wrapXml($xml), $val->saveXML());
  418. }
  419. /**
  420. * @group ZF-3947
  421. */
  422. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys()
  423. {
  424. $native = array('foo' => '');
  425. $xml = '<value><struct><member><name>foo</name>'
  426. . '<value/></member></struct></value>';
  427. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  428. Zend_XmlRpc_Value::XML_STRING);
  429. $this->assertXmlRpcType('struct', $val);
  430. $this->assertEquals('struct', $val->getType());
  431. $this->assertSame($native, $val->getValue());
  432. $this->assertType('DomElement', $val->getAsDOM());
  433. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  434. }
  435. // DateTime
  436. public function testMarshalDateTimeFromNativeString()
  437. {
  438. $native = '1997-07-16T19:20+01:00';
  439. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  440. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  441. $this->assertXmlRpcType('dateTime', $val);
  442. $expected = '1997-07-16T19:20+01:00';
  443. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  444. }
  445. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  446. {
  447. $native = '1997-07-16T19:20+01:00';
  448. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  449. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  450. $this->assertXmlRpcType('dateTime', $val);
  451. $expected = date('c', strtotime($native));
  452. $expected = substr($expected, 0, strlen($expected) - 6);
  453. $expected = str_replace('-', '', $expected);
  454. $received = $val->getValue();
  455. $this->assertEquals($expected, $received);
  456. }
  457. public function testMarshalDateTimeFromInvalidString()
  458. {
  459. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  460. "Cannot convert given value 'foobarbaz' to a timestamp");
  461. Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  462. }
  463. public function testMarshalDateTimeFromNativeInteger()
  464. {
  465. $native = strtotime('1997-07-16T19:20+01:00');
  466. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  467. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  468. $this->assertXmlRpcType('dateTime', $val);
  469. $this->assertSame($native, strtotime($val->getValue()));
  470. }
  471. public function testMarshalDateTimeFromXmlRpc()
  472. {
  473. $iso8601 = '1997-07-16T19:20+01:00';
  474. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  475. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  476. Zend_XmlRpc_Value::XML_STRING);
  477. $this->assertXmlRpcType('dateTime', $val);
  478. $this->assertEquals('dateTime.iso8601', $val->getType());
  479. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  480. $this->assertType('DomElement', $val->getAsDOM());
  481. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  482. }
  483. // Base64
  484. public function testMarshalBase64FromString()
  485. {
  486. $native = 'foo';
  487. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  488. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  489. $this->assertXmlRpcType('base64', $val);
  490. $this->assertSame($native, $val->getValue());
  491. }
  492. public function testMarshalBase64FromXmlRpc()
  493. {
  494. $native = 'foo';
  495. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  496. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  497. Zend_XmlRpc_Value::XML_STRING);
  498. $this->assertXmlRpcType('base64', $val);
  499. $this->assertEquals('base64', $val->getType());
  500. $this->assertSame($native, $val->getValue());
  501. $this->assertType('DomElement', $val->getAsDOM());
  502. $this->assertEquals($this->wrapXml($xml), $val->saveXML());
  503. }
  504. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  505. {
  506. $native = 'foo';
  507. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  508. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  509. $this->assertXmlRpcType('base64', $val);
  510. $xml = $val->saveXML();
  511. $encoded = base64_encode($native);
  512. $this->assertContains($encoded, $xml);
  513. }
  514. /**
  515. * @group ZF-3862
  516. */
  517. public function testMarshalSerializedObjectAsBase64()
  518. {
  519. $o = new Zend_XmlRpc_SerializableTestClass();
  520. $o->setProperty('foobar');
  521. $serialized = serialize($o);
  522. $val = Zend_XmlRpc_Value::getXmlRpcValue($serialized,
  523. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  524. $this->assertXmlRpcType('base64', $val);
  525. $o2 = unserialize($val->getValue());
  526. $this->assertSame('foobar', $o2->getProperty());
  527. }
  528. // Exceptions
  529. public function testFactoryThrowsWhenInvalidTypeSpecified()
  530. {
  531. try {
  532. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  533. $this->fail();
  534. } catch (Exception $e) {
  535. $this->assertRegexp('/given type is not/i', $e->getMessage());
  536. }
  537. }
  538. // Custom Assertions and Helper Methods
  539. public function assertXmlRpcType($type, $object)
  540. {
  541. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  542. $this->assertType($type, $object);
  543. }
  544. public function wrapXml($xml)
  545. {
  546. return "<?xml version=\"1.0\"?>\n$xml\n";
  547. }
  548. }
  549. class Zend_XmlRpc_SerializableTestClass
  550. {
  551. protected $_property;
  552. public function setProperty($property)
  553. {
  554. $this->_property = $property;
  555. }
  556. public function getProperty()
  557. {
  558. return $this->_property;
  559. }
  560. }
  561. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  562. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  563. Zend_XmlRpc_ValueTest::main();
  564. }