ValueTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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>foo&lt;&gt;</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<br/>bar';
  271. $xml = "<string>foo&lt;br/&gt;bar</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, 'bar' => 'foo<>bar');
  416. $xml = '<value><struct><member><name>foo</name><value><int>0</int>'
  417. . '</value></member><member><name>bar</name><value><string>'
  418. . 'foo&lt;&gt;bar</string></value></member></struct></value>';
  419. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  420. Zend_XmlRpc_Value::XML_STRING);
  421. $this->assertXmlRpcType('struct', $val);
  422. $this->assertEquals('struct', $val->getType());
  423. $this->assertSame($native, $val->getValue());
  424. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  425. }
  426. /**
  427. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  428. */
  429. public function testMarshallingNestedStructFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  430. {
  431. Zend_XmlRpc_Value::setGenerator($generator);
  432. $native = array('foo' => array('bar' => '<br/>'));
  433. $xml = '<value><struct><member><name>foo</name><value><struct><member>'
  434. . '<name>bar</name><value><string>&lt;br/&gt;</string></value>'
  435. . '</member></struct></value></member></struct></value>';
  436. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  437. $this->assertXmlRpcType('struct', $val);
  438. $this->assertEquals('struct', $val->getType());
  439. $this->assertSame($native, $val->getValue());
  440. $this->assertSame($this->wrapXml($xml), $val->saveXml());
  441. $val = Zend_XmlRpc_Value::getXmlRpcValue($native);
  442. $this->assertSame(trim($xml), trim($val->saveXml()));
  443. }
  444. /**
  445. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  446. */
  447. public function testMarshallingStructWithMemberWithoutValue(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><name>foo</name><bar/></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. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  465. */
  466. public function testMarshallingStructWithMemberWithoutName(Zend_XmlRpc_Generator_Abstract $generator)
  467. {
  468. Zend_XmlRpc_Value::setGenerator($generator);
  469. $native = array('foo' => 0, 'bar' => 1);
  470. $xml = '<value><struct>'
  471. . '<member><name>foo</name><value><int>0</int></value></member>'
  472. . '<member><value><string>foo</string></value></member>'
  473. . '<member><name>bar</name><value><int>1</int></value></member>'
  474. . '</struct></value>';
  475. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  476. Zend_XmlRpc_Value::XML_STRING);
  477. $this->assertXmlRpcType('struct', $val);
  478. $this->assertEquals('struct', $val->getType());
  479. $this->assertSame($native, $val->getValue());
  480. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  481. }
  482. /**
  483. * @group ZF-7639
  484. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  485. */
  486. public function testMarshalStructFromXmlRpcWithEntities(Zend_XmlRpc_Generator_Abstract $generator)
  487. {
  488. Zend_XmlRpc_Value::setGenerator($generator);
  489. $native = array('&nbsp;' => 0);
  490. $xml = '<value><struct><member><name>&amp;nbsp;</name><value><int>0</int>'
  491. . '</value></member></struct></value>';
  492. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  493. $this->assertXmlRpcType('struct', $val);
  494. $this->assertSame($native, $val->getValue());
  495. $this->assertSame($this->wrapXml($xml), $val->saveXml());
  496. }
  497. /**
  498. * @group ZF-3947
  499. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  500. */
  501. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys(Zend_XmlRpc_Generator_Abstract $generator)
  502. {
  503. Zend_XmlRpc_Value::setGenerator($generator);
  504. $native = array('foo' => '');
  505. $xml = '<value><struct><member><name>foo</name>'
  506. . '<value><string/></value></member></struct></value>';
  507. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  508. Zend_XmlRpc_Value::XML_STRING);
  509. $this->assertXmlRpcType('struct', $val);
  510. $this->assertEquals('struct', $val->getType());
  511. $this->assertSame($native, $val->getValue());
  512. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  513. }
  514. /**
  515. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  516. */
  517. public function testMarshallingStructWithMultibyteValueFromXmlRpcRetainsMultibyteValue(Zend_XmlRpc_Generator_Abstract $generator)
  518. {
  519. Zend_XmlRpc_Value::setGenerator($generator);
  520. $native = array('foo' => 'ß');
  521. $xmlDecl = '<?xml version="1.0" encoding="UTF-8"?>';
  522. $xml = '<value><struct><member><name>foo</name><value><string>ß</string></value></member></struct></value>';
  523. $val = Zend_XmlRpc_Value::getXmlRpcValue($xmlDecl . $xml,
  524. Zend_XmlRpc_Value::XML_STRING);
  525. $this->assertXmlRpcType('struct', $val);
  526. $this->assertEquals('struct', $val->getType());
  527. $this->assertSame($native, $val->getValue());
  528. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  529. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  530. $this->assertSame($native, $val->getValue());
  531. $this->assertSame(trim($xml), trim($val->saveXml()));
  532. }
  533. // DateTime
  534. public function testMarshalDateTimeFromNativeString()
  535. {
  536. $native = '1997-07-16T19:20+01:00';
  537. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  538. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  539. $this->assertXmlRpcType('dateTime', $val);
  540. $expected = '1997-07-16T19:20+01:00';
  541. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  542. }
  543. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  544. {
  545. $native = '1997-07-16T19:20+01:00';
  546. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  547. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  548. $this->assertXmlRpcType('dateTime', $val);
  549. $expected = date('c', strtotime($native));
  550. $expected = substr($expected, 0, strlen($expected) - 6);
  551. $expected = str_replace('-', '', $expected);
  552. $received = $val->getValue();
  553. $this->assertEquals($expected, $received);
  554. }
  555. public function testMarshalDateTimeFromInvalidString()
  556. {
  557. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  558. "Cannot convert given value 'foobarbaz' to a timestamp");
  559. Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  560. }
  561. public function testMarshalDateTimeFromNativeInteger()
  562. {
  563. $native = strtotime('1997-07-16T19:20+01:00');
  564. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  565. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  566. $this->assertXmlRpcType('dateTime', $val);
  567. $this->assertSame($native, strtotime($val->getValue()));
  568. }
  569. /**
  570. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  571. */
  572. public function testMarshalDateTimeFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  573. {
  574. Zend_XmlRpc_Value::setGenerator($generator);
  575. $iso8601 = '1997-07-16T19:20+01:00';
  576. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  577. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  578. Zend_XmlRpc_Value::XML_STRING);
  579. $this->assertXmlRpcType('dateTime', $val);
  580. $this->assertEquals('dateTime.iso8601', $val->getType());
  581. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  582. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  583. }
  584. /**
  585. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  586. * @group ZF-4249
  587. */
  588. public function testMarshalDateTimeFromFromZendDate(Zend_XmlRpc_Generator_Abstract $generator)
  589. {
  590. Zend_XmlRpc_Value::setGenerator($generator);
  591. $date = new Zend_Date(array('year' => 2039, 'month' => 4, 'day' => 18,
  592. 'hour' => 13, 'minute' => 14, 'second' => 15));
  593. $dateString = '20390418T13:14:15';
  594. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  595. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  596. $this->assertXmlRpcType('dateTime', $val);
  597. $this->assertEquals('dateTime.iso8601', $val->getType());
  598. $this->assertSame($dateString, $val->getValue());
  599. $this->assertEquals(trim($xml), trim($val->saveXml()));
  600. }
  601. /**
  602. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  603. * @group ZF-4249
  604. */
  605. public function testMarshalDateTimeFromZendDateAndAutodetectingType(Zend_XmlRpc_Generator_Abstract $generator)
  606. {
  607. Zend_XmlRpc_Value::setGenerator($generator);
  608. $date = new Zend_Date(array('year' => 2039, 'month' => 4, 'day' => 18,
  609. 'hour' => 13, 'minute' => 14, 'second' => 15));
  610. $dateString = '20390418T13:14:15';
  611. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  612. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::AUTO_DETECT_TYPE);
  613. $this->assertXmlRpcType('dateTime', $val);
  614. $this->assertEquals('dateTime.iso8601', $val->getType());
  615. $this->assertSame($dateString, $val->getValue());
  616. $this->assertEquals(trim($xml), trim($val->saveXml()));
  617. }
  618. /**
  619. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  620. * @group ZF-4249
  621. */
  622. public function testMarshalDateTimeFromFromDateTime(Zend_XmlRpc_Generator_Abstract $generator)
  623. {
  624. Zend_XmlRpc_Value::setGenerator($generator);
  625. $dateString = '20390418T13:14:15';
  626. $date = new DateTime($dateString);
  627. $dateString = '20390418T13:14:15';
  628. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  629. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  630. $this->assertXmlRpcType('dateTime', $val);
  631. $this->assertEquals('dateTime.iso8601', $val->getType());
  632. $this->assertSame($dateString, $val->getValue());
  633. $this->assertEquals(trim($xml), trim($val->saveXml()));
  634. }
  635. /**
  636. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  637. * @group ZF-4249
  638. */
  639. public function testMarshalDateTimeFromDateTimeAndAutodetectingType(Zend_XmlRpc_Generator_Abstract $generator)
  640. {
  641. Zend_XmlRpc_Value::setGenerator($generator);
  642. $dateString = '20390418T13:14:15';
  643. $date = new DateTime($dateString);
  644. $xml = "<value><dateTime.iso8601>$dateString</dateTime.iso8601></value>";
  645. $val = Zend_XmlRpc_Value::getXmlRpcValue($date, Zend_XmlRpc_Value::AUTO_DETECT_TYPE);
  646. $this->assertXmlRpcType('dateTime', $val);
  647. $this->assertEquals('dateTime.iso8601', $val->getType());
  648. $this->assertSame($dateString, $val->getValue());
  649. $this->assertEquals(trim($xml), trim($val->saveXml()));
  650. }
  651. // Base64
  652. public function testMarshalBase64FromString()
  653. {
  654. $native = 'foo';
  655. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  656. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  657. $this->assertXmlRpcType('base64', $val);
  658. $this->assertSame($native, $val->getValue());
  659. }
  660. /**
  661. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  662. */
  663. public function testMarshalBase64FromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  664. {
  665. Zend_XmlRpc_Value::setGenerator($generator);
  666. $native = 'foo';
  667. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  668. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  669. Zend_XmlRpc_Value::XML_STRING);
  670. $this->assertXmlRpcType('base64', $val);
  671. $this->assertEquals('base64', $val->getType());
  672. $this->assertSame($native, $val->getValue());
  673. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  674. }
  675. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  676. {
  677. $native = 'foo';
  678. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  679. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  680. $this->assertXmlRpcType('base64', $val);
  681. $xml = $val->saveXml();
  682. $encoded = base64_encode($native);
  683. $this->assertContains($encoded, $xml);
  684. }
  685. /**
  686. * @group ZF-3862
  687. */
  688. public function testMarshalSerializedObjectAsBase64()
  689. {
  690. $o = new Zend_XmlRpc_SerializableTestClass();
  691. $o->setProperty('foobar');
  692. $serialized = serialize($o);
  693. $val = Zend_XmlRpc_Value::getXmlRpcValue($serialized,
  694. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  695. $this->assertXmlRpcType('base64', $val);
  696. $o2 = unserialize($val->getValue());
  697. $this->assertSame('foobar', $o2->getProperty());
  698. }
  699. public function testChangingExceptionResetsGeneratorObject()
  700. {
  701. $generator = Zend_XmlRpc_Value::getGenerator();
  702. Zend_XmlRpc_Value::setEncoding('UTF-8');
  703. $this->assertNotSame($generator, Zend_XmlRpc_Value::getGenerator());
  704. $this->assertEquals($generator, Zend_XmlRpc_Value::getGenerator());
  705. $generator = Zend_XmlRpc_Value::getGenerator();
  706. Zend_XmlRpc_Value::setEncoding('ISO-8859-1');
  707. $this->assertNotSame($generator, Zend_XmlRpc_Value::getGenerator());
  708. $this->assertNotEquals($generator, Zend_XmlRpc_Value::getGenerator());
  709. }
  710. // Exceptions
  711. public function testFactoryThrowsWhenInvalidTypeSpecified()
  712. {
  713. try {
  714. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  715. $this->fail();
  716. } catch (Exception $e) {
  717. $this->assertRegexp('/given type is not/i', $e->getMessage());
  718. }
  719. }
  720. public function testPassingXmlRpcObjectReturnsTheSameObject()
  721. {
  722. $xmlRpcValue = new Zend_XmlRpc_Value_String('foo');
  723. $this->assertSame($xmlRpcValue, Zend_XmlRpc_Value::getXmlRpcValue($xmlRpcValue));
  724. }
  725. // Custom Assertions and Helper Methods
  726. public function assertXmlRpcType($type, $object)
  727. {
  728. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  729. $this->assertType($type, $object);
  730. }
  731. public function wrapXml($xml)
  732. {
  733. return $xml . "\n";
  734. }
  735. }
  736. class Zend_XmlRpc_SerializableTestClass
  737. {
  738. protected $_property;
  739. public function setProperty($property)
  740. {
  741. $this->_property = $property;
  742. }
  743. public function getProperty()
  744. {
  745. return $this->_property;
  746. }
  747. }
  748. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  749. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  750. Zend_XmlRpc_ValueTest::main();
  751. }