UnserializeTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Tests\Brumann\Polyfill;
  3. use Brumann\Polyfill\Unserialize;
  4. class UnserializeTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function test_unserialize_without_options_returns_instance()
  7. {
  8. $foo = new Foo();
  9. $serialized = serialize($foo);
  10. $unserialized = Unserialize::unserialize($serialized);
  11. $this->assertInstanceOf('Tests\\Brumann\\Polyfill\\Foo', $unserialized);
  12. }
  13. public function test_unserialize_with_cqn_returns_instance()
  14. {
  15. $foo = new Foo();
  16. $serialized = serialize($foo);
  17. $options = array(
  18. 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
  19. );
  20. $unserialized = Unserialize::unserialize($serialized, $options);
  21. $this->assertInstanceOf('Tests\\Brumann\\Polyfill\\Foo', $unserialized);
  22. }
  23. public function test_unserialize_with_fqcn_allowed_returns_instance()
  24. {
  25. $foo = new Foo();
  26. $serialized = serialize($foo);
  27. $options = array(
  28. 'allowed_classes' => array('\\Tests\\Brumann\\Polyfill\\Foo'),
  29. );
  30. $unserialized = Unserialize::unserialize($serialized, $options);
  31. $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
  32. }
  33. public function test_unserialize_with_allowed_classes_false_returns_incomplete_object()
  34. {
  35. $foo = new Foo();
  36. $serialized = serialize($foo);
  37. $options = array(
  38. 'allowed_classes' => false,
  39. );
  40. $unserialized = Unserialize::unserialize($serialized, $options);
  41. $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
  42. }
  43. /**
  44. * @requires PHP < 7.0
  45. *
  46. * @expectedException \PHPUnit_Framework_Error_Warning
  47. * @expectedMessage allowed_classes option should be array or boolean
  48. */
  49. public function test_unserialize_with_allowed_classes_null_behaves_like_php71()
  50. {
  51. $foo = new Foo();
  52. $serialized = serialize($foo);
  53. $options = array(
  54. 'allowed_classes' => null,
  55. );
  56. Unserialize::unserialize($serialized, $options);
  57. }
  58. /**
  59. * @expectedException \PHPUnit_Framework_Error_Notice
  60. * @expectedExceptionMessage tried to execute a method or access a property of an incomplete object.
  61. */
  62. public function test_accessing_property_of_incomplete_object_returns_warning()
  63. {
  64. $bar = new \stdClass();
  65. $bar->foo = new Foo();
  66. $serialized = serialize($bar);
  67. $options = array(
  68. 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
  69. );
  70. $unserialized = Unserialize::unserialize($serialized, $options);
  71. $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
  72. $unserialized->foo;
  73. }
  74. public function test_unserialize_only_parent_object()
  75. {
  76. $foo = new Foo();
  77. $foo->bar = new \stdClass();
  78. $serialized = serialize($foo);
  79. $options = array(
  80. 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
  81. );
  82. $unserialized = Unserialize::unserialize($serialized, $options);
  83. $this->assertInstanceOf('\\Tests\\Brumann\\Polyfill\\Foo', $unserialized);
  84. $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized->bar);
  85. }
  86. public function test_unserialize_parent_and_embedded_object()
  87. {
  88. $foo = new Foo();
  89. $foo->foo = new Foo();
  90. $serialized = serialize($foo);
  91. $options = array(
  92. 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
  93. );
  94. $unserialized = Unserialize::unserialize($serialized, $options);
  95. $this->assertInstanceOf('\\Tests\\Brumann\\Polyfill\\Foo', $unserialized);
  96. $this->assertInstanceOf('\\Tests\\Brumann\\Polyfill\\Foo', $unserialized->foo);
  97. }
  98. public function test_unserialize_with_allowed_classes_false_serializes_string()
  99. {
  100. $string = 'This is an ordinary string';
  101. $serialized = serialize($string);
  102. $options = array(
  103. 'allowed_classes' => false,
  104. );
  105. $unserialized = Unserialize::unserialize($serialized, $options);
  106. $this->assertEquals($string, $unserialized);
  107. }
  108. public function test_unserialize_with_allowed_classes_false_serializes_bool()
  109. {
  110. $bool = true;
  111. $serialized = serialize($bool);
  112. $options = array(
  113. 'allowed_classes' => false,
  114. );
  115. $unserialized = Unserialize::unserialize($serialized, $options);
  116. $this->assertEquals($bool, $unserialized);
  117. }
  118. public function test_unserialize_with_allowed_classes_false_serializes_array()
  119. {
  120. $array = array(
  121. 'key' => 42,
  122. 1 => 'foo',
  123. 'bar' => 'baz',
  124. 2 => 23,
  125. 4 => true,
  126. );
  127. $serialized = serialize($array);
  128. $options = array(
  129. 'allowed_classes' => false,
  130. );
  131. $unserialized = Unserialize::unserialize($serialized, $options);
  132. $this->assertSame($array, $unserialized);
  133. }
  134. public function test_double_serialized_unserializes_as_first_serialized()
  135. {
  136. $foo = new Foo();
  137. $first = serialize($foo);
  138. $second = serialize($first);
  139. $options = array(
  140. 'allowed_classes' => false,
  141. );
  142. $unserialized = Unserialize::unserialize($second, $options);
  143. $this->assertSame($first, $unserialized);
  144. }
  145. public function test_double_unserialize_double_serialized()
  146. {
  147. $foo = new Foo();
  148. $serialized = serialize(serialize($foo));
  149. $options = array(
  150. 'allowed_classes' => false,
  151. );
  152. $first = Unserialize::unserialize($serialized, $options);
  153. $unserialized = Unserialize::unserialize($first, $options);
  154. $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
  155. }
  156. }