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