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