blob: 946334d9da111c7a671c573d18824d34542778d1 [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
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900113define(ERR_BASETYPES, 1);
114// ERR_STRUCTS = 2;
115// ERR_CONTAINERS = 4;
116// ERR_EXCEPTIONS = 8;
117// ERR_UNKNOWN = 64;
118$exitcode = 0;
Mark Slee6e536442006-06-30 18:28:50 +0000119/**
120 * VOID TEST
121 */
122print_r("testVoid()");
123$testClient->testVoid();
124print_r(" = void\n");
125
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900126function roundtrip($testClient, $method, $value) {
127 global $exitcode;
128 print_r("$method($value)");
129 $ret = $testClient->$method($value);
130 print_r(" = \"$ret\"\n");
131 if ($value != $ret) {
132 print_r("*** FAILED ***\n");
133 $exitcode |= ERR_BASETYPES;
134 }
135}
136
Mark Slee6e536442006-06-30 18:28:50 +0000137/**
138 * STRING TEST
139 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900140roundtrip($testClient, 'testString', "Test");
Mark Slee5b743072007-11-13 04:00:29 +0000141
Mark Slee6e536442006-06-30 18:28:50 +0000142/**
143 * BYTE TEST
144 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900145roundtrip($testClient, 'testByte', 1);
Mark Slee6e536442006-06-30 18:28:50 +0000146
147/**
148 * I32 TEST
149 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900150roundtrip($testClient, 'testI32', -1);
Mark Slee6e536442006-06-30 18:28:50 +0000151
152/**
153 * I64 TEST
154 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900155roundtrip($testClient, 'testI64', -34359738368);
Mark Slee6e536442006-06-30 18:28:50 +0000156
157/**
Mark Sleec98d0502006-09-06 02:42:25 +0000158 * DOUBLE TEST
159 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900160roundtrip($testClient, 'testDouble', -852.234234234);
Mark Sleec98d0502006-09-06 02:42:25 +0000161
162/**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100163 * BINARY TEST -- TODO
164 */
165
166/**
Mark Slee6e536442006-06-30 18:28:50 +0000167 * STRUCT TEST
168 */
169print_r("testStruct({\"Zero\", 1, -3, -5})");
Roger Meier21c0a852012-09-05 19:47:14 +0000170$out = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000171$out->string_thing = "Zero";
172$out->byte_thing = 1;
173$out->i32_thing = -3;
174$out->i64_thing = -5;
175$in = $testClient->testStruct($out);
176print_r(" = {\"".$in->string_thing."\", ".
177 $in->byte_thing.", ".
178 $in->i32_thing.", ".
179 $in->i64_thing."}\n");
180
181/**
182 * NESTED STRUCT TEST
183 */
184print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Roger Meier21c0a852012-09-05 19:47:14 +0000185$out2 = new \ThriftTest\Xtruct2();
Mark Slee6e536442006-06-30 18:28:50 +0000186$out2->byte_thing = 1;
187$out2->struct_thing = $out;
188$out2->i32_thing = 5;
189$in2 = $testClient->testNest($out2);
190$in = $in2->struct_thing;
191print_r(" = {".$in2->byte_thing.", {\"".
192 $in->string_thing."\", ".
193 $in->byte_thing.", ".
194 $in->i32_thing.", ".
195 $in->i64_thing."}, ".
196 $in2->i32_thing."}\n");
197
198/**
199 * MAP TEST
200 */
201$mapout = array();
202for ($i = 0; $i < 5; ++$i) {
203 $mapout[$i] = $i-10;
204}
205print_r("testMap({");
206$first = true;
207foreach ($mapout as $key => $val) {
208 if ($first) {
209 $first = false;
210 } else {
211 print_r(", ");
212 }
213 print_r("$key => $val");
214}
215print_r("})");
216
217$mapin = $testClient->testMap($mapout);
218print_r(" = {");
219$first = true;
220foreach ($mapin as $key => $val) {
221 if ($first) {
222 $first = false;
223 } else {
224 print_r(", ");
225 }
226 print_r("$key => $val");
227}
228print_r("}\n");
229
230/**
231 * SET TEST
232 */
233$setout = array();;
234for ($i = -2; $i < 3; ++$i) {
235 $setout []= $i;
236}
237print_r("testSet({");
238$first = true;
239foreach ($setout as $val) {
240 if ($first) {
241 $first = false;
242 } else {
243 print_r(", ");
244 }
245 print_r($val);
246}
247print_r("})");
248$setin = $testClient->testSet($setout);
249print_r(" = {");
250$first = true;
251foreach ($setin as $val) {
252 if ($first) {
253 $first = false;
254 } else {
255 print_r(", ");
256 }
257 print_r($val);
258}
259print_r("}\n");
260
261/**
262 * LIST TEST
263 */
264$listout = array();
265for ($i = -2; $i < 3; ++$i) {
266 $listout []= $i;
267}
268print_r("testList({");
269$first = true;
270foreach ($listout as $val) {
271 if ($first) {
272 $first = false;
273 } else {
274 print_r(", ");
275 }
276 print_r($val);
277}
278print_r("})");
279$listin = $testClient->testList($listout);
280print_r(" = {");
281$first = true;
282foreach ($listin as $val) {
283 if ($first) {
284 $first = false;
285 } else {
286 print_r(", ");
287 }
288 print_r($val);
289}
290print_r("}\n");
291
292/**
293 * ENUM TEST
294 */
295print_r("testEnum(ONE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100296$ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000297print_r(" = $ret\n");
298
299print_r("testEnum(TWO)");
Roger Meier87afaac2013-01-06 20:10:42 +0100300$ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000301print_r(" = $ret\n");
302
303print_r("testEnum(THREE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100304$ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000305print_r(" = $ret\n");
306
307print_r("testEnum(FIVE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100308$ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000309print_r(" = $ret\n");
310
311print_r("testEnum(EIGHT)");
Roger Meier87afaac2013-01-06 20:10:42 +0100312$ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT);
Mark Slee6e536442006-06-30 18:28:50 +0000313print_r(" = $ret\n");
314
315/**
316 * TYPEDEF TEST
317 */
318print_r("testTypedef(309858235082523)");
319$uid = $testClient->testTypedef(309858235082523);
320print_r(" = $uid\n");
321
322/**
323 * NESTED MAP TEST
324 */
325print_r("testMapMap(1)");
326$mm = $testClient->testMapMap(1);
327print_r(" = {");
328foreach ($mm as $key => $val) {
329 print_r("$key => {");
330 foreach ($val as $k2 => $v2) {
331 print_r("$k2 => $v2, ");
332 }
333 print_r("}, ");
334}
335print_r("}\n");
336
337/**
338 * INSANITY TEST
339 */
Roger Meier21c0a852012-09-05 19:47:14 +0000340$insane = new \ThriftTest\Insanity();
Roger Meier87afaac2013-01-06 20:10:42 +0100341$insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
Roger Meier21c0a852012-09-05 19:47:14 +0000342$truck = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000343$truck->string_thing = "Truck";
344$truck->byte_thing = 8;
345$truck->i32_thing = 8;
346$truck->i64_thing = 8;
347$insane->xtructs []= $truck;
348print_r("testInsanity()");
349$whoa = $testClient->testInsanity($insane);
350print_r(" = {");
351foreach ($whoa as $key => $val) {
352 print_r("$key => {");
353 foreach ($val as $k2 => $v2) {
354 print_r("$k2 => {");
355 $userMap = $v2->userMap;
356 print_r("{");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000357 if (is_array($userMap)) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000358 foreach ($userMap as $k3 => $v3) {
359 print_r("$k3 => $v3, ");
360 }
Mark Slee6e536442006-06-30 18:28:50 +0000361 }
362 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000363
Mark Slee6e536442006-06-30 18:28:50 +0000364 $xtructs = $v2->xtructs;
365 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000366 if (is_array($xtructs)) {
367 foreach ($xtructs as $x) {
368 print_r("{\"".$x->string_thing."\", ".
369 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
370 }
Mark Slee6e536442006-06-30 18:28:50 +0000371 }
372 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000373
Mark Slee6e536442006-06-30 18:28:50 +0000374 print_r("}, ");
375 }
376 print_r("}, ");
377}
378print_r("}\n");
379
Mark Sleed3d733a2006-09-01 22:19:06 +0000380/**
381 * EXCEPTION TEST
382 */
383print_r("testException('Xception')");
384try {
385 $testClient->testException('Xception');
386 print_r(" void\nFAILURE\n");
Roger Meier87afaac2013-01-06 20:10:42 +0100387} catch (\ThriftTest\Xception $x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000388 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
389}
390
Mark Slee6e536442006-06-30 18:28:50 +0000391
392/**
393 * Normal tests done.
394 */
395
396$stop = microtime(true);
397$elp = round(1000*($stop - $start), 0);
398print_r("Total time: $elp ms\n");
399
400/**
401 * Extraneous "I don't trust PHP to pack/unpack integer" tests
402 */
403
404// Max I32
405$num = pow(2, 30) + (pow(2, 30) - 1);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900406roundtrip($testClient, testI32, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000407
408// Min I32
409$num = 0 - pow(2, 31);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900410roundtrip($testClient, testI32, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000411
412// Max I64
413$num = pow(2, 62) + (pow(2, 62) - 1);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900414roundtrip($testClient, testI64, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000415
416// Min I64
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900417$num = 0 - pow(2, 62) - pow(2, 62);
418roundtrip($testClient, testI64, $num);
Mark Slee6e536442006-06-30 18:28:50 +0000419
Mark Sleed3d733a2006-09-01 22:19:06 +0000420$transport->close();
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900421exit($exitcode);