1--TEST-- 2Check for class methods 3--SKIPIF-- 4<?php 5if ((version_compare(PHP_VERSION, '5.2.13') <= 0) || 6 (version_compare(PHP_VERSION, '5.3.0') >= 0 && 7 version_compare(PHP_VERSION, '5.3.2') <= 0)) { 8 echo "skip tests in PHP 5.2.14/5.3.3 or newer"; 9} 10--FILE-- 11<?php 12if(!extension_loaded('msgpack')) { 13 dl('msgpack.' . PHP_SHLIB_SUFFIX); 14} 15 16function test($type, $variable, $test = null) { 17 $msgpack = new MessagePack(); 18 19 $serialized = $msgpack->pack($variable); 20 $unserialized = $msgpack->unpack($serialized); 21 22 var_dump($unserialized); 23 24 if (!is_bool($test)) 25 { 26 echo $unserialized === $variable ? 'OK' : 'ERROR', PHP_EOL; 27 } 28 else 29 { 30 echo $test || $unserialized == $variable ? 'OK' : 'ERROR', PHP_EOL; 31 } 32} 33 34test('null', null); 35 36test('bool: true', true); 37test('bool: false', false); 38 39test('zero: 0', 0); 40test('small: 1', 1); 41test('small: -1', -1); 42test('medium: 1000', 1000); 43test('medium: -1000', -1000); 44test('large: 100000', 100000); 45test('large: -100000', -100000); 46 47test('double: 123.456', 123.456); 48 49test('empty: ""', ""); 50test('string: "foobar"', "foobar"); 51 52test('array: empty', array(), false); 53test('array(1, 2, 3)', array(1, 2, 3), false); 54test('array(array(1, 2, 3), arr...', array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9)), false); 55 56test('array("foo", "foo", "foo")', array("foo", "foo", "foo"), false); 57test('array("one" => 1, "two" => 2))', array("one" => 1, "two" => 2), false); 58test('array("kek" => "lol", "lol" => "kek")', array("kek" => "lol", "lol" => "kek"), false); 59test('array("" => "empty")', array("" => "empty"), false); 60 61$a = array('foo'); 62test('array($a, $a)', array($a, $a), false); 63test('array(&$a, &$a)', array(&$a, &$a), false); 64 65$a = array(null); 66$b = array(&$a); 67$a[0] = &$b; 68 69test('cyclic', $a, true); 70 71$a = array( 72 'a' => array( 73 'b' => 'c', 74 'd' => 'e' 75 ), 76 'f' => array( 77 'g' => 'h' 78 ) 79 ); 80 81test('array', $a, false); 82 83class Obj { 84 public $a; 85 protected $b; 86 private $c; 87 88 function __construct($a, $b, $c) { 89 $this->a = $a; 90 $this->b = $b; 91 $this->c = $c; 92 } 93} 94 95test('object', new Obj(1, 2, 3), false); 96 97test('object', array(new Obj(1, 2, 3), new Obj(4, 5, 6)), false); 98 99$o = new Obj(1, 2, 3); 100 101test('object', array(&$o, &$o), false); 102--EXPECTF-- 103NULL 104OK 105bool(true) 106OK 107bool(false) 108OK 109int(0) 110OK 111int(1) 112OK 113int(-1) 114OK 115int(1000) 116OK 117int(-1000) 118OK 119int(100000) 120OK 121int(-100000) 122OK 123float(123.456) 124OK 125string(0) "" 126OK 127string(6) "foobar" 128OK 129array(0) { 130} 131OK 132array(3) { 133 [0]=> 134 int(1) 135 [1]=> 136 int(2) 137 [2]=> 138 int(3) 139} 140OK 141array(3) { 142 [0]=> 143 array(3) { 144 [0]=> 145 int(1) 146 [1]=> 147 int(2) 148 [2]=> 149 int(3) 150 } 151 [1]=> 152 array(3) { 153 [0]=> 154 int(4) 155 [1]=> 156 int(5) 157 [2]=> 158 int(6) 159 } 160 [2]=> 161 array(3) { 162 [0]=> 163 int(7) 164 [1]=> 165 int(8) 166 [2]=> 167 int(9) 168 } 169} 170OK 171array(3) { 172 [0]=> 173 string(3) "foo" 174 [1]=> 175 string(3) "foo" 176 [2]=> 177 string(3) "foo" 178} 179OK 180array(2) { 181 ["one"]=> 182 int(1) 183 ["two"]=> 184 int(2) 185} 186OK 187array(2) { 188 ["kek"]=> 189 string(3) "lol" 190 ["lol"]=> 191 string(3) "kek" 192} 193OK 194array(1) { 195 [""]=> 196 string(5) "empty" 197} 198OK 199array(2) { 200 [0]=> 201 array(1) { 202 [0]=> 203 string(3) "foo" 204 } 205 [1]=> 206 array(1) { 207 [0]=> 208 string(3) "foo" 209 } 210} 211OK 212array(2) { 213 [0]=> 214 &array(1) { 215 [0]=> 216 string(3) "foo" 217 } 218 [1]=> 219 &array(1) { 220 [0]=> 221 string(3) "foo" 222 } 223} 224OK 225array(1) { 226 [0]=> 227 &array(1) { 228 [0]=> 229 array(1) { 230 [0]=> 231 *RECURSION* 232 } 233 } 234} 235OK 236array(2) { 237 ["a"]=> 238 array(2) { 239 ["b"]=> 240 string(1) "c" 241 ["d"]=> 242 string(1) "e" 243 } 244 ["f"]=> 245 array(1) { 246 ["g"]=> 247 string(1) "h" 248 } 249} 250OK 251object(Obj)#%d (3) { 252 ["a"]=> 253 int(1) 254 [%r"?b"?:protected"?%r]=> 255 int(2) 256 [%r"?c"?:("Obj":)?private"?%r]=> 257 int(3) 258} 259OK 260array(2) { 261 [0]=> 262 object(Obj)#%d (3) { 263 ["a"]=> 264 int(1) 265 [%r"?b"?:protected"?%r]=> 266 int(2) 267 [%r"?c"?:("Obj":)?private"?%r]=> 268 int(3) 269 } 270 [1]=> 271 object(Obj)#%d (3) { 272 ["a"]=> 273 int(4) 274 [%r"?b"?:protected"?%r]=> 275 int(5) 276 [%r"?c"?:("Obj":)?private"?%r]=> 277 int(6) 278 } 279} 280OK 281array(2) { 282 [0]=> 283 &object(Obj)#%d (3) { 284 ["a"]=> 285 int(1) 286 [%r"?b"?:protected"?%r]=> 287 int(2) 288 [%r"?c"?:("Obj":)?private"?%r]=> 289 int(3) 290 } 291 [1]=> 292 &object(Obj)#%d (3) { 293 ["a"]=> 294 int(1) 295 [%r"?b"?:protected"?%r]=> 296 int(2) 297 [%r"?c"?:("Obj":)?private"?%r]=> 298 int(3) 299 } 300} 301OK 302