PythonPickleSerializeProtocol0Test.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-2010 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_PythonPickle
  24. */
  25. require_once 'Zend/Serializer/Adapter/PythonPickle.php';
  26. /**
  27. * PHPUnit test case
  28. */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Serializer
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Serializer_Adapter_PythonPickleSerializeProtocol0Test extends PHPUnit_Framework_TestCase
  38. {
  39. private $_adapter;
  40. public function setUp()
  41. {
  42. $this->_adapter = new Zend_Serializer_Adapter_PythonPickle(array('protocol' => 0));
  43. }
  44. public function tearDown()
  45. {
  46. $this->_adapter = null;
  47. }
  48. public function testSerializeNull()
  49. {
  50. $value = null;
  51. $expected = 'N.';
  52. $data = $this->_adapter->serialize($value);
  53. $this->assertEquals($expected, $data);
  54. }
  55. public function testSerializeTrue()
  56. {
  57. $value = true;
  58. $expected = "I01\r\n.";
  59. $data = $this->_adapter->serialize($value);
  60. $this->assertEquals($expected, $data);
  61. }
  62. public function testSerializeFalse()
  63. {
  64. $value = false;
  65. $expected = "I00\r\n.";
  66. $data = $this->_adapter->serialize($value);
  67. $this->assertEquals($expected, $data);
  68. }
  69. public function testSerializeInt()
  70. {
  71. $value = -12345;
  72. $expected = "I-12345\r\n.";
  73. $data = $this->_adapter->serialize($value);
  74. $this->assertEquals($expected, $data);
  75. }
  76. public function testSerializeFloat()
  77. {
  78. $value = -12345.6789;
  79. $expected = "F-12345.6789\r\n.";
  80. $data = $this->_adapter->serialize($value);
  81. $this->assertEquals($expected, $data);
  82. }
  83. public function testSerializeString()
  84. {
  85. $value = 'test';
  86. $expected = "S'test'\r\np0\r\n.";
  87. $data = $this->_adapter->serialize($value);
  88. $this->assertEquals($expected, $data);
  89. }
  90. public function testSerializeStringWithSpecialChars()
  91. {
  92. $value = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  93. . "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  94. . "\xff\\\"'";
  95. $expected = "S'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f"
  96. . "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f"
  97. . "\\xff\\\\\"\\''\r\n"
  98. . "p0\r\n.";
  99. $data = $this->_adapter->serialize($value);
  100. $this->assertEquals($expected, $data);
  101. }
  102. public function testSerializeArrayList()
  103. {
  104. $value = array('1', '2', 'test');
  105. $expected = "(lp0\r\n"
  106. . "S'1'\r\n"
  107. . "p1\r\n"
  108. . "aS'2'\r\n"
  109. . "p2\r\n"
  110. . "aS'test'\r\n"
  111. . "p3\r\n"
  112. . "a.";
  113. $data = $this->_adapter->serialize($value);
  114. $this->assertEquals($expected, $data);
  115. }
  116. public function testSerializeArrayDict()
  117. {
  118. $value = array('1', '2', 'three' => 'test');
  119. $expected = "(dp0\r\n"
  120. . "I0\r\n"
  121. . "S'1'\r\n"
  122. . "p1\r\n"
  123. . "sI1\r\n"
  124. . "S'2'\r\n"
  125. . "p2\r\n"
  126. . "sS'three'\r\n"
  127. . "p3\r\n"
  128. . "S'test'\r\n"
  129. . "p4\r\n"
  130. . "s.";
  131. $data = $this->_adapter->serialize($value);
  132. $this->assertEquals($expected, $data);
  133. }
  134. public function testSerializeObject()
  135. {
  136. $value = new StdClass();
  137. $value->test = 'test';
  138. $value->test2 = 2;
  139. $expected = "(dp0\r\n"
  140. . "S'test'\r\n"
  141. . "p1\r\n"
  142. . "g1\r\n"
  143. . "sS'test2'\r\n"
  144. . "p2\r\n"
  145. . "I2\r\n"
  146. . "s.";
  147. $data = $this->_adapter->serialize($value);
  148. $this->assertEquals($expected, $data);
  149. }
  150. }