PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/php/TestClient.php

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