PythonPickleSerializeProtocol0Test.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * @package Zend_Cache
  4. * @subpackage UnitTests
  5. */
  6. /** @see Zend_Serializer_Adapter_PythonPickle */
  7. require_once 'Zend/Serializer/Adapter/PythonPickle.php';
  8. /**
  9. * PHPUnit test case
  10. */
  11. require_once 'PHPUnit/Framework/TestCase.php';
  12. /**
  13. * @package Zend_Serializer
  14. * @subpackage UnitTests
  15. */
  16. class Zend_Serializer_Adapter_PythonPickleSerializeProtocol0Test extends PHPUnit_Framework_TestCase
  17. {
  18. private $_adapter;
  19. public function setUp()
  20. {
  21. $this->_adapter = new Zend_Serializer_Adapter_PythonPickle(array('protocol' => 0));
  22. }
  23. public function tearDown()
  24. {
  25. $this->_adapter = null;
  26. }
  27. public function testSerializeNull() {
  28. $value = null;
  29. $expected = 'N.';
  30. $data = $this->_adapter->serialize($value);
  31. $this->assertEquals($expected, $data);
  32. }
  33. public function testSerializeTrue() {
  34. $value = true;
  35. $expected = "I01\r\n.";
  36. $data = $this->_adapter->serialize($value);
  37. $this->assertEquals($expected, $data);
  38. }
  39. public function testSerializeFalse() {
  40. $value = false;
  41. $expected = "I00\r\n.";
  42. $data = $this->_adapter->serialize($value);
  43. $this->assertEquals($expected, $data);
  44. }
  45. public function testSerializeInt() {
  46. $value = -12345;
  47. $expected = "I-12345\r\n.";
  48. $data = $this->_adapter->serialize($value);
  49. $this->assertEquals($expected, $data);
  50. }
  51. public function testSerializeFloat() {
  52. $value = -12345.6789;
  53. $expected = "F-12345.6789\r\n.";
  54. $data = $this->_adapter->serialize($value);
  55. $this->assertEquals($expected, $data);
  56. }
  57. public function testSerializeString() {
  58. $value = 'test';
  59. $expected = "S'test'\r\np0\r\n.";
  60. $data = $this->_adapter->serialize($value);
  61. $this->assertEquals($expected, $data);
  62. }
  63. public function testSerializeStringWithSpecialChars() {
  64. $value = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  65. . "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  66. . "\xff\\\"'";
  67. $expected = "S'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f"
  68. . "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f"
  69. . "\\xff\\\\\"\\''\r\n"
  70. . "p0\r\n.";
  71. $data = $this->_adapter->serialize($value);
  72. $this->assertEquals($expected, $data);
  73. }
  74. public function testSerializeArrayList() {
  75. $value = array('1', '2', 'test');
  76. $expected = "(lp0\r\n"
  77. . "S'1'\r\n"
  78. . "p1\r\n"
  79. . "aS'2'\r\n"
  80. . "p2\r\n"
  81. . "aS'test'\r\n"
  82. . "p3\r\n"
  83. . "a.";
  84. $data = $this->_adapter->serialize($value);
  85. $this->assertEquals($expected, $data);
  86. }
  87. public function testSerializeArrayDict() {
  88. $value = array('1', '2', 'three' => 'test');
  89. $expected = "(dp0\r\n"
  90. . "I0\r\n"
  91. . "S'1'\r\n"
  92. . "p1\r\n"
  93. . "sI1\r\n"
  94. . "S'2'\r\n"
  95. . "p2\r\n"
  96. . "sS'three'\r\n"
  97. . "p3\r\n"
  98. . "S'test'\r\n"
  99. . "p4\r\n"
  100. . "s.";
  101. $data = $this->_adapter->serialize($value);
  102. $this->assertEquals($expected, $data);
  103. }
  104. public function testSerializeObject() {
  105. $value = new StdClass();
  106. $value->test = 'test';
  107. $value->test2 = 2;
  108. $expected = "(dp0\r\n"
  109. . "S'test'\r\n"
  110. . "p1\r\n"
  111. . "g1\r\n"
  112. . "sS'test2'\r\n"
  113. . "p2\r\n"
  114. . "I2\r\n"
  115. . "s.";
  116. $data = $this->_adapter->serialize($value);
  117. $this->assertEquals($expected, $data);
  118. }
  119. }