WddxTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_Serializer
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 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. /**
  23. * @see Zend_Serializer_Adapter_Wddx
  24. */
  25. require_once 'Zend/Serializer/Adapter/Wddx.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Serializer
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Serializer_Adapter_WddxTest extends PHPUnit_Framework_TestCase
  34. {
  35. private $_adapter;
  36. public function setUp()
  37. {
  38. $this->_adapter = new Zend_Serializer_Adapter_Wddx();
  39. }
  40. public function tearDown()
  41. {
  42. $this->_adapter = null;
  43. }
  44. public function testSerializeString()
  45. {
  46. $value = 'test';
  47. $expected = '<wddxPacket version=\'1.0\'><header/>'
  48. . '<data><string>test</string></data></wddxPacket>';
  49. $data = $this->_adapter->serialize($value);
  50. $this->assertEquals($expected, $data);
  51. }
  52. public function testSerializeStringWithComment()
  53. {
  54. $value = 'test';
  55. $expected = '<wddxPacket version=\'1.0\'><header><comment>a test comment</comment></header>'
  56. . '<data><string>test</string></data></wddxPacket>';
  57. $data = $this->_adapter->serialize($value, array('comment' => 'a test comment'));
  58. $this->assertEquals($expected, $data);
  59. }
  60. public function testSerializeFalse()
  61. {
  62. $value = false;
  63. $expected = '<wddxPacket version=\'1.0\'><header/>'
  64. . '<data><boolean value=\'false\'/></data></wddxPacket>';
  65. $data = $this->_adapter->serialize($value);
  66. $this->assertEquals($expected, $data);
  67. }
  68. public function testSerializeTrue()
  69. {
  70. $value = true;
  71. $expected = '<wddxPacket version=\'1.0\'><header/>'
  72. . '<data><boolean value=\'true\'/></data></wddxPacket>';
  73. $data = $this->_adapter->serialize($value);
  74. $this->assertEquals($expected, $data);
  75. }
  76. public function testSerializeNull()
  77. {
  78. $value = null;
  79. $expected = '<wddxPacket version=\'1.0\'><header/>'
  80. . '<data><null/></data></wddxPacket>';
  81. $data = $this->_adapter->serialize($value);
  82. $this->assertEquals($expected, $data);
  83. }
  84. public function testSerializeNumeric()
  85. {
  86. $value = 100;
  87. $expected = '<wddxPacket version=\'1.0\'><header/>'
  88. . '<data><number>100</number></data></wddxPacket>';
  89. $data = $this->_adapter->serialize($value);
  90. $this->assertEquals($expected, $data);
  91. }
  92. public function testSerializeObject()
  93. {
  94. $value = new stdClass();
  95. $value->test = "test";
  96. $expected = '<wddxPacket version=\'1.0\'><header/>'
  97. . '<data><struct>'
  98. . '<var name=\'php_class_name\'><string>stdClass</string></var>'
  99. . '<var name=\'test\'><string>test</string></var>'
  100. . '</struct></data></wddxPacket>';
  101. $data = $this->_adapter->serialize($value);
  102. $this->assertEquals($expected, $data);
  103. }
  104. public function testUnserializeString()
  105. {
  106. $value = '<wddxPacket version=\'1.0\'><header/>'
  107. . '<data><string>test</string></data></wddxPacket>';
  108. $expected = 'test';
  109. $data = $this->_adapter->unserialize($value);
  110. $this->assertEquals($expected, $data);
  111. }
  112. public function testUnserializeFalse()
  113. {
  114. $value = '<wddxPacket version=\'1.0\'><header/>'
  115. . '<data><boolean value=\'false\'/></data></wddxPacket>';
  116. $expected = false;
  117. $data = $this->_adapter->unserialize($value);
  118. $this->assertEquals($expected, $data);
  119. }
  120. public function testUnserializeTrue()
  121. {
  122. $value = '<wddxPacket version=\'1.0\'><header/>'
  123. . '<data><boolean value=\'true\'/></data></wddxPacket>';
  124. $expected = true;
  125. $data = $this->_adapter->unserialize($value);
  126. $this->assertEquals($expected, $data);
  127. }
  128. public function testUnserializeNull1()
  129. {
  130. $value = '<wddxPacket version=\'1.0\'><header/>'
  131. . '<data><null/></data></wddxPacket>';
  132. $expected = null;
  133. $data = $this->_adapter->unserialize($value);
  134. $this->assertEquals($expected, $data);
  135. }
  136. /**
  137. * test to unserialize a valid null value by an valid wddx
  138. * but with some differenzes to the null cenerated by php
  139. * -> the invalid check have to success for all valid wddx null
  140. */
  141. public function testUnserializeNull2()
  142. {
  143. $value = '<wddxPacket version=\'1.0\'><header/>' . "\n"
  144. . '<data><null/></data></wddxPacket>';
  145. $expected = null;
  146. $data = $this->_adapter->unserialize($value);
  147. $this->assertEquals($expected, $data);
  148. }
  149. public function testUnserializeNumeric()
  150. {
  151. $value = '<wddxPacket version=\'1.0\'><header/>'
  152. . '<data><number>100</number></data></wddxPacket>';
  153. $expected = 100;
  154. $data = $this->_adapter->unserialize($value);
  155. $this->assertEquals($expected, $data);
  156. }
  157. public function testUnserializeObject()
  158. {
  159. $value = '<wddxPacket version=\'1.0\'><header/>'
  160. . '<data><struct>'
  161. . '<var name=\'php_class_name\'><string>stdClass</string></var>'
  162. . '<var name=\'test\'><string>test</string></var>'
  163. . '</struct></data></wddxPacket>';
  164. $expected = new stdClass();
  165. $expected->test = 'test';
  166. $data = $this->_adapter->unserialize($value);
  167. $this->assertEquals($expected, $data);
  168. }
  169. public function testUnserialzeInvalid()
  170. {
  171. $value = 'not a serialized string';
  172. $this->setExpectedException('Zend_Serializer_Exception');
  173. $this->_adapter->unserialize($value);
  174. }
  175. /**
  176. * ZF-8911 and PHP-Bug #46496
  177. * This bug effects php < 5.2.7
  178. *
  179. * No workaround implemented !!! - This test failes on php < 5.2.7
  180. */
  181. public function testSerializeStringUtf8() {
  182. $value = "\xc2\xbf"; // &Xi;
  183. $expected = '<wddxPacket version=\'1.0\'><header/>'
  184. . "<data><string>\xc2\xbf</string></data></wddxPacket>";
  185. $data = $this->_adapter->serialize($value);
  186. $this->assertEquals($expected, $data);
  187. }
  188. public function testUnserializeInvalidXml()
  189. {
  190. if (!class_exists('SimpleXMLElement', false)) {
  191. $this->markTestSkipped('Skipped by missing ext/simplexml');
  192. }
  193. $value = 'not a serialized string';
  194. $this->setExpectedException(
  195. 'Zend_Serializer_Exception',
  196. 'DOMDocument::loadXML(): Start tag expected'
  197. );
  198. $this->_adapter->unserialize($value);
  199. }
  200. public function testShouldThrowExceptionIfXmlToUnserializeFromContainsADoctype()
  201. {
  202. $value = '<!DOCTYPE>'
  203. . '<wddxPacket version=\'1.0\'><header/>'
  204. . '<data><string>test</string></data></wddxPacket>';
  205. $this->setExpectedException("Zend_Serializer_Exception");
  206. $data = $this->_adapter->unserialize($value);
  207. }
  208. }
  209. /**
  210. * @category Zend
  211. * @package Zend_Serializer
  212. * @subpackage UnitTests
  213. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  214. * @license http://framework.zend.com/license/new-bsd New BSD License
  215. */
  216. class Zend_Serializer_Adapter_WddxSkipTest extends PHPUnit_Framework_TestCase
  217. {
  218. public $message = null;
  219. public function setUp()
  220. {
  221. $message = 'Skipped Zend_Serializer_Adapter_WddxTest';
  222. if ($this->message) {
  223. $message.= ': ' . $this->message;
  224. }
  225. $this->markTestSkipped($message);
  226. }
  227. public function testEmpty()
  228. {
  229. // this is here only so we have at least one test
  230. }
  231. }