PythonPickleSerializeProtocol0Test.php 4.8 KB

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