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