ValueTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. */
  218. public function testMarshallingDoubleWithHigherPrecisionFromNative()
  219. {
  220. if (ini_get('precision') < 7) {
  221. $this->markTestSkipped('precision is too low');
  222. }
  223. $native = 0.1234567;
  224. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_DOUBLE);
  225. $this->assertXmlRpcType('double', $value);
  226. $this->assertSame($native, $value->getValue());
  227. }
  228. // String
  229. public function testFactoryAutodetectsString()
  230. {
  231. $val = Zend_XmlRpc_Value::getXmlRpcValue('');
  232. $this->assertXmlRpcType('string', $val);
  233. }
  234. public function testMarshalStringFromNative()
  235. {
  236. $native = 'foo';
  237. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  238. Zend_XmlRpc_Value::XMLRPC_TYPE_STRING);
  239. $this->assertXmlRpcType('string', $val);
  240. $this->assertSame($native, $val->getValue());
  241. }
  242. /**
  243. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  244. */
  245. public function testMarshalStringFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  246. {
  247. Zend_XmlRpc_Value::setGenerator($generator);
  248. $native = 'foo';
  249. $xml = "<value><string>$native</string></value>";
  250. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  251. Zend_XmlRpc_Value::XML_STRING);
  252. $this->assertXmlRpcType('string', $val);
  253. $this->assertEquals('string', $val->getType());
  254. $this->assertSame($native, $val->getValue());
  255. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  256. }
  257. /**
  258. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  259. */
  260. public function testMarshalStringFromDefault(Zend_XmlRpc_Generator_Abstract $generator)
  261. {
  262. Zend_XmlRpc_Value::setGenerator($generator);
  263. $native = 'foo';
  264. $xml = "<string>$native</string>";
  265. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  266. Zend_XmlRpc_Value::XML_STRING);
  267. $this->assertXmlRpcType('string', $val);
  268. $this->assertEquals('string', $val->getType());
  269. $this->assertSame($native, $val->getValue());
  270. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  271. }
  272. //Nil
  273. public function testFactoryAutodetectsNil()
  274. {
  275. $val = Zend_XmlRpc_Value::getXmlRpcValue(NULL);
  276. $this->assertXmlRpcType('nil', $val);
  277. }
  278. public function testMarshalNilFromNative()
  279. {
  280. $native = NULL;
  281. $types = array(Zend_XmlRpc_Value::XMLRPC_TYPE_NIL,
  282. Zend_XmlRpc_Value::XMLRPC_TYPE_APACHENIL);
  283. foreach ($types as $type) {
  284. $value = Zend_XmlRpc_Value::getXmlRpcValue($native, $type);
  285. $this->assertXmlRpcType('nil', $value);
  286. $this->assertSame($native, $value->getValue());
  287. }
  288. }
  289. /**
  290. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  291. */
  292. public function testMarshalNilFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  293. {
  294. Zend_XmlRpc_Value::setGenerator($generator);
  295. $xmls = array('<value><nil/></value>',
  296. '<value><ex:nil xmlns:ex="http://ws.apache.org/xmlrpc/namespaces/extensions"/></value>');
  297. foreach ($xmls as $xml) {
  298. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  299. Zend_XmlRpc_Value::XML_STRING);
  300. $this->assertXmlRpcType('nil', $val);
  301. $this->assertEquals('nil', $val->getType());
  302. $this->assertSame(NULL, $val->getValue());
  303. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  304. }
  305. }
  306. // Array
  307. public function testFactoryAutodetectsArray()
  308. {
  309. $val = Zend_XmlRpc_Value::getXmlRpcValue(array(0, 'foo'));
  310. $this->assertXmlRpcType('array', $val);
  311. }
  312. public function testMarshalArrayFromNative()
  313. {
  314. $native = array(0,1);
  315. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  316. Zend_XmlRpc_Value::XMLRPC_TYPE_ARRAY);
  317. $this->assertXmlRpcType('array', $val);
  318. $this->assertSame($native, $val->getValue());
  319. }
  320. /**
  321. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  322. */
  323. public function testMarshalArrayFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  324. {
  325. Zend_XmlRpc_Value::setGenerator($generator);
  326. $native = array(0,1);
  327. $xml = '<value><array><data><value><int>0</int></value>'
  328. . '<value><int>1</int></value></data></array></value>';
  329. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  330. Zend_XmlRpc_Value::XML_STRING);
  331. $this->assertXmlRpcType('array', $val);
  332. $this->assertEquals('array', $val->getType());
  333. $this->assertSame($native, $val->getValue());
  334. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  335. }
  336. /**
  337. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  338. */
  339. public function testEmptyXmlRpcArrayResultsInEmptyArray(Zend_XmlRpc_Generator_Abstract $generator)
  340. {
  341. Zend_XmlRpc_Value::setGenerator($generator);
  342. $native = array();
  343. $xml = '<value><array><data/></array></value>';
  344. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  345. Zend_XmlRpc_Value::XML_STRING);
  346. $this->assertXmlRpcType('array', $val);
  347. $this->assertEquals('array', $val->getType());
  348. $this->assertSame($native, $val->getValue());
  349. $value = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  350. $this->assertXmlRpcType('array', $value);
  351. $this->assertEquals('array', $value->getType());
  352. $this->assertSame($native, $value->getValue());
  353. }
  354. /**
  355. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  356. */
  357. public function testArrayMustContainDataElement(Zend_XmlRpc_Generator_Abstract $generator)
  358. {
  359. Zend_XmlRpc_Value::setGenerator($generator);
  360. $native = array();
  361. $xml = '<value><array/></value>';
  362. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  363. 'Invalid XML for XML-RPC native array type: ARRAY tag must contain DATA tag');
  364. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  365. Zend_XmlRpc_Value::XML_STRING);
  366. }
  367. /**
  368. * @group ZF-5405
  369. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  370. */
  371. public function testMarshalNilInStructWrappedInArray(Zend_XmlRpc_Generator_Abstract $generator)
  372. {
  373. Zend_XmlRpc_Value::setGenerator($generator);
  374. $expected = array(array('id' => '1', 'name' => 'vertebra, caudal', 'description' => null));
  375. $xml = '<value>'
  376. . '<array><data><value><struct><member><name>id</name><value><string>1</string></value></member>'
  377. . '<member><name>name</name><value><string>vertebra, caudal</string></value></member>'
  378. . '<member><name>description</name><value><nil/></value></member></struct></value></data></array>'
  379. . '</value>';
  380. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  381. $this->assertSame($expected, $val->getValue());
  382. }
  383. // Struct
  384. public function testFactoryAutodetectsStruct()
  385. {
  386. $val = Zend_XmlRpc_Value::getXmlRpcValue(array('foo' => 0));
  387. $this->assertXmlRpcType('struct', $val);
  388. }
  389. public function testFactoryAutodetectsStructFromObject()
  390. {
  391. $val = Zend_XmlRpc_Value::getXmlRpcValue((object)array('foo' => 0));
  392. $this->assertXmlRpcType('struct', $val);
  393. }
  394. public function testMarshalStructFromNative()
  395. {
  396. $native = array('foo' => 0);
  397. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  398. Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  399. $this->assertXmlRpcType('struct', $val);
  400. $this->assertSame($native, $val->getValue());
  401. }
  402. /**
  403. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  404. */
  405. public function testMarshalStructFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  406. {
  407. Zend_XmlRpc_Value::setGenerator($generator);
  408. $native = array('foo' => 0);
  409. $xml = '<value><struct><member><name>foo</name><value><int>0</int>'
  410. . '</value></member></struct></value>';
  411. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  412. Zend_XmlRpc_Value::XML_STRING);
  413. $this->assertXmlRpcType('struct', $val);
  414. $this->assertEquals('struct', $val->getType());
  415. $this->assertSame($native, $val->getValue());
  416. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  417. }
  418. /**
  419. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  420. */
  421. public function testMarshallingStructWithMemberWithoutValue(Zend_XmlRpc_Generator_Abstract $generator)
  422. {
  423. Zend_XmlRpc_Value::setGenerator($generator);
  424. $native = array('foo' => 0, 'bar' => 1);
  425. $xml = '<value><struct>'
  426. . '<member><name>foo</name><value><int>0</int></value></member>'
  427. . '<member><name>foo</name><bar/></member>'
  428. . '<member><name>bar</name><value><int>1</int></value></member>'
  429. . '</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 testMarshallingStructWithMemberWithoutName(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><value><string>foo</string></value></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. * @group ZF-7639
  458. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  459. */
  460. public function testMarshalStructFromXmlRpcWithEntities(Zend_XmlRpc_Generator_Abstract $generator)
  461. {
  462. Zend_XmlRpc_Value::setGenerator($generator);
  463. $native = array('&nbsp;' => 0);
  464. $xml = '<value><struct><member><name>&amp;nbsp;</name><value><int>0</int>'
  465. . '</value></member></struct></value>';
  466. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml, Zend_XmlRpc_Value::XML_STRING);
  467. $this->assertXmlRpcType('struct', $val);
  468. $this->assertSame($native, $val->getValue());
  469. $this->assertSame($this->wrapXml($xml), $val->saveXml());
  470. }
  471. /**
  472. * @group ZF-3947
  473. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  474. */
  475. public function testMarshallingStructsWithEmptyValueFromXmlRpcShouldRetainKeys(Zend_XmlRpc_Generator_Abstract $generator)
  476. {
  477. Zend_XmlRpc_Value::setGenerator($generator);
  478. $native = array('foo' => '');
  479. $xml = '<value><struct><member><name>foo</name>'
  480. . '<value><string/></value></member></struct></value>';
  481. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  482. Zend_XmlRpc_Value::XML_STRING);
  483. $this->assertXmlRpcType('struct', $val);
  484. $this->assertEquals('struct', $val->getType());
  485. $this->assertSame($native, $val->getValue());
  486. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  487. }
  488. /**
  489. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  490. */
  491. public function testMarshallingStructWithMultibyteValueFromXmlRpcRetainsMultibyteValue(Zend_XmlRpc_Generator_Abstract $generator)
  492. {
  493. Zend_XmlRpc_Value::setGenerator($generator);
  494. $native = array('foo' => 'ß');
  495. $xmlDecl = '<?xml version="1.0" encoding="UTF-8"?>';
  496. $xml = '<value><struct><member><name>foo</name><value><string>ß</string></value></member></struct></value>';
  497. $val = Zend_XmlRpc_Value::getXmlRpcValue($xmlDecl . $xml,
  498. Zend_XmlRpc_Value::XML_STRING);
  499. $this->assertXmlRpcType('struct', $val);
  500. $this->assertEquals('struct', $val->getType());
  501. $this->assertSame($native, $val->getValue());
  502. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  503. $val = Zend_XmlRpc_Value::getXmlRpcValue($native, Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT);
  504. $this->assertSame($native, $val->getValue());
  505. $this->assertSame(trim($xml), trim($val->saveXml()));
  506. }
  507. // DateTime
  508. public function testMarshalDateTimeFromNativeString()
  509. {
  510. $native = '1997-07-16T19:20+01:00';
  511. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  512. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  513. $this->assertXmlRpcType('dateTime', $val);
  514. $expected = '1997-07-16T19:20+01:00';
  515. $this->assertSame(strtotime($native), strtotime($val->getValue()));
  516. }
  517. public function testMarshalDateTimeFromNativeStringProducesIsoOutput()
  518. {
  519. $native = '1997-07-16T19:20+01:00';
  520. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  521. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  522. $this->assertXmlRpcType('dateTime', $val);
  523. $expected = date('c', strtotime($native));
  524. $expected = substr($expected, 0, strlen($expected) - 6);
  525. $expected = str_replace('-', '', $expected);
  526. $received = $val->getValue();
  527. $this->assertEquals($expected, $received);
  528. }
  529. public function testMarshalDateTimeFromInvalidString()
  530. {
  531. $this->setExpectedException('Zend_XmlRpc_Value_Exception',
  532. "Cannot convert given value 'foobarbaz' to a timestamp");
  533. Zend_XmlRpc_Value::getXmlRpcValue('foobarbaz', Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  534. }
  535. public function testMarshalDateTimeFromNativeInteger()
  536. {
  537. $native = strtotime('1997-07-16T19:20+01:00');
  538. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  539. Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME);
  540. $this->assertXmlRpcType('dateTime', $val);
  541. $this->assertSame($native, strtotime($val->getValue()));
  542. }
  543. /**
  544. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  545. */
  546. public function testMarshalDateTimeFromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  547. {
  548. Zend_XmlRpc_Value::setGenerator($generator);
  549. $iso8601 = '1997-07-16T19:20+01:00';
  550. $xml = "<value><dateTime.iso8601>$iso8601</dateTime.iso8601></value>";
  551. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  552. Zend_XmlRpc_Value::XML_STRING);
  553. $this->assertXmlRpcType('dateTime', $val);
  554. $this->assertEquals('dateTime.iso8601', $val->getType());
  555. $this->assertSame(strtotime($iso8601), strtotime($val->getValue()));
  556. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  557. }
  558. // Base64
  559. public function testMarshalBase64FromString()
  560. {
  561. $native = 'foo';
  562. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  563. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  564. $this->assertXmlRpcType('base64', $val);
  565. $this->assertSame($native, $val->getValue());
  566. }
  567. /**
  568. * @dataProvider Zend_XmlRpc_TestProvider::provideGenerators
  569. */
  570. public function testMarshalBase64FromXmlRpc(Zend_XmlRpc_Generator_Abstract $generator)
  571. {
  572. Zend_XmlRpc_Value::setGenerator($generator);
  573. $native = 'foo';
  574. $xml = '<value><base64>' .base64_encode($native). '</base64></value>';
  575. $val = Zend_XmlRpc_Value::getXmlRpcValue($xml,
  576. Zend_XmlRpc_Value::XML_STRING);
  577. $this->assertXmlRpcType('base64', $val);
  578. $this->assertEquals('base64', $val->getType());
  579. $this->assertSame($native, $val->getValue());
  580. $this->assertEquals($this->wrapXml($xml), $val->saveXml());
  581. }
  582. public function testXmlRpcValueBase64GeneratedXmlContainsBase64EncodedText()
  583. {
  584. $native = 'foo';
  585. $val = Zend_XmlRpc_Value::getXmlRpcValue($native,
  586. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  587. $this->assertXmlRpcType('base64', $val);
  588. $xml = $val->saveXml();
  589. $encoded = base64_encode($native);
  590. $this->assertContains($encoded, $xml);
  591. }
  592. /**
  593. * @group ZF-3862
  594. */
  595. public function testMarshalSerializedObjectAsBase64()
  596. {
  597. $o = new Zend_XmlRpc_SerializableTestClass();
  598. $o->setProperty('foobar');
  599. $serialized = serialize($o);
  600. $val = Zend_XmlRpc_Value::getXmlRpcValue($serialized,
  601. Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64);
  602. $this->assertXmlRpcType('base64', $val);
  603. $o2 = unserialize($val->getValue());
  604. $this->assertSame('foobar', $o2->getProperty());
  605. }
  606. // Exceptions
  607. public function testFactoryThrowsWhenInvalidTypeSpecified()
  608. {
  609. try {
  610. Zend_XmlRpc_Value::getXmlRpcValue('', 'bad type here');
  611. $this->fail();
  612. } catch (Exception $e) {
  613. $this->assertRegexp('/given type is not/i', $e->getMessage());
  614. }
  615. }
  616. // Custom Assertions and Helper Methods
  617. public function assertXmlRpcType($type, $object)
  618. {
  619. $type = 'Zend_XmlRpc_Value_' . ucfirst($type);
  620. $this->assertType($type, $object);
  621. }
  622. public function wrapXml($xml)
  623. {
  624. return $xml . "\n";
  625. }
  626. }
  627. class Zend_XmlRpc_SerializableTestClass
  628. {
  629. protected $_property;
  630. public function setProperty($property)
  631. {
  632. $this->_property = $property;
  633. }
  634. public function getProperty()
  635. {
  636. return $this->_property;
  637. }
  638. }
  639. // Call Zend_XmlRpc_ValueTest::main() if this source file is executed directly.
  640. if (PHPUnit_MAIN_METHOD == "Zend_XmlRpc_ValueTest::main") {
  641. Zend_XmlRpc_ValueTest::main();
  642. }