PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/thrift-0.9.0/test/php/TestClient.php

https://bitbucket.org/brian_/le-bypass
PHP | 398 lines | 268 code | 50 blank | 80 comment | 35 complexity | deeeb66b68b21a60424a8ea50d3eef75 MD5 | raw file
Possible License(s): MPL-2.0, 0BSD, Apache-2.0
  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 binary protocol */
  35. use Thrift\Protocol\TBinaryProtocol;
  36. /** Include the socket layer */
  37. use Thrift\Transport\TSocket;
  38. use Thrift\Transport\TSocketPool;
  39. /** Include the socket layer */
  40. use Thrift\Transport\TFramedTransport;
  41. use Thrift\Transport\TBufferedTransport;
  42. $host = 'localhost';
  43. $port = 9090;
  44. if ($argc > 1) {
  45. $host = $argv[0];
  46. }
  47. if ($argc > 2) {
  48. $host = $argv[1];
  49. }
  50. $hosts = array('localhost');
  51. $socket = new TSocket($host, $port);
  52. $socket = new TSocketPool($hosts, $port);
  53. $socket->setDebug(TRUE);
  54. if ($MODE == 'inline') {
  55. $transport = $socket;
  56. $testClient = new \ThriftTest\ThriftTestClient($transport);
  57. } else if ($MODE == 'framed') {
  58. $framedSocket = new TFramedTransport($socket);
  59. $transport = $framedSocket;
  60. $protocol = new TBinaryProtocol($transport);
  61. $testClient = new \ThriftTest\ThriftTestClient($protocol);
  62. } else {
  63. $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
  64. $transport = $bufferedSocket;
  65. $protocol = new TBinaryProtocol($transport);
  66. $testClient = new \ThriftTest\ThriftTestClient($protocol);
  67. }
  68. $transport->open();
  69. $start = microtime(true);
  70. /**
  71. * VOID TEST
  72. */
  73. print_r("testVoid()");
  74. $testClient->testVoid();
  75. print_r(" = void\n");
  76. /**
  77. * STRING TEST
  78. */
  79. print_r("testString(\"Test\")");
  80. $s = $testClient->testString("Test");
  81. print_r(" = \"$s\"\n");
  82. /**
  83. * BYTE TEST
  84. */
  85. print_r("testByte(1)");
  86. $u8 = $testClient->testByte(1);
  87. print_r(" = $u8\n");
  88. /**
  89. * I32 TEST
  90. */
  91. print_r("testI32(-1)");
  92. $i32 = $testClient->testI32(-1);
  93. print_r(" = $i32\n");
  94. /**
  95. * I64 TEST
  96. */
  97. print_r("testI64(-34359738368)");
  98. $i64 = $testClient->testI64(-34359738368);
  99. print_r(" = $i64\n");
  100. /**
  101. * DOUBLE TEST
  102. */
  103. print_r("testDouble(-852.234234234)");
  104. $dub = $testClient->testDouble(-852.234234234);
  105. print_r(" = $dub\n");
  106. /**
  107. * STRUCT TEST
  108. */
  109. print_r("testStruct({\"Zero\", 1, -3, -5})");
  110. $out = new \ThriftTest\Xtruct();
  111. $out->string_thing = "Zero";
  112. $out->byte_thing = 1;
  113. $out->i32_thing = -3;
  114. $out->i64_thing = -5;
  115. $in = $testClient->testStruct($out);
  116. print_r(" = {\"".$in->string_thing."\", ".
  117. $in->byte_thing.", ".
  118. $in->i32_thing.", ".
  119. $in->i64_thing."}\n");
  120. /**
  121. * NESTED STRUCT TEST
  122. */
  123. print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
  124. $out2 = new \ThriftTest\Xtruct2();
  125. $out2->byte_thing = 1;
  126. $out2->struct_thing = $out;
  127. $out2->i32_thing = 5;
  128. $in2 = $testClient->testNest($out2);
  129. $in = $in2->struct_thing;
  130. print_r(" = {".$in2->byte_thing.", {\"".
  131. $in->string_thing."\", ".
  132. $in->byte_thing.", ".
  133. $in->i32_thing.", ".
  134. $in->i64_thing."}, ".
  135. $in2->i32_thing."}\n");
  136. /**
  137. * MAP TEST
  138. */
  139. $mapout = array();
  140. for ($i = 0; $i < 5; ++$i) {
  141. $mapout[$i] = $i-10;
  142. }
  143. print_r("testMap({");
  144. $first = true;
  145. foreach ($mapout as $key => $val) {
  146. if ($first) {
  147. $first = false;
  148. } else {
  149. print_r(", ");
  150. }
  151. print_r("$key => $val");
  152. }
  153. print_r("})");
  154. $mapin = $testClient->testMap($mapout);
  155. print_r(" = {");
  156. $first = true;
  157. foreach ($mapin as $key => $val) {
  158. if ($first) {
  159. $first = false;
  160. } else {
  161. print_r(", ");
  162. }
  163. print_r("$key => $val");
  164. }
  165. print_r("}\n");
  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. * 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. * ENUM TEST
  228. */
  229. print_r("testEnum(ONE)");
  230. $ret = $testClient->testEnum(ThriftTest_Numberz::ONE);
  231. print_r(" = $ret\n");
  232. print_r("testEnum(TWO)");
  233. $ret = $testClient->testEnum(ThriftTest_Numberz::TWO);
  234. print_r(" = $ret\n");
  235. print_r("testEnum(THREE)");
  236. $ret = $testClient->testEnum(ThriftTest_Numberz::THREE);
  237. print_r(" = $ret\n");
  238. print_r("testEnum(FIVE)");
  239. $ret = $testClient->testEnum(ThriftTest_Numberz::FIVE);
  240. print_r(" = $ret\n");
  241. print_r("testEnum(EIGHT)");
  242. $ret = $testClient->testEnum(ThriftTest_Numberz::EIGHT);
  243. print_r(" = $ret\n");
  244. /**
  245. * TYPEDEF TEST
  246. */
  247. print_r("testTypedef(309858235082523)");
  248. $uid = $testClient->testTypedef(309858235082523);
  249. print_r(" = $uid\n");
  250. /**
  251. * NESTED MAP TEST
  252. */
  253. print_r("testMapMap(1)");
  254. $mm = $testClient->testMapMap(1);
  255. print_r(" = {");
  256. foreach ($mm as $key => $val) {
  257. print_r("$key => {");
  258. foreach ($val as $k2 => $v2) {
  259. print_r("$k2 => $v2, ");
  260. }
  261. print_r("}, ");
  262. }
  263. print_r("}\n");
  264. /**
  265. * INSANITY TEST
  266. */
  267. $insane = new \ThriftTest\Insanity();
  268. $insane->userMap[ThriftTest_Numberz::FIVE] = 5000;
  269. $truck = new \ThriftTest\Xtruct();
  270. $truck->string_thing = "Truck";
  271. $truck->byte_thing = 8;
  272. $truck->i32_thing = 8;
  273. $truck->i64_thing = 8;
  274. $insane->xtructs []= $truck;
  275. print_r("testInsanity()");
  276. $whoa = $testClient->testInsanity($insane);
  277. print_r(" = {");
  278. foreach ($whoa as $key => $val) {
  279. print_r("$key => {");
  280. foreach ($val as $k2 => $v2) {
  281. print_r("$k2 => {");
  282. $userMap = $v2->userMap;
  283. print_r("{");
  284. if (is_array($userMap)) {
  285. foreach ($userMap as $k3 => $v3) {
  286. print_r("$k3 => $v3, ");
  287. }
  288. }
  289. print_r("}, ");
  290. $xtructs = $v2->xtructs;
  291. print_r("{");
  292. if (is_array($xtructs)) {
  293. foreach ($xtructs as $x) {
  294. print_r("{\"".$x->string_thing."\", ".
  295. $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
  296. }
  297. }
  298. print_r("}");
  299. print_r("}, ");
  300. }
  301. print_r("}, ");
  302. }
  303. print_r("}\n");
  304. /**
  305. * EXCEPTION TEST
  306. */
  307. print_r("testException('Xception')");
  308. try {
  309. $testClient->testException('Xception');
  310. print_r(" void\nFAILURE\n");
  311. } catch (ThriftTest_Xception $x) {
  312. print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
  313. }
  314. /**
  315. * Normal tests done.
  316. */
  317. $stop = microtime(true);
  318. $elp = round(1000*($stop - $start), 0);
  319. print_r("Total time: $elp ms\n");
  320. /**
  321. * Extraneous "I don't trust PHP to pack/unpack integer" tests
  322. */
  323. // Max I32
  324. $num = pow(2, 30) + (pow(2, 30) - 1);
  325. $num2 = $testClient->testI32($num);
  326. if ($num != $num2) {
  327. print "Missed $num = $num2\n";
  328. }
  329. // Min I32
  330. $num = 0 - pow(2, 31);
  331. $num2 = $testClient->testI32($num);
  332. if ($num != $num2) {
  333. print "Missed $num = $num2\n";
  334. }
  335. // Max I64
  336. $num = pow(2, 62) + (pow(2, 62) - 1);
  337. $num2 = $testClient->testI64($num);
  338. if ($num != $num2) {
  339. print "Missed $num = $num2\n";
  340. }
  341. // Min I64
  342. $num = 0 - pow(2, 63);
  343. $num2 = $testClient->testI64($num);
  344. if ($num != $num2) {
  345. print "Missed $num = $num2\n";
  346. }
  347. $transport->close();
  348. return;