| 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'); | 
| Andreas Scheja | d1ceba4 | 2016-05-15 21:49:04 +0200 | [diff] [blame] | 18 | if ($GEN_DIR === 'gen-php-psr4') { | 
 | 19 |   $loader->registerNamespace('ThriftTest', $GEN_DIR); | 
 | 20 | } else { | 
 | 21 |   $loader->registerDefinition('ThriftTest', $GEN_DIR); | 
 | 22 | } | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 23 | $loader->register(); | 
 | 24 |  | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 25 | /* | 
 | 26 |  * Licensed to the Apache Software Foundation (ASF) under one | 
 | 27 |  * or more contributor license agreements. See the NOTICE file | 
 | 28 |  * distributed with this work for additional information | 
 | 29 |  * regarding copyright ownership. The ASF licenses this file | 
 | 30 |  * to you under the Apache License, Version 2.0 (the | 
 | 31 |  * "License"); you may not use this file except in compliance | 
 | 32 |  * with the License. You may obtain a copy of the License at | 
 | 33 |  * | 
 | 34 |  *   http://www.apache.org/licenses/LICENSE-2.0 | 
 | 35 |  * | 
 | 36 |  * Unless required by applicable law or agreed to in writing, | 
 | 37 |  * software distributed under the License is distributed on an | 
 | 38 |  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
 | 39 |  * KIND, either express or implied. See the License for the | 
 | 40 |  * specific language governing permissions and limitations | 
 | 41 |  * under the License. | 
 | 42 |  */ | 
 | 43 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 44 | /** Include the Thrift base */ | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 45 | /** Include the protocols */ | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 46 | use Thrift\Protocol\TBinaryProtocol; | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 47 | use Thrift\Protocol\TBinaryProtocolAccelerated; | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 48 | use Thrift\Protocol\TCompactProtocol; | 
 | 49 | use Thrift\Protocol\TJSONProtocol; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 50 |  | 
 | 51 | /** Include the socket layer */ | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 52 | use Thrift\Transport\TSocket; | 
 | 53 | use Thrift\Transport\TSocketPool; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 54 |  | 
 | 55 | /** Include the socket layer */ | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 56 | use Thrift\Transport\TFramedTransport; | 
 | 57 | use Thrift\Transport\TBufferedTransport; | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 58 |  | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 59 | function makeProtocol($transport, $PROTO) | 
 | 60 | { | 
 | 61 |   if ($PROTO == 'binary') { | 
 | 62 |     return new TBinaryProtocol($transport); | 
 | 63 |   } else if ($PROTO == 'compact') { | 
 | 64 |     return new TCompactProtocol($transport); | 
 | 65 |   } else if ($PROTO == 'json') { | 
 | 66 |     return new TJSONProtocol($transport); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 67 |   } else if ($PROTO == 'accel') { | 
 | 68 |     if (!function_exists('thrift_protocol_write_binary')) { | 
 | 69 |       echo "Acceleration extension is not loaded\n"; | 
 | 70 |       exit(1); | 
 | 71 |     } | 
 | 72 |     return new TBinaryProtocolAccelerated($transport); | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 73 |   } | 
 | 74 |  | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 75 |   echo "--protocol must be one of {binary|compact|json|accel}\n"; | 
 | 76 |   exit(1); | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 77 | } | 
 | 78 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 79 | $host = 'localhost'; | 
 | 80 | $port = 9090; | 
 | 81 |  | 
 | 82 | if ($argc > 1) { | 
 | 83 |   $host = $argv[0]; | 
 | 84 | } | 
 | 85 |  | 
 | 86 | if ($argc > 2) { | 
 | 87 |   $host = $argv[1]; | 
 | 88 | } | 
 | 89 |  | 
| Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 90 | foreach ($argv as $arg) { | 
 | 91 |   if (substr($arg, 0, 7) == '--port=') { | 
 | 92 |     $port = substr($arg, 7); | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 93 |   } else if (substr($arg, 0, 12) == '--transport=') { | 
 | 94 |     $MODE = substr($arg, 12); | 
 | 95 |   } else if (substr($arg, 0, 11) == '--protocol=') { | 
 | 96 |     $PROTO = substr($arg, 11); | 
 | 97 |   }  | 
| Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 98 | } | 
 | 99 |  | 
| Mark Slee | 1dd819c | 2006-10-26 04:56:18 +0000 | [diff] [blame] | 100 | $hosts = array('localhost'); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 101 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 102 | $socket = new TSocket($host, $port); | 
| Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 103 | $socket = new TSocketPool($hosts, $port); | 
 | 104 | $socket->setDebug(TRUE); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 105 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 106 | if ($MODE == 'inline') { | 
 | 107 |   $transport = $socket; | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 108 |   $testClient = new \ThriftTest\ThriftTestClient($transport); | 
| Bryan Duxbury | a971fb0 | 2011-03-04 00:49:40 +0000 | [diff] [blame] | 109 | } else if ($MODE == 'framed') { | 
 | 110 |   $framedSocket = new TFramedTransport($socket); | 
 | 111 |   $transport = $framedSocket; | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 112 |   $protocol = makeProtocol($transport, $PROTO); | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 113 |   $testClient = new \ThriftTest\ThriftTestClient($protocol); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 114 | } else { | 
 | 115 |   $bufferedSocket = new TBufferedTransport($socket, 1024, 1024); | 
 | 116 |   $transport = $bufferedSocket; | 
| Jim King | 5903d67 | 2015-06-29 18:12:48 -0400 | [diff] [blame] | 117 |   $protocol = makeProtocol($transport, $PROTO); | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 118 |   $testClient = new \ThriftTest\ThriftTestClient($protocol); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 119 | } | 
 | 120 |  | 
 | 121 | $transport->open(); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 122 |  | 
 | 123 | $start = microtime(true); | 
 | 124 |  | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 125 | define('ERR_BASETYPES', 1); | 
 | 126 | define('ERR_STRUCTS', 2); | 
 | 127 | define('ERR_CONTAINERS', 4); | 
 | 128 | define('ERR_EXCEPTIONS', 8); | 
 | 129 | define('ERR_UNKNOWN', 64); | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 130 | $exitcode = 0; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 131 | /** | 
 | 132 |  * VOID TEST | 
 | 133 |  */ | 
 | 134 | print_r("testVoid()"); | 
 | 135 | $testClient->testVoid(); | 
 | 136 | print_r(" = void\n"); | 
 | 137 |  | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 138 | function roundtrip($testClient, $method, $value) { | 
 | 139 |   global $exitcode; | 
 | 140 |   print_r("$method($value)"); | 
 | 141 |   $ret = $testClient->$method($value); | 
 | 142 |   print_r(" = \"$ret\"\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 143 |   if ($value !== $ret) { | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 144 |     print_r("*** FAILED ***\n"); | 
 | 145 |     $exitcode |= ERR_BASETYPES; | 
 | 146 |   } | 
 | 147 | } | 
 | 148 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 149 | /** | 
 | 150 |  * STRING TEST | 
 | 151 |  */ | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 152 | roundtrip($testClient, 'testString', "Test"); | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 153 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 154 | /** | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 155 |  * BOOL TEST | 
 | 156 |  */ | 
 | 157 | roundtrip($testClient, 'testBool', true); | 
 | 158 | roundtrip($testClient, 'testBool', false); | 
 | 159 |  | 
 | 160 | /** | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 161 |  * BYTE TEST | 
 | 162 |  */ | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 163 | roundtrip($testClient, 'testByte', 1); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 164 | roundtrip($testClient, 'testByte', -1); | 
 | 165 | roundtrip($testClient, 'testByte', 127); | 
 | 166 | roundtrip($testClient, 'testByte', -128); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 167 |  | 
 | 168 | /** | 
 | 169 |  * I32 TEST | 
 | 170 |  */ | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 171 | roundtrip($testClient, 'testI32', -1); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 172 |  | 
 | 173 | /** | 
 | 174 |  * I64 TEST | 
 | 175 |  */ | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 176 | roundtrip($testClient, 'testI64', 0); | 
 | 177 | roundtrip($testClient, 'testI64', 1); | 
 | 178 | roundtrip($testClient, 'testI64', -1); | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 179 | roundtrip($testClient, 'testI64', -34359738368); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 180 |  | 
 | 181 | /** | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 182 |  * DOUBLE TEST | 
 | 183 |  */ | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 184 | roundtrip($testClient, 'testDouble', -852.234234234); | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 185 |  | 
 | 186 | /** | 
| Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 187 |  * BINARY TEST  --  TODO | 
 | 188 |  */ | 
 | 189 |  | 
 | 190 | /** | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 191 |  * STRUCT TEST | 
 | 192 |  */ | 
 | 193 | print_r("testStruct({\"Zero\", 1, -3, -5})"); | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 194 | $out = new \ThriftTest\Xtruct(); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 195 | $out->string_thing = "Zero"; | 
 | 196 | $out->byte_thing = 1; | 
 | 197 | $out->i32_thing = -3; | 
 | 198 | $out->i64_thing = -5; | 
 | 199 | $in = $testClient->testStruct($out); | 
 | 200 | print_r(" = {\"".$in->string_thing."\", ". | 
 | 201 |         $in->byte_thing.", ". | 
 | 202 |         $in->i32_thing.", ". | 
 | 203 |         $in->i64_thing."}\n"); | 
 | 204 |  | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 205 | if ($in != $out) { | 
 | 206 |     echo "**FAILED**\n"; | 
 | 207 |     $exitcode |= ERR_STRUCTS; | 
 | 208 | } | 
 | 209 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 210 | /** | 
 | 211 |  * NESTED STRUCT TEST | 
 | 212 |  */ | 
 | 213 | print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 214 | $out2 = new \ThriftTest\Xtruct2(); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 215 | $out2->byte_thing = 1; | 
 | 216 | $out2->struct_thing = $out; | 
 | 217 | $out2->i32_thing = 5; | 
 | 218 | $in2 = $testClient->testNest($out2); | 
 | 219 | $in = $in2->struct_thing; | 
 | 220 | print_r(" = {".$in2->byte_thing.", {\"". | 
 | 221 |         $in->string_thing."\", ". | 
 | 222 |         $in->byte_thing.", ". | 
 | 223 |         $in->i32_thing.", ". | 
 | 224 |         $in->i64_thing."}, ". | 
 | 225 |         $in2->i32_thing."}\n"); | 
 | 226 |  | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 227 | if ($in2 != $out2) { | 
 | 228 |     echo "**FAILED**\n"; | 
 | 229 |     $exitcode |= ERR_STRUCTS; | 
 | 230 | } | 
 | 231 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 232 | /** | 
 | 233 |  * MAP TEST | 
 | 234 |  */ | 
 | 235 | $mapout = array(); | 
 | 236 | for ($i = 0; $i < 5; ++$i) { | 
 | 237 |   $mapout[$i] = $i-10; | 
 | 238 | } | 
 | 239 | print_r("testMap({"); | 
 | 240 | $first = true; | 
 | 241 | foreach ($mapout as $key => $val) { | 
 | 242 |   if ($first) { | 
 | 243 |     $first = false; | 
 | 244 |   } else { | 
 | 245 |     print_r(", "); | 
 | 246 |   } | 
 | 247 |   print_r("$key => $val"); | 
 | 248 | } | 
 | 249 | print_r("})"); | 
 | 250 |  | 
 | 251 | $mapin = $testClient->testMap($mapout); | 
 | 252 | print_r(" = {"); | 
 | 253 | $first = true; | 
 | 254 | foreach ($mapin as $key => $val) { | 
 | 255 |   if ($first) { | 
 | 256 |     $first = false; | 
 | 257 |   } else { | 
 | 258 |     print_r(", "); | 
 | 259 |   } | 
 | 260 |   print_r("$key => $val"); | 
 | 261 | } | 
 | 262 | print_r("}\n"); | 
 | 263 |  | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 264 | if ($mapin != $mapout) { | 
 | 265 |     echo "**FAILED**\n"; | 
 | 266 |     $exitcode |= ERR_CONTAINERS; | 
 | 267 | } | 
 | 268 |  | 
| Håkon Hitland | e66b8fc | 2016-12-05 18:42:41 +0100 | [diff] [blame] | 269 | $mapout = array(); | 
 | 270 | for ($i = 0; $i < 10; $i++) { | 
 | 271 |     $mapout["key$i"] = "val$i"; | 
 | 272 | } | 
 | 273 | print_r('testStringMap({'); | 
 | 274 | $first = true; | 
 | 275 | foreach ($mapout as $key => $val) { | 
 | 276 |   if ($first) { | 
 | 277 |     $first = false; | 
 | 278 |   } else { | 
 | 279 |     print_r(", "); | 
 | 280 |   } | 
 | 281 |   print_r("\"$key\" => \"$val\""); | 
 | 282 | } | 
 | 283 | print_r("})"); | 
 | 284 | $mapin = $testClient->testStringMap($mapout); | 
 | 285 | print_r(" = {"); | 
 | 286 | $first = true; | 
 | 287 | foreach ($mapin as $key => $val) { | 
 | 288 |   if ($first) { | 
 | 289 |     $first = false; | 
 | 290 |   } else { | 
 | 291 |     print_r(", "); | 
 | 292 |   } | 
 | 293 |   print_r("\"$key\" => \"$val\""); | 
 | 294 | } | 
 | 295 | print_r("}\n"); | 
 | 296 | ksort($mapin); | 
 | 297 | if ($mapin != $mapout) { | 
 | 298 |     echo "**FAILED**\n"; | 
 | 299 |     $exitcode |= ERR_CONTAINERS; | 
 | 300 | } | 
 | 301 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 302 | /** | 
 | 303 |  * SET TEST | 
 | 304 |  */ | 
 | 305 | $setout = array();; | 
 | 306 | for ($i = -2; $i < 3; ++$i) { | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 307 |   $setout[$i]= true; | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 308 | } | 
 | 309 | print_r("testSet({"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 310 | echo implode(',', array_keys($setout)); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 311 | print_r("})"); | 
 | 312 | $setin = $testClient->testSet($setout); | 
 | 313 | print_r(" = {"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 314 | echo implode(', ', array_keys($setin)); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 315 | print_r("}\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 316 | // Order of keys in set does not matter | 
 | 317 | ksort($setin); | 
 | 318 | if ($setout !== $setin) { | 
 | 319 |     echo "**FAILED**\n"; | 
 | 320 |     $exitcode |= ERR_CONTAINERS; | 
 | 321 | } | 
 | 322 | // Regression test for corrupted array | 
 | 323 | if ($setin[2] !== $setout[2] || is_int($setin[2])) { | 
 | 324 |     echo "**FAILED**\n"; | 
 | 325 |     $exitcode |= ERR_CONTAINERS; | 
 | 326 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 327 |  | 
 | 328 | /** | 
 | 329 |  * LIST TEST | 
 | 330 |  */ | 
 | 331 | $listout = array(); | 
 | 332 | for ($i = -2; $i < 3; ++$i) { | 
 | 333 |   $listout []= $i; | 
 | 334 | } | 
 | 335 | print_r("testList({"); | 
 | 336 | $first = true; | 
 | 337 | foreach ($listout as $val) { | 
 | 338 |   if ($first) { | 
 | 339 |     $first = false; | 
 | 340 |   } else { | 
 | 341 |     print_r(", "); | 
 | 342 |   } | 
 | 343 |   print_r($val); | 
 | 344 | } | 
 | 345 | print_r("})"); | 
 | 346 | $listin = $testClient->testList($listout); | 
 | 347 | print_r(" = {"); | 
 | 348 | $first = true; | 
 | 349 | foreach ($listin as $val) { | 
 | 350 |   if ($first) { | 
 | 351 |     $first = false; | 
 | 352 |   } else { | 
 | 353 |     print_r(", "); | 
 | 354 |   } | 
 | 355 |   print_r($val); | 
 | 356 | } | 
 | 357 | print_r("}\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 358 | if ($listin !== $listout) { | 
 | 359 |     echo "**FAILED**\n"; | 
 | 360 |     $exitcode |= ERR_CONTAINERS; | 
 | 361 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 362 |  | 
 | 363 | /** | 
 | 364 |  * ENUM TEST | 
 | 365 |  */ | 
 | 366 | print_r("testEnum(ONE)"); | 
| Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 367 | $ret = $testClient->testEnum(\ThriftTest\Numberz::ONE); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 368 | print_r(" = $ret\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 369 | if ($ret != \ThriftTest\Numberz::ONE) { | 
 | 370 |     echo "**FAILED**\n"; | 
 | 371 |     $exitcode |= ERR_STRUCTS; | 
 | 372 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 373 |  | 
 | 374 | print_r("testEnum(TWO)"); | 
| Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 375 | $ret = $testClient->testEnum(\ThriftTest\Numberz::TWO); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 376 | print_r(" = $ret\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 377 | if ($ret != \ThriftTest\Numberz::TWO) { | 
 | 378 |     echo "**FAILED**\n"; | 
 | 379 |     $exitcode |= ERR_STRUCTS; | 
 | 380 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 381 |  | 
 | 382 | print_r("testEnum(THREE)"); | 
| Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 383 | $ret = $testClient->testEnum(\ThriftTest\Numberz::THREE); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 384 | print_r(" = $ret\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 385 | if ($ret != \ThriftTest\Numberz::THREE) { | 
 | 386 |     echo "**FAILED**\n"; | 
 | 387 |     $exitcode |= ERR_STRUCTS; | 
 | 388 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 389 |  | 
 | 390 | print_r("testEnum(FIVE)"); | 
| Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 391 | $ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 392 | print_r(" = $ret\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 393 | if ($ret != \ThriftTest\Numberz::FIVE) { | 
 | 394 |     echo "**FAILED**\n"; | 
 | 395 |     $exitcode |= ERR_STRUCTS; | 
 | 396 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 397 |  | 
 | 398 | print_r("testEnum(EIGHT)"); | 
| Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 399 | $ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 400 | print_r(" = $ret\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 401 | if ($ret != \ThriftTest\Numberz::EIGHT) { | 
 | 402 |     echo "**FAILED**\n"; | 
 | 403 |     $exitcode |= ERR_STRUCTS; | 
 | 404 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 405 |  | 
 | 406 | /** | 
 | 407 |  * TYPEDEF TEST | 
 | 408 |  */ | 
 | 409 | print_r("testTypedef(309858235082523)"); | 
 | 410 | $uid = $testClient->testTypedef(309858235082523); | 
 | 411 | print_r(" = $uid\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 412 | if ($uid !== 309858235082523) { | 
 | 413 |     echo "**FAILED**\n"; | 
 | 414 |     $exitcode |= ERR_STRUCTS; | 
 | 415 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 416 |  | 
 | 417 | /** | 
 | 418 |  * NESTED MAP TEST | 
 | 419 |  */ | 
 | 420 | print_r("testMapMap(1)"); | 
 | 421 | $mm = $testClient->testMapMap(1); | 
 | 422 | print_r(" = {"); | 
 | 423 | foreach ($mm as $key => $val) { | 
 | 424 |   print_r("$key => {"); | 
 | 425 |   foreach ($val as $k2 => $v2) { | 
 | 426 |     print_r("$k2 => $v2, "); | 
 | 427 |   } | 
 | 428 |   print_r("}, "); | 
 | 429 | } | 
 | 430 | print_r("}\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 431 | $expected_mm = [ | 
 | 432 |   -4 => [-4 => -4, -3 => -3, -2 => -2, -1 => -1], | 
 | 433 |   4 => [4 => 4, 3 => 3, 2 => 2, 1 => 1], | 
 | 434 | ]; | 
 | 435 | if ($mm != $expected_mm) { | 
 | 436 |     echo "**FAILED**\n"; | 
 | 437 |     $exitcode |= ERR_CONTAINERS; | 
 | 438 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 439 |  | 
 | 440 | /** | 
 | 441 |  * INSANITY TEST | 
 | 442 |  */ | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 443 | $insane = new \ThriftTest\Insanity(); | 
| Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 444 | $insane->userMap[\ThriftTest\Numberz::FIVE] = 5000; | 
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 445 | $truck = new \ThriftTest\Xtruct(); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 446 | $truck->string_thing = "Truck"; | 
 | 447 | $truck->byte_thing = 8; | 
 | 448 | $truck->i32_thing = 8; | 
 | 449 | $truck->i64_thing = 8; | 
 | 450 | $insane->xtructs []= $truck; | 
 | 451 | print_r("testInsanity()"); | 
 | 452 | $whoa = $testClient->testInsanity($insane); | 
 | 453 | print_r(" = {"); | 
 | 454 | foreach ($whoa as $key => $val) { | 
 | 455 |   print_r("$key => {"); | 
 | 456 |   foreach ($val as $k2 => $v2) { | 
 | 457 |     print_r("$k2 => {"); | 
 | 458 |     $userMap = $v2->userMap; | 
 | 459 |     print_r("{"); | 
| Bryan Duxbury | a971fb0 | 2011-03-04 00:49:40 +0000 | [diff] [blame] | 460 |     if (is_array($userMap)) { | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 461 |       foreach ($userMap as $k3 => $v3) { | 
 | 462 |         print_r("$k3 => $v3, "); | 
 | 463 |       } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 464 |     } | 
 | 465 |     print_r("}, "); | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 466 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 467 |     $xtructs = $v2->xtructs; | 
 | 468 |     print_r("{"); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 469 |     if (is_array($xtructs)) { | 
 | 470 |       foreach ($xtructs as $x) { | 
 | 471 |         print_r("{\"".$x->string_thing."\", ". | 
 | 472 |                 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, "); | 
 | 473 |       } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 474 |     } | 
 | 475 |     print_r("}"); | 
| Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 476 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 477 |     print_r("}, "); | 
 | 478 |   } | 
 | 479 |   print_r("}, "); | 
 | 480 | } | 
 | 481 | print_r("}\n"); | 
 | 482 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 483 | /** | 
 | 484 |  * EXCEPTION TEST | 
 | 485 |  */ | 
 | 486 | print_r("testException('Xception')"); | 
 | 487 | try { | 
 | 488 |   $testClient->testException('Xception'); | 
 | 489 |   print_r("  void\nFAILURE\n"); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 490 |   $exitcode |= ERR_EXCEPTIONS; | 
| Roger Meier | 87afaac | 2013-01-06 20:10:42 +0100 | [diff] [blame] | 491 | } catch (\ThriftTest\Xception $x) { | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 492 |   print_r(' caught xception '.$x->errorCode.': '.$x->message."\n"); | 
 | 493 | } | 
 | 494 |  | 
| Roy Sindre Norangshol | ec64f23 | 2017-07-26 20:49:38 +0200 | [diff] [blame] | 495 | // Regression test for THRIFT-4263 | 
 | 496 | print_r("testBinarySerializer_Deserialize('foo')"); | 
 | 497 | try { | 
 | 498 |   \Thrift\Serializer\TBinarySerializer::deserialize(base64_decode('foo'), \ThriftTest\Xtruct2::class); | 
 | 499 |   echo "**FAILED**\n"; | 
 | 500 |   $exitcode |= ERR_STRUCTS; | 
 | 501 | } catch (\Thrift\Exception\TTransportException $happy_exception) { | 
 | 502 |   // We expected this due to binary data of base64_decode('foo') is less then 4 | 
 | 503 |   // bytes and it tries to find thrift version number in the transport by | 
 | 504 |   // reading i32() at the beginning.  Casting to string validates that | 
 | 505 |   // exception is still accessible in memory and not corrupted.  Without patch, | 
 | 506 |   // PHP will error log that the exception doesn't have any tostring method, | 
 | 507 |   // which is a lie due to corrupted memory. | 
 | 508 |   for($i=99; $i > 0; $i--) { | 
 | 509 |     (string)$happy_exception; | 
 | 510 |   } | 
 | 511 |   print_r("  SUCCESS\n"); | 
 | 512 | } | 
 | 513 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 514 | /** | 
 | 515 |  * Normal tests done. | 
 | 516 |  */ | 
 | 517 |  | 
 | 518 | $stop = microtime(true); | 
 | 519 | $elp = round(1000*($stop - $start), 0); | 
 | 520 | print_r("Total time: $elp ms\n"); | 
 | 521 |  | 
 | 522 | /** | 
 | 523 |  * Extraneous "I don't trust PHP to pack/unpack integer" tests | 
 | 524 |  */ | 
 | 525 |  | 
| Håkon Hitland | e66b8fc | 2016-12-05 18:42:41 +0100 | [diff] [blame] | 526 | if ($protocol instanceof TBinaryProtocolAccelerated) { | 
 | 527 |     // Regression check: check that method name is not double-freed | 
 | 528 |     // Method name should not be an interned string. | 
 | 529 |     $method_name = "Void"; | 
 | 530 |     $method_name = "test$method_name"; | 
 | 531 |  | 
 | 532 |     $seqid = 0; | 
 | 533 |     $args = new \ThriftTest\ThriftTest_testVoid_args(); | 
 | 534 |     thrift_protocol_write_binary($protocol, $method_name, \Thrift\Type\TMessageType::CALL, $args, $seqid, $protocol->isStrictWrite()); | 
 | 535 |     $testClient->recv_testVoid(); | 
 | 536 |  | 
 | 537 | } | 
 | 538 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 539 | // Max I32 | 
 | 540 | $num = pow(2, 30) + (pow(2, 30) - 1); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 541 | roundtrip($testClient, 'testI32', $num); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 542 |  | 
 | 543 | // Min I32 | 
 | 544 | $num = 0 - pow(2, 31); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 545 | roundtrip($testClient, 'testI32', $num); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 546 |  | 
 | 547 | // Max I64 | 
 | 548 | $num = pow(2, 62) + (pow(2, 62) - 1); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 549 | roundtrip($testClient, 'testI64', $num); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 550 |  | 
 | 551 | // Min I64 | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 552 | $num = 0 - pow(2, 62) - pow(2, 62); | 
| Håkon Hitland | f39d4c8 | 2016-11-17 16:18:03 +0100 | [diff] [blame] | 553 | roundtrip($testClient, 'testI64', $num); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 554 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 555 | $transport->close(); | 
| Nobuaki Sukegawa | e4ba164 | 2016-07-22 18:09:32 +0900 | [diff] [blame] | 556 | exit($exitcode); |