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