blob: c1f64359b577d72b7a62f4d9fdcfed831dac15b7 [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;
Håkon Hitlandf39d4c82016-11-17 16:18:03 +010047use Thrift\Protocol\TBinaryProtocolAccelerated;
Jim King5903d672015-06-29 18:12:48 -040048use Thrift\Protocol\TCompactProtocol;
49use Thrift\Protocol\TJSONProtocol;
Mark Slee6e536442006-06-30 18:28:50 +000050
51/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000052use Thrift\Transport\TSocket;
53use Thrift\Transport\TSocketPool;
Mark Slee6e536442006-06-30 18:28:50 +000054
55/** Include the socket layer */
Roger Meier21c0a852012-09-05 19:47:14 +000056use Thrift\Transport\TFramedTransport;
57use Thrift\Transport\TBufferedTransport;
Mark Slee5b743072007-11-13 04:00:29 +000058
Jim King5903d672015-06-29 18:12:48 -040059function makeProtocol($transport, $PROTO)
60{
61 if ($PROTO == 'binary') {
62 return new TBinaryProtocol($transport);
63 } else if ($PROTO == 'compact') {
64 return new TCompactProtocol($transport);
65 } else if ($PROTO == 'json') {
66 return new TJSONProtocol($transport);
Håkon Hitlandf39d4c82016-11-17 16:18:03 +010067 } else if ($PROTO == 'accel') {
68 if (!function_exists('thrift_protocol_write_binary')) {
69 echo "Acceleration extension is not loaded\n";
70 exit(1);
71 }
72 return new TBinaryProtocolAccelerated($transport);
Jim King5903d672015-06-29 18:12:48 -040073 }
74
Håkon Hitlandf39d4c82016-11-17 16:18:03 +010075 echo "--protocol must be one of {binary|compact|json|accel}\n";
76 exit(1);
Jim King5903d672015-06-29 18:12:48 -040077}
78
Mark Slee6e536442006-06-30 18:28:50 +000079$host = 'localhost';
80$port = 9090;
81
82if ($argc > 1) {
83 $host = $argv[0];
84}
85
86if ($argc > 2) {
87 $host = $argv[1];
88}
89
Roger Meier41ad4342015-03-24 22:30:40 +010090foreach ($argv as $arg) {
91 if (substr($arg, 0, 7) == '--port=') {
92 $port = substr($arg, 7);
Jim King5903d672015-06-29 18:12:48 -040093 } else if (substr($arg, 0, 12) == '--transport=') {
94 $MODE = substr($arg, 12);
95 } else if (substr($arg, 0, 11) == '--protocol=') {
96 $PROTO = substr($arg, 11);
97 }
Roger Meier41ad4342015-03-24 22:30:40 +010098}
99
Mark Slee1dd819c2006-10-26 04:56:18 +0000100$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +0000101
Mark Slee6e536442006-06-30 18:28:50 +0000102$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +0000103$socket = new TSocketPool($hosts, $port);
104$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +0000105
Mark Sleed3d733a2006-09-01 22:19:06 +0000106if ($MODE == 'inline') {
107 $transport = $socket;
Roger Meier21c0a852012-09-05 19:47:14 +0000108 $testClient = new \ThriftTest\ThriftTestClient($transport);
Bryan Duxburya971fb02011-03-04 00:49:40 +0000109} else if ($MODE == 'framed') {
110 $framedSocket = new TFramedTransport($socket);
111 $transport = $framedSocket;
Jim King5903d672015-06-29 18:12:48 -0400112 $protocol = makeProtocol($transport, $PROTO);
Roger Meier21c0a852012-09-05 19:47:14 +0000113 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +0000114} else {
115 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
116 $transport = $bufferedSocket;
Jim King5903d672015-06-29 18:12:48 -0400117 $protocol = makeProtocol($transport, $PROTO);
Roger Meier21c0a852012-09-05 19:47:14 +0000118 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +0000119}
120
121$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +0000122
123$start = microtime(true);
124
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100125define('ERR_BASETYPES', 1);
126define('ERR_STRUCTS', 2);
127define('ERR_CONTAINERS', 4);
128define('ERR_EXCEPTIONS', 8);
129define('ERR_UNKNOWN', 64);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900130$exitcode = 0;
Mark Slee6e536442006-06-30 18:28:50 +0000131/**
132 * VOID TEST
133 */
134print_r("testVoid()");
135$testClient->testVoid();
136print_r(" = void\n");
137
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900138function roundtrip($testClient, $method, $value) {
139 global $exitcode;
140 print_r("$method($value)");
141 $ret = $testClient->$method($value);
142 print_r(" = \"$ret\"\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100143 if ($value !== $ret) {
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900144 print_r("*** FAILED ***\n");
145 $exitcode |= ERR_BASETYPES;
146 }
147}
148
Mark Slee6e536442006-06-30 18:28:50 +0000149/**
150 * STRING TEST
151 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900152roundtrip($testClient, 'testString', "Test");
Mark Slee5b743072007-11-13 04:00:29 +0000153
Mark Slee6e536442006-06-30 18:28:50 +0000154/**
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100155 * BOOL TEST
156 */
157roundtrip($testClient, 'testBool', true);
158roundtrip($testClient, 'testBool', false);
159
160/**
Mark Slee6e536442006-06-30 18:28:50 +0000161 * BYTE TEST
162 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900163roundtrip($testClient, 'testByte', 1);
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100164roundtrip($testClient, 'testByte', -1);
165roundtrip($testClient, 'testByte', 127);
166roundtrip($testClient, 'testByte', -128);
Mark Slee6e536442006-06-30 18:28:50 +0000167
168/**
169 * I32 TEST
170 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900171roundtrip($testClient, 'testI32', -1);
Mark Slee6e536442006-06-30 18:28:50 +0000172
173/**
174 * I64 TEST
175 */
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100176roundtrip($testClient, 'testI64', 0);
177roundtrip($testClient, 'testI64', 1);
178roundtrip($testClient, 'testI64', -1);
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900179roundtrip($testClient, 'testI64', -34359738368);
Mark Slee6e536442006-06-30 18:28:50 +0000180
181/**
Mark Sleec98d0502006-09-06 02:42:25 +0000182 * DOUBLE TEST
183 */
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900184roundtrip($testClient, 'testDouble', -852.234234234);
Mark Sleec98d0502006-09-06 02:42:25 +0000185
186/**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100187 * BINARY TEST -- TODO
188 */
189
190/**
Mark Slee6e536442006-06-30 18:28:50 +0000191 * STRUCT TEST
192 */
193print_r("testStruct({\"Zero\", 1, -3, -5})");
Roger Meier21c0a852012-09-05 19:47:14 +0000194$out = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000195$out->string_thing = "Zero";
196$out->byte_thing = 1;
197$out->i32_thing = -3;
198$out->i64_thing = -5;
199$in = $testClient->testStruct($out);
200print_r(" = {\"".$in->string_thing."\", ".
201 $in->byte_thing.", ".
202 $in->i32_thing.", ".
203 $in->i64_thing."}\n");
204
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100205if ($in != $out) {
206 echo "**FAILED**\n";
207 $exitcode |= ERR_STRUCTS;
208}
209
Mark Slee6e536442006-06-30 18:28:50 +0000210/**
211 * NESTED STRUCT TEST
212 */
213print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Roger Meier21c0a852012-09-05 19:47:14 +0000214$out2 = new \ThriftTest\Xtruct2();
Mark Slee6e536442006-06-30 18:28:50 +0000215$out2->byte_thing = 1;
216$out2->struct_thing = $out;
217$out2->i32_thing = 5;
218$in2 = $testClient->testNest($out2);
219$in = $in2->struct_thing;
220print_r(" = {".$in2->byte_thing.", {\"".
221 $in->string_thing."\", ".
222 $in->byte_thing.", ".
223 $in->i32_thing.", ".
224 $in->i64_thing."}, ".
225 $in2->i32_thing."}\n");
226
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100227if ($in2 != $out2) {
228 echo "**FAILED**\n";
229 $exitcode |= ERR_STRUCTS;
230}
231
Mark Slee6e536442006-06-30 18:28:50 +0000232/**
233 * MAP TEST
234 */
235$mapout = array();
236for ($i = 0; $i < 5; ++$i) {
237 $mapout[$i] = $i-10;
238}
239print_r("testMap({");
240$first = true;
241foreach ($mapout as $key => $val) {
242 if ($first) {
243 $first = false;
244 } else {
245 print_r(", ");
246 }
247 print_r("$key => $val");
248}
249print_r("})");
250
251$mapin = $testClient->testMap($mapout);
252print_r(" = {");
253$first = true;
254foreach ($mapin as $key => $val) {
255 if ($first) {
256 $first = false;
257 } else {
258 print_r(", ");
259 }
260 print_r("$key => $val");
261}
262print_r("}\n");
263
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100264if ($mapin != $mapout) {
265 echo "**FAILED**\n";
266 $exitcode |= ERR_CONTAINERS;
267}
268
Mark Slee6e536442006-06-30 18:28:50 +0000269/**
270 * SET TEST
271 */
272$setout = array();;
273for ($i = -2; $i < 3; ++$i) {
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100274 $setout[$i]= true;
Mark Slee6e536442006-06-30 18:28:50 +0000275}
276print_r("testSet({");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100277echo implode(',', array_keys($setout));
Mark Slee6e536442006-06-30 18:28:50 +0000278print_r("})");
279$setin = $testClient->testSet($setout);
280print_r(" = {");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100281echo implode(', ', array_keys($setin));
Mark Slee6e536442006-06-30 18:28:50 +0000282print_r("}\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100283// Order of keys in set does not matter
284ksort($setin);
285if ($setout !== $setin) {
286 echo "**FAILED**\n";
287 $exitcode |= ERR_CONTAINERS;
288}
289// Regression test for corrupted array
290if ($setin[2] !== $setout[2] || is_int($setin[2])) {
291 echo "**FAILED**\n";
292 $exitcode |= ERR_CONTAINERS;
293}
Mark Slee6e536442006-06-30 18:28:50 +0000294
295/**
296 * LIST TEST
297 */
298$listout = array();
299for ($i = -2; $i < 3; ++$i) {
300 $listout []= $i;
301}
302print_r("testList({");
303$first = true;
304foreach ($listout as $val) {
305 if ($first) {
306 $first = false;
307 } else {
308 print_r(", ");
309 }
310 print_r($val);
311}
312print_r("})");
313$listin = $testClient->testList($listout);
314print_r(" = {");
315$first = true;
316foreach ($listin as $val) {
317 if ($first) {
318 $first = false;
319 } else {
320 print_r(", ");
321 }
322 print_r($val);
323}
324print_r("}\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100325if ($listin !== $listout) {
326 echo "**FAILED**\n";
327 $exitcode |= ERR_CONTAINERS;
328}
Mark Slee6e536442006-06-30 18:28:50 +0000329
330/**
331 * ENUM TEST
332 */
333print_r("testEnum(ONE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100334$ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000335print_r(" = $ret\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100336if ($ret != \ThriftTest\Numberz::ONE) {
337 echo "**FAILED**\n";
338 $exitcode |= ERR_STRUCTS;
339}
Mark Slee6e536442006-06-30 18:28:50 +0000340
341print_r("testEnum(TWO)");
Roger Meier87afaac2013-01-06 20:10:42 +0100342$ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000343print_r(" = $ret\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100344if ($ret != \ThriftTest\Numberz::TWO) {
345 echo "**FAILED**\n";
346 $exitcode |= ERR_STRUCTS;
347}
Mark Slee6e536442006-06-30 18:28:50 +0000348
349print_r("testEnum(THREE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100350$ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000351print_r(" = $ret\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100352if ($ret != \ThriftTest\Numberz::THREE) {
353 echo "**FAILED**\n";
354 $exitcode |= ERR_STRUCTS;
355}
Mark Slee6e536442006-06-30 18:28:50 +0000356
357print_r("testEnum(FIVE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100358$ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000359print_r(" = $ret\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100360if ($ret != \ThriftTest\Numberz::FIVE) {
361 echo "**FAILED**\n";
362 $exitcode |= ERR_STRUCTS;
363}
Mark Slee6e536442006-06-30 18:28:50 +0000364
365print_r("testEnum(EIGHT)");
Roger Meier87afaac2013-01-06 20:10:42 +0100366$ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT);
Mark Slee6e536442006-06-30 18:28:50 +0000367print_r(" = $ret\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100368if ($ret != \ThriftTest\Numberz::EIGHT) {
369 echo "**FAILED**\n";
370 $exitcode |= ERR_STRUCTS;
371}
Mark Slee6e536442006-06-30 18:28:50 +0000372
373/**
374 * TYPEDEF TEST
375 */
376print_r("testTypedef(309858235082523)");
377$uid = $testClient->testTypedef(309858235082523);
378print_r(" = $uid\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100379if ($uid !== 309858235082523) {
380 echo "**FAILED**\n";
381 $exitcode |= ERR_STRUCTS;
382}
Mark Slee6e536442006-06-30 18:28:50 +0000383
384/**
385 * NESTED MAP TEST
386 */
387print_r("testMapMap(1)");
388$mm = $testClient->testMapMap(1);
389print_r(" = {");
390foreach ($mm as $key => $val) {
391 print_r("$key => {");
392 foreach ($val as $k2 => $v2) {
393 print_r("$k2 => $v2, ");
394 }
395 print_r("}, ");
396}
397print_r("}\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100398$expected_mm = [
399 -4 => [-4 => -4, -3 => -3, -2 => -2, -1 => -1],
400 4 => [4 => 4, 3 => 3, 2 => 2, 1 => 1],
401];
402if ($mm != $expected_mm) {
403 echo "**FAILED**\n";
404 $exitcode |= ERR_CONTAINERS;
405}
Mark Slee6e536442006-06-30 18:28:50 +0000406
407/**
408 * INSANITY TEST
409 */
Roger Meier21c0a852012-09-05 19:47:14 +0000410$insane = new \ThriftTest\Insanity();
Roger Meier87afaac2013-01-06 20:10:42 +0100411$insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
Roger Meier21c0a852012-09-05 19:47:14 +0000412$truck = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000413$truck->string_thing = "Truck";
414$truck->byte_thing = 8;
415$truck->i32_thing = 8;
416$truck->i64_thing = 8;
417$insane->xtructs []= $truck;
418print_r("testInsanity()");
419$whoa = $testClient->testInsanity($insane);
420print_r(" = {");
421foreach ($whoa as $key => $val) {
422 print_r("$key => {");
423 foreach ($val as $k2 => $v2) {
424 print_r("$k2 => {");
425 $userMap = $v2->userMap;
426 print_r("{");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000427 if (is_array($userMap)) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000428 foreach ($userMap as $k3 => $v3) {
429 print_r("$k3 => $v3, ");
430 }
Mark Slee6e536442006-06-30 18:28:50 +0000431 }
432 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000433
Mark Slee6e536442006-06-30 18:28:50 +0000434 $xtructs = $v2->xtructs;
435 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000436 if (is_array($xtructs)) {
437 foreach ($xtructs as $x) {
438 print_r("{\"".$x->string_thing."\", ".
439 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
440 }
Mark Slee6e536442006-06-30 18:28:50 +0000441 }
442 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000443
Mark Slee6e536442006-06-30 18:28:50 +0000444 print_r("}, ");
445 }
446 print_r("}, ");
447}
448print_r("}\n");
449
Mark Sleed3d733a2006-09-01 22:19:06 +0000450/**
451 * EXCEPTION TEST
452 */
453print_r("testException('Xception')");
454try {
455 $testClient->testException('Xception');
456 print_r(" void\nFAILURE\n");
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100457 $exitcode |= ERR_EXCEPTIONS;
Roger Meier87afaac2013-01-06 20:10:42 +0100458} catch (\ThriftTest\Xception $x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000459 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
460}
461
Mark Slee6e536442006-06-30 18:28:50 +0000462
463/**
464 * Normal tests done.
465 */
466
467$stop = microtime(true);
468$elp = round(1000*($stop - $start), 0);
469print_r("Total time: $elp ms\n");
470
471/**
472 * Extraneous "I don't trust PHP to pack/unpack integer" tests
473 */
474
475// Max I32
476$num = pow(2, 30) + (pow(2, 30) - 1);
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100477roundtrip($testClient, 'testI32', $num);
Mark Slee6e536442006-06-30 18:28:50 +0000478
479// Min I32
480$num = 0 - pow(2, 31);
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100481roundtrip($testClient, 'testI32', $num);
Mark Slee6e536442006-06-30 18:28:50 +0000482
483// Max I64
484$num = pow(2, 62) + (pow(2, 62) - 1);
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100485roundtrip($testClient, 'testI64', $num);
Mark Slee6e536442006-06-30 18:28:50 +0000486
487// Min I64
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900488$num = 0 - pow(2, 62) - pow(2, 62);
Håkon Hitlandf39d4c82016-11-17 16:18:03 +0100489roundtrip($testClient, 'testI64', $num);
Mark Slee6e536442006-06-30 18:28:50 +0000490
Mark Sleed3d733a2006-09-01 22:19:06 +0000491$transport->close();
Nobuaki Sukegawae4ba1642016-07-22 18:09:32 +0900492exit($exitcode);