PageRenderTime 35ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 1ms

/test/php/TestClient.php

https://gitlab.com/peter.tiedemann/thrift
PHP | 427 lines | 290 code | 54 blank | 83 comment | 51 complexity | 60f84d4c8a9d572ee577368727203fdb MD5 | raw file
  1. <?php
  2. namespace test\php;
  3. require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
  4. use Thrift\ClassLoader\ThriftClassLoader;
  5. if (!isset($GEN_DIR)) {
  6. $GEN_DIR = 'gen-php';
  7. }
  8. if (!isset($MODE)) {
  9. $MODE = 'normal';
  10. }
  11. $loader = new ThriftClassLoader();
  12. $loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib');
  13. $loader->registerDefinition('ThriftTest', $GEN_DIR);
  14. $loader->register();
  15. /*
  16. * Licensed to the Apache Software Foundation (ASF) under one
  17. * or more contributor license agreements. See the NOTICE file
  18. * distributed with this work for additional information
  19. * regarding copyright ownership. The ASF licenses this file
  20. * to you under the Apache License, Version 2.0 (the
  21. * "License"); you may not use this file except in compliance
  22. * with the License. You may obtain a copy of the License at
  23. *
  24. * http://www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing,
  27. * software distributed under the License is distributed on an
  28. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  29. * KIND, either express or implied. See the License for the
  30. * specific language governing permissions and limitations
  31. * under the License.
  32. */
  33. /** Include the Thrift base */
  34. /** Include the protocols */
  35. use Thrift\Protocol\TBinaryProtocol;
  36. use Thrift\Protocol\TCompactProtocol;
  37. use Thrift\Protocol\TJSONProtocol;
  38. /** Include the socket layer */
  39. use Thrift\Transport\TSocket;
  40. use Thrift\Transport\TSocketPool;
  41. /** Include the socket layer */
  42. use Thrift\Transport\TFramedTransport;
  43. use Thrift\Transport\TBufferedTransport;
  44. function makeProtocol($transport, $PROTO)
  45. {
  46. if ($PROTO == 'binary') {
  47. return new TBinaryProtocol($transport);
  48. } else if ($PROTO == 'compact') {
  49. return new TCompactProtocol($transport);
  50. } else if ($PROTO == 'json') {
  51. return new TJSONProtocol($transport);
  52. }
  53. die ("--protocol must be one of {binary|compact|json}");
  54. }
  55. $host = 'localhost';
  56. $port = 9090;
  57. if ($argc > 1) {
  58. $host = $argv[0];
  59. }
  60. if ($argc > 2) {
  61. $host = $argv[1];
  62. }
  63. foreach ($argv as $arg) {
  64. if (substr($arg, 0, 7) == '--port=') {
  65. $port = substr($arg, 7);
  66. } else if (substr($arg, 0, 12) == '--transport=') {
  67. $MODE = substr($arg, 12);
  68. } else if (substr($arg, 0, 11) == '--protocol=') {
  69. $PROTO = substr($arg, 11);
  70. }
  71. }
  72. $hosts = array('localhost');
  73. $socket = new TSocket($host, $port);
  74. $socket = new TSocketPool($hosts, $port);
  75. $socket->setDebug(TRUE);
  76. if ($MODE == 'inline') {
  77. $transport = $socket;
  78. $testClient = new \ThriftTest\ThriftTestClient($transport);
  79. } else if ($MODE == 'framed') {
  80. $framedSocket = new TFramedTransport($socket);
  81. $transport = $framedSocket;
  82. $protocol = makeProtocol($transport, $PROTO);
  83. $testClient = new \ThriftTest\ThriftTestClient($protocol);
  84. } else {
  85. $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
  86. $transport = $bufferedSocket;
  87. $protocol = makeProtocol($transport, $PROTO);
  88. $testClient = new \ThriftTest\ThriftTestClient($protocol);
  89. }
  90. $transport->open();
  91. $start = microtime(true);
  92. /**
  93. * VOID TEST
  94. */
  95. print_r("testVoid()");
  96. $testClient->testVoid();
  97. print_r(" = void\n");
  98. /**
  99. * STRING TEST
  100. */
  101. print_r("testString(\"Test\")");
  102. $s = $testClient->testString("Test");
  103. print_r(" = \"$s\"\n");
  104. /**
  105. * BYTE TEST
  106. */
  107. print_r("testByte(1)");
  108. $u8 = $testClient->testByte(1);
  109. print_r(" = $u8\n");
  110. /**
  111. * I32 TEST
  112. */
  113. print_r("testI32(-1)");
  114. $i32 = $testClient->testI32(-1);
  115. print_r(" = $i32\n");
  116. /**
  117. * I64 TEST
  118. */
  119. print_r("testI64(-34359738368)");
  120. $i64 = $testClient->testI64(-34359738368);
  121. print_r(" = $i64\n");
  122. /**
  123. * DOUBLE TEST
  124. */
  125. print_r("testDouble(-852.234234234)");
  126. $dub = $testClient->testDouble(-852.234234234);
  127. print_r(" = $dub\n");
  128. /**
  129. * BINARY TEST -- TODO
  130. */
  131. /**
  132. * STRUCT TEST
  133. */
  134. print_r("testStruct({\"Zero\", 1, -3, -5})");
  135. $out = new \ThriftTest\Xtruct();
  136. $out->string_thing = "Zero";
  137. $out->byte_thing = 1;
  138. $out->i32_thing = -3;
  139. $out->i64_thing = -5;
  140. $in = $testClient->testStruct($out);
  141. print_r(" = {\"".$in->string_thing."\", ".
  142. $in->byte_thing.", ".
  143. $in->i32_thing.", ".
  144. $in->i64_thing."}\n");
  145. /**
  146. * NESTED STRUCT TEST
  147. */
  148. print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
  149. $out2 = new \ThriftTest\Xtruct2();
  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. * MAP TEST
  163. */
  164. $mapout = array();
  165. for ($i = 0; $i < 5; ++$i) {
  166. $mapout[$i] = $i-10;
  167. }
  168. print_r("testMap({");
  169. $first = true;
  170. foreach ($mapout as $key => $val) {
  171. if ($first) {
  172. $first = false;
  173. } else {
  174. print_r(", ");
  175. }
  176. print_r("$key => $val");
  177. }
  178. print_r("})");
  179. $mapin = $testClient->testMap($mapout);
  180. print_r(" = {");
  181. $first = true;
  182. foreach ($mapin as $key => $val) {
  183. if ($first) {
  184. $first = false;
  185. } else {
  186. print_r(", ");
  187. }
  188. print_r("$key => $val");
  189. }
  190. print_r("}\n");
  191. /**
  192. * SET TEST
  193. */
  194. $setout = array();;
  195. for ($i = -2; $i < 3; ++$i) {
  196. $setout []= $i;
  197. }
  198. print_r("testSet({");
  199. $first = true;
  200. foreach ($setout as $val) {
  201. if ($first) {
  202. $first = false;
  203. } else {
  204. print_r(", ");
  205. }
  206. print_r($val);
  207. }
  208. print_r("})");
  209. $setin = $testClient->testSet($setout);
  210. print_r(" = {");
  211. $first = true;
  212. foreach ($setin as $val) {
  213. if ($first) {
  214. $first = false;
  215. } else {
  216. print_r(", ");
  217. }
  218. print_r($val);
  219. }
  220. print_r("}\n");
  221. /**
  222. * LIST TEST
  223. */
  224. $listout = array();
  225. for ($i = -2; $i < 3; ++$i) {
  226. $listout []= $i;
  227. }
  228. print_r("testList({");
  229. $first = true;
  230. foreach ($listout as $val) {
  231. if ($first) {
  232. $first = false;
  233. } else {
  234. print_r(", ");
  235. }
  236. print_r($val);
  237. }
  238. print_r("})");
  239. $listin = $testClient->testList($listout);
  240. print_r(" = {");
  241. $first = true;
  242. foreach ($listin as $val) {
  243. if ($first) {
  244. $first = false;
  245. } else {
  246. print_r(", ");
  247. }
  248. print_r($val);
  249. }
  250. print_r("}\n");
  251. /**
  252. * ENUM TEST
  253. */
  254. print_r("testEnum(ONE)");
  255. $ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
  256. print_r(" = $ret\n");
  257. print_r("testEnum(TWO)");
  258. $ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
  259. print_r(" = $ret\n");
  260. print_r("testEnum(THREE)");
  261. $ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
  262. print_r(" = $ret\n");
  263. print_r("testEnum(FIVE)");
  264. $ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
  265. print_r(" = $ret\n");
  266. print_r("testEnum(EIGHT)");
  267. $ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT);
  268. print_r(" = $ret\n");
  269. /**
  270. * TYPEDEF TEST
  271. */
  272. print_r("testTypedef(309858235082523)");
  273. $uid = $testClient->testTypedef(309858235082523);
  274. print_r(" = $uid\n");
  275. /**
  276. * NESTED MAP TEST
  277. */
  278. print_r("testMapMap(1)");
  279. $mm = $testClient->testMapMap(1);
  280. print_r(" = {");
  281. foreach ($mm as $key => $val) {
  282. print_r("$key => {");
  283. foreach ($val as $k2 => $v2) {
  284. print_r("$k2 => $v2, ");
  285. }
  286. print_r("}, ");
  287. }
  288. print_r("}\n");
  289. /**
  290. * INSANITY TEST
  291. */
  292. $insane = new \ThriftTest\Insanity();
  293. $insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
  294. $truck = new \ThriftTest\Xtruct();
  295. $truck->string_thing = "Truck";
  296. $truck->byte_thing = 8;
  297. $truck->i32_thing = 8;
  298. $truck->i64_thing = 8;
  299. $insane->xtructs []= $truck;
  300. print_r("testInsanity()");
  301. $whoa = $testClient->testInsanity($insane);
  302. print_r(" = {");
  303. foreach ($whoa as $key => $val) {
  304. print_r("$key => {");
  305. foreach ($val as $k2 => $v2) {
  306. print_r("$k2 => {");
  307. $userMap = $v2->userMap;
  308. print_r("{");
  309. if (is_array($userMap)) {
  310. foreach ($userMap as $k3 => $v3) {
  311. print_r("$k3 => $v3, ");
  312. }
  313. }
  314. print_r("}, ");
  315. $xtructs = $v2->xtructs;
  316. print_r("{");
  317. if (is_array($xtructs)) {
  318. foreach ($xtructs as $x) {
  319. print_r("{\"".$x->string_thing."\", ".
  320. $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
  321. }
  322. }
  323. print_r("}");
  324. print_r("}, ");
  325. }
  326. print_r("}, ");
  327. }
  328. print_r("}\n");
  329. /**
  330. * EXCEPTION TEST
  331. */
  332. print_r("testException('Xception')");
  333. try {
  334. $testClient->testException('Xception');
  335. print_r(" void\nFAILURE\n");
  336. } catch (\ThriftTest\Xception $x) {
  337. print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
  338. }
  339. /**
  340. * Normal tests done.
  341. */
  342. $stop = microtime(true);
  343. $elp = round(1000*($stop - $start), 0);
  344. print_r("Total time: $elp ms\n");
  345. /**
  346. * Extraneous "I don't trust PHP to pack/unpack integer" tests
  347. */
  348. // Max I32
  349. $num = pow(2, 30) + (pow(2, 30) - 1);
  350. $num2 = $testClient->testI32($num);
  351. if ($num != $num2) {
  352. print "Missed $num = $num2\n";
  353. }
  354. // Min I32
  355. $num = 0 - pow(2, 31);
  356. $num2 = $testClient->testI32($num);
  357. if ($num != $num2) {
  358. print "Missed $num = $num2\n";
  359. }
  360. // Max I64
  361. $num = pow(2, 62) + (pow(2, 62) - 1);
  362. $num2 = $testClient->testI64($num);
  363. if ($num != $num2) {
  364. print "Missed $num = $num2\n";
  365. }
  366. // Min I64
  367. $num = 0 - pow(2, 63);
  368. $num2 = $testClient->testI64($num);
  369. if ($num != $num2) {
  370. print "Missed $num = $num2\n";
  371. }
  372. $transport->close();
  373. return;