ValueTest.php 33 KB

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