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