blob: ce57dc08ebc02f9cf01c9679ed55e39b018c8428 [file] [log] [blame]
Bryan Duxburya971fb02011-03-04 00:49:40 +00001<?php
Roger Meier21c0a852012-09-05 19:47:14 +00002
3namespace test\php;
4
5require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
6
7use Thrift\ClassLoader\ThriftClassLoader;
8
9if (!isset($GEN_DIR)) {
10 $GEN_DIR = 'gen-php';
11}
12if (!isset($MODE)) {
13 $MODE = 'normal';
14}
15
16$loader = new ThriftClassLoader();
17$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib');
18$loader->registerDefinition('ThriftTest', $GEN_DIR);
19$loader->register();
20
David Reissea2cba82009-03-30 21:35:00 +000021/*
22 * Licensed to the Apache Software Foundation (ASF) under one
23 * or more contributor license agreements. See the NOTICE file
24 * distributed with this work for additional information
25 * regarding copyright ownership. The ASF licenses this file
26 * to you under the Apache License, Version 2.0 (the
27 * "License"); you may not use this file except in compliance
28 * with the License. You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing,
33 * software distributed under the License is distributed on an
34 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35 * KIND, either express or implied. See the License for the
36 * specific language governing permissions and limitations
37 * under the License.
38 */
39
Mark Slee6e536442006-06-30 18:28:50 +000040/** Include the Thrift base */
Jim King5903d672015-06-29 18:12:48 -040041/** Include the protocols */
Roger Meier21c0a852012-09-05 19:47:14 +000042use Thrift\Protocol\TBinaryProtocol;
Jim King5903d672015-06-29 18:12:48 -040043use Thrift\Protocol\TCompactProtocol;
44use Thrift\Protocol\TJSONProtocol;
Mark Slee6e536442006-06-30 18:28:50 +000045
46/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000047use Thrift\Transport\TSocket;
48use Thrift\Transport\TSocketPool;
Mark Slee6e536442006-06-30 18:28:50 +000049
50/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000051use Thrift\Transport\TFramedTransport;
52use Thrift\Transport\TBufferedTransport;
Mark Slee5b743072007-11-13 04:00:29 +000053
Jim King5903d672015-06-29 18:12:48 -040054function makeProtocol($transport, $PROTO)
55{
56 if ($PROTO == 'binary') {
57 return new TBinaryProtocol($transport);
58 } else if ($PROTO == 'compact') {
59 return new TCompactProtocol($transport);
60 } else if ($PROTO == 'json') {
61 return new TJSONProtocol($transport);
62 }
63
64 die ("--protocol must be one of {binary|compact|json}");
65}
66
Mark Slee6e536442006-06-30 18:28:50 +000067$host = 'localhost';
68$port = 9090;
69
70if ($argc > 1) {
71 $host = $argv[0];
72}
73
74if ($argc > 2) {
75 $host = $argv[1];
76}
77
Roger Meier41ad4342015-03-24 22:30:40 +010078foreach ($argv as $arg) {
79 if (substr($arg, 0, 7) == '--port=') {
80 $port = substr($arg, 7);
Jim King5903d672015-06-29 18:12:48 -040081 } else if (substr($arg, 0, 12) == '--transport=') {
82 $MODE = substr($arg, 12);
83 } else if (substr($arg, 0, 11) == '--protocol=') {
84 $PROTO = substr($arg, 11);
85 }
Roger Meier41ad4342015-03-24 22:30:40 +010086}
87
Mark Slee1dd819c2006-10-26 04:56:18 +000088$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +000089
Mark Slee6e536442006-06-30 18:28:50 +000090$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +000091$socket = new TSocketPool($hosts, $port);
92$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +000093
Mark Sleed3d733a2006-09-01 22:19:06 +000094if ($MODE == 'inline') {
95 $transport = $socket;
Roger Meier21c0a852012-09-05 19:47:14 +000096 $testClient = new \ThriftTest\ThriftTestClient($transport);
Bryan Duxburya971fb02011-03-04 00:49:40 +000097} else if ($MODE == 'framed') {
98 $framedSocket = new TFramedTransport($socket);
99 $transport = $framedSocket;
Jim King5903d672015-06-29 18:12:48 -0400100 $protocol = makeProtocol($transport, $PROTO);
Roger Meier21c0a852012-09-05 19:47:14 +0000101 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +0000102} else {
103 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
104 $transport = $bufferedSocket;
Jim King5903d672015-06-29 18:12:48 -0400105 $protocol = makeProtocol($transport, $PROTO);
Roger Meier21c0a852012-09-05 19:47:14 +0000106 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +0000107}
108
109$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +0000110
111$start = microtime(true);
112
113/**
114 * VOID TEST
115 */
116print_r("testVoid()");
117$testClient->testVoid();
118print_r(" = void\n");
119
120/**
121 * STRING TEST
122 */
123print_r("testString(\"Test\")");
124$s = $testClient->testString("Test");
125print_r(" = \"$s\"\n");
Mark Slee5b743072007-11-13 04:00:29 +0000126
Mark Slee6e536442006-06-30 18:28:50 +0000127/**
128 * BYTE TEST
129 */
130print_r("testByte(1)");
131$u8 = $testClient->testByte(1);
132print_r(" = $u8\n");
133
134/**
135 * I32 TEST
136 */
137print_r("testI32(-1)");
138$i32 = $testClient->testI32(-1);
139print_r(" = $i32\n");
140
141/**
142 * I64 TEST
143 */
144print_r("testI64(-34359738368)");
145$i64 = $testClient->testI64(-34359738368);
146print_r(" = $i64\n");
147
148/**
Mark Sleec98d0502006-09-06 02:42:25 +0000149 * DOUBLE TEST
150 */
151print_r("testDouble(-852.234234234)");
152$dub = $testClient->testDouble(-852.234234234);
153print_r(" = $dub\n");
154
155/**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100156 * BINARY TEST -- TODO
157 */
158
159/**
Mark Slee6e536442006-06-30 18:28:50 +0000160 * STRUCT TEST
161 */
162print_r("testStruct({\"Zero\", 1, -3, -5})");
Roger Meier21c0a852012-09-05 19:47:14 +0000163$out = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000164$out->string_thing = "Zero";
165$out->byte_thing = 1;
166$out->i32_thing = -3;
167$out->i64_thing = -5;
168$in = $testClient->testStruct($out);
169print_r(" = {\"".$in->string_thing."\", ".
170 $in->byte_thing.", ".
171 $in->i32_thing.", ".
172 $in->i64_thing."}\n");
173
174/**
175 * NESTED STRUCT TEST
176 */
177print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Roger Meier21c0a852012-09-05 19:47:14 +0000178$out2 = new \ThriftTest\Xtruct2();
Mark Slee6e536442006-06-30 18:28:50 +0000179$out2->byte_thing = 1;
180$out2->struct_thing = $out;
181$out2->i32_thing = 5;
182$in2 = $testClient->testNest($out2);
183$in = $in2->struct_thing;
184print_r(" = {".$in2->byte_thing.", {\"".
185 $in->string_thing."\", ".
186 $in->byte_thing.", ".
187 $in->i32_thing.", ".
188 $in->i64_thing."}, ".
189 $in2->i32_thing."}\n");
190
191/**
192 * MAP TEST
193 */
194$mapout = array();
195for ($i = 0; $i < 5; ++$i) {
196 $mapout[$i] = $i-10;
197}
198print_r("testMap({");
199$first = true;
200foreach ($mapout as $key => $val) {
201 if ($first) {
202 $first = false;
203 } else {
204 print_r(", ");
205 }
206 print_r("$key => $val");
207}
208print_r("})");
209
210$mapin = $testClient->testMap($mapout);
211print_r(" = {");
212$first = true;
213foreach ($mapin as $key => $val) {
214 if ($first) {
215 $first = false;
216 } else {
217 print_r(", ");
218 }
219 print_r("$key => $val");
220}
221print_r("}\n");
222
223/**
224 * SET TEST
225 */
226$setout = array();;
227for ($i = -2; $i < 3; ++$i) {
228 $setout []= $i;
229}
230print_r("testSet({");
231$first = true;
232foreach ($setout as $val) {
233 if ($first) {
234 $first = false;
235 } else {
236 print_r(", ");
237 }
238 print_r($val);
239}
240print_r("})");
241$setin = $testClient->testSet($setout);
242print_r(" = {");
243$first = true;
244foreach ($setin as $val) {
245 if ($first) {
246 $first = false;
247 } else {
248 print_r(", ");
249 }
250 print_r($val);
251}
252print_r("}\n");
253
254/**
255 * LIST TEST
256 */
257$listout = array();
258for ($i = -2; $i < 3; ++$i) {
259 $listout []= $i;
260}
261print_r("testList({");
262$first = true;
263foreach ($listout as $val) {
264 if ($first) {
265 $first = false;
266 } else {
267 print_r(", ");
268 }
269 print_r($val);
270}
271print_r("})");
272$listin = $testClient->testList($listout);
273print_r(" = {");
274$first = true;
275foreach ($listin as $val) {
276 if ($first) {
277 $first = false;
278 } else {
279 print_r(", ");
280 }
281 print_r($val);
282}
283print_r("}\n");
284
285/**
286 * ENUM TEST
287 */
288print_r("testEnum(ONE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100289$ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000290print_r(" = $ret\n");
291
292print_r("testEnum(TWO)");
Roger Meier87afaac2013-01-06 20:10:42 +0100293$ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000294print_r(" = $ret\n");
295
296print_r("testEnum(THREE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100297$ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000298print_r(" = $ret\n");
299
300print_r("testEnum(FIVE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100301$ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000302print_r(" = $ret\n");
303
304print_r("testEnum(EIGHT)");
Roger Meier87afaac2013-01-06 20:10:42 +0100305$ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT);
Mark Slee6e536442006-06-30 18:28:50 +0000306print_r(" = $ret\n");
307
308/**
309 * TYPEDEF TEST
310 */
311print_r("testTypedef(309858235082523)");
312$uid = $testClient->testTypedef(309858235082523);
313print_r(" = $uid\n");
314
315/**
316 * NESTED MAP TEST
317 */
318print_r("testMapMap(1)");
319$mm = $testClient->testMapMap(1);
320print_r(" = {");
321foreach ($mm as $key => $val) {
322 print_r("$key => {");
323 foreach ($val as $k2 => $v2) {
324 print_r("$k2 => $v2, ");
325 }
326 print_r("}, ");
327}
328print_r("}\n");
329
330/**
331 * INSANITY TEST
332 */
Roger Meier21c0a852012-09-05 19:47:14 +0000333$insane = new \ThriftTest\Insanity();
Roger Meier87afaac2013-01-06 20:10:42 +0100334$insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
Roger Meier21c0a852012-09-05 19:47:14 +0000335$truck = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000336$truck->string_thing = "Truck";
337$truck->byte_thing = 8;
338$truck->i32_thing = 8;
339$truck->i64_thing = 8;
340$insane->xtructs []= $truck;
341print_r("testInsanity()");
342$whoa = $testClient->testInsanity($insane);
343print_r(" = {");
344foreach ($whoa as $key => $val) {
345 print_r("$key => {");
346 foreach ($val as $k2 => $v2) {
347 print_r("$k2 => {");
348 $userMap = $v2->userMap;
349 print_r("{");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000350 if (is_array($userMap)) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000351 foreach ($userMap as $k3 => $v3) {
352 print_r("$k3 => $v3, ");
353 }
Mark Slee6e536442006-06-30 18:28:50 +0000354 }
355 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000356
Mark Slee6e536442006-06-30 18:28:50 +0000357 $xtructs = $v2->xtructs;
358 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000359 if (is_array($xtructs)) {
360 foreach ($xtructs as $x) {
361 print_r("{\"".$x->string_thing."\", ".
362 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
363 }
Mark Slee6e536442006-06-30 18:28:50 +0000364 }
365 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000366
Mark Slee6e536442006-06-30 18:28:50 +0000367 print_r("}, ");
368 }
369 print_r("}, ");
370}
371print_r("}\n");
372
Mark Sleed3d733a2006-09-01 22:19:06 +0000373/**
374 * EXCEPTION TEST
375 */
376print_r("testException('Xception')");
377try {
378 $testClient->testException('Xception');
379 print_r(" void\nFAILURE\n");
Roger Meier87afaac2013-01-06 20:10:42 +0100380} catch (\ThriftTest\Xception $x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000381 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
382}
383
Mark Slee6e536442006-06-30 18:28:50 +0000384
385/**
386 * Normal tests done.
387 */
388
389$stop = microtime(true);
390$elp = round(1000*($stop - $start), 0);
391print_r("Total time: $elp ms\n");
392
393/**
394 * Extraneous "I don't trust PHP to pack/unpack integer" tests
395 */
396
397// Max I32
398$num = pow(2, 30) + (pow(2, 30) - 1);
399$num2 = $testClient->testI32($num);
400if ($num != $num2) {
401 print "Missed $num = $num2\n";
402}
403
404// Min I32
405$num = 0 - pow(2, 31);
406$num2 = $testClient->testI32($num);
407if ($num != $num2) {
408 print "Missed $num = $num2\n";
409}
410
411// Max I64
412$num = pow(2, 62) + (pow(2, 62) - 1);
413$num2 = $testClient->testI64($num);
414if ($num != $num2) {
415 print "Missed $num = $num2\n";
416}
417
418// Min I64
419$num = 0 - pow(2, 63);
420$num2 = $testClient->testI64($num);
421if ($num != $num2) {
422 print "Missed $num = $num2\n";
423}
424
Mark Sleed3d733a2006-09-01 22:19:06 +0000425$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000426return;
427