PhpCodeTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @package Zend_Cache
  4. * @subpackage UnitTests
  5. */
  6. /** @see Zend_Serializer_Adapter_PhpCode */
  7. require_once 'Zend/Serializer/Adapter/PhpCode.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_PhpCodeTest extends PHPUnit_Framework_TestCase
  17. {
  18. private $_adapter;
  19. public function setUp()
  20. {
  21. $this->_adapter = new Zend_Serializer_Adapter_PhpCode();
  22. }
  23. public function tearDown()
  24. {
  25. $this->_adapter = null;
  26. }
  27. public function testSerializeString() {
  28. $value = 'test';
  29. $expected = "'test'";
  30. $data = $this->_adapter->serialize($value);
  31. $this->assertEquals($expected, $data);
  32. }
  33. public function testSerializeFalse() {
  34. $value = false;
  35. $expected = 'false';
  36. $data = $this->_adapter->serialize($value);
  37. $this->assertEquals($expected, $data);
  38. }
  39. public function testSerializeNull() {
  40. $value = null;
  41. $expected = 'NULL';
  42. $data = $this->_adapter->serialize($value);
  43. $this->assertEquals($expected, $data);
  44. }
  45. public function testSerializeNumeric() {
  46. $value = 100.12345;
  47. $expected = '100.12345';
  48. $data = $this->_adapter->serialize($value);
  49. $this->assertEquals($expected, $data);
  50. }
  51. public function testSerializeObject() {
  52. $value = new stdClass();
  53. $expected = "stdClass::__set_state(array(\n))";
  54. $data = $this->_adapter->serialize($value);
  55. $this->assertEquals($expected, $data);
  56. }
  57. public function testUnserializeString() {
  58. $value = "'test'";
  59. $expected = 'test';
  60. $data = $this->_adapter->unserialize($value);
  61. $this->assertEquals($expected, $data);
  62. }
  63. public function testUnserializeFalse() {
  64. $value = 'false';
  65. $expected = false;
  66. $data = $this->_adapter->unserialize($value);
  67. $this->assertEquals($expected, $data);
  68. }
  69. public function testUnserializeNull() {
  70. $value = 'NULL';
  71. $expected = null;
  72. $data = $this->_adapter->unserialize($value);
  73. $this->assertEquals($expected, $data);
  74. }
  75. public function testUnserializeNumeric() {
  76. $value = '100';
  77. $expected = 100;
  78. $data = $this->_adapter->unserialize($value);
  79. $this->assertEquals($expected, $data);
  80. }
  81. /* TODO: PHP Fatal error: Call to undefined method stdClass::__set_state()
  82. public function testUnserializeObject() {
  83. $value = "stdClass::__set_state(array(\n))";
  84. $expected = new stdClass();
  85. $data = $this->_adapter->unserialize($value);
  86. $this->assertEquals($expected, $data);
  87. }
  88. */
  89. public function testUnserialzeInvalid() {
  90. $value = 'not a serialized string';
  91. $this->setExpectedException('Zend_Serializer_Exception');
  92. $this->_adapter->unserialize($value);
  93. }
  94. }