blob: a508512a22a459e4849c88e75cd183f6cf92f363 [file] [log] [blame]
Mark Slee6e536442006-06-30 18:28:50 +00001<?php
2
Mark Sleed3d733a2006-09-01 22:19:06 +00003if (!isset($GEN_DIR)) {
4 $GEN_DIR = 'gen-php';
5}
6if (!isset($MODE)) {
7 $MODE = 'normal';
8}
9
Mark Slee018b6992006-09-07 21:31:12 +000010/** Set the Thrift root */
11$GLOBALS['THRIFT_ROOT'] = '../../lib/php/src';
12
Mark Slee6e536442006-06-30 18:28:50 +000013/** Include the Thrift base */
Mark Slee018b6992006-09-07 21:31:12 +000014require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
Mark Slee6e536442006-06-30 18:28:50 +000015
16/** Include the binary protocol */
Mark Sleed3d733a2006-09-01 22:19:06 +000017require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
Mark Slee6e536442006-06-30 18:28:50 +000018
19/** Include the socket layer */
Mark Sleeade2c832006-09-08 03:41:50 +000020require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocketPool.php';
Mark Slee6e536442006-06-30 18:28:50 +000021
22/** Include the socket layer */
Mark Sleed3d733a2006-09-01 22:19:06 +000023require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
Mark Slee6e536442006-06-30 18:28:50 +000024
Mark Slee5b743072007-11-13 04:00:29 +000025echo '==============================='."\n";
26echo ' SAFE TO IGNORE THESE IN TEST'."\n";
27echo '==============================='."\n";
28
Mark Slee6e536442006-06-30 18:28:50 +000029/** Include the generated code */
Mark Sleee129a2d2007-02-21 05:17:48 +000030require_once $GEN_DIR.'/ThriftTest.php';
31require_once $GEN_DIR.'/ThriftTest_types.php';
Mark Slee6e536442006-06-30 18:28:50 +000032
Mark Slee5b743072007-11-13 04:00:29 +000033echo '==============================='."\n";
34echo ' END OF SAFE ERRORS SECTION'."\n";
35echo '==============================='."\n\n";
36
Mark Slee6e536442006-06-30 18:28:50 +000037$host = 'localhost';
38$port = 9090;
39
40if ($argc > 1) {
41 $host = $argv[0];
42}
43
44if ($argc > 2) {
45 $host = $argv[1];
46}
47
Mark Slee1dd819c2006-10-26 04:56:18 +000048$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +000049
Mark Slee6e536442006-06-30 18:28:50 +000050$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +000051$socket = new TSocketPool($hosts, $port);
52$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +000053
Mark Sleed3d733a2006-09-01 22:19:06 +000054if ($MODE == 'inline') {
55 $transport = $socket;
56 $testClient = new ThriftTestClient($transport);
57} else {
58 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
59 $transport = $bufferedSocket;
Mark Slee1dd819c2006-10-26 04:56:18 +000060 $protocol = new TBinaryProtocol($transport);
61 $testClient = new ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000062}
63
64$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +000065
66$start = microtime(true);
67
68/**
69 * VOID TEST
70 */
71print_r("testVoid()");
72$testClient->testVoid();
73print_r(" = void\n");
74
75/**
76 * STRING TEST
77 */
78print_r("testString(\"Test\")");
79$s = $testClient->testString("Test");
80print_r(" = \"$s\"\n");
Mark Slee5b743072007-11-13 04:00:29 +000081
Mark Slee6e536442006-06-30 18:28:50 +000082/**
83 * BYTE TEST
84 */
85print_r("testByte(1)");
86$u8 = $testClient->testByte(1);
87print_r(" = $u8\n");
88
89/**
90 * I32 TEST
91 */
92print_r("testI32(-1)");
93$i32 = $testClient->testI32(-1);
94print_r(" = $i32\n");
95
96/**
97 * I64 TEST
98 */
99print_r("testI64(-34359738368)");
100$i64 = $testClient->testI64(-34359738368);
101print_r(" = $i64\n");
102
103/**
Mark Sleec98d0502006-09-06 02:42:25 +0000104 * DOUBLE TEST
105 */
106print_r("testDouble(-852.234234234)");
107$dub = $testClient->testDouble(-852.234234234);
108print_r(" = $dub\n");
109
110/**
Mark Slee6e536442006-06-30 18:28:50 +0000111 * STRUCT TEST
112 */
113print_r("testStruct({\"Zero\", 1, -3, -5})");
114$out = new 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);
120print_r(" = {\"".$in->string_thing."\", ".
121 $in->byte_thing.", ".
122 $in->i32_thing.", ".
123 $in->i64_thing."}\n");
124
125/**
126 * NESTED STRUCT TEST
127 */
128print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
129$out2 = new Xtruct2();
130$out2->byte_thing = 1;
131$out2->struct_thing = $out;
132$out2->i32_thing = 5;
133$in2 = $testClient->testNest($out2);
134$in = $in2->struct_thing;
135print_r(" = {".$in2->byte_thing.", {\"".
136 $in->string_thing."\", ".
137 $in->byte_thing.", ".
138 $in->i32_thing.", ".
139 $in->i64_thing."}, ".
140 $in2->i32_thing."}\n");
141
142/**
143 * MAP TEST
144 */
145$mapout = array();
146for ($i = 0; $i < 5; ++$i) {
147 $mapout[$i] = $i-10;
148}
149print_r("testMap({");
150$first = true;
151foreach ($mapout as $key => $val) {
152 if ($first) {
153 $first = false;
154 } else {
155 print_r(", ");
156 }
157 print_r("$key => $val");
158}
159print_r("})");
160
161$mapin = $testClient->testMap($mapout);
162print_r(" = {");
163$first = true;
164foreach ($mapin as $key => $val) {
165 if ($first) {
166 $first = false;
167 } else {
168 print_r(", ");
169 }
170 print_r("$key => $val");
171}
172print_r("}\n");
173
174/**
175 * SET TEST
176 */
177$setout = array();;
178for ($i = -2; $i < 3; ++$i) {
179 $setout []= $i;
180}
181print_r("testSet({");
182$first = true;
183foreach ($setout as $val) {
184 if ($first) {
185 $first = false;
186 } else {
187 print_r(", ");
188 }
189 print_r($val);
190}
191print_r("})");
192$setin = $testClient->testSet($setout);
193print_r(" = {");
194$first = true;
195foreach ($setin as $val) {
196 if ($first) {
197 $first = false;
198 } else {
199 print_r(", ");
200 }
201 print_r($val);
202}
203print_r("}\n");
204
205/**
206 * LIST TEST
207 */
208$listout = array();
209for ($i = -2; $i < 3; ++$i) {
210 $listout []= $i;
211}
212print_r("testList({");
213$first = true;
214foreach ($listout as $val) {
215 if ($first) {
216 $first = false;
217 } else {
218 print_r(", ");
219 }
220 print_r($val);
221}
222print_r("})");
223$listin = $testClient->testList($listout);
224print_r(" = {");
225$first = true;
226foreach ($listin as $val) {
227 if ($first) {
228 $first = false;
229 } else {
230 print_r(", ");
231 }
232 print_r($val);
233}
234print_r("}\n");
235
236/**
237 * ENUM TEST
238 */
239print_r("testEnum(ONE)");
240$ret = $testClient->testEnum(Numberz::ONE);
241print_r(" = $ret\n");
242
243print_r("testEnum(TWO)");
244$ret = $testClient->testEnum(Numberz::TWO);
245print_r(" = $ret\n");
246
247print_r("testEnum(THREE)");
248$ret = $testClient->testEnum(Numberz::THREE);
249print_r(" = $ret\n");
250
251print_r("testEnum(FIVE)");
252$ret = $testClient->testEnum(Numberz::FIVE);
253print_r(" = $ret\n");
254
255print_r("testEnum(EIGHT)");
256$ret = $testClient->testEnum(Numberz::EIGHT);
257print_r(" = $ret\n");
258
259/**
260 * TYPEDEF TEST
261 */
262print_r("testTypedef(309858235082523)");
263$uid = $testClient->testTypedef(309858235082523);
264print_r(" = $uid\n");
265
266/**
267 * NESTED MAP TEST
268 */
269print_r("testMapMap(1)");
270$mm = $testClient->testMapMap(1);
271print_r(" = {");
272foreach ($mm as $key => $val) {
273 print_r("$key => {");
274 foreach ($val as $k2 => $v2) {
275 print_r("$k2 => $v2, ");
276 }
277 print_r("}, ");
278}
279print_r("}\n");
280
281/**
282 * INSANITY TEST
283 */
284$insane = new Insanity();
285$insane->userMap[Numberz::FIVE] = 5000;
286$truck = new Xtruct();
287$truck->string_thing = "Truck";
288$truck->byte_thing = 8;
289$truck->i32_thing = 8;
290$truck->i64_thing = 8;
291$insane->xtructs []= $truck;
292print_r("testInsanity()");
293$whoa = $testClient->testInsanity($insane);
294print_r(" = {");
295foreach ($whoa as $key => $val) {
296 print_r("$key => {");
297 foreach ($val as $k2 => $v2) {
298 print_r("$k2 => {");
299 $userMap = $v2->userMap;
300 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000301 if (is_array($usermap)) {
302 foreach ($userMap as $k3 => $v3) {
303 print_r("$k3 => $v3, ");
304 }
Mark Slee6e536442006-06-30 18:28:50 +0000305 }
306 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000307
Mark Slee6e536442006-06-30 18:28:50 +0000308 $xtructs = $v2->xtructs;
309 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000310 if (is_array($xtructs)) {
311 foreach ($xtructs as $x) {
312 print_r("{\"".$x->string_thing."\", ".
313 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
314 }
Mark Slee6e536442006-06-30 18:28:50 +0000315 }
316 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000317
Mark Slee6e536442006-06-30 18:28:50 +0000318 print_r("}, ");
319 }
320 print_r("}, ");
321}
322print_r("}\n");
323
Mark Sleed3d733a2006-09-01 22:19:06 +0000324/**
325 * EXCEPTION TEST
326 */
327print_r("testException('Xception')");
328try {
329 $testClient->testException('Xception');
330 print_r(" void\nFAILURE\n");
331} catch (Xception $x) {
332 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
333}
334
Mark Slee6e536442006-06-30 18:28:50 +0000335
336/**
337 * Normal tests done.
338 */
339
340$stop = microtime(true);
341$elp = round(1000*($stop - $start), 0);
342print_r("Total time: $elp ms\n");
343
344/**
345 * Extraneous "I don't trust PHP to pack/unpack integer" tests
346 */
347
348// Max I32
349$num = pow(2, 30) + (pow(2, 30) - 1);
350$num2 = $testClient->testI32($num);
351if ($num != $num2) {
352 print "Missed $num = $num2\n";
353}
354
355// Min I32
356$num = 0 - pow(2, 31);
357$num2 = $testClient->testI32($num);
358if ($num != $num2) {
359 print "Missed $num = $num2\n";
360}
361
362// Max I64
363$num = pow(2, 62) + (pow(2, 62) - 1);
364$num2 = $testClient->testI64($num);
365if ($num != $num2) {
366 print "Missed $num = $num2\n";
367}
368
369// Min I64
370$num = 0 - pow(2, 63);
371$num2 = $testClient->testI64($num);
372if ($num != $num2) {
373 print "Missed $num = $num2\n";
374}
375
Mark Sleed3d733a2006-09-01 22:19:06 +0000376$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000377return;
378
379?>