1--TEST--
2unpack of object converter (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    $serialized = msgpack_pack($variable);
20    $unserialized = msgpack_unpack($serialized, $object);
21    var_dump($unserialized);
22    if ($result)
23    {
24        echo $unserialized == $result ? 'OK' : 'ERROR', PHP_EOL;
25    }
26    else
27    {
28        echo 'SKIP', PHP_EOL;
29    }
30}
31
32class Obj
33{
34    public $a;
35    protected $b;
36    private $c;
37
38    public function __construct($a = null, $b = null, $c = null, $d = null)
39    {
40        $this->a = $a;
41        $this->b = $b;
42        $this->c = $c;
43        if (is_array($d))
44        {
45            foreach ($d as $key => $val)
46            {
47                $this->{$key} = $val;
48            }
49        }
50    }
51}
52
53test('null', null, 'Obj', new Obj(null, null, null));
54
55test('bool: true', true, 'Obj', new Obj(true, null, null));
56test('bool: false', false, 'Obj', new Obj(false, null, null));
57
58test('zero: 0', 0, 'Obj', new Obj(0, null, null));
59test('small: 1', 1, 'Obj', new Obj(1, null, null));
60test('small: -1', -1, 'Obj', new Obj(-1, null, null));
61test('medium: 1000', 1000, 'Obj', new Obj(1000, null, null));
62test('medium: -1000', -1000, 'Obj', new Obj(-1000, null, null));
63test('large: 100000', 100000, 'Obj', new Obj(100000, null, null));
64test('large: -100000', -100000, 'Obj', new Obj(-100000, null, null));
65
66test('double: 123.456', 123.456, 'Obj', new Obj(123.456, null, null));
67
68test('empty: ""', "", 'Obj', new Obj("", null, null));
69test('string: "foobar"', "foobar", 'Obj', new Obj("foobar", null, null));
70
71test('array: empty', array(), 'Obj', new Obj(null, null, null));
72test('array(1, 2, 3)', array(1, 2, 3), 'Obj', new Obj(1, 2, 3));
73test('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)));
74test('array(1, 2, 3, 4)', array(1, 2, 3, 4), 'Obj');
75
76test('array("foo", "foobar", "foohoge")', array("foo", "foobar", "hoge"), 'Obj', new Obj("foo", "foobar", "hoge"));
77test('array("a" => 1, "b" => 2))', array("a" => 1, "b" => 2), 'Obj', new Obj(1, 2, null));
78test('array("one" => 1, "two" => 2))', array("one" => 1, "two" => 2), 'Obj', new Obj(null, null, null, array("one" => 1, "two" => 2)));
79
80test('array("a" => 1, "b" => 2, 3))', array("a" => 1, "b" => 2, 3), 'Obj', new Obj(1, 2, 3));
81test('array(3, "a" => 1, "b" => 2))', array(3, "a" => 1, "b" => 2), 'Obj', new Obj(1, 2, 3));
82test('array("a" => 1, 3, "b" => 2))', array("a" => 1, 3, "b" => 2), 'Obj', new Obj(1, 2, 3));
83
84$a = array('foo');
85test('array($a, $a)', array($a, $a), 'Obj', new Obj($a, $a, null));
86
87$a = array(
88    'a' => array(
89        'b' => 'c',
90        'd' => 'e'
91        ),
92    'f' => array(
93        'g' => 'h'
94        )
95    );
96test('array', $a, 'Obj', new Obj(null, null, null, $a));
97
98$o = new Obj(1, 2, 3);
99test('object', $o, 'Obj', new Obj(1, 2, 3));
100
101class Obj2 {
102    public $A;
103    protected $B;
104    private $C;
105
106    function __construct($a, $b, $c) {
107        $this->A = $a;
108        $this->B = $b;
109        $this->C = $c;
110    }
111}
112
113$o = new Obj2(1, 2, 3);
114test('object', $o, 'Obj', new Obj($o));
115
116$o1 = new Obj2(1, 2, 3);
117$o2 = new Obj2(4, 5, 6);
118test('object', array($o1, $o2), 'Obj', new Obj($o1, $o2));
119
120--EXPECTF--
121object(Obj)#%d (3) {
122  ["a"]=>
123  NULL
124  [%r"?b"?:protected"?%r]=>
125  NULL
126  [%r"?c"?:("Obj":)?private"?%r]=>
127  NULL
128}
129OK
130object(Obj)#%d (3) {
131  ["a"]=>
132  bool(true)
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(false)
142  [%r"?b"?:protected"?%r]=>
143  NULL
144  [%r"?c"?:("Obj":)?private"?%r]=>
145  NULL
146}
147OK
148object(Obj)#%d (3) {
149  ["a"]=>
150  int(0)
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(1)
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(1000)
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(100000)
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  float(123.456)
214  [%r"?b"?:protected"?%r]=>
215  NULL
216  [%r"?c"?:("Obj":)?private"?%r]=>
217  NULL
218}
219OK
220object(Obj)#%d (3) {
221  ["a"]=>
222  string(0) ""
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(6) "foobar"
232  [%r"?b"?:protected"?%r]=>
233  NULL
234  [%r"?c"?:("Obj":)?private"?%r]=>
235  NULL
236}
237OK
238object(Obj)#%d (3) {
239  ["a"]=>
240  NULL
241  [%r"?b"?:protected"?%r]=>
242  NULL
243  [%r"?c"?:("Obj":)?private"?%r]=>
244  NULL
245}
246OK
247object(Obj)#%d (3) {
248  ["a"]=>
249  int(1)
250  [%r"?b"?:protected"?%r]=>
251  int(2)
252  [%r"?c"?:("Obj":)?private"?%r]=>
253  int(3)
254}
255OK
256object(Obj)#%d (3) {
257  ["a"]=>
258  array(3) {
259    [0]=>
260    int(1)
261    [1]=>
262    int(2)
263    [2]=>
264    int(3)
265  }
266  [%r"?b"?:protected"?%r]=>
267  array(3) {
268    [0]=>
269    int(4)
270    [1]=>
271    int(5)
272    [2]=>
273    int(6)
274  }
275  [%r"?c"?:("Obj":)?private"?%r]=>
276  array(3) {
277    [0]=>
278    int(7)
279    [1]=>
280    int(8)
281    [2]=>
282    int(9)
283  }
284}
285OK
286object(Obj)#%d (4) {
287  ["a"]=>
288  int(1)
289  [%r"?b"?:protected"?%r]=>
290  int(2)
291  [%r"?c"?:("Obj":)?private"?%r]=>
292  int(3)
293  ["3"]=>
294  int(4)
295}
296SKIP
297object(Obj)#%d (3) {
298  ["a"]=>
299  string(3) "foo"
300  [%r"?b"?:protected"?%r]=>
301  string(6) "foobar"
302  [%r"?c"?:("Obj":)?private"?%r]=>
303  string(4) "hoge"
304}
305OK
306object(Obj)#%d (3) {
307  ["a"]=>
308  int(1)
309  [%r"?b"?:protected"?%r]=>
310  int(2)
311  [%r"?c"?:("Obj":)?private"?%r]=>
312  NULL
313}
314OK
315object(Obj)#%d (5) {
316  ["a"]=>
317  NULL
318  [%r"?b"?:protected"?%r]=>
319  NULL
320  [%r"?c"?:("Obj":)?private"?%r]=>
321  NULL
322  ["one"]=>
323  int(1)
324  ["two"]=>
325  int(2)
326}
327OK
328object(Obj)#%d (3) {
329  ["a"]=>
330  int(1)
331  [%r"?b"?:protected"?%r]=>
332  int(2)
333  [%r"?c"?:("Obj":)?private"?%r]=>
334  int(3)
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  array(1) {
358    [0]=>
359    string(3) "foo"
360  }
361  [%r"?b"?:protected"?%r]=>
362  array(1) {
363    [0]=>
364    string(3) "foo"
365  }
366  [%r"?c"?:("Obj":)?private"?%r]=>
367  NULL
368}
369OK
370object(Obj)#%d (4) {
371  ["a"]=>
372  array(2) {
373    ["b"]=>
374    string(1) "c"
375    ["d"]=>
376    string(1) "e"
377  }
378  [%r"?b"?:protected"?%r]=>
379  NULL
380  [%r"?c"?:("Obj":)?private"?%r]=>
381  NULL
382  ["f"]=>
383  array(1) {
384    ["g"]=>
385    string(1) "h"
386  }
387}
388OK
389object(Obj)#%d (3) {
390  ["a"]=>
391  int(1)
392  [%r"?b"?:protected"?%r]=>
393  int(2)
394  [%r"?c"?:("Obj":)?private"?%r]=>
395  int(3)
396}
397OK
398object(Obj)#%d (3) {
399  ["a"]=>
400  object(Obj2)#%d (3) {
401    ["A"]=>
402    int(1)
403    [%r"?B"?:protected"?%r]=>
404    int(2)
405    [%r"?C"?:("Obj2":)?private"?%r]=>
406    int(3)
407  }
408  [%r"?b"?:protected"?%r]=>
409  NULL
410  [%r"?c"?:("Obj":)?private"?%r]=>
411  NULL
412}
413OK
414object(Obj)#%d (3) {
415  ["a"]=>
416  object(Obj2)#%d (3) {
417    ["A"]=>
418    int(1)
419    [%r"?B"?:protected"?%r]=>
420    int(2)
421    [%r"?C"?:("Obj2":)?private"?%r]=>
422    int(3)
423  }
424  [%r"?b"?:protected"?%r]=>
425  object(Obj2)#%d (3) {
426    ["A"]=>
427    int(4)
428    [%r"?B"?:protected"?%r]=>
429    int(5)
430    [%r"?C"?:("Obj2":)?private"?%r]=>
431    int(6)
432  }
433  [%r"?c"?:("Obj":)?private"?%r]=>
434  NULL
435}
436OK
437