PhpCodeTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_PhpCode
  24. */
  25. require_once 'Zend/Serializer/Adapter/PhpCode.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_PhpCodeTest extends PHPUnit_Framework_TestCase
  34. {
  35. private $_adapter;
  36. public function setUp()
  37. {
  38. $this->_adapter = new Zend_Serializer_Adapter_PhpCode();
  39. }
  40. public function tearDown()
  41. {
  42. $this->_adapter = null;
  43. }
  44. public function testSerializeString()
  45. {
  46. $value = 'test';
  47. $expected = "'test'";
  48. $data = $this->_adapter->serialize($value);
  49. $this->assertEquals($expected, $data);
  50. }
  51. public function testSerializeFalse()
  52. {
  53. $value = false;
  54. $expected = 'false';
  55. $data = $this->_adapter->serialize($value);
  56. $this->assertEquals($expected, $data);
  57. }
  58. public function testSerializeNull()
  59. {
  60. $value = null;
  61. $expected = 'NULL';
  62. $data = $this->_adapter->serialize($value);
  63. $this->assertEquals($expected, $data);
  64. }
  65. public function testSerializeNumeric()
  66. {
  67. $value = 100.12345;
  68. $expected = '100.12345';
  69. $data = $this->_adapter->serialize($value);
  70. $this->assertEquals($expected, $data);
  71. }
  72. public function testSerializeObject()
  73. {
  74. $value = new stdClass();
  75. $expected = "stdClass::__set_state(array(\n))";
  76. $data = $this->_adapter->serialize($value);
  77. $this->assertEquals($expected, $data);
  78. }
  79. public function testUnserializeString()
  80. {
  81. $value = "'test'";
  82. $expected = 'test';
  83. $data = $this->_adapter->unserialize($value);
  84. $this->assertEquals($expected, $data);
  85. }
  86. public function testUnserializeFalse()
  87. {
  88. $value = 'false';
  89. $expected = false;
  90. $data = $this->_adapter->unserialize($value);
  91. $this->assertEquals($expected, $data);
  92. }
  93. public function testUnserializeNull()
  94. {
  95. $value = 'NULL';
  96. $expected = null;
  97. $data = $this->_adapter->unserialize($value);
  98. $this->assertEquals($expected, $data);
  99. }
  100. public function testUnserializeNumeric()
  101. {
  102. $value = '100';
  103. $expected = 100;
  104. $data = $this->_adapter->unserialize($value);
  105. $this->assertEquals($expected, $data);
  106. }
  107. /* TODO: PHP Fatal error: Call to undefined method stdClass::__set_state()
  108. public function testUnserializeObject()
  109. {
  110. $value = "stdClass::__set_state(array(\n))";
  111. $expected = new stdClass();
  112. $data = $this->_adapter->unserialize($value);
  113. $this->assertEquals($expected, $data);
  114. }
  115. */
  116. public function testUnserialzeInvalid()
  117. {
  118. if (version_compare(phpversion(), '7', '>=')) {
  119. $this->markTestSkipped('Evaling of invalid input is PHP Parse error in PHP7+');
  120. }
  121. $value = 'not a serialized string';
  122. $this->setExpectedException('Zend_Serializer_Exception');
  123. $this->_adapter->unserialize($value);
  124. }
  125. }