comparison vendor/brumann/polyfill-unserialize/tests/UnserializeTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Tests\Brumann\Polyfill;
4
5 use Brumann\Polyfill\Unserialize;
6
7 class UnserializeTest extends \PHPUnit_Framework_TestCase
8 {
9 public function test_unserialize_without_options_returns_instance()
10 {
11 $foo = new Foo();
12 $serialized = serialize($foo);
13
14 $unserialized = Unserialize::unserialize($serialized);
15
16 $this->assertInstanceOf('Tests\\Brumann\\Polyfill\\Foo', $unserialized);
17 }
18
19 public function test_unserialize_with_cqn_returns_instance()
20 {
21 $foo = new Foo();
22 $serialized = serialize($foo);
23 $options = array(
24 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
25 );
26
27 $unserialized = Unserialize::unserialize($serialized, $options);
28
29 $this->assertInstanceOf('Tests\\Brumann\\Polyfill\\Foo', $unserialized);
30 }
31
32 public function test_unserialize_with_fqcn_allowed_returns_instance()
33 {
34 $foo = new Foo();
35 $serialized = serialize($foo);
36 $options = array(
37 'allowed_classes' => array('\\Tests\\Brumann\\Polyfill\\Foo'),
38 );
39
40 $unserialized = Unserialize::unserialize($serialized, $options);
41
42 $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
43 }
44
45 public function test_unserialize_with_allowed_classes_false_returns_incomplete_object()
46 {
47 $foo = new Foo();
48 $serialized = serialize($foo);
49 $options = array(
50 'allowed_classes' => false,
51 );
52
53 $unserialized = Unserialize::unserialize($serialized, $options);
54
55 $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
56 }
57
58 /**
59 * @requires PHP < 7.0
60 *
61 * @expectedException \PHPUnit_Framework_Error_Warning
62 * @expectedMessage allowed_classes option should be array or boolean
63 */
64 public function test_unserialize_with_allowed_classes_null_behaves_like_php71()
65 {
66 $foo = new Foo();
67 $serialized = serialize($foo);
68 $options = array(
69 'allowed_classes' => null,
70 );
71
72 Unserialize::unserialize($serialized, $options);
73 }
74
75 /**
76 * @expectedException \PHPUnit_Framework_Error_Notice
77 * @expectedExceptionMessage tried to execute a method or access a property of an incomplete object.
78 */
79 public function test_accessing_property_of_incomplete_object_returns_warning()
80 {
81 $bar = new \stdClass();
82 $bar->foo = new Foo();
83 $serialized = serialize($bar);
84 $options = array(
85 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
86 );
87
88 $unserialized = Unserialize::unserialize($serialized, $options);
89
90 $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
91 $unserialized->foo;
92 }
93
94 public function test_unserialize_only_parent_object()
95 {
96 $foo = new Foo();
97 $foo->bar = new \stdClass();
98 $serialized = serialize($foo);
99 $options = array(
100 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
101 );
102
103 $unserialized = Unserialize::unserialize($serialized, $options);
104
105 $this->assertInstanceOf('\\Tests\\Brumann\\Polyfill\\Foo', $unserialized);
106 $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized->bar);
107 }
108
109 public function test_unserialize_parent_and_embedded_object()
110 {
111 $foo = new Foo();
112 $foo->foo = new Foo();
113 $serialized = serialize($foo);
114 $options = array(
115 'allowed_classes' => array('Tests\\Brumann\\Polyfill\\Foo'),
116 );
117
118 $unserialized = Unserialize::unserialize($serialized, $options);
119
120 $this->assertInstanceOf('\\Tests\\Brumann\\Polyfill\\Foo', $unserialized);
121 $this->assertInstanceOf('\\Tests\\Brumann\\Polyfill\\Foo', $unserialized->foo);
122 }
123
124 public function test_unserialize_with_allowed_classes_false_serializes_string()
125 {
126 $string = 'This is an ordinary string';
127 $serialized = serialize($string);
128 $options = array(
129 'allowed_classes' => false,
130 );
131
132 $unserialized = Unserialize::unserialize($serialized, $options);
133
134 $this->assertEquals($string, $unserialized);
135 }
136
137 public function test_unserialize_with_allowed_classes_false_serializes_bool()
138 {
139 $bool = true;
140 $serialized = serialize($bool);
141 $options = array(
142 'allowed_classes' => false,
143 );
144
145 $unserialized = Unserialize::unserialize($serialized, $options);
146
147 $this->assertEquals($bool, $unserialized);
148 }
149
150 public function test_unserialize_with_allowed_classes_false_serializes_array()
151 {
152 $array = array(
153 'key' => 42,
154 1 => 'foo',
155 'bar' => 'baz',
156 2 => 23,
157 4 => true,
158 );
159 $serialized = serialize($array);
160 $options = array(
161 'allowed_classes' => false,
162 );
163
164 $unserialized = Unserialize::unserialize($serialized, $options);
165
166 $this->assertSame($array, $unserialized);
167 }
168
169 public function test_double_serialized_unserializes_as_first_serialized()
170 {
171 $foo = new Foo();
172 $first = serialize($foo);
173 $second = serialize($first);
174 $options = array(
175 'allowed_classes' => false,
176 );
177
178 $unserialized = Unserialize::unserialize($second, $options);
179
180 $this->assertSame($first, $unserialized);
181 }
182
183 public function test_double_unserialize_double_serialized()
184 {
185 $foo = new Foo();
186 $serialized = serialize(serialize($foo));
187 $options = array(
188 'allowed_classes' => false,
189 );
190
191 $first = Unserialize::unserialize($serialized, $options);
192 $unserialized = Unserialize::unserialize($first, $options);
193
194 $this->assertInstanceOf('__PHP_Incomplete_Class', $unserialized);
195 }
196 }