blob: 2443ee058d0b89d56987da5ee9dbcbc74b8461f5 [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');
Andreas Schejad1ceba42016-05-15 21:49:04 +020018if ($GEN_DIR === 'gen-php-psr4') {
19 $loader->registerNamespace('ThriftTest', $GEN_DIR);
20} else {
21 $loader->registerDefinition('ThriftTest', $GEN_DIR);
22}
Roger Meier21c0a852012-09-05 19:47:14 +000023$loader->register();
24
David Reissea2cba82009-03-30 21:35:00 +000025/*
26 * Licensed to the Apache Software Foundation (ASF) under one
27 * or more contributor license agreements. See the NOTICE file
28 * distributed with this work for additional information
29 * regarding copyright ownership. The ASF licenses this file
30 * to you under the Apache License, Version 2.0 (the
31 * "License"); you may not use this file except in compliance
32 * with the License. You may obtain a copy of the License at
33 *
34 * http://www.apache.org/licenses/LICENSE-2.0
35 *
36 * Unless required by applicable law or agreed to in writing,
37 * software distributed under the License is distributed on an
38 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
39 * KIND, either express or implied. See the License for the
40 * specific language governing permissions and limitations
41 * under the License.
42 */
43
Mark Slee6e536442006-06-30 18:28:50 +000044/** Include the Thrift base */
Jim King5903d672015-06-29 18:12:48 -040045/** Include the protocols */
Roger Meier21c0a852012-09-05 19:47:14 +000046use Thrift\Protocol\TBinaryProtocol;
Jim King5903d672015-06-29 18:12:48 -040047use Thrift\Protocol\TCompactProtocol;
48use Thrift\Protocol\TJSONProtocol;
Mark Slee6e536442006-06-30 18:28:50 +000049
50/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000051use Thrift\Transport\TSocket;
52use Thrift\Transport\TSocketPool;
Mark Slee6e536442006-06-30 18:28:50 +000053
54/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000055use Thrift\Transport\TFramedTransport;
56use Thrift\Transport\TBufferedTransport;
Mark Slee5b743072007-11-13 04:00:29 +000057
Jim King5903d672015-06-29 18:12:48 -040058function makeProtocol($transport, $PROTO)
59{
60 if ($PROTO == 'binary') {
61 return new TBinaryProtocol($transport);
62 } else if ($PROTO == 'compact') {
63 return new TCompactProtocol($transport);
64 } else if ($PROTO == 'json') {
65 return new TJSONProtocol($transport);
66 }
67
68 die ("--protocol must be one of {binary|compact|json}");
69}
70
Mark Slee6e536442006-06-30 18:28:50 +000071$host = 'localhost';
72$port = 9090;
73
74if ($argc > 1) {
75 $host = $argv[0];
76}
77
78if ($argc > 2) {
79 $host = $argv[1];
80}
81
Roger Meier41ad4342015-03-24 22:30:40 +010082foreach ($argv as $arg) {
83 if (substr($arg, 0, 7) == '--port=') {
84 $port = substr($arg, 7);
Jim King5903d672015-06-29 18:12:48 -040085 } else if (substr($arg, 0, 12) == '--transport=') {
86 $MODE = substr($arg, 12);
87 } else if (substr($arg, 0, 11) == '--protocol=') {
88 $PROTO = substr($arg, 11);
89 }
Roger Meier41ad4342015-03-24 22:30:40 +010090}
91
Mark Slee1dd819c2006-10-26 04:56:18 +000092$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +000093
Mark Slee6e536442006-06-30 18:28:50 +000094$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +000095$socket = new TSocketPool($hosts, $port);
96$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +000097
Mark Sleed3d733a2006-09-01 22:19:06 +000098if ($MODE == 'inline') {
99 $transport = $socket;
Roger Meier21c0a852012-09-05 19:47:14 +0000100 $testClient = new \ThriftTest\ThriftTestClient($transport);
Bryan Duxburya971fb02011-03-04 00:49:40 +0000101} else if ($MODE == 'framed') {
102 $framedSocket = new TFramedTransport($socket);
103 $transport = $framedSocket;
Jim King5903d672015-06-29 18:12:48 -0400104 $protocol = makeProtocol($transport, $PROTO);
Roger Meier21c0a852012-09-05 19:47:14 +0000105 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +0000106} else {
107 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
108 $transport = $bufferedSocket;
Jim King5903d672015-06-29 18:12:48 -0400109 $protocol = makeProtocol($transport, $PROTO);
Roger Meier21c0a852012-09-05 19:47:14 +0000110 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +0000111}
112
113$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +0000114
115$start = microtime(true);
116
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900117define(ERR_BASETYPES, 1);
118// ERR_STRUCTS = 2;
119// ERR_CONTAINERS = 4;
120// ERR_EXCEPTIONS = 8;
121// ERR_UNKNOWN = 64;
122$exitcode = 0;
Mark Slee6e536442006-06-30 18:28:50 +0000123/**
124 * VOID TEST
125 */
126print_r("testVoid()");
127$testClient->testVoid();
128print_r(" = void\n");
129
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900130function roundtrip($testClient, $method, $value) {
131 global $exitcode;
132 print_r("$method($value)");
133 $ret = $testClient->$method($value);
134 print_r(" = \"$ret\"\n");
135 if ($value != $ret) {
136 print_r("*** FAILED ***\n");
137 $exitcode |= ERR_BASETYPES;
138 }
139}
140
Mark Slee6e536442006-06-30 18:28:50 +0000141/**
142 * STRING TEST
143 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900144roundtrip($testClient, 'testString', "Test");
Mark Slee5b743072007-11-13 04:00:29 +0000145
Mark Slee6e536442006-06-30 18:28:50 +0000146/**
147 * BYTE TEST
148 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900149roundtrip($testClient, 'testByte', 1);
Mark Slee6e536442006-06-30 18:28:50 +0000150
151/**
152 * I32 TEST
153 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900154roundtrip($testClient, 'testI32', -1);
Mark Slee6e536442006-06-30 18:28:50 +0000155
156/**
157 * I64 TEST
158 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900159roundtrip($testClient, 'testI64', -34359738368);
Mark Slee6e536442006-06-30 18:28:50 +0000160
161/**
Mark Sleec98d0502006-09-06 02:42:25 +0000162 * DOUBLE TEST
163 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900164roundtrip($testClient, 'testDouble', -852.234234234);
Mark Sleec98d0502006-09-06 02:42:25 +0000165
166/**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100167 * BINARY TEST -- TODO
168 */
169
170/**
Mark Slee6e536442006-06-30 18:28:50 +0000171 * STRUCT TEST
172 */
173print_r("testStruct({\"Zero\", 1, -3, -5})");
Roger Meier21c0a852012-09-05 19:47:14 +0000174$out = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000175$out->string_thing = "Zero";
176$out->byte_thing = 1;
177$out->i32_thing = -3;
178$out->i64_thing = -5;
179$in = $testClient->testStruct($out);
180print_r(" = {\"".$in->string_thing."\", ".
181 $in->byte_thing.", ".
182 $in->i32_thing.", ".
183 $in->i64_thing."}\n");
184
185/**
186 * NESTED STRUCT TEST
187 */
188print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Roger Meier21c0a852012-09-05 19:47:14 +0000189$out2 = new \ThriftTest\Xtruct2();
Mark Slee6e536442006-06-30 18:28:50 +0000190$out2->byte_thing = 1;
191$out2->struct_thing = $out;
192$out2->i32_thing = 5;
193$in2 = $testClient->testNest($out2);
194$in = $in2->struct_thing;
195print_r(" = {".$in2->byte_thing.", {\"".
196 $in->string_thing."\", ".
197 $in->byte_thing.", ".
198 $in->i32_thing.", ".
199 $in->i64_thing."}, ".
200 $in2->i32_thing."}\n");
201
202/**
203 * MAP TEST
204 */
205$mapout = array();
206for ($i = 0; $i < 5; ++$i) {
207 $mapout[$i] = $i-10;
208}
209print_r("testMap({");
210$first = true;
211foreach ($mapout as $key => $val) {
212 if ($first) {
213 $first = false;
214 } else {
215 print_r(", ");
216 }
217 print_r("$key => $val");
218}
219print_r("})");
220
221$mapin = $testClient->testMap($mapout);
222print_r(" = {");
223$first = true;
224foreach ($mapin as $key => $val) {
225 if ($first) {
226 $first = false;
227 } else {
228 print_r(", ");
229 }
230 print_r("$key => $val");
231}
232print_r("}\n");
233
234/**
235 * SET TEST
236 */
237$setout = array();;
238for ($i = -2; $i < 3; ++$i) {
239 $setout []= $i;
240}
241print_r("testSet({");
242$first = true;
243foreach ($setout as $val) {
244 if ($first) {
245 $first = false;
246 } else {
247 print_r(", ");
248 }
249 print_r($val);
250}
251print_r("})");
252$setin = $testClient->testSet($setout);
253print_r(" = {");
254$first = true;
255foreach ($setin as $val) {
256 if ($first) {
257 $first = false;
258 } else {
259 print_r(", ");
260 }
261 print_r($val);
262}
263print_r("}\n");
264
265/**
266 * LIST TEST
267 */
268$listout = array();
269for ($i = -2; $i < 3; ++$i) {
270 $listout []= $i;
271}
272print_r("testList({");
273$first = true;
274foreach ($listout as $val) {
275 if ($first) {
276 $first = false;
277 } else {
278 print_r(", ");
279 }
280 print_r($val);
281}
282print_r("})");
283$listin = $testClient->testList($listout);
284print_r(" = {");
285$first = true;
286foreach ($listin as $val) {
287 if ($first) {
288 $first = false;
289 } else {
290 print_r(", ");
291 }
292 print_r($val);
293}
294print_r("}\n");
295
296/**
297 * ENUM TEST
298 */
299print_r("testEnum(ONE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100300$ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000301print_r(" = $ret\n");
302
303print_r("testEnum(TWO)");
Roger Meier87afaac2013-01-06 20:10:42 +0100304$ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000305print_r(" = $ret\n");
306
307print_r("testEnum(THREE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100308$ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000309print_r(" = $ret\n");
310
311print_r("testEnum(FIVE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100312$ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000313print_r(" = $ret\n");
314
315print_r("testEnum(EIGHT)");
Roger Meier87afaac2013-01-06 20:10:42 +0100316$ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT);
Mark Slee6e536442006-06-30 18:28:50 +0000317print_r(" = $ret\n");
318
319/**
320 * TYPEDEF TEST
321 */
322print_r("testTypedef(309858235082523)");
323$uid = $testClient->testTypedef(309858235082523);
324print_r(" = $uid\n");
325
326/**
327 * NESTED MAP TEST
328 */
329print_r("testMapMap(1)");
330$mm = $testClient->testMapMap(1);
331print_r(" = {");
332foreach ($mm as $key => $val) {
333 print_r("$key => {");
334 foreach ($val as $k2 => $v2) {
335 print_r("$k2 => $v2, ");
336 }
337 print_r("}, ");
338}
339print_r("}\n");
340
341/**
342 * INSANITY TEST
343 */
Roger Meier21c0a852012-09-05 19:47:14 +0000344$insane = new \ThriftTest\Insanity();
Roger Meier87afaac2013-01-06 20:10:42 +0100345$insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
Roger Meier21c0a852012-09-05 19:47:14 +0000346$truck = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000347$truck->string_thing = "Truck";
348$truck->byte_thing = 8;
349$truck->i32_thing = 8;
350$truck->i64_thing = 8;
351$insane->xtructs []= $truck;
352print_r("testInsanity()");
353$whoa = $testClient->testInsanity($insane);
354print_r(" = {");
355foreach ($whoa as $key => $val) {
356 print_r("$key => {");
357 foreach ($val as $k2 => $v2) {
358 print_r("$k2 => {");
359 $userMap = $v2->userMap;
360 print_r("{");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000361 if (is_array($userMap)) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000362 foreach ($userMap as $k3 => $v3) {
363 print_r("$k3 => $v3, ");
364 }
Mark Slee6e536442006-06-30 18:28:50 +0000365 }
366 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000367
Mark Slee6e536442006-06-30 18:28:50 +0000368 $xtructs = $v2->xtructs;
369 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000370 if (is_array($xtructs)) {
371 foreach ($xtructs as $x) {
372 print_r("{\"".$x->string_thing."\", ".
373 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
374 }
Mark Slee6e536442006-06-30 18:28:50 +0000375 }
376 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000377
Mark Slee6e536442006-06-30 18:28:50 +0000378 print_r("}, ");
379 }
380 print_r("}, ");
381}
382print_r("}\n");
383
Mark Sleed3d733a2006-09-01 22:19:06 +0000384/**
385 * EXCEPTION TEST
386 */
387print_r("testException('Xception')");
388try {
389 $testClient->testException('Xception');
390 print_r(" void\nFAILURE\n");
Roger Meier87afaac2013-01-06 20:10:42 +0100391} catch (\ThriftTest\Xception $x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000392 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
393}
394
Mark Slee6e536442006-06-30 18:28:50 +0000395
396/**
397 * Normal tests done.
398 */
399
400$stop = microtime(true);
401$elp = round(1000*($stop - $start), 0);
402print_r("Total time: $elp ms\n");
403
404/**
405 * Extraneous "I don't trust PHP to pack/unpack integer" tests
406 */
407
408// Max I32
409$num = pow(2, 30) + (pow(2, 30) - 1);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900410roundtrip($testClient, testI32, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000411
412// Min I32
413$num = 0 - pow(2, 31);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900414roundtrip($testClient, testI32, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000415
416// Max I64
417$num = pow(2, 62) + (pow(2, 62) - 1);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900418roundtrip($testClient, testI64, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000419
420// Min I64
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900421$num = 0 - pow(2, 62) - pow(2, 62);
422roundtrip($testClient, testI64, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000423
Mark Sleed3d733a2006-09-01 22:19:06 +0000424$transport->close();
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900425exit($exitcode);