| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php | 
|  | 2 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 3 | if (!isset($GEN_DIR)) { | 
|  | 4 | $GEN_DIR = 'gen-php'; | 
|  | 5 | } | 
|  | 6 | if (!isset($MODE)) { | 
|  | 7 | $MODE = 'normal'; | 
|  | 8 | } | 
|  | 9 |  | 
| Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 10 | /** Set the Thrift root */ | 
|  | 11 | $GLOBALS['THRIFT_ROOT'] = '../../lib/php/src'; | 
|  | 12 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 13 | /** Include the Thrift base */ | 
| Mark Slee | 018b699 | 2006-09-07 21:31:12 +0000 | [diff] [blame] | 14 | require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php'; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 15 |  | 
|  | 16 | /** Include the binary protocol */ | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 17 | require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 18 |  | 
|  | 19 | /** Include the socket layer */ | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 20 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocketPool.php'; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | /** Include the socket layer */ | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 23 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 24 |  | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 25 | echo '==============================='."\n"; | 
|  | 26 | echo ' SAFE TO IGNORE THESE IN TEST'."\n"; | 
|  | 27 | echo '==============================='."\n"; | 
|  | 28 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 29 | /** Include the generated code */ | 
| Mark Slee | e129a2d | 2007-02-21 05:17:48 +0000 | [diff] [blame] | 30 | require_once $GEN_DIR.'/ThriftTest.php'; | 
|  | 31 | require_once $GEN_DIR.'/ThriftTest_types.php'; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 32 |  | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 33 | echo '==============================='."\n"; | 
|  | 34 | echo ' END OF SAFE ERRORS SECTION'."\n"; | 
|  | 35 | echo '==============================='."\n\n"; | 
|  | 36 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 37 | $host = 'localhost'; | 
|  | 38 | $port = 9090; | 
|  | 39 |  | 
|  | 40 | if ($argc > 1) { | 
|  | 41 | $host = $argv[0]; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | if ($argc > 2) { | 
|  | 45 | $host = $argv[1]; | 
|  | 46 | } | 
|  | 47 |  | 
| Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 48 | $hosts = array('localhost'); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 49 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 50 | $socket = new TSocket($host, $port); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 51 | $socket = new TSocketPool($hosts, $port); | 
|  | 52 | $socket->setDebug(TRUE); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 53 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 54 | if ($MODE == 'inline') { | 
|  | 55 | $transport = $socket; | 
|  | 56 | $testClient = new ThriftTestClient($transport); | 
|  | 57 | } else { | 
|  | 58 | $bufferedSocket = new TBufferedTransport($socket, 1024, 1024); | 
|  | 59 | $transport = $bufferedSocket; | 
| Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 60 | $protocol = new TBinaryProtocol($transport); | 
|  | 61 | $testClient = new ThriftTestClient($protocol); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
|  | 64 | $transport->open(); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | $start = microtime(true); | 
|  | 67 |  | 
|  | 68 | /** | 
|  | 69 | * VOID TEST | 
|  | 70 | */ | 
|  | 71 | print_r("testVoid()"); | 
|  | 72 | $testClient->testVoid(); | 
|  | 73 | print_r(" = void\n"); | 
|  | 74 |  | 
|  | 75 | /** | 
|  | 76 | * STRING TEST | 
|  | 77 | */ | 
|  | 78 | print_r("testString(\"Test\")"); | 
|  | 79 | $s = $testClient->testString("Test"); | 
|  | 80 | print_r(" = \"$s\"\n"); | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 81 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 82 | /** | 
|  | 83 | * BYTE TEST | 
|  | 84 | */ | 
|  | 85 | print_r("testByte(1)"); | 
|  | 86 | $u8 = $testClient->testByte(1); | 
|  | 87 | print_r(" = $u8\n"); | 
|  | 88 |  | 
|  | 89 | /** | 
|  | 90 | * I32 TEST | 
|  | 91 | */ | 
|  | 92 | print_r("testI32(-1)"); | 
|  | 93 | $i32 = $testClient->testI32(-1); | 
|  | 94 | print_r(" = $i32\n"); | 
|  | 95 |  | 
|  | 96 | /** | 
|  | 97 | * I64 TEST | 
|  | 98 | */ | 
|  | 99 | print_r("testI64(-34359738368)"); | 
|  | 100 | $i64 = $testClient->testI64(-34359738368); | 
|  | 101 | print_r(" = $i64\n"); | 
|  | 102 |  | 
|  | 103 | /** | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 104 | * DOUBLE TEST | 
|  | 105 | */ | 
|  | 106 | print_r("testDouble(-852.234234234)"); | 
|  | 107 | $dub = $testClient->testDouble(-852.234234234); | 
|  | 108 | print_r(" = $dub\n"); | 
|  | 109 |  | 
|  | 110 | /** | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 111 | * STRUCT TEST | 
|  | 112 | */ | 
|  | 113 | print_r("testStruct({\"Zero\", 1, -3, -5})"); | 
|  | 114 | $out = new Xtruct(); | 
|  | 115 | $out->string_thing = "Zero"; | 
|  | 116 | $out->byte_thing = 1; | 
|  | 117 | $out->i32_thing = -3; | 
|  | 118 | $out->i64_thing = -5; | 
|  | 119 | $in = $testClient->testStruct($out); | 
|  | 120 | print_r(" = {\"".$in->string_thing."\", ". | 
|  | 121 | $in->byte_thing.", ". | 
|  | 122 | $in->i32_thing.", ". | 
|  | 123 | $in->i64_thing."}\n"); | 
|  | 124 |  | 
|  | 125 | /** | 
|  | 126 | * NESTED STRUCT TEST | 
|  | 127 | */ | 
|  | 128 | print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); | 
|  | 129 | $out2 = new Xtruct2(); | 
|  | 130 | $out2->byte_thing = 1; | 
|  | 131 | $out2->struct_thing = $out; | 
|  | 132 | $out2->i32_thing = 5; | 
|  | 133 | $in2 = $testClient->testNest($out2); | 
|  | 134 | $in = $in2->struct_thing; | 
|  | 135 | print_r(" = {".$in2->byte_thing.", {\"". | 
|  | 136 | $in->string_thing."\", ". | 
|  | 137 | $in->byte_thing.", ". | 
|  | 138 | $in->i32_thing.", ". | 
|  | 139 | $in->i64_thing."}, ". | 
|  | 140 | $in2->i32_thing."}\n"); | 
|  | 141 |  | 
|  | 142 | /** | 
|  | 143 | * MAP TEST | 
|  | 144 | */ | 
|  | 145 | $mapout = array(); | 
|  | 146 | for ($i = 0; $i < 5; ++$i) { | 
|  | 147 | $mapout[$i] = $i-10; | 
|  | 148 | } | 
|  | 149 | print_r("testMap({"); | 
|  | 150 | $first = true; | 
|  | 151 | foreach ($mapout as $key => $val) { | 
|  | 152 | if ($first) { | 
|  | 153 | $first = false; | 
|  | 154 | } else { | 
|  | 155 | print_r(", "); | 
|  | 156 | } | 
|  | 157 | print_r("$key => $val"); | 
|  | 158 | } | 
|  | 159 | print_r("})"); | 
|  | 160 |  | 
|  | 161 | $mapin = $testClient->testMap($mapout); | 
|  | 162 | print_r(" = {"); | 
|  | 163 | $first = true; | 
|  | 164 | foreach ($mapin as $key => $val) { | 
|  | 165 | if ($first) { | 
|  | 166 | $first = false; | 
|  | 167 | } else { | 
|  | 168 | print_r(", "); | 
|  | 169 | } | 
|  | 170 | print_r("$key => $val"); | 
|  | 171 | } | 
|  | 172 | print_r("}\n"); | 
|  | 173 |  | 
|  | 174 | /** | 
|  | 175 | * SET TEST | 
|  | 176 | */ | 
|  | 177 | $setout = array();; | 
|  | 178 | for ($i = -2; $i < 3; ++$i) { | 
|  | 179 | $setout []= $i; | 
|  | 180 | } | 
|  | 181 | print_r("testSet({"); | 
|  | 182 | $first = true; | 
|  | 183 | foreach ($setout as $val) { | 
|  | 184 | if ($first) { | 
|  | 185 | $first = false; | 
|  | 186 | } else { | 
|  | 187 | print_r(", "); | 
|  | 188 | } | 
|  | 189 | print_r($val); | 
|  | 190 | } | 
|  | 191 | print_r("})"); | 
|  | 192 | $setin = $testClient->testSet($setout); | 
|  | 193 | print_r(" = {"); | 
|  | 194 | $first = true; | 
|  | 195 | foreach ($setin as $val) { | 
|  | 196 | if ($first) { | 
|  | 197 | $first = false; | 
|  | 198 | } else { | 
|  | 199 | print_r(", "); | 
|  | 200 | } | 
|  | 201 | print_r($val); | 
|  | 202 | } | 
|  | 203 | print_r("}\n"); | 
|  | 204 |  | 
|  | 205 | /** | 
|  | 206 | * LIST TEST | 
|  | 207 | */ | 
|  | 208 | $listout = array(); | 
|  | 209 | for ($i = -2; $i < 3; ++$i) { | 
|  | 210 | $listout []= $i; | 
|  | 211 | } | 
|  | 212 | print_r("testList({"); | 
|  | 213 | $first = true; | 
|  | 214 | foreach ($listout as $val) { | 
|  | 215 | if ($first) { | 
|  | 216 | $first = false; | 
|  | 217 | } else { | 
|  | 218 | print_r(", "); | 
|  | 219 | } | 
|  | 220 | print_r($val); | 
|  | 221 | } | 
|  | 222 | print_r("})"); | 
|  | 223 | $listin = $testClient->testList($listout); | 
|  | 224 | print_r(" = {"); | 
|  | 225 | $first = true; | 
|  | 226 | foreach ($listin as $val) { | 
|  | 227 | if ($first) { | 
|  | 228 | $first = false; | 
|  | 229 | } else { | 
|  | 230 | print_r(", "); | 
|  | 231 | } | 
|  | 232 | print_r($val); | 
|  | 233 | } | 
|  | 234 | print_r("}\n"); | 
|  | 235 |  | 
|  | 236 | /** | 
|  | 237 | * ENUM TEST | 
|  | 238 | */ | 
|  | 239 | print_r("testEnum(ONE)"); | 
|  | 240 | $ret = $testClient->testEnum(Numberz::ONE); | 
|  | 241 | print_r(" = $ret\n"); | 
|  | 242 |  | 
|  | 243 | print_r("testEnum(TWO)"); | 
|  | 244 | $ret = $testClient->testEnum(Numberz::TWO); | 
|  | 245 | print_r(" = $ret\n"); | 
|  | 246 |  | 
|  | 247 | print_r("testEnum(THREE)"); | 
|  | 248 | $ret = $testClient->testEnum(Numberz::THREE); | 
|  | 249 | print_r(" = $ret\n"); | 
|  | 250 |  | 
|  | 251 | print_r("testEnum(FIVE)"); | 
|  | 252 | $ret = $testClient->testEnum(Numberz::FIVE); | 
|  | 253 | print_r(" = $ret\n"); | 
|  | 254 |  | 
|  | 255 | print_r("testEnum(EIGHT)"); | 
|  | 256 | $ret = $testClient->testEnum(Numberz::EIGHT); | 
|  | 257 | print_r(" = $ret\n"); | 
|  | 258 |  | 
|  | 259 | /** | 
|  | 260 | * TYPEDEF TEST | 
|  | 261 | */ | 
|  | 262 | print_r("testTypedef(309858235082523)"); | 
|  | 263 | $uid = $testClient->testTypedef(309858235082523); | 
|  | 264 | print_r(" = $uid\n"); | 
|  | 265 |  | 
|  | 266 | /** | 
|  | 267 | * NESTED MAP TEST | 
|  | 268 | */ | 
|  | 269 | print_r("testMapMap(1)"); | 
|  | 270 | $mm = $testClient->testMapMap(1); | 
|  | 271 | print_r(" = {"); | 
|  | 272 | foreach ($mm as $key => $val) { | 
|  | 273 | print_r("$key => {"); | 
|  | 274 | foreach ($val as $k2 => $v2) { | 
|  | 275 | print_r("$k2 => $v2, "); | 
|  | 276 | } | 
|  | 277 | print_r("}, "); | 
|  | 278 | } | 
|  | 279 | print_r("}\n"); | 
|  | 280 |  | 
|  | 281 | /** | 
|  | 282 | * INSANITY TEST | 
|  | 283 | */ | 
|  | 284 | $insane = new Insanity(); | 
|  | 285 | $insane->userMap[Numberz::FIVE] = 5000; | 
|  | 286 | $truck = new Xtruct(); | 
|  | 287 | $truck->string_thing = "Truck"; | 
|  | 288 | $truck->byte_thing = 8; | 
|  | 289 | $truck->i32_thing = 8; | 
|  | 290 | $truck->i64_thing = 8; | 
|  | 291 | $insane->xtructs []= $truck; | 
|  | 292 | print_r("testInsanity()"); | 
|  | 293 | $whoa = $testClient->testInsanity($insane); | 
|  | 294 | print_r(" = {"); | 
|  | 295 | foreach ($whoa as $key => $val) { | 
|  | 296 | print_r("$key => {"); | 
|  | 297 | foreach ($val as $k2 => $v2) { | 
|  | 298 | print_r("$k2 => {"); | 
|  | 299 | $userMap = $v2->userMap; | 
|  | 300 | print_r("{"); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 301 | if (is_array($usermap)) { | 
|  | 302 | foreach ($userMap as $k3 => $v3) { | 
|  | 303 | print_r("$k3 => $v3, "); | 
|  | 304 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 305 | } | 
|  | 306 | print_r("}, "); | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 307 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 308 | $xtructs = $v2->xtructs; | 
|  | 309 | print_r("{"); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 310 | if (is_array($xtructs)) { | 
|  | 311 | foreach ($xtructs as $x) { | 
|  | 312 | print_r("{\"".$x->string_thing."\", ". | 
|  | 313 | $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, "); | 
|  | 314 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 315 | } | 
|  | 316 | print_r("}"); | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 317 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 318 | print_r("}, "); | 
|  | 319 | } | 
|  | 320 | print_r("}, "); | 
|  | 321 | } | 
|  | 322 | print_r("}\n"); | 
|  | 323 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 324 | /** | 
|  | 325 | * EXCEPTION TEST | 
|  | 326 | */ | 
|  | 327 | print_r("testException('Xception')"); | 
|  | 328 | try { | 
|  | 329 | $testClient->testException('Xception'); | 
|  | 330 | print_r("  void\nFAILURE\n"); | 
|  | 331 | } catch (Xception $x) { | 
|  | 332 | print_r(' caught xception '.$x->errorCode.': '.$x->message."\n"); | 
|  | 333 | } | 
|  | 334 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 335 |  | 
|  | 336 | /** | 
|  | 337 | * Normal tests done. | 
|  | 338 | */ | 
|  | 339 |  | 
|  | 340 | $stop = microtime(true); | 
|  | 341 | $elp = round(1000*($stop - $start), 0); | 
|  | 342 | print_r("Total time: $elp ms\n"); | 
|  | 343 |  | 
|  | 344 | /** | 
|  | 345 | * Extraneous "I don't trust PHP to pack/unpack integer" tests | 
|  | 346 | */ | 
|  | 347 |  | 
|  | 348 | // Max I32 | 
|  | 349 | $num = pow(2, 30) + (pow(2, 30) - 1); | 
|  | 350 | $num2 = $testClient->testI32($num); | 
|  | 351 | if ($num != $num2) { | 
|  | 352 | print "Missed $num = $num2\n"; | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | // Min I32 | 
|  | 356 | $num = 0 - pow(2, 31); | 
|  | 357 | $num2 = $testClient->testI32($num); | 
|  | 358 | if ($num != $num2) { | 
|  | 359 | print "Missed $num = $num2\n"; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | // Max I64 | 
|  | 363 | $num = pow(2, 62) + (pow(2, 62) - 1); | 
|  | 364 | $num2 = $testClient->testI64($num); | 
|  | 365 | if ($num != $num2) { | 
|  | 366 | print "Missed $num = $num2\n"; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | // Min I64 | 
|  | 370 | $num = 0 - pow(2, 63); | 
|  | 371 | $num2 = $testClient->testI64($num); | 
|  | 372 | if ($num != $num2) { | 
|  | 373 | print "Missed $num = $num2\n"; | 
|  | 374 | } | 
|  | 375 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 376 | $transport->close(); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 377 | return; | 
|  | 378 |  | 
|  | 379 | ?> |