ValueTest.php 28 KB

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