blob: ea17435aea5a5a4ecb7105f0a93926ea78051c2f [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 */
Mark Slee6e536442006-06-30 18:28:50 +000041/** Include the binary protocol */
Roger Meier21c0a852012-09-05 19:47:14 +000042use Thrift\Protocol\TBinaryProtocol;
Mark Slee6e536442006-06-30 18:28:50 +000043
44/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000045use Thrift\Transport\TSocket;
46use Thrift\Transport\TSocketPool;
Mark Slee6e536442006-06-30 18:28:50 +000047
48/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000049use Thrift\Transport\TFramedTransport;
50use Thrift\Transport\TBufferedTransport;
Mark Slee5b743072007-11-13 04:00:29 +000051
Mark Slee6e536442006-06-30 18:28:50 +000052$host = 'localhost';
53$port = 9090;
54
55if ($argc > 1) {
56 $host = $argv[0];
57}
58
59if ($argc > 2) {
60 $host = $argv[1];
61}
62
Mark Slee1dd819c2006-10-26 04:56:18 +000063$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +000064
Mark Slee6e536442006-06-30 18:28:50 +000065$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +000066$socket = new TSocketPool($hosts, $port);
67$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +000068
Mark Sleed3d733a2006-09-01 22:19:06 +000069if ($MODE == 'inline') {
70 $transport = $socket;
Roger Meier21c0a852012-09-05 19:47:14 +000071 $testClient = new \ThriftTest\ThriftTestClient($transport);
Bryan Duxburya971fb02011-03-04 00:49:40 +000072} else if ($MODE == 'framed') {
73 $framedSocket = new TFramedTransport($socket);
74 $transport = $framedSocket;
75 $protocol = new TBinaryProtocol($transport);
Roger Meier21c0a852012-09-05 19:47:14 +000076 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000077} else {
78 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
79 $transport = $bufferedSocket;
Mark Slee1dd819c2006-10-26 04:56:18 +000080 $protocol = new TBinaryProtocol($transport);
Roger Meier21c0a852012-09-05 19:47:14 +000081 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000082}
83
84$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +000085
86$start = microtime(true);
87
88/**
89 * VOID TEST
90 */
91print_r("testVoid()");
92$testClient->testVoid();
93print_r(" = void\n");
94
95/**
96 * STRING TEST
97 */
98print_r("testString(\"Test\")");
99$s = $testClient->testString("Test");
100print_r(" = \"$s\"\n");
Mark Slee5b743072007-11-13 04:00:29 +0000101
Mark Slee6e536442006-06-30 18:28:50 +0000102/**
103 * BYTE TEST
104 */
105print_r("testByte(1)");
106$u8 = $testClient->testByte(1);
107print_r(" = $u8\n");
108
109/**
110 * I32 TEST
111 */
112print_r("testI32(-1)");
113$i32 = $testClient->testI32(-1);
114print_r(" = $i32\n");
115
116/**
117 * I64 TEST
118 */
119print_r("testI64(-34359738368)");
120$i64 = $testClient->testI64(-34359738368);
121print_r(" = $i64\n");
122
123/**
Mark Sleec98d0502006-09-06 02:42:25 +0000124 * DOUBLE TEST
125 */
126print_r("testDouble(-852.234234234)");
127$dub = $testClient->testDouble(-852.234234234);
128print_r(" = $dub\n");
129
130/**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100131 * BINARY TEST -- TODO
132 */
133
134/**
Mark Slee6e536442006-06-30 18:28:50 +0000135 * STRUCT TEST
136 */
137print_r("testStruct({\"Zero\", 1, -3, -5})");
Roger Meier21c0a852012-09-05 19:47:14 +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}");
Roger Meier21c0a852012-09-05 19:47:14 +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)");
Roger Meier87afaac2013-01-06 20:10:42 +0100264$ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000265print_r(" = $ret\n");
266
267print_r("testEnum(TWO)");
Roger Meier87afaac2013-01-06 20:10:42 +0100268$ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000269print_r(" = $ret\n");
270
271print_r("testEnum(THREE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100272$ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000273print_r(" = $ret\n");
274
275print_r("testEnum(FIVE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100276$ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000277print_r(" = $ret\n");
278
279print_r("testEnum(EIGHT)");
Roger Meier87afaac2013-01-06 20:10:42 +0100280$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 */
Roger Meier21c0a852012-09-05 19:47:14 +0000308$insane = new \ThriftTest\Insanity();
Roger Meier87afaac2013-01-06 20:10:42 +0100309$insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
Roger Meier21c0a852012-09-05 19:47:14 +0000310$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");
Roger Meier87afaac2013-01-06 20:10:42 +0100355} 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