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