1--TEST--
2unpack of object converter : class unpack (object)
3--SKIPIF--
4<?php
5if (version_compare(PHP_VERSION, '5.2.0') < 0) {
6    echo "skip tests in PHP 5.2 or newer";
7}
8--FILE--
9<?php
10if(!extension_loaded('msgpack'))
11{
12    dl('msgpack.' . PHP_SHLIB_SUFFIX);
13}
14
15error_reporting(0);
16
17function test($type, $variable, $object, $result = null)
18{
19    $msgpack = new MessagePack();
20
21    $serialized = $msgpack->pack($variable);
22    $unserialized = $msgpack->unpack($serialized, $object);
23
24    var_dump($unserialized);
25    if ($result)
26    {
27        echo $unserialized == $result ? 'OK' : 'ERROR', PHP_EOL;
28    }
29    else
30    {
31        echo 'SKIP', PHP_EOL;
32    }
33}
34
35class Obj
36{
37    public $a;
38    protected $b;
39    private $c;
40
41    public function __construct($a = null, $b = null, $c = null, $d = null)
42    {
43        $this->a = $a;
44        $this->b = $b;
45        $this->c = $c;
46        if (is_array($d))
47        {
48            foreach ($d as $key => $val)
49            {
50                $this->{$key} = $val;
51            }
52        }
53    }
54}
55
56$object = new Obj();
57
58test('null', null, new Obj(), new Obj(null, null, null));
59
60test('bool: true', true, new Obj(), new Obj(true, null, null));
61test('bool: false', false, new Obj(), new Obj(false, null, null));
62
63test('zero: 0', 0, new Obj(), new Obj(0, null, null));
64test('small: 1', 1, new Obj(), new Obj(1, null, null));
65test('small: -1', -1, new Obj(), new Obj(-1, null, null));
66test('medium: 1000', 1000, new Obj(), new Obj(1000, null, null));
67test('medium: -1000', -1000, new Obj(), new Obj(-1000, null, null));
68test('large: 100000', 100000, new Obj(), new Obj(100000, null, null));
69test('large: -100000', -100000, new Obj(), new Obj(-100000, null, null));
70
71test('double: 123.456', 123.456, new Obj(), new Obj(123.456, null, null));
72
73test('empty: ""', "", new Obj(), new Obj("", null, null));
74test('string: "foobar"', "foobar", new Obj(), new Obj("foobar", null, null));
75
76test('array: empty', array(), new Obj(), new Obj(null, null, null));
77test('array(1, 2, 3)', array(1, 2, 3), new Obj(), new Obj(1, 2, 3));
78test('array(array(1, 2, 3), arr...', array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9)), new Obj(), new Obj(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9)));
79test('array(1, 2, 3, 4)', array(1, 2, 3, 4), new Obj());
80
81test('array("foo", "foobar", "foohoge")', array("foo", "foobar", "hoge"), new Obj(), new Obj("foo", "foobar", "hoge"));
82test('array("a" => 1, "b" => 2))', array("a" => 1, "b" => 2), new Obj(), new Obj(1, 2, null));
83test('array("one" => 1, "two" => 2))', array("one" => 1, "two" => 2), new Obj(), new Obj(null, null, null, array("one" => 1, "two" => 2)));
84
85test('array("a" => 1, "b" => 2, 3))', array("a" => 1, "b" => 2, 3), new Obj(), new Obj(1, 2, 3));
86test('array(3, "a" => 1, "b" => 2))', array(3, "a" => 1, "b" => 2), new Obj(), new Obj(1, 2, 3));
87test('array("a" => 1, 3, "b" => 2))', array("a" => 1, 3, "b" => 2), new Obj(), new Obj(1, 2, 3));
88
89$a = array('foo');
90test('array($a, $a)', array($a, $a), new Obj(), new Obj($a, $a, null));
91
92$a = array(
93    'a' => array(
94        'b' => 'c',
95        'd' => 'e'
96        ),
97    'f' => array(
98        'g' => 'h'
99        )
100    );
101test('array', $a, new Obj(), new Obj(null, null, null, $a));
102
103$o = new Obj(1, 2, 3);
104test('object', $o, new Obj(), new Obj(1, 2, 3));
105
106class Obj2 {
107    public $A;
108    protected $B;
109    private $C;
110
111    function __construct($a, $b, $c) {
112        $this->A = $a;
113        $this->B = $b;
114        $this->C = $c;
115    }
116}
117
118$o = new Obj2(1, 2, 3);
119test('object', $o, new Obj(), new Obj($o));
120
121$o1 = new Obj2(1, 2, 3);
122$o2 = new Obj2(4, 5, 6);
123test('object', array($o1, $o2), new Obj(), new Obj($o1, $o2));
124
125--EXPECTF--
126object(Obj)#%d (3) {
127  ["a"]=>
128  NULL
129  [%r"?b"?:protected"?%r]=>
130  NULL
131  [%r"?c"?:("Obj":)?private"?%r]=>
132  NULL
133}
134OK
135object(Obj)#%d (3) {
136  ["a"]=>
137  bool(true)
138  [%r"?b"?:protected"?%r]=>
139  NULL
140  [%r"?c"?:("Obj":)?private"?%r]=>
141  NULL
142}
143OK
144object(Obj)#%d (3) {
145  ["a"]=>
146  bool(false)
147  [%r"?b"?:protected"?%r]=>
148  NULL
149  [%r"?c"?:("Obj":)?private"?%r]=>
150  NULL
151}
152OK
153object(Obj)#%d (3) {
154  ["a"]=>
155  int(0)
156  [%r"?b"?:protected"?%r]=>
157  NULL
158  [%r"?c"?:("Obj":)?private"?%r]=>
159  NULL
160}
161OK
162object(Obj)#%d (3) {
163  ["a"]=>
164  int(1)
165  [%r"?b"?:protected"?%r]=>
166  NULL
167  [%r"?c"?:("Obj":)?private"?%r]=>
168  NULL
169}
170OK
171object(Obj)#%d (3) {
172  ["a"]=>
173  int(-1)
174  [%r"?b"?:protected"?%r]=>
175  NULL
176  [%r"?c"?:("Obj":)?private"?%r]=>
177  NULL
178}
179OK
180object(Obj)#%d (3) {
181  ["a"]=>
182  int(1000)
183  [%r"?b"?:protected"?%r]=>
184  NULL
185  [%r"?c"?:("Obj":)?private"?%r]=>
186  NULL
187}
188OK
189object(Obj)#%d (3) {
190  ["a"]=>
191  int(-1000)
192  [%r"?b"?:protected"?%r]=>
193  NULL
194  [%r"?c"?:("Obj":)?private"?%r]=>
195  NULL
196}
197OK
198object(Obj)#%d (3) {
199  ["a"]=>
200  int(100000)
201  [%r"?b"?:protected"?%r]=>
202  NULL
203  [%r"?c"?:("Obj":)?private"?%r]=>
204  NULL
205}
206OK
207object(Obj)#%d (3) {
208  ["a"]=>
209  int(-100000)
210  [%r"?b"?:protected"?%r]=>
211  NULL
212  [%r"?c"?:("Obj":)?private"?%r]=>
213  NULL
214}
215OK
216object(Obj)#%d (3) {
217  ["a"]=>
218  float(123.456)
219  [%r"?b"?:protected"?%r]=>
220  NULL
221  [%r"?c"?:("Obj":)?private"?%r]=>
222  NULL
223}
224OK
225object(Obj)#%d (3) {
226  ["a"]=>
227  string(0) ""
228  [%r"?b"?:protected"?%r]=>
229  NULL
230  [%r"?c"?:("Obj":)?private"?%r]=>
231  NULL
232}
233OK
234object(Obj)#%d (3) {
235  ["a"]=>
236  string(6) "foobar"
237  [%r"?b"?:protected"?%r]=>
238  NULL
239  [%r"?c"?:("Obj":)?private"?%r]=>
240  NULL
241}
242OK
243object(Obj)#%d (3) {
244  ["a"]=>
245  NULL
246  [%r"?b"?:protected"?%r]=>
247  NULL
248  [%r"?c"?:("Obj":)?private"?%r]=>
249  NULL
250}
251OK
252object(Obj)#%d (3) {
253  ["a"]=>
254  int(1)
255  [%r"?b"?:protected"?%r]=>
256  int(2)
257  [%r"?c"?:("Obj":)?private"?%r]=>
258  int(3)
259}
260OK
261object(Obj)#%d (3) {
262  ["a"]=>
263  array(3) {
264    [0]=>
265    int(1)
266    [1]=>
267    int(2)
268    [2]=>
269    int(3)
270  }
271  [%r"?b"?:protected"?%r]=>
272  array(3) {
273    [0]=>
274    int(4)
275    [1]=>
276    int(5)
277    [2]=>
278    int(6)
279  }
280  [%r"?c"?:("Obj":)?private"?%r]=>
281  array(3) {
282    [0]=>
283    int(7)
284    [1]=>
285    int(8)
286    [2]=>
287    int(9)
288  }
289}
290OK
291object(Obj)#%d (4) {
292  ["a"]=>
293  int(1)
294  [%r"?b"?:protected"?%r]=>
295  int(2)
296  [%r"?c"?:("Obj":)?private"?%r]=>
297  int(3)
298  ["3"]=>
299  int(4)
300}
301SKIP
302object(Obj)#%d (3) {
303  ["a"]=>
304  string(3) "foo"
305  [%r"?b"?:protected"?%r]=>
306  string(6) "foobar"
307  [%r"?c"?:("Obj":)?private"?%r]=>
308  string(4) "hoge"
309}
310OK
311object(Obj)#%d (3) {
312  ["a"]=>
313  int(1)
314  [%r"?b"?:protected"?%r]=>
315  int(2)
316  [%r"?c"?:("Obj":)?private"?%r]=>
317  NULL
318}
319OK
320object(Obj)#%d (5) {
321  ["a"]=>
322  NULL
323  [%r"?b"?:protected"?%r]=>
324  NULL
325  [%r"?c"?:("Obj":)?private"?%r]=>
326  NULL
327  ["one"]=>
328  int(1)
329  ["two"]=>
330  int(2)
331}
332OK
333object(Obj)#%d (3) {
334  ["a"]=>
335  int(1)
336  [%r"?b"?:protected"?%r]=>
337  int(2)
338  [%r"?c"?:("Obj":)?private"?%r]=>
339  int(3)
340}
341OK
342object(Obj)#%d (3) {
343  ["a"]=>
344  int(1)
345  [%r"?b"?:protected"?%r]=>
346  int(2)
347  [%r"?c"?:("Obj":)?private"?%r]=>
348  int(3)
349}
350OK
351object(Obj)#%d (3) {
352  ["a"]=>
353  int(1)
354  [%r"?b"?:protected"?%r]=>
355  int(2)
356  [%r"?c"?:("Obj":)?private"?%r]=>
357  int(3)
358}
359OK
360object(Obj)#%d (3) {
361  ["a"]=>
362  array(1) {
363    [0]=>
364    string(3) "foo"
365  }
366  [%r"?b"?:protected"?%r]=>
367  array(1) {
368    [0]=>
369    string(3) "foo"
370  }
371  [%r"?c"?:("Obj":)?private"?%r]=>
372  NULL
373}
374OK
375object(Obj)#%d (4) {
376  ["a"]=>
377  array(2) {
378    ["b"]=>
379    string(1) "c"
380    ["d"]=>
381    string(1) "e"
382  }
383  [%r"?b"?:protected"?%r]=>
384  NULL
385  [%r"?c"?:("Obj":)?private"?%r]=>
386  NULL
387  ["f"]=>
388  array(1) {
389    ["g"]=>
390    string(1) "h"
391  }
392}
393OK
394object(Obj)#%d (3) {
395  ["a"]=>
396  int(1)
397  [%r"?b"?:protected"?%r]=>
398  int(2)
399  [%r"?c"?:("Obj":)?private"?%r]=>
400  int(3)
401}
402OK
403object(Obj)#%d (3) {
404  ["a"]=>
405  object(Obj2)#%d (3) {
406    ["A"]=>
407    int(1)
408    [%r"?B"?:protected"?%r]=>
409    int(2)
410    [%r"?C"?:("Obj2":)?private"?%r]=>
411    int(3)
412  }
413  [%r"?b"?:protected"?%r]=>
414  NULL
415  [%r"?c"?:("Obj":)?private"?%r]=>
416  NULL
417}
418OK
419object(Obj)#%d (3) {
420  ["a"]=>
421  object(Obj2)#%d (3) {
422    ["A"]=>
423    int(1)
424    [%r"?B"?:protected"?%r]=>
425    int(2)
426    [%r"?C"?:("Obj2":)?private"?%r]=>
427    int(3)
428  }
429  [%r"?b"?:protected"?%r]=>
430  object(Obj2)#%d (3) {
431    ["A"]=>
432    int(4)
433    [%r"?B"?:protected"?%r]=>
434    int(5)
435    [%r"?C"?:("Obj2":)?private"?%r]=>
436    int(6)
437  }
438  [%r"?c"?:("Obj":)?private"?%r]=>
439  NULL
440}
441OK
442