forked from opis/closure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializeTest.php
300 lines (243 loc) · 7.27 KB
/
SerializeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/* ===========================================================================
* Copyright (c) 2018-2021 Zindex Software
*
* Licensed under the MIT License
* =========================================================================== */
namespace Opis\Closure\Test;
use Opis\Closure\ReflectionClosure;
use stdClass;
use Closure;
class SerializeTest extends \PHPUnit\Framework\TestCase
{
public function testCustomSerialization()
{
$f = function ($value){
return $value;
};
$a = new Abc($f);
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($a));
$this->assertTrue($u->test(true));
}
public function testCustomSerializationSameObjects()
{
$f = function ($value){
return $value;
};
$i = new Abc($f);
$a = array($i, $i);
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($a));
$this->assertTrue($u[0] === $u[1]);
}
public function testCustomSerializationThisObject1()
{
$a = new A2();
$a = \Opis\Closure\unserialize(\Opis\Closure\serialize($a));
$this->assertEquals('Hello, World!', $a->getPhrase());
}
public function testCustomSerializationThisObject2()
{
$a = new A2();
$a = \Opis\Closure\unserialize(\Opis\Closure\serialize($a));
$this->assertTrue($a->getEquality());
}
public function testCustomSerializationSameClosures()
{
$f = function ($value){
return $value;
};
$i = new Abc($f);
$a = array($i, $i);
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($a));
$this->assertTrue($u[0]->getF() === $u[1]->getF());
}
public function testCustomSerializationSameClosures2()
{
$f = function ($value){
return $value;
};
$a = array(new Abc($f), new Abc($f));
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($a));
$this->assertTrue($u[0]->getF() === $u[1]->getF());
}
public function testPrivateMethodClone()
{
$a = new Clone1();
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($a));
$this->assertEquals(1, $u->value());
}
public function testPrivateMethodClone2()
{
$a = new Clone1();
$f = function () use($a){
return $a->value();
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($f));
$this->assertEquals(1, $u());
}
public function testNestedObjects()
{
$parent = new Entity();
$child = new Entity();
$parent->children[] = $child;
$child->parent = $parent;
$f = function () use($parent, $child){
return $parent === $child->parent;
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($f));
$this->assertTrue($u());
}
public function testNestedObjects2()
{
$child = new stdClass();
$parent = new stdClass();
$child->parent = $parent;
$parent->childern = [$child];
$parent->closure = function () use($child){
return true;
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($parent))->closure;
$this->assertTrue($u());
}
public function testNestedObjects3()
{
$obj = new \stdClass;
$obj->closure = function ($arg) use ($obj) {
return $arg === $obj;
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($obj));
$c = $u->closure;
$this->assertTrue($c($u));
}
public function testNestedObjects4()
{
$parent = new \stdClass;
$child1 = new \stdClass;
$child1->parent = $parent;
$parent->closure = function ($p) use ($child1) {
return $child1->parent === $p;
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($parent));
$c = $u->closure;
$this->assertTrue($c($u));
}
public function testNestedObjects5()
{
$parent = new \stdClass;
$child1 = new \stdClass;
$child2 = new \stdClass;
$child1->parent = $parent;
$child2->parent = $parent;
$parent->closure = function ($p) use ($child1, $child2) {
return $child1->parent === $child2->parent && $child1->parent === $p;
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($parent));
$c = $u->closure;
$this->assertTrue($c($u));
}
public function testPrivatePropertyInParentClass()
{
$instance = new ChildClass;
$closure = function () use ($instance) {
return $instance->getFoobar();
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($closure));
$this->assertSame(['test'], $u());
}
public function testInternalClass1()
{
$date = new \DateTime();
$date->setDate(2018, 2, 23);
$closure = function () use($date){
return $date->format('Y-m-d');
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($closure));
$this->assertEquals('2018-02-23', $u());
}
public function testInternalClass2()
{
$date = new \DateTime();
$date->setDate(2018, 2, 23);
$instance = (object)['date' => $date];
$closure = function () use($instance){
return $instance->date->format('Y-m-d');
};
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($closure));
$this->assertEquals('2018-02-23', $u());
}
public function testInternalClass3()
{
$date = new \DateTime();
$date->setDate(2018, 2, 23);
$instance = (object)['date' => $date];
$u = \Opis\Closure\unserialize(\Opis\Closure\serialize($instance));
$this->assertEquals('2018-02-23', $u->date->format('Y-m-d'));
}
public function testObjectReserialization()
{
$o = (object)['foo'=>'bar'];
$s1 = \Opis\Closure\serialize($o);
$o = \Opis\Closure\unserialize($s1);
$s2 = \Opis\Closure\serialize($o);
$this->assertEquals($s1, $s2);
}
public function testIsShortClosure()
{
$f = function () { };
$this->assertFalse((new ReflectionClosure($f))->isShortClosure());
}
public function testIfThisIsCorrectlySerialized() {
$o = new Clone1();
$f = $o->create();
$f = \Opis\Closure\unserialize(\Opis\Closure\serialize($f));
$f = \Opis\Closure\unserialize(\Opis\Closure\serialize($f));
$this->assertEquals(1, $f());
}
}
class Abc
{
private $f;
public function __construct(Closure $f)
{
$this->f = $f;
}
public function getF()
{
return $this->f;
}
public function test($value)
{
$f = $this->f;
return $f($value);
}
}
class Clone1
{
private $a = 1;
private function __clone()
{
}
public function value()
{
return $this->a;
}
public function create() {
return function () {
return $this->a;
};
}
}
class Entity
{
public $parent;
public $children = [];
}
class ParentClass
{
private $foobar = ['test'];
public function getFoobar()
{
return $this->foobar;
}
}
class ChildClass extends ParentClass { }