ValueTest.php 33 KB

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