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