blob: c334aeea26584cd78e646ea1c45f0725c64bb03b [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/**
Mark Slee6e536442006-06-30 18:28:50 +0000131 * STRUCT TEST
132 */
133print_r("testStruct({\"Zero\", 1, -3, -5})");
Roger Meier21c0a852012-09-05 19:47:14 +0000134$out = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000135$out->string_thing = "Zero";
136$out->byte_thing = 1;
137$out->i32_thing = -3;
138$out->i64_thing = -5;
139$in = $testClient->testStruct($out);
140print_r(" = {\"".$in->string_thing."\", ".
141 $in->byte_thing.", ".
142 $in->i32_thing.", ".
143 $in->i64_thing."}\n");
144
145/**
146 * NESTED STRUCT TEST
147 */
148print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
Roger Meier21c0a852012-09-05 19:47:14 +0000149$out2 = new \ThriftTest\Xtruct2();
Mark Slee6e536442006-06-30 18:28:50 +0000150$out2->byte_thing = 1;
151$out2->struct_thing = $out;
152$out2->i32_thing = 5;
153$in2 = $testClient->testNest($out2);
154$in = $in2->struct_thing;
155print_r(" = {".$in2->byte_thing.", {\"".
156 $in->string_thing."\", ".
157 $in->byte_thing.", ".
158 $in->i32_thing.", ".
159 $in->i64_thing."}, ".
160 $in2->i32_thing."}\n");
161
162/**
163 * MAP TEST
164 */
165$mapout = array();
166for ($i = 0; $i < 5; ++$i) {
167 $mapout[$i] = $i-10;
168}
169print_r("testMap({");
170$first = true;
171foreach ($mapout as $key => $val) {
172 if ($first) {
173 $first = false;
174 } else {
175 print_r(", ");
176 }
177 print_r("$key => $val");
178}
179print_r("})");
180
181$mapin = $testClient->testMap($mapout);
182print_r(" = {");
183$first = true;
184foreach ($mapin as $key => $val) {
185 if ($first) {
186 $first = false;
187 } else {
188 print_r(", ");
189 }
190 print_r("$key => $val");
191}
192print_r("}\n");
193
194/**
195 * SET TEST
196 */
197$setout = array();;
198for ($i = -2; $i < 3; ++$i) {
199 $setout []= $i;
200}
201print_r("testSet({");
202$first = true;
203foreach ($setout as $val) {
204 if ($first) {
205 $first = false;
206 } else {
207 print_r(", ");
208 }
209 print_r($val);
210}
211print_r("})");
212$setin = $testClient->testSet($setout);
213print_r(" = {");
214$first = true;
215foreach ($setin as $val) {
216 if ($first) {
217 $first = false;
218 } else {
219 print_r(", ");
220 }
221 print_r($val);
222}
223print_r("}\n");
224
225/**
226 * LIST TEST
227 */
228$listout = array();
229for ($i = -2; $i < 3; ++$i) {
230 $listout []= $i;
231}
232print_r("testList({");
233$first = true;
234foreach ($listout as $val) {
235 if ($first) {
236 $first = false;
237 } else {
238 print_r(", ");
239 }
240 print_r($val);
241}
242print_r("})");
243$listin = $testClient->testList($listout);
244print_r(" = {");
245$first = true;
246foreach ($listin as $val) {
247 if ($first) {
248 $first = false;
249 } else {
250 print_r(", ");
251 }
252 print_r($val);
253}
254print_r("}\n");
255
256/**
257 * ENUM TEST
258 */
259print_r("testEnum(ONE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100260$ret = $testClient->testEnum(\ThriftTest\Numberz::ONE);
Mark Slee6e536442006-06-30 18:28:50 +0000261print_r(" = $ret\n");
262
263print_r("testEnum(TWO)");
Roger Meier87afaac2013-01-06 20:10:42 +0100264$ret = $testClient->testEnum(\ThriftTest\Numberz::TWO);
Mark Slee6e536442006-06-30 18:28:50 +0000265print_r(" = $ret\n");
266
267print_r("testEnum(THREE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100268$ret = $testClient->testEnum(\ThriftTest\Numberz::THREE);
Mark Slee6e536442006-06-30 18:28:50 +0000269print_r(" = $ret\n");
270
271print_r("testEnum(FIVE)");
Roger Meier87afaac2013-01-06 20:10:42 +0100272$ret = $testClient->testEnum(\ThriftTest\Numberz::FIVE);
Mark Slee6e536442006-06-30 18:28:50 +0000273print_r(" = $ret\n");
274
275print_r("testEnum(EIGHT)");
Roger Meier87afaac2013-01-06 20:10:42 +0100276$ret = $testClient->testEnum(\ThriftTest\Numberz::EIGHT);
Mark Slee6e536442006-06-30 18:28:50 +0000277print_r(" = $ret\n");
278
279/**
280 * TYPEDEF TEST
281 */
282print_r("testTypedef(309858235082523)");
283$uid = $testClient->testTypedef(309858235082523);
284print_r(" = $uid\n");
285
286/**
287 * NESTED MAP TEST
288 */
289print_r("testMapMap(1)");
290$mm = $testClient->testMapMap(1);
291print_r(" = {");
292foreach ($mm as $key => $val) {
293 print_r("$key => {");
294 foreach ($val as $k2 => $v2) {
295 print_r("$k2 => $v2, ");
296 }
297 print_r("}, ");
298}
299print_r("}\n");
300
301/**
302 * INSANITY TEST
303 */
Roger Meier21c0a852012-09-05 19:47:14 +0000304$insane = new \ThriftTest\Insanity();
Roger Meier87afaac2013-01-06 20:10:42 +0100305$insane->userMap[\ThriftTest\Numberz::FIVE] = 5000;
Roger Meier21c0a852012-09-05 19:47:14 +0000306$truck = new \ThriftTest\Xtruct();
Mark Slee6e536442006-06-30 18:28:50 +0000307$truck->string_thing = "Truck";
308$truck->byte_thing = 8;
309$truck->i32_thing = 8;
310$truck->i64_thing = 8;
311$insane->xtructs []= $truck;
312print_r("testInsanity()");
313$whoa = $testClient->testInsanity($insane);
314print_r(" = {");
315foreach ($whoa as $key => $val) {
316 print_r("$key => {");
317 foreach ($val as $k2 => $v2) {
318 print_r("$k2 => {");
319 $userMap = $v2->userMap;
320 print_r("{");
Bryan Duxburya971fb02011-03-04 00:49:40 +0000321 if (is_array($userMap)) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000322 foreach ($userMap as $k3 => $v3) {
323 print_r("$k3 => $v3, ");
324 }
Mark Slee6e536442006-06-30 18:28:50 +0000325 }
326 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000327
Mark Slee6e536442006-06-30 18:28:50 +0000328 $xtructs = $v2->xtructs;
329 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000330 if (is_array($xtructs)) {
331 foreach ($xtructs as $x) {
332 print_r("{\"".$x->string_thing."\", ".
333 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
334 }
Mark Slee6e536442006-06-30 18:28:50 +0000335 }
336 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000337
Mark Slee6e536442006-06-30 18:28:50 +0000338 print_r("}, ");
339 }
340 print_r("}, ");
341}
342print_r("}\n");
343
Mark Sleed3d733a2006-09-01 22:19:06 +0000344/**
345 * EXCEPTION TEST
346 */
347print_r("testException('Xception')");
348try {
349 $testClient->testException('Xception');
350 print_r(" void\nFAILURE\n");
Roger Meier87afaac2013-01-06 20:10:42 +0100351} catch (\ThriftTest\Xception $x) {
Mark Sleed3d733a2006-09-01 22:19:06 +0000352 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
353}
354
Mark Slee6e536442006-06-30 18:28:50 +0000355
356/**
357 * Normal tests done.
358 */
359
360$stop = microtime(true);
361$elp = round(1000*($stop - $start), 0);
362print_r("Total time: $elp ms\n");
363
364/**
365 * Extraneous "I don't trust PHP to pack/unpack integer" tests
366 */
367
368// Max I32
369$num = pow(2, 30) + (pow(2, 30) - 1);
370$num2 = $testClient->testI32($num);
371if ($num != $num2) {
372 print "Missed $num = $num2\n";
373}
374
375// Min I32
376$num = 0 - pow(2, 31);
377$num2 = $testClient->testI32($num);
378if ($num != $num2) {
379 print "Missed $num = $num2\n";
380}
381
382// Max I64
383$num = pow(2, 62) + (pow(2, 62) - 1);
384$num2 = $testClient->testI64($num);
385if ($num != $num2) {
386 print "Missed $num = $num2\n";
387}
388
389// Min I64
390$num = 0 - pow(2, 63);
391$num2 = $testClient->testI64($num);
392if ($num != $num2) {
393 print "Missed $num = $num2\n";
394}
395
Mark Sleed3d733a2006-09-01 22:19:06 +0000396$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000397return;
398