blob: 4ec4eab26f8e21c89f20c9907f1871b07d6af9c3 [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
Roger Meier41ad4342015-03-24 22:30:40 +010063foreach ($argv as $arg) {
64 if (substr($arg, 0, 7) == '--port=') {
65 $port = substr($arg, 7);
66 } else if (substr($arg, 0, 11) == '--transport=') {
67 $MODE = substr($arg, 11);
68 }
69}
70
Mark Slee1dd819c2006-10-26 04:56:18 +000071$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +000072
Mark Slee6e536442006-06-30 18:28:50 +000073$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +000074$socket = new TSocketPool($hosts, $port);
75$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +000076
Mark Sleed3d733a2006-09-01 22:19:06 +000077if ($MODE == 'inline') {
78 $transport = $socket;
Roger Meier21c0a852012-09-05 19:47:14 +000079 $testClient = new \ThriftTest\ThriftTestClient($transport);
Bryan Duxburya971fb02011-03-04 00:49:40 +000080} else if ($MODE == 'framed') {
81 $framedSocket = new TFramedTransport($socket);
82 $transport = $framedSocket;
83 $protocol = new TBinaryProtocol($transport);
Roger Meier21c0a852012-09-05 19:47:14 +000084 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000085} else {
86 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
87 $transport = $bufferedSocket;
Mark Slee1dd819c2006-10-26 04:56:18 +000088 $protocol = new TBinaryProtocol($transport);
Roger Meier21c0a852012-09-05 19:47:14 +000089 $testClient = new \ThriftTest\ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000090}
91
92$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +000093
94$start = microtime(true);
95
96/**
97 * VOID TEST
98 */
99print_r("testVoid()");
100$testClient->testVoid();
101print_r(" = void\n");
102
103/**
104 * STRING TEST
105 */
106print_r("testString(\"Test\")");
107$s = $testClient->testString("Test");
108print_r(" = \"$s\"\n");
Mark Slee5b743072007-11-13 04:00:29 +0000109
Mark Slee6e536442006-06-30 18:28:50 +0000110/**
111 * BYTE TEST
112 */
113print_r("testByte(1)");
114$u8 = $testClient->testByte(1);
115print_r(" = $u8\n");
116
117/**
118 * I32 TEST
119 */
120print_r("testI32(-1)");
121$i32 = $testClient->testI32(-1);
122print_r(" = $i32\n");
123
124/**
125 * I64 TEST
126 */
127print_r("testI64(-34359738368)");
128$i64 = $testClient->testI64(-34359738368);
129print_r(" = $i64\n");
130
131/**
Mark Sleec98d0502006-09-06 02:42:25 +0000132 * DOUBLE TEST
133 */
134print_r("testDouble(-852.234234234)");
135$dub = $testClient->testDouble(-852.234234234);
136print_r(" = $dub\n");
137
138/**
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100139 * BINARY TEST -- TODO
140 */
141
142/**
Mark Slee6e536442006-06-30 18:28:50 +0000143 * STRUCT TEST
144 */
145print_r("testStruct({\"Zero\", 1, -3, -5})");
Roger Meier21c0a852012-09-05 19:47:14 +0000146$out = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000147$out->string_thing = "Zero";
148$out->byte_thing = 1;
149$out->i32_thing = -3;
150$out->i64_thing = -5;
151$in = $testClient->testStruct($out);
152print_r(" = {\"".$in->string_thing."\", ".
153 $in->byte_thing.", ".
154 $in->i32_thing.", ".
155 $in->i64_thing."}\n");
156
157/**
158 * NESTED STRUCT TEST
159 */
160print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Roger Meier21c0a852012-09-05 19:47:14 +0000161$out2 = new \ThriftTest\Xtruct2();
Mark Slee6e536442006-06-30 18:28:50 +0000162$out2->byte_thing = 1;
163$out2->struct_thing = $out;
164$out2->i32_thing = 5;
165$in2 = $testClient->testNest($out2);
166$in = $in2->struct_thing;
167print_r(" = {".$in2->byte_thing.", {\"".
168 $in->string_thing."\", ".
169 $in->byte_thing.", ".
170 $in->i32_thing.", ".
171 $in->i64_thing."}, ".
172 $in2->i32_thing."}\n");
173
174/**
175 * MAP TEST
176 */
177$mapout = array();
178for ($i = 0; $i < 5; ++$i) {
179 $mapout[$i] = $i-10;
180}
181print_r("testMap({");
182$first = true;
183foreach ($mapout as $key => $val) {
184 if ($first) {
185 $first = false;
186 } else {
187 print_r(", ");
188 }
189 print_r("$key => $val");
190}
191print_r("})");
192
193$mapin = $testClient->testMap($mapout);
194print_r(" = {");
195$first = true;
196foreach ($mapin as $key => $val) {
197 if ($first) {
198 $first = false;
199 } else {
200 print_r(", ");
201 }
202 print_r("$key => $val");
203}
204print_r("}\n");
205
206/**
207 * SET TEST
208 */
209$setout = array();;
210for ($i = -2; $i < 3; ++$i) {
211 $setout []= $i;
212}
213print_r("testSet({");
214$first = true;
215foreach ($setout as $val) {
216 if ($first) {
217 $first = false;
218 } else {
219 print_r(", ");
220 }
221 print_r($val);
222}
223print_r("})");
224$setin = $testClient->testSet($setout);
225print_r(" = {");
226$first = true;
227foreach ($setin as $val) {
228 if ($first) {
229 $first = false;
230 } else {
231 print_r(", ");
232 }
233 print_r($val);
234}
235print_r("}\n");
236
237/**
238 * LIST TEST
239 */
240$listout = array();
241for ($i = -2; $i < 3; ++$i) {
242 $listout []= $i;
243}
244print_r("testList({");
245$first = true;
246foreach ($listout as $val) {
247 if ($first) {
248 $first = false;
249 } else {
250 print_r(", ");
251 }
252 print_r($val);
253}
254print_r("})");
255$listin = $testClient->testList($listout);
256print_r(" = {");
257$first = true;
258foreach ($listin as $val) {
259 if ($first) {
260 $first = false;
261 } else {
262 print_r(", ");
263 }
264 print_r($val);
265}
266print_r("}\n");
267
268/**
269 * ENUM TEST
270 */
271print_r("testEnum(ONE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100272$ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000273print_r(" = $ret\n");
274
275print_r("testEnum(TWO)");
Roger Meier87afaac2013-01-06 20:10:42 +0100276$ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000277print_r(" = $ret\n");
278
279print_r("testEnum(THREE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100280$ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000281print_r(" = $ret\n");
282
283print_r("testEnum(FIVE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100284$ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000285print_r(" = $ret\n");
286
287print_r("testEnum(EIGHT)");
Roger Meier87afaac2013-01-06 20:10:42 +0100288$ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT);
Mark Slee6e536442006-06-30 18:28:50 +0000289print_r(" = $ret\n");
290
291/**
292 * TYPEDEF TEST
293 */
294print_r("testTypedef(309858235082523)");
295$uid = $testClient->testTypedef(309858235082523);
296print_r(" = $uid\n");
297
298/**
299 * NESTED MAP TEST
300 */
301print_r("testMapMap(1)");
302$mm = $testClient->testMapMap(1);
303print_r(" = {");
304foreach ($mm as $key => $val) {
305 print_r("$key => {");
306 foreach ($val as $k2 => $v2) {
307 print_r("$k2 => $v2, ");
308 }
309 print_r("}, ");
310}
311print_r("}\n");
312
313/**
314 * INSANITY TEST
315 */
Roger Meier21c0a852012-09-05 19:47:14 +0000316$insane = new \ThriftTest\Insanity();
Roger Meier87afaac2013-01-06 20:10:42 +0100317$insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
Roger Meier21c0a852012-09-05 19:47:14 +0000318$truck = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000319$truck->string_thing = "Truck";
320$truck->byte_thing = 8;
321$truck->i32_thing = 8;
322$truck->i64_thing = 8;
323$insane->xtructs []= $truck;
324print_r("testInsanity()");
325$whoa = $testClient->testInsanity($insane);
326print_r(" = {");
327foreach ($whoa as $key => $val) {
328 print_r("$key => {");
329 foreach ($val as $k2 => $v2) {
330 print_r("$k2 => {");
331 $userMap = $v2->userMap;
332 print_r("{");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000333 if (is_array($userMap)) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000334 foreach ($userMap as $k3 => $v3) {
335 print_r("$k3 => $v3, ");
336 }
Mark Slee6e536442006-06-30 18:28:50 +0000337 }
338 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000339
Mark Slee6e536442006-06-30 18:28:50 +0000340 $xtructs = $v2->xtructs;
341 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000342 if (is_array($xtructs)) {
343 foreach ($xtructs as $x) {
344 print_r("{\"".$x->string_thing."\", ".
345 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
346 }
Mark Slee6e536442006-06-30 18:28:50 +0000347 }
348 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000349
Mark Slee6e536442006-06-30 18:28:50 +0000350 print_r("}, ");
351 }
352 print_r("}, ");
353}
354print_r("}\n");
355
Mark Sleed3d733a2006-09-01 22:19:06 +0000356/**
357 * EXCEPTION TEST
358 */
359print_r("testException('Xception')");
360try {
361 $testClient->testException('Xception');
362 print_r(" void\nFAILURE\n");
Roger Meier87afaac2013-01-06 20:10:42 +0100363} catch (\ThriftTest\Xception $x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000364 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
365}
366
Mark Slee6e536442006-06-30 18:28:50 +0000367
368/**
369 * Normal tests done.
370 */
371
372$stop = microtime(true);
373$elp = round(1000*($stop - $start), 0);
374print_r("Total time: $elp ms\n");
375
376/**
377 * Extraneous "I don't trust PHP to pack/unpack integer" tests
378 */
379
380// Max I32
381$num = pow(2, 30) + (pow(2, 30) - 1);
382$num2 = $testClient->testI32($num);
383if ($num != $num2) {
384 print "Missed $num = $num2\n";
385}
386
387// Min I32
388$num = 0 - pow(2, 31);
389$num2 = $testClient->testI32($num);
390if ($num != $num2) {
391 print "Missed $num = $num2\n";
392}
393
394// Max I64
395$num = pow(2, 62) + (pow(2, 62) - 1);
396$num2 = $testClient->testI64($num);
397if ($num != $num2) {
398 print "Missed $num = $num2\n";
399}
400
401// Min I64
402$num = 0 - pow(2, 63);
403$num2 = $testClient->testI64($num);
404if ($num != $num2) {
405 print "Missed $num = $num2\n";
406}
407
Mark Sleed3d733a2006-09-01 22:19:06 +0000408$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000409return;
410