ValueTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. require_once "PHPUnit/Framework/TestCase.php";
  23. require_once "PHPUnit/Framework/TestSuite.php";
  24. require_once 'Zend/XmlRpc/Value.php';
  25. require_once 'Zend/XmlRpc/Value/Scalar.php';
  26. require_once 'Zend/XmlRpc/Value/BigInteger.php';
  27. require_once 'Zend/XmlRpc/Value/Collection.php';
  28. require_once 'Zend/XmlRpc/Value/Array.php';
  29. require_once 'Zend/XmlRpc/Value/Base64.php';
  30. require_once 'Zend/XmlRpc/Value/Boolean.php';
  31. require_once 'Zend/XmlRpc/Value/DateTime.php';
  32. require_once 'Zend/XmlRpc/Value/Double.php';
  33. require_once 'Zend/XmlRpc/Value/Integer.php';
  34. require_once 'Zend/XmlRpc/Value/String.php';
  35. require_once 'Zend/XmlRpc/Value/Nil.php';
  36. require_once 'Zend/XmlRpc/Value/Struct.php';
  37. require_once 'Zend/Crypt/Math/BigInteger.php';
  38. require_once 'Zend/XmlRpc/Generator/DOMDocument.php';
  39. require_once 'Zend/XmlRpc/Generator/XMLWriter.php';
  40. require_once 'Zend/XmlRpc/TestProvider.php';
  41. require_once 'Zend/Date.php';
  42. /**
  43. * Test case for Zend_XmlRpc_Value
  44. *
  45. * @category Zend
  46. * @package Zend_XmlRpc
  47. * @subpackage UnitTests
  48. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  49. * @license http://framework.zend.com/license/new-bsd New BSD License
  50. * @group Zend_XmlRpc
  51. */
  52. class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
  53. {
  54. // Boolean
  55. public function testFactoryAutodetectsBoolean()
  56. {
  57. foreach (array(true, false) as $native) {
  58. $val = Zend_XmlRpc_Value::getXmlRpcValue($native);
  59. $this->assertXmlRpcType('boolean', $val);
  60. }
  61. }
  62. public function testMarshalBooleanFromNative()
  63. {
  64. $native = true;
  65. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  66. Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN);
  67. $this->assertXmlRpcType('boolean', $val);
  68. $this->assertSame($native, $val->getValue());
  69. }
  70. /**
  71. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  72. */
  73. public function testMarshalBooleanFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  74. {
  75. Zend_XmlRpc_Value::setGenerator($generator);
  76. $xml = '<value><boolean>1</boolean></value>';
  77. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  78. Zend_XmlRpc_Value::XML_STRING);
  79. $this->assertXmlRpcType('boolean', $val);
  80. $this->assertEquals('boolean', $val->getType());
  81. $this->assertSame(true, $val->getValue());
  82. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  83. }
  84. // Integer
  85. public function testFactoryAutodetectsInteger()
  86. {
  87. $val = Zend_XmlRpc_Value::getXmlRpcValue(1);
  88. $this->assertXmlRpcType('integer', $val);
  89. }
  90. public function testMarshalIntegerFromNative()
  91. {
  92. $native = 1;
  93. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_I4,
  94. Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER);
  95. foreach ($types as $type) {
  96. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  97. $this->assertXmlRpcType('integer', $val);
  98. $this->assertSame($native, $val->getValue());
  99. }
  100. }
  101. /**
  102. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  103. */
  104. public function testMarshalIntegerFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  105. {
  106. Zend_XmlRpc_Value::setGenerator($generator);
  107. $native = 1;
  108. $xmls = array("<value><int>$native</int></value>",
  109. "<value><i4>$native</i4></value>");
  110. foreach ($xmls as $xml) {
  111. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  112. Zend_XmlRpc_Value::XML_STRING);
  113. $this->assertXmlRpcType('integer', $val);
  114. $this->assertEquals('int', $val->getType());
  115. $this->assertSame($native, $val->getValue());
  116. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  117. }
  118. }
  119. /**
  120. * @group ZF-3310
  121. */
  122. public function testMarshalI4FromOverlongNativeThrowsException()
  123. {
  124. $this->setExpectedException('Zend_XmlRpc_Value_Exception', 'Overlong integer given');
  125. Zend_XmlRpc_Value::getXmlRpcValue(PHP_INT_MAX + 1, Zend_XmlRpc_Value::XMLRPC_TYPE_I4);
  126. }
  127. /**
  128. * @group ZF-3310
  129. */
  130. public function testMarshalIntegerFromOverlongNativeThrowsException()
  131. {
  132. $this->setExpectedException('Zend_XmlRpc_Value_Exception', 'Overlong integer given');
  133. Zend_XmlRpc_Value::getXmlRpcValue(PHP_INT_MAX + 1, Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER);
  134. }
  135. // BigInteger
  136. /**
  137. * @group ZF-6445
  138. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  139. */
  140. public function testMarshalBigIntegerFromFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  141. {
  142. Zend_XmlRpc_Value::setGenerator($generator);
  143. $bigInt = (string)(PHP_INT_MAX + 1);
  144. $native = new Zend_Crypt_Math_BigInteger();
  145. $native->init($bigInt);
  146. $xmlStrings = array("<value><i8>$bigInt</i8></value>",
  147. "<value><ex:i8 xmlns:ex=\"http://ws.apache.org/xmlrpc/namespaces/extensions\">$bigInt</ex:i8></value>");
  148. foreach ($xmlStrings as $xml) {
  149. $value = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  150. $this->assertEquals($native, $value->getValue());
  151. $this->assertEquals('i8', $value->getType());
  152. $this->assertEquals($this->wrapXml($xml), $value->saveXml());
  153. }
  154. }
  155. /**
  156. * @group ZF-6445
  157. */
  158. public function testMarshalBigIntegerFromNative()
  159. {
  160. $native = (string)(PHP_INT_MAX + 1);
  161. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_APACHEI8,
  162. Zend_XmlRpc_Value::XMLRPC_TYPE_I8);
  163. $bigInt = new Zend_Crypt_Math_BigInteger();
  164. $bigInt->init($native);
  165. foreach ($types as $type) {
  166. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  167. $this->assertSame('i8', $value->getType());
  168. $this->assertEquals($bigInt, $value->getValue());
  169. }
  170. $value = Zend_XmlRpc_Value::getXmlRpcValue($bigInt);
  171. $this->assertSame('i8', $value->getType());
  172. $this->assertEquals($bigInt, $value->getValue());
  173. }
  174. // Double
  175. public function testFactoryAutodetectsFloat()
  176. {
  177. $val = Zend_XmlRpc_Value::getXmlRpcValue((float)1);
  178. $this->assertXmlRpcType('double', $val);
  179. }
  180. public function testMarshalDoubleFromNative()
  181. {
  182. $native = 1.1;
  183. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  184. Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
  185. $this->assertXmlRpcType('double', $val);
  186. $this->assertSame($native, $val->getValue());
  187. }
  188. /**
  189. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  190. */
  191. public function testMarshalDoubleFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  192. {
  193. Zend_XmlRpc_Value::setGenerator($generator);
  194. $native = 1.1;
  195. $xml = "<value><double>$native</double></value>";
  196. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  197. Zend_XmlRpc_Value::XML_STRING);
  198. $this->assertXmlRpcType('double', $val);
  199. $this->assertEquals('double', $val->getType());
  200. $this->assertSame($native, $val->getValue());
  201. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  202. }
  203. /**
  204. * @group ZF-7712
  205. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  206. */
  207. public function testMarshallingDoubleWithHigherPrecisionFromNative(Zend_XmlRpc_Generator_Abstract $generator)
  208. {
  209. Zend_XmlRpc_Value::setGenerator($generator);
  210. if (ini_get('precision') < 7) {
  211. $this->markTestSkipped('precision is too low');
  212. }
  213. $native = 0.1234567;
  214. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
  215. $this->assertXmlRpcType('double', $value);
  216. $this->assertSame($native, $value->getValue());
  217. $this->assertSame('<value><double>0.1234567</double></value>', trim($value->saveXml()));
  218. }
  219. /**
  220. * @group ZF-7712
  221. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  222. */
  223. public function testMarshallingDoubleWithHigherPrecisionFromNativeWithTrailingZeros(Zend_XmlRpc_Generator_Abstract $generator)
  224. {
  225. Zend_XmlRpc_Value::setGenerator($generator);
  226. if (ini_get('precision') < 7) {
  227. $this->markTestSkipped('precision is too low');
  228. }
  229. $native = 0.1;
  230. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
  231. $this->assertXmlRpcType('double', $value);
  232. $this->assertSame($native, $value->getValue());
  233. $this->assertSame('<value><double>0.1</double></value>', trim($value->saveXml()));
  234. }
  235. // String
  236. public function testFactoryAutodetectsString()
  237. {
  238. $val = Zend_XmlRpc_Value::getXmlRpcValue('');
  239. $this->assertXmlRpcType('string', $val);
  240. }
  241. public function testMarshalStringFromNative()
  242. {
  243. $native = 'foo';
  244. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  245. Zend_XmlRpc_Value::XMLRPC_TYPE_STRING);
  246. $this->assertXmlRpcType('string', $val);
  247. $this->assertSame($native, $val->getValue());
  248. }
  249. /**
  250. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  251. */
  252. public function testMarshalStringFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  253. {
  254. Zend_XmlRpc_Value::setGenerator($generator);
  255. $native = 'foo';
  256. $xml = "<value><string>$native</string></value>";
  257. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  258. Zend_XmlRpc_Value::XML_STRING);
  259. $this->assertXmlRpcType('string', $val);
  260. $this->assertEquals('string', $val->getType());
  261. $this->assertSame($native, $val->getValue());
  262. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  263. }
  264. /**
  265. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  266. */
  267. public function testMarshalStringFromDefault(Zend_XmlRpc_Generator_Abstract $generator)
  268. {
  269. Zend_XmlRpc_Value::setGenerator($generator);
  270. $native = 'foo';
  271. $xml = "<string>$native</string>";
  272. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  273. Zend_XmlRpc_Value::XML_STRING);
  274. $this->assertXmlRpcType('string', $val);
  275. $this->assertEquals('string', $val->getType());
  276. $this->assertSame($native, $val->getValue());
  277. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  278. }
  279. //Nil
  280. public function testFactoryAutodetectsNil()
  281. {
  282. $val = Zend_XmlRpc_Value::getXmlRpcValue(NULL);
  283. $this->assertXmlRpcType('nil', $val);
  284. }
  285. public function testMarshalNilFromNative()
  286. {
  287. $native = NULL;
  288. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_NIL,
  289. Zend_XmlRpc_Value::XMLRPC_TYPE_APACHENIL);
  290. foreach ($types as $type) {
  291. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  292. $this->assertXmlRpcType('nil', $value);
  293. $this->assertSame($native, $value->getValue());
  294. }
  295. }
  296. /**
  297. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  298. */
  299. public function testMarshalNilFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  300. {
  301. Zend_XmlRpc_Value::setGenerator($generator);
  302. $xmls = array('<value><nil/></value>',
  303. '<value><ex:nil xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions"/></value>');
  304. foreach ($xmls as $xml) {
  305. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  306. Zend_XmlRpc_Value::XML_STRING);
  307. $this->assertXmlRpcType('nil', $val);
  308. $this->assertEquals('nil', $val->getType());
  309. $this->assertSame(NULL, $val->getValue());
  310. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  311. }
  312. }
  313. // Array
  314. public function testFactoryAutodetectsArray()
  315. {
  316. $val = Zend_XmlRpc_Value::getXmlRpcValue(array(0, 'foo'));
  317. $this->assertXmlRpcType('array', $val);
  318. }
  319. public function testMarshalArrayFromNative()
  320. {
  321. $native = array(0,1);
  322. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  323. Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY);
  324. $this->assertXmlRpcType('array', $val);
  325. $this->assertSame($native, $val->getValue());
  326. }
  327. /**
  328. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  329. */
  330. public function testMarshalArrayFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  331. {
  332. Zend_XmlRpc_Value::setGenerator($generator);
  333. $native = array(0,1);
  334. $xml = '<value><array><data><value><int>0</int></value>'
  335. . '<value><int>1</int></value></data></array></value>';
  336. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  337. Zend_XmlRpc_Value::XML_STRING);
  338. $this->assertXmlRpcType('array', $val);
  339. $this->assertEquals('array', $val->getType());
  340. $this->assertSame($native, $val->getValue());
  341. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  342. }
  343. /**
  344. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  345. */
  346. public function testEmptyXmlRpcArrayResultsInEmptyArray(Zend_XmlRpc_Generator_Abstract $generator)
  347. {
  348. Zend_XmlRpc_Value::setGenerator($generator);
  349. $native = array();
  350. $xml = '<value><array><data/></array></value>';
  351. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  352. Zend_XmlRpc_Value::XML_STRING);
  353. $this->assertXmlRpcType('array', $val);
  354. $this->assertEquals('array', $val->getType());
  355. $this->assertSame($native, $val->getValue());
  356. $value = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  357. $this->assertXmlRpcType('array', $value);
  358. $this->assertEquals('array', $value->getType());
  359. $this->assertSame($native, $value->getValue());
  360. }
  361. /**
  362. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  363. */
  364. public function testArrayMustContainDataElement(Zend_XmlRpc_Generator_Abstract $generator)
  365. {
  366. Zend_XmlRpc_Value::setGenerator($generator);
  367. $native = array();
  368. $xml = '<value><array/></value>';
  369. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  370. 'Invalid XML for XML-RPC native array type: ARRAY tag must contain DATA tag');
  371. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  372. Zend_XmlRpc_Value::XML_STRING);
  373. }
  374. /**
  375. * @group ZF-5405
  376. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  377. */
  378. public function testMarshalNilInStructWrappedInArray(Zend_XmlRpc_Generator_Abstract $generator)
  379. {
  380. Zend_XmlRpc_Value::setGenerator($generator);
  381. $expected = array(array('id' => '1', 'name' => 'vertebra, caudal', 'description' => null));
  382. $xml = '<value>'
  383. . '<array><data><value><struct><member><name>id</name><value><string>1</string></value></member>'
  384. . '<member><name>name</name><value><string>vertebra, caudal</string></value></member>'
  385. . '<member><name>description</name><value><nil/></value></member></struct></value></data></array>'
  386. . '</value>';
  387. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  388. $this->assertSame($expected, $val->getValue());
  389. }
  390. // Struct
  391. public function testFactoryAutodetectsStruct()
  392. {
  393. $val = Zend_XmlRpc_Value::getXmlRpcValue(array('foo' => 0));
  394. $this->assertXmlRpcType('struct', $val);
  395. }
  396. public function testFactoryAutodetectsStructFromObject()
  397. {
  398. $val = Zend_XmlRpc_Value::getXmlRpcValue((object)array('foo' => 0));
  399. $this->assertXmlRpcType('struct', $val);
  400. }
  401. public function testMarshalStructFromNative()
  402. {
  403. $native = array('foo' => 0);
  404. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  405. Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  406. $this->assertXmlRpcType('struct', $val);
  407. $this->assertSame($native, $val->getValue());
  408. }
  409. /**
  410. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  411. */
  412. public function testMarshalStructFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  413. {
  414. Zend_XmlRpc_Value::setGenerator($generator);
  415. $native = array('foo' => 0);
  416. $xml = '<value><struct><member><name>foo</name><value><int>0</int>'
  417. . '</value></member></struct></value>';
  418. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  419. Zend_XmlRpc_Value::XML_STRING);
  420. $this->assertXmlRpcType('struct', $val);
  421. $this->assertEquals('struct', $val->getType());
  422. $this->assertSame($native, $val->getValue());
  423. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  424. }
  425. /**
  426. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  427. */
  428. public function testMarshallingStructWithMemberWithoutValue(Zend_XmlRpc_Generator_Abstract $generator)
  429. {
  430. Zend_XmlRpc_Value::setGenerator($generator);
  431. $native = array('foo' => 0, 'bar' => 1);
  432. $xml = '<value><struct>'
  433. . '<member><name>foo</name><value><int>0</int></value></member>'
  434. . '<member><name>foo</name><bar/></member>'
  435. . '<member><name>bar</name><value><int>1</int></value></member>'
  436. . '</struct></value>';
  437. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  438. Zend_XmlRpc_Value::XML_STRING);
  439. $this->assertXmlRpcType('struct', $val);
  440. $this->assertEquals('struct', $val->getType());
  441. $this->assertSame($native, $val->getValue());
  442. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  443. }
  444. /**
  445. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  446. */
  447. public function testMarshallingStructWithMemberWithoutName(Zend_XmlRpc_Generator_Abstract $generator)
  448. {
  449. Zend_XmlRpc_Value::setGenerator($generator);
  450. $native = array('foo' => 0, 'bar' => 1);
  451. $xml = '<value><struct>'
  452. . '<member><name>foo</name><value><int>0</int></value></member>'
  453. . '<member><value><string>foo</string></value></member>'
  454. . '<member><name>bar</name><value><int>1</int></value></member>'
  455. . '</struct></value>';
  456. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  457. Zend_XmlRpc_Value::XML_STRING);
  458. $this->assertXmlRpcType('struct', $val);
  459. $this->assertEquals('struct', $val->getType());
  460. $this->assertSame($native, $val->getValue());
  461. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  462. }
  463. /**
  464. * @group ZF-7639
  465. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  466. */
  467. public function testMarshalStructFromXmlRpcWithEntities(Zend_XmlRpc_Generator_Abstract $generator)
  468. {
  469. Zend_XmlRpc_Value::setGenerator($generator);
  470. $native = array('&nbsp;' => 0);
  471. $xml = '<value><struct><member><name>&amp;nbsp;</name><value><int>0</int>'
  472. . '</value></member></struct></value>';
  473. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  474. $this->assertXmlRpcType('struct', $val);
  475. $this->assertSame($native, $val->getValue());
  476. $this->assertSame($this->wrapXml($xml), $val->saveXml());
  477. }
  478. /**
  479. * @group ZF-3947
  480. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  481. */
  482. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys(Zend_XmlRpc_Generator_Abstract $generator)
  483. {
  484. Zend_XmlRpc_Value::setGenerator($generator);
  485. $native = array('foo' => '');
  486. $xml = '<value><struct><member><name>foo</name>'
  487. . '<value><string/></value></member></struct></value>';
  488. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  489. Zend_XmlRpc_Value::XML_STRING);
  490. $this->assertXmlRpcType('struct', $val);
  491. $this->assertEquals('struct', $val->getType());
  492. $this->assertSame($native, $val->getValue());
  493. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  494. }
  495. /**
  496. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  497. */
  498. public function testMarshallingStructWithMultibyteValueFromXmlRpcRetainsMultibyteValue(Zend_XmlRpc_Generator_Abstract $generator)
  499. {
  500. Zend_XmlRpc_Value::setGenerator($generator);
  501. $native = array('foo' => 'ß');
  502. $xmlDecl = '<?xml version="1.0" encoding="UTF-8"?>';
  503. $xml = '<value><struct><member><name>foo</name><value><string>ß</string></value></member></struct></value>';
  504. $val = Zend_XmlRpc_Value::getXmlRpcValue($xmlDecl . $xml,
  505. Zend_XmlRpc_Value::XML_STRING);
  506. $this->assertXmlRpcType('struct', $val);
  507. $this->assertEquals('struct', $val->getType());
  508. $this->assertSame($native, $val->getValue());
  509. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  510. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  511. $this->assertSame($native, $val->getValue());
  512. $this->assertSame(trim($xml), trim($val->saveXml()));
  513. }
  514. // DateTime
  515. public function testMarshalDateTimeFromNativeString()
  516. {
  517. $native = '1997-07-16T19:20+01:00';
  518. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  519. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  520. $this->assertXmlRpcType('dateTime', $val);
  521. $expected = '1997-07-16T19:20+01:00';
  522. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  523. }
  524. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  525. {
  526. $native = '1997-07-16T19:20+01:00';
  527. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  528. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  529. $this->assertXmlRpcType('dateTime', $val);
  530. $expected = date('c', strtotime($native));
  531. $expected = substr($expected, 0, strlen($expected) - 6);
  532. $expected = str_replace('-', '', $expected);
  533. $received = $val->getValue();
  534. $this->assertEquals($expected, $received);
  535. }
  536. public function testMarshalDateTimeFromInvalidString()
  537. {
  538. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  539. "Cannot convert given value 'foobarbaz' to a timestamp");
  540. Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  541. }
  542. public function testMarshalDateTimeFromNativeInteger()
  543. {
  544. $native = strtotime('1997-07-16T19:20+01:00');
  545. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  546. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  547. $this->assertXmlRpcType('dateTime', $val);
  548. $this->assertSame($native, strtotime($val->getValue()));
  549. }
  550. /**
  551. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  552. */
  553. public function testMarshalDateTimeFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  554. {
  555. Zend_XmlRpc_Value::setGenerator($generator);
  556. $iso8601 = '1997-07-16T19:20+01:00';
  557. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  558. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  559. Zend_XmlRpc_Value::XML_STRING);
  560. $this->assertXmlRpcType('dateTime', $val);
  561. $this->assertEquals('dateTime.iso8601', $val->getType());
  562. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  563. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  564. }
  565. /**
  566. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  567. * @group ZF-4249
  568. */
  569. public function testMarshalDateTimeFromFromZendDate(Zend_XmlRpc_Generator_Abstract $generator)
  570. {
  571. Zend_XmlRpc_Value::setGenerator($generator);
  572. $date = new Zend_Date(array('year' => 2039, 'month' => 4, 'day' => 18,
  573. 'hour' => 13, 'minute' => 14, 'second' => 15));
  574. $dateString = '20390418T13:14:15';
  575. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  576. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  577. $this->assertXmlRpcType('dateTime', $val);
  578. $this->assertEquals('dateTime.iso8601', $val->getType());
  579. $this->assertSame($dateString, $val->getValue());
  580. $this->assertEquals(trim($xml), trim($val->saveXml()));
  581. }
  582. /**
  583. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  584. * @group ZF-4249
  585. */
  586. public function testMarshalDateTimeFromZendDateAndAutodetectingType(Zend_XmlRpc_Generator_Abstract $generator)
  587. {
  588. Zend_XmlRpc_Value::setGenerator($generator);
  589. $date = new Zend_Date(array('year' => 2039, 'month' => 4, 'day' => 18,
  590. 'hour' => 13, 'minute' => 14, 'second' => 15));
  591. $dateString = '20390418T13:14:15';
  592. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  593. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::AUTO_DETECT_TYPE);
  594. $this->assertXmlRpcType('dateTime', $val);
  595. $this->assertEquals('dateTime.iso8601', $val->getType());
  596. $this->assertSame($dateString, $val->getValue());
  597. $this->assertEquals(trim($xml), trim($val->saveXml()));
  598. }
  599. /**
  600. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  601. * @group ZF-4249
  602. */
  603. public function testMarshalDateTimeFromFromDateTime(Zend_XmlRpc_Generator_Abstract $generator)
  604. {
  605. Zend_XmlRpc_Value::setGenerator($generator);
  606. $dateString = '20390418T13:14:15';
  607. $date = new DateTime($dateString);
  608. $dateString = '20390418T13:14:15';
  609. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  610. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  611. $this->assertXmlRpcType('dateTime', $val);
  612. $this->assertEquals('dateTime.iso8601', $val->getType());
  613. $this->assertSame($dateString, $val->getValue());
  614. $this->assertEquals(trim($xml), trim($val->saveXml()));
  615. }
  616. /**
  617. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  618. * @group ZF-4249
  619. */
  620. public function testMarshalDateTimeFromDateTimeAndAutodetectingType(Zend_XmlRpc_Generator_Abstract $generator)
  621. {
  622. Zend_XmlRpc_Value::setGenerator($generator);
  623. $dateString = '20390418T13:14:15';
  624. $date = new DateTime($dateString);
  625. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  626. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::AUTO_DETECT_TYPE);
  627. $this->assertXmlRpcType('dateTime', $val);
  628. $this->assertEquals('dateTime.iso8601', $val->getType());
  629. $this->assertSame($dateString, $val->getValue());
  630. $this->assertEquals(trim($xml), trim($val->saveXml()));
  631. }
  632. // Base64
  633. public function testMarshalBase64FromString()
  634. {
  635. $native = 'foo';
  636. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  637. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  638. $this->assertXmlRpcType('base64', $val);
  639. $this->assertSame($native, $val->getValue());
  640. }
  641. /**
  642. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  643. */
  644. public function testMarshalBase64FromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  645. {
  646. Zend_XmlRpc_Value::setGenerator($generator);
  647. $native = 'foo';
  648. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  649. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  650. Zend_XmlRpc_Value::XML_STRING);
  651. $this->assertXmlRpcType('base64', $val);
  652. $this->assertEquals('base64', $val->getType());
  653. $this->assertSame($native, $val->getValue());
  654. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  655. }
  656. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  657. {
  658. $native = 'foo';
  659. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  660. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  661. $this->assertXmlRpcType('base64', $val);
  662. $xml = $val->saveXml();
  663. $encoded = base64_encode($native);
  664. $this->assertContains($encoded, $xml);
  665. }
  666. /**
  667. * @group ZF-3862
  668. */
  669. public function testMarshalSerializedObjectAsBase64()
  670. {
  671. $o = new Zend_XmlRpc_SerializableTestClass();
  672. $o->setProperty('foobar');
  673. $serialized = serialize($o);
  674. $val = Zend_XmlRpc_Value::getXmlRpcValue($serialized,
  675. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  676. $this->assertXmlRpcType('base64', $val);
  677. $o2 = unserialize($val->getValue());
  678. $this->assertSame('foobar', $o2->getProperty());
  679. }
  680. public function testChangingExceptionResetsGeneratorObject()
  681. {
  682. $generator = Zend_XmlRpc_Value::getGenerator();
  683. Zend_XmlRpc_Value::setEncoding('UTF-8');
  684. $this->assertNotSame($generator, Zend_XmlRpc_Value::getGenerator());
  685. $this->assertEquals($generator, Zend_XmlRpc_Value::getGenerator());
  686. $generator = Zend_XmlRpc_Value::getGenerator();
  687. Zend_XmlRpc_Value::setEncoding('ISO-8859-1');
  688. $this->assertNotSame($generator, Zend_XmlRpc_Value::getGenerator());
  689. $this->assertNotEquals($generator, Zend_XmlRpc_Value::getGenerator());
  690. }
  691. // Exceptions
  692. public function testFactoryThrowsWhenInvalidTypeSpecified()
  693. {
  694. try {
  695. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  696. $this->fail();
  697. } catch (Exception $e) {
  698. $this->assertRegexp('/given type is not/i', $e->getMessage());
  699. }
  700. }
  701. public function testPassingXmlRpcObjectReturnsTheSameObject()
  702. {
  703. $xmlRpcValue = new Zend_XmlRpc_Value_String('foo');
  704. $this->assertSame($xmlRpcValue, Zend_XmlRpc_Value::getXmlRpcValue($xmlRpcValue));
  705. }
  706. // Custom Assertions and Helper Methods
  707. public function assertXmlRpcType($type, $object)
  708. {
  709. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  710. $this->assertType($type, $object);
  711. }
  712. public function wrapXml($xml)
  713. {
  714. return $xml . "\n";
  715. }
  716. }
  717. class Zend_XmlRpc_SerializableTestClass
  718. {
  719. protected $_property;
  720. public function setProperty($property)
  721. {
  722. $this->_property = $property;
  723. }
  724. public function getProperty()
  725. {
  726. return $this->_property;
  727. }
  728. }
  729. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  730. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  731. Zend_XmlRpc_ValueTest::main();
  732. }