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