ValueTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. /**
  42. * Test case for Zend_XmlRpc_Value
  43. *
  44. * @category Zend
  45. * @package Zend_XmlRpc
  46. * @subpackage UnitTests
  47. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  48. * @license http://framework.zend.com/license/new-bsd New BSD License
  49. * @group Zend_XmlRpc
  50. */
  51. class Zend_XmlRpc_ValueTest extends PHPUnit_Framework_TestCase
  52. {
  53. // Boolean
  54. public function testFactoryAutodetectsBoolean()
  55. {
  56. foreach (array(true, false) as $native) {
  57. $val = Zend_XmlRpc_Value::getXmlRpcValue($native);
  58. $this->assertXmlRpcType('boolean', $val);
  59. }
  60. }
  61. public function testMarshalBooleanFromNative()
  62. {
  63. $native = true;
  64. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  65. Zend_XmlRpc_Value::XMLRPC_TYPE_BOOLEAN);
  66. $this->assertXmlRpcType('boolean', $val);
  67. $this->assertSame($native, $val->getValue());
  68. }
  69. /**
  70. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  71. */
  72. public function testMarshalBooleanFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  73. {
  74. Zend_XmlRpc_Value::setGenerator($generator);
  75. $xml = '<value><boolean>1</boolean></value>';
  76. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  77. Zend_XmlRpc_Value::XML_STRING);
  78. $this->assertXmlRpcType('boolean', $val);
  79. $this->assertEquals('boolean', $val->getType());
  80. $this->assertSame(true, $val->getValue());
  81. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  82. }
  83. // Integer
  84. public function testFactoryAutodetectsInteger()
  85. {
  86. $val = Zend_XmlRpc_Value::getXmlRpcValue(1);
  87. $this->assertXmlRpcType('integer', $val);
  88. }
  89. public function testMarshalIntegerFromNative()
  90. {
  91. $native = 1;
  92. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_I4,
  93. Zend_XmlRpc_Value::XMLRPC_TYPE_INTEGER);
  94. foreach ($types as $type) {
  95. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  96. $this->assertXmlRpcType('integer', $val);
  97. $this->assertSame($native, $val->getValue());
  98. }
  99. }
  100. /**
  101. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  102. */
  103. public function testMarshalIntegerFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  104. {
  105. Zend_XmlRpc_Value::setGenerator($generator);
  106. $native = 1;
  107. $xmls = array("<value><int>$native</int></value>",
  108. "<value><i4>$native</i4></value>");
  109. foreach ($xmls as $xml) {
  110. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  111. Zend_XmlRpc_Value::XML_STRING);
  112. $this->assertXmlRpcType('integer', $val);
  113. $this->assertEquals('int', $val->getType());
  114. $this->assertSame($native, $val->getValue());
  115. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  116. }
  117. }
  118. /**
  119. * @group ZF-3310
  120. */
  121. public function testMarshalI4FromOverlongNativeThrowsException()
  122. {
  123. $this->setExpectedException('Zend_XmlRpc_Value_Exception', 'Overlong integer given');
  124. Zend_XmlRpc_Value::getXmlRpcValue(PHP_INT_MAX + 1, Zend_XmlRpc_Value::XMLRPC_TYPE_I4);
  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 + 1, 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_Abstract $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_Abstract $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_Abstract $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_Abstract $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_Abstract $generator)
  252. {
  253. Zend_XmlRpc_Value::setGenerator($generator);
  254. $native = 'foo';
  255. $xml = "<value><string>$native</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_Abstract $generator)
  267. {
  268. Zend_XmlRpc_Value::setGenerator($generator);
  269. $native = 'foo';
  270. $xml = "<string>$native</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_Abstract $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_Abstract $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_Abstract $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_Abstract $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_Abstract $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_Abstract $generator)
  412. {
  413. Zend_XmlRpc_Value::setGenerator($generator);
  414. $native = array('foo' => 0);
  415. $xml = '<value><struct><member><name>foo</name><value><int>0</int>'
  416. . '</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 testMarshallingStructWithMemberWithoutValue(Zend_XmlRpc_Generator_Abstract $generator)
  428. {
  429. Zend_XmlRpc_Value::setGenerator($generator);
  430. $native = array('foo' => 0, 'bar' => 1);
  431. $xml = '<value><struct>'
  432. . '<member><name>foo</name><value><int>0</int></value></member>'
  433. . '<member><name>foo</name><bar/></member>'
  434. . '<member><name>bar</name><value><int>1</int></value></member>'
  435. . '</struct></value>';
  436. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  437. Zend_XmlRpc_Value::XML_STRING);
  438. $this->assertXmlRpcType('struct', $val);
  439. $this->assertEquals('struct', $val->getType());
  440. $this->assertSame($native, $val->getValue());
  441. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  442. }
  443. /**
  444. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  445. */
  446. public function testMarshallingStructWithMemberWithoutName(Zend_XmlRpc_Generator_Abstract $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><value><string>foo</string></value></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. * @group ZF-7639
  464. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  465. */
  466. public function testMarshalStructFromXmlRpcWithEntities(Zend_XmlRpc_Generator_Abstract $generator)
  467. {
  468. Zend_XmlRpc_Value::setGenerator($generator);
  469. $native = array('&nbsp;' => 0);
  470. $xml = '<value><struct><member><name>&amp;nbsp;</name><value><int>0</int>'
  471. . '</value></member></struct></value>';
  472. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  473. $this->assertXmlRpcType('struct', $val);
  474. $this->assertSame($native, $val->getValue());
  475. $this->assertSame($this->wrapXml($xml), $val->saveXml());
  476. }
  477. /**
  478. * @group ZF-3947
  479. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  480. */
  481. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys(Zend_XmlRpc_Generator_Abstract $generator)
  482. {
  483. Zend_XmlRpc_Value::setGenerator($generator);
  484. $native = array('foo' => '');
  485. $xml = '<value><struct><member><name>foo</name>'
  486. . '<value><string/></value></member></struct></value>';
  487. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  488. Zend_XmlRpc_Value::XML_STRING);
  489. $this->assertXmlRpcType('struct', $val);
  490. $this->assertEquals('struct', $val->getType());
  491. $this->assertSame($native, $val->getValue());
  492. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  493. }
  494. /**
  495. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  496. */
  497. public function testMarshallingStructWithMultibyteValueFromXmlRpcRetainsMultibyteValue(Zend_XmlRpc_Generator_Abstract $generator)
  498. {
  499. Zend_XmlRpc_Value::setGenerator($generator);
  500. $native = array('foo' => 'ß');
  501. $xmlDecl = '<?xml version="1.0" encoding="UTF-8"?>';
  502. $xml = '<value><struct><member><name>foo</name><value><string>ß</string></value></member></struct></value>';
  503. $val = Zend_XmlRpc_Value::getXmlRpcValue($xmlDecl . $xml,
  504. Zend_XmlRpc_Value::XML_STRING);
  505. $this->assertXmlRpcType('struct', $val);
  506. $this->assertEquals('struct', $val->getType());
  507. $this->assertSame($native, $val->getValue());
  508. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  509. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  510. $this->assertSame($native, $val->getValue());
  511. $this->assertSame(trim($xml), trim($val->saveXml()));
  512. }
  513. // DateTime
  514. public function testMarshalDateTimeFromNativeString()
  515. {
  516. $native = '1997-07-16T19:20+01:00';
  517. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  518. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  519. $this->assertXmlRpcType('dateTime', $val);
  520. $expected = '1997-07-16T19:20+01:00';
  521. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  522. }
  523. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  524. {
  525. $native = '1997-07-16T19:20+01:00';
  526. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  527. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  528. $this->assertXmlRpcType('dateTime', $val);
  529. $expected = date('c', strtotime($native));
  530. $expected = substr($expected, 0, strlen($expected) - 6);
  531. $expected = str_replace('-', '', $expected);
  532. $received = $val->getValue();
  533. $this->assertEquals($expected, $received);
  534. }
  535. public function testMarshalDateTimeFromInvalidString()
  536. {
  537. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  538. "Cannot convert given value 'foobarbaz' to a timestamp");
  539. Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  540. }
  541. public function testMarshalDateTimeFromNativeInteger()
  542. {
  543. $native = strtotime('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. $this->assertSame($native, strtotime($val->getValue()));
  548. }
  549. /**
  550. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  551. */
  552. public function testMarshalDateTimeFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  553. {
  554. Zend_XmlRpc_Value::setGenerator($generator);
  555. $iso8601 = '1997-07-16T19:20+01:00';
  556. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  557. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  558. Zend_XmlRpc_Value::XML_STRING);
  559. $this->assertXmlRpcType('dateTime', $val);
  560. $this->assertEquals('dateTime.iso8601', $val->getType());
  561. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  562. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  563. }
  564. // Base64
  565. public function testMarshalBase64FromString()
  566. {
  567. $native = 'foo';
  568. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  569. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  570. $this->assertXmlRpcType('base64', $val);
  571. $this->assertSame($native, $val->getValue());
  572. }
  573. /**
  574. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  575. */
  576. public function testMarshalBase64FromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  577. {
  578. Zend_XmlRpc_Value::setGenerator($generator);
  579. $native = 'foo';
  580. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  581. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  582. Zend_XmlRpc_Value::XML_STRING);
  583. $this->assertXmlRpcType('base64', $val);
  584. $this->assertEquals('base64', $val->getType());
  585. $this->assertSame($native, $val->getValue());
  586. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  587. }
  588. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  589. {
  590. $native = 'foo';
  591. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  592. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  593. $this->assertXmlRpcType('base64', $val);
  594. $xml = $val->saveXml();
  595. $encoded = base64_encode($native);
  596. $this->assertContains($encoded, $xml);
  597. }
  598. /**
  599. * @group ZF-3862
  600. */
  601. public function testMarshalSerializedObjectAsBase64()
  602. {
  603. $o = new Zend_XmlRpc_SerializableTestClass();
  604. $o->setProperty('foobar');
  605. $serialized = serialize($o);
  606. $val = Zend_XmlRpc_Value::getXmlRpcValue($serialized,
  607. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  608. $this->assertXmlRpcType('base64', $val);
  609. $o2 = unserialize($val->getValue());
  610. $this->assertSame('foobar', $o2->getProperty());
  611. }
  612. public function testChangingExceptionResetsGeneratorObject()
  613. {
  614. $generator = Zend_XmlRpc_Value::getGenerator();
  615. Zend_XmlRpc_Value::setEncoding('UTF-8');
  616. $this->assertNotSame($generator, Zend_XmlRpc_Value::getGenerator());
  617. $this->assertEquals($generator, Zend_XmlRpc_Value::getGenerator());
  618. $generator = Zend_XmlRpc_Value::getGenerator();
  619. Zend_XmlRpc_Value::setEncoding('ISO-8859-1');
  620. $this->assertNotSame($generator, Zend_XmlRpc_Value::getGenerator());
  621. $this->assertNotEquals($generator, Zend_XmlRpc_Value::getGenerator());
  622. }
  623. // Exceptions
  624. public function testFactoryThrowsWhenInvalidTypeSpecified()
  625. {
  626. try {
  627. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  628. $this->fail();
  629. } catch (Exception $e) {
  630. $this->assertRegexp('/given type is not/i', $e->getMessage());
  631. }
  632. }
  633. public function testPassingXmlRpcObjectReturnsTheSameObject()
  634. {
  635. $xmlRpcValue = new Zend_XmlRpc_Value_String('foo');
  636. $this->assertSame($xmlRpcValue, Zend_XmlRpc_Value::getXmlRpcValue($xmlRpcValue));
  637. }
  638. // Custom Assertions and Helper Methods
  639. public function assertXmlRpcType($type, $object)
  640. {
  641. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  642. $this->assertType($type, $object);
  643. }
  644. public function wrapXml($xml)
  645. {
  646. return $xml . "\n";
  647. }
  648. }
  649. class Zend_XmlRpc_SerializableTestClass
  650. {
  651. protected $_property;
  652. public function setProperty($property)
  653. {
  654. $this->_property = $property;
  655. }
  656. public function getProperty()
  657. {
  658. return $this->_property;
  659. }
  660. }
  661. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  662. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  663. Zend_XmlRpc_ValueTest::main();
  664. }