File: 140.phpt

package info (click to toggle)
php-msgpack 1%3A2.2.0~rc2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,200 kB
  • sloc: ansic: 3,817; xml: 416; makefile: 2
file content (132 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download | duplicates (6)
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
--TEST--
Null byte key position while unpacking objects
--SKIPIF--
<?php
if (version_compare(PHP_VERSION, '5.2.0') < 0) {
    echo "skip tests in PHP 5.2 or newer";
}
--FILE--
<?php
if(!extension_loaded('msgpack'))
{
    dl('msgpack.' . PHP_SHLIB_SUFFIX);
}

function test($type, $array)
{
    $stdClass = hex2bin('c0a8737464436c617373'); // "\0" => 'stdClass'
    $placeholder = hex2bin('a178a178'); // 'x' => 'x'

    $serialized = msgpack_pack($array);
    $serialized = str_replace($placeholder, $stdClass, $serialized);
    $unserialized = msgpack_unpack($serialized);

    var_dump($unserialized);
    unset($array['x']);

    echo $unserialized == (object) $array ? 'OK' : 'ERROR', PHP_EOL;
}

$array = array('x' => 'x', 'foo' => 1);
test('single property, key at the beginning', $array);

$array = array('foo' => 1, 'x' => 'x');
test('single property, key at the end', $array);

$array = array('x' => 'x', 'foo' => 1, 'bar' => 2);
test('multiple properties, key at the beginning', $array);

$array = array('foo' => 1, 'x' => 'x', 'bar' => 2);
test('multiple properties, key in the middle', $array);

$array = array('foo' => 1, 'bar' => 2, 'x' => 'x');
test('multiple properties, key at the end', $array);

$array = array('null' => null, 'x' => 'x');
test('null, key at the end', $array);

$array = array('int' => 1, 'x' => 'x');
test('int, key at the end', $array);

$array = array('float' => 4.2, 'x' => 'x');
test('float, key at the end', $array);

$array = array('string' => 'str', 'x' => 'x');
test('string, key at the end', $array);

$array = array('array' => array(42), 'x' => 'x');
test('array, key at the end', $array);

class Foo { public $a = null; }
$obj = new Foo();
$array = array('object' => $obj, 'x' => 'x');
test('object, key at the end', $array);

--EXPECTF--
object(stdClass)#%d (1) {
  ["foo"]=>
  int(1)
}
OK
object(stdClass)#%d (1) {
  ["foo"]=>
  int(1)
}
OK
object(stdClass)#%d (2) {
  ["foo"]=>
  int(1)
  ["bar"]=>
  int(2)
}
OK
object(stdClass)#%d (2) {
  ["foo"]=>
  int(1)
  ["bar"]=>
  int(2)
}
OK
object(stdClass)#%d (2) {
  ["foo"]=>
  int(1)
  ["bar"]=>
  int(2)
}
OK
object(stdClass)#%d (1) {
  ["null"]=>
  NULL
}
OK
object(stdClass)#%d (1) {
  ["int"]=>
  int(1)
}
OK
object(stdClass)#%d (1) {
  ["float"]=>
  float(4.2)
}
OK
object(stdClass)#%d (1) {
  ["string"]=>
  string(3) "str"
}
OK
object(stdClass)#%d (1) {
  ["array"]=>
  array(1) {
    [0]=>
    int(42)
  }
}
OK
object(stdClass)#%d (1) {
  ["object"]=>
  object(Foo)#%d (1) {
    ["a"]=>
    NULL
  }
}
OK