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