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