blob: 009137b3766eb64a250a11b5a19a834d2465c249 [file] [log] [blame]
Bryan Duxburya971fb02011-03-04 00:49:40 +00001<?php
David Reissea2cba82009-03-30 21:35:00 +00002/*
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
Mark Sleed3d733a2006-09-01 22:19:06 +000021if (!isset($GEN_DIR)) {
22 $GEN_DIR = 'gen-php';
23}
24if (!isset($MODE)) {
25 $MODE = 'normal';
26}
27
Mark Slee018b6992006-09-07 21:31:12 +000028/** Set the Thrift root */
29$GLOBALS['THRIFT_ROOT'] = '../../lib/php/src';
30
Mark Slee6e536442006-06-30 18:28:50 +000031/** Include the Thrift base */
Mark Slee018b6992006-09-07 21:31:12 +000032require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
Mark Slee6e536442006-06-30 18:28:50 +000033
34/** Include the binary protocol */
Mark Sleed3d733a2006-09-01 22:19:06 +000035require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
Mark Slee6e536442006-06-30 18:28:50 +000036
37/** Include the socket layer */
Mark Sleeade2c832006-09-08 03:41:50 +000038require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocketPool.php';
Mark Slee6e536442006-06-30 18:28:50 +000039
40/** Include the socket layer */
Bryan Duxburya971fb02011-03-04 00:49:40 +000041require_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
Mark Sleed3d733a2006-09-01 22:19:06 +000042require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
Mark Slee6e536442006-06-30 18:28:50 +000043
Mark Slee5b743072007-11-13 04:00:29 +000044echo '==============================='."\n";
45echo ' SAFE TO IGNORE THESE IN TEST'."\n";
46echo '==============================='."\n";
47
Mark Slee6e536442006-06-30 18:28:50 +000048/** Include the generated code */
David Reisseb032222009-12-09 19:29:59 +000049require_once $GEN_DIR.'/ThriftTest/ThriftTest.php';
50require_once $GEN_DIR.'/ThriftTest/ThriftTest_types.php';
Mark Slee6e536442006-06-30 18:28:50 +000051
Mark Slee5b743072007-11-13 04:00:29 +000052echo '==============================='."\n";
53echo ' END OF SAFE ERRORS SECTION'."\n";
54echo '==============================='."\n\n";
55
Mark Slee6e536442006-06-30 18:28:50 +000056$host = 'localhost';
57$port = 9090;
58
59if ($argc > 1) {
60 $host = $argv[0];
61}
62
63if ($argc > 2) {
64 $host = $argv[1];
65}
66
Mark Slee1dd819c2006-10-26 04:56:18 +000067$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +000068
Mark Slee6e536442006-06-30 18:28:50 +000069$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +000070$socket = new TSocketPool($hosts, $port);
71$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +000072
Mark Sleed3d733a2006-09-01 22:19:06 +000073if ($MODE == 'inline') {
74 $transport = $socket;
Roger Meier3ac6d872012-01-20 15:31:45 +000075 $testClient = new ThriftTest_ThriftTestClient($transport);
Bryan Duxburya971fb02011-03-04 00:49:40 +000076} else if ($MODE == 'framed') {
77 $framedSocket = new TFramedTransport($socket);
78 $transport = $framedSocket;
79 $protocol = new TBinaryProtocol($transport);
Roger Meier3ac6d872012-01-20 15:31:45 +000080 $testClient = new ThriftTest_ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000081} else {
82 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
83 $transport = $bufferedSocket;
Mark Slee1dd819c2006-10-26 04:56:18 +000084 $protocol = new TBinaryProtocol($transport);
Roger Meier3ac6d872012-01-20 15:31:45 +000085 $testClient = new ThriftTest_ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000086}
87
88$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +000089
90$start = microtime(true);
91
92/**
93 * VOID TEST
94 */
95print_r("testVoid()");
96$testClient->testVoid();
97print_r(" = void\n");
98
99/**
100 * STRING TEST
101 */
102print_r("testString(\"Test\")");
103$s = $testClient->testString("Test");
104print_r(" = \"$s\"\n");
Mark Slee5b743072007-11-13 04:00:29 +0000105
Mark Slee6e536442006-06-30 18:28:50 +0000106/**
107 * BYTE TEST
108 */
109print_r("testByte(1)");
110$u8 = $testClient->testByte(1);
111print_r(" = $u8\n");
112
113/**
114 * I32 TEST
115 */
116print_r("testI32(-1)");
117$i32 = $testClient->testI32(-1);
118print_r(" = $i32\n");
119
120/**
121 * I64 TEST
122 */
123print_r("testI64(-34359738368)");
124$i64 = $testClient->testI64(-34359738368);
125print_r(" = $i64\n");
126
127/**
Mark Sleec98d0502006-09-06 02:42:25 +0000128 * DOUBLE TEST
129 */
130print_r("testDouble(-852.234234234)");
131$dub = $testClient->testDouble(-852.234234234);
132print_r(" = $dub\n");
133
134/**
Mark Slee6e536442006-06-30 18:28:50 +0000135 * STRUCT TEST
136 */
137print_r("testStruct({\"Zero\", 1, -3, -5})");
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000138$out = new ThriftTest_Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000139$out->string_thing = "Zero";
140$out->byte_thing = 1;
141$out->i32_thing = -3;
142$out->i64_thing = -5;
143$in = $testClient->testStruct($out);
144print_r(" = {\"".$in->string_thing."\", ".
145 $in->byte_thing.", ".
146 $in->i32_thing.", ".
147 $in->i64_thing."}\n");
148
149/**
150 * NESTED STRUCT TEST
151 */
152print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000153$out2 = new ThriftTest_Xtruct2();
Mark Slee6e536442006-06-30 18:28:50 +0000154$out2->byte_thing = 1;
155$out2->struct_thing = $out;
156$out2->i32_thing = 5;
157$in2 = $testClient->testNest($out2);
158$in = $in2->struct_thing;
159print_r(" = {".$in2->byte_thing.", {\"".
160 $in->string_thing."\", ".
161 $in->byte_thing.", ".
162 $in->i32_thing.", ".
163 $in->i64_thing."}, ".
164 $in2->i32_thing."}\n");
165
166/**
167 * MAP TEST
168 */
169$mapout = array();
170for ($i = 0; $i < 5; ++$i) {
171 $mapout[$i] = $i-10;
172}
173print_r("testMap({");
174$first = true;
175foreach ($mapout as $key => $val) {
176 if ($first) {
177 $first = false;
178 } else {
179 print_r(", ");
180 }
181 print_r("$key => $val");
182}
183print_r("})");
184
185$mapin = $testClient->testMap($mapout);
186print_r(" = {");
187$first = true;
188foreach ($mapin as $key => $val) {
189 if ($first) {
190 $first = false;
191 } else {
192 print_r(", ");
193 }
194 print_r("$key => $val");
195}
196print_r("}\n");
197
198/**
199 * SET TEST
200 */
201$setout = array();;
202for ($i = -2; $i < 3; ++$i) {
203 $setout []= $i;
204}
205print_r("testSet({");
206$first = true;
207foreach ($setout as $val) {
208 if ($first) {
209 $first = false;
210 } else {
211 print_r(", ");
212 }
213 print_r($val);
214}
215print_r("})");
216$setin = $testClient->testSet($setout);
217print_r(" = {");
218$first = true;
219foreach ($setin as $val) {
220 if ($first) {
221 $first = false;
222 } else {
223 print_r(", ");
224 }
225 print_r($val);
226}
227print_r("}\n");
228
229/**
230 * LIST TEST
231 */
232$listout = array();
233for ($i = -2; $i < 3; ++$i) {
234 $listout []= $i;
235}
236print_r("testList({");
237$first = true;
238foreach ($listout as $val) {
239 if ($first) {
240 $first = false;
241 } else {
242 print_r(", ");
243 }
244 print_r($val);
245}
246print_r("})");
247$listin = $testClient->testList($listout);
248print_r(" = {");
249$first = true;
250foreach ($listin as $val) {
251 if ($first) {
252 $first = false;
253 } else {
254 print_r(", ");
255 }
256 print_r($val);
257}
258print_r("}\n");
259
260/**
261 * ENUM TEST
262 */
263print_r("testEnum(ONE)");
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000264$ret = $testClient->testEnum(ThriftTest_Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000265print_r(" = $ret\n");
266
267print_r("testEnum(TWO)");
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000268$ret = $testClient->testEnum(ThriftTest_Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000269print_r(" = $ret\n");
270
271print_r("testEnum(THREE)");
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000272$ret = $testClient->testEnum(ThriftTest_Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000273print_r(" = $ret\n");
274
275print_r("testEnum(FIVE)");
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000276$ret = $testClient->testEnum(ThriftTest_Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000277print_r(" = $ret\n");
278
279print_r("testEnum(EIGHT)");
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000280$ret = $testClient->testEnum(ThriftTest_Numberz::EIGHT);
Mark Slee6e536442006-06-30 18:28:50 +0000281print_r(" = $ret\n");
282
283/**
284 * TYPEDEF TEST
285 */
286print_r("testTypedef(309858235082523)");
287$uid = $testClient->testTypedef(309858235082523);
288print_r(" = $uid\n");
289
290/**
291 * NESTED MAP TEST
292 */
293print_r("testMapMap(1)");
294$mm = $testClient->testMapMap(1);
295print_r(" = {");
296foreach ($mm as $key => $val) {
297 print_r("$key => {");
298 foreach ($val as $k2 => $v2) {
299 print_r("$k2 => $v2, ");
300 }
301 print_r("}, ");
302}
303print_r("}\n");
304
305/**
306 * INSANITY TEST
307 */
Bryan Duxbury1d373bc2011-03-02 18:13:30 +0000308$insane = new ThriftTest_Insanity();
309$insane->userMap[ThriftTest_Numberz::FIVE] = 5000;
310$truck = new ThriftTest_Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000311$truck->string_thing = "Truck";
312$truck->byte_thing = 8;
313$truck->i32_thing = 8;
314$truck->i64_thing = 8;
315$insane->xtructs []= $truck;
316print_r("testInsanity()");
317$whoa = $testClient->testInsanity($insane);
318print_r(" = {");
319foreach ($whoa as $key => $val) {
320 print_r("$key => {");
321 foreach ($val as $k2 => $v2) {
322 print_r("$k2 => {");
323 $userMap = $v2->userMap;
324 print_r("{");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000325 if (is_array($userMap)) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000326 foreach ($userMap as $k3 => $v3) {
327 print_r("$k3 => $v3, ");
328 }
Mark Slee6e536442006-06-30 18:28:50 +0000329 }
330 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000331
Mark Slee6e536442006-06-30 18:28:50 +0000332 $xtructs = $v2->xtructs;
333 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000334 if (is_array($xtructs)) {
335 foreach ($xtructs as $x) {
336 print_r("{\"".$x->string_thing."\", ".
337 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
338 }
Mark Slee6e536442006-06-30 18:28:50 +0000339 }
340 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000341
Mark Slee6e536442006-06-30 18:28:50 +0000342 print_r("}, ");
343 }
344 print_r("}, ");
345}
346print_r("}\n");
347
Mark Sleed3d733a2006-09-01 22:19:06 +0000348/**
349 * EXCEPTION TEST
350 */
351print_r("testException('Xception')");
352try {
353 $testClient->testException('Xception');
354 print_r(" void\nFAILURE\n");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000355} catch (ThriftTest_Xception $x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000356 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
357}
358
Mark Slee6e536442006-06-30 18:28:50 +0000359
360/**
361 * Normal tests done.
362 */
363
364$stop = microtime(true);
365$elp = round(1000*($stop - $start), 0);
366print_r("Total time: $elp ms\n");
367
368/**
369 * Extraneous "I don't trust PHP to pack/unpack integer" tests
370 */
371
372// Max I32
373$num = pow(2, 30) + (pow(2, 30) - 1);
374$num2 = $testClient->testI32($num);
375if ($num != $num2) {
376 print "Missed $num = $num2\n";
377}
378
379// Min I32
380$num = 0 - pow(2, 31);
381$num2 = $testClient->testI32($num);
382if ($num != $num2) {
383 print "Missed $num = $num2\n";
384}
385
386// Max I64
387$num = pow(2, 62) + (pow(2, 62) - 1);
388$num2 = $testClient->testI64($num);
389if ($num != $num2) {
390 print "Missed $num = $num2\n";
391}
392
393// Min I64
394$num = 0 - pow(2, 63);
395$num2 = $testClient->testI64($num);
396if ($num != $num2) {
397 print "Missed $num = $num2\n";
398}
399
Mark Sleed3d733a2006-09-01 22:19:06 +0000400$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000401return;
402