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