blob: bab4389a05748fc53666c4ec388288c8716e996c [file] [log] [blame]
Robert Lu12f124c2018-01-25 23:19:41 +08001<?php
2
3/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 *
21 * @package thrift.test
22 */
23
24namespace Test\Thrift\Protocol;
25
26use PHPUnit\Framework\TestCase;
27use Test\Thrift\Fixtures;
28use Thrift\ClassLoader\ThriftClassLoader;
29use Thrift\Protocol\TJSONProtocol;
30use Thrift\Transport\TMemoryBuffer;
31
32require_once __DIR__ . '/../../../../vendor/autoload.php';
33
34/***
35 * This test suite depends on running the compiler against the
36 * standard ThriftTest.thrift file:
37 *
38 * lib/php/test$ ../../../compiler/cpp/thrift --gen php -r \
39 * --out ./packages ../../../test/ThriftTest.thrift
40 *
41 * @runTestsInSeparateProcesses
42 */
43class TJSONProtocolTest extends TestCase
44{
45 private $transport;
46 private $protocol;
47
48 public static function setUpBeforeClass()
49 {
50 $loader = new ThriftClassLoader();
51 $loader->registerDefinition('ThriftTest', __DIR__ . '/../packages');
52 $loader->register();
53
54 Fixtures::populateTestArgs();
55 TJSONProtocolFixtures::populateTestArgsJSON();
56 }
57
58 public function setUp()
59 {
60 $this->transport = new TMemoryBuffer();
61 $this->protocol = new TJSONProtocol($this->transport);
62 $this->transport->open();
63 }
64
65 /**
66 * WRITE TESTS
67 */
68 public function testVoidWrite()
69 {
70 $args = new \ThriftTest\ThriftTest_testVoid_args();
71 $args->write($this->protocol);
72
73 $actual = $this->transport->read(Fixtures::$bufsize);
74 $expected = TJSONProtocolFixtures::$testArgsJSON['testVoid'];
75
76 $this->assertEquals($expected, $actual);
77 }
78
79 public function testString1Write()
80 {
81 $args = new \ThriftTest\ThriftTest_testString_args();
82 $args->thing = Fixtures::$testArgs['testString1'];
83 $args->write($this->protocol);
84
85 $actual = $this->transport->read(Fixtures::$bufsize);
86 $expected = TJSONProtocolFixtures::$testArgsJSON['testString1'];
87
88 $this->assertEquals($expected, $actual);
89 }
90
91 public function testString2Write()
92 {
93 $args = new \ThriftTest\ThriftTest_testString_args();
94 $args->thing = Fixtures::$testArgs['testString2'];
95 $args->write($this->protocol);
96
97 $actual = $this->transport->read(Fixtures::$bufsize);
98 $expected = TJSONProtocolFixtures::$testArgsJSON['testString2'];
99
100 $this->assertEquals($expected, $actual);
101 }
102
103 public function testDoubleWrite()
104 {
105 $args = new \ThriftTest\ThriftTest_testDouble_args();
106 $args->thing = Fixtures::$testArgs['testDouble'];
107 $args->write($this->protocol);
108
109 $actual = $this->transport->read(Fixtures::$bufsize);
110 $expected = TJSONProtocolFixtures::$testArgsJSON['testDouble'];
111
112 $this->assertEquals($expected, $actual);
113 }
114
115 public function testByteWrite()
116 {
117 $args = new \ThriftTest\ThriftTest_testByte_args();
118 $args->thing = Fixtures::$testArgs['testByte'];
119 $args->write($this->protocol);
120
121 $actual = $this->transport->read(Fixtures::$bufsize);
122 $expected = TJSONProtocolFixtures::$testArgsJSON['testByte'];
123
124 $this->assertEquals($expected, $actual);
125 }
126
127 public function testI32Write()
128 {
129 $args = new \ThriftTest\ThriftTest_testI32_args();
130 $args->thing = Fixtures::$testArgs['testI32'];
131 $args->write($this->protocol);
132
133 $actual = $this->transport->read(Fixtures::$bufsize);
134 $expected = TJSONProtocolFixtures::$testArgsJSON['testI32'];
135
136 $this->assertEquals($expected, $actual);
137 }
138
139 public function testI64Write()
140 {
141 $args = new \ThriftTest\ThriftTest_testI64_args();
142 $args->thing = Fixtures::$testArgs['testI64'];
143 $args->write($this->protocol);
144
145 $actual = $this->transport->read(Fixtures::$bufsize);
146 $expected = TJSONProtocolFixtures::$testArgsJSON['testI64'];
147
148 $this->assertEquals($expected, $actual);
149 }
150
151 public function testStructWrite()
152 {
153 $args = new \ThriftTest\ThriftTest_testStruct_args();
154 $args->thing = Fixtures::$testArgs['testStruct'];
155
156 $args->write($this->protocol);
157
158 $actual = $this->transport->read(Fixtures::$bufsize);
159 $expected = TJSONProtocolFixtures::$testArgsJSON['testStruct'];
160
161 $this->assertEquals($expected, $actual);
162 }
163
164 public function testNestWrite()
165 {
166 $args = new \ThriftTest\ThriftTest_testNest_args();
167 $args->thing = Fixtures::$testArgs['testNest'];
168
169 $args->write($this->protocol);
170
171 $actual = $this->transport->read(Fixtures::$bufsize);
172 $expected = TJSONProtocolFixtures::$testArgsJSON['testNest'];
173
174 $this->assertEquals($expected, $actual);
175 }
176
177 public function testMapWrite()
178 {
179 $args = new \ThriftTest\ThriftTest_testMap_args();
180 $args->thing = Fixtures::$testArgs['testMap'];
181
182 $args->write($this->protocol);
183
184 $actual = $this->transport->read(Fixtures::$bufsize);
185 $expected = TJSONProtocolFixtures::$testArgsJSON['testMap'];
186
187 $this->assertEquals($expected, $actual);
188 }
189
190 public function testStringMapWrite()
191 {
192 $args = new \ThriftTest\ThriftTest_testStringMap_args();
193 $args->thing = Fixtures::$testArgs['testStringMap'];
194
195 $args->write($this->protocol);
196
197 $actual = $this->transport->read(Fixtures::$bufsize);
198 $expected = TJSONProtocolFixtures::$testArgsJSON['testStringMap'];
199
200 /*
201 * The $actual returns unescaped string.
202 * It is required to to decode then encode it again
203 * to get the expected escaped unicode.
204 */
205 $this->assertEquals($expected, json_encode(json_decode($actual)));
206 }
207
208 public function testSetWrite()
209 {
210 $args = new \ThriftTest\ThriftTest_testSet_args();
211 $args->thing = Fixtures::$testArgs['testSet'];
212
213 $args->write($this->protocol);
214
215 $actual = $this->transport->read(Fixtures::$bufsize);
216 $expected = TJSONProtocolFixtures::$testArgsJSON['testSet'];
217
218 $this->assertEquals($expected, $actual);
219 }
220
221 public function testListWrite()
222 {
223 $args = new \ThriftTest\ThriftTest_testList_args();
224 $args->thing = Fixtures::$testArgs['testList'];
225
226 $args->write($this->protocol);
227
228 $actual = $this->transport->read(Fixtures::$bufsize);
229 $expected = TJSONProtocolFixtures::$testArgsJSON['testList'];
230
231 $this->assertEquals($expected, $actual);
232 }
233
234 public function testEnumWrite()
235 {
236 $args = new \ThriftTest\ThriftTest_testEnum_args();
237 $args->thing = Fixtures::$testArgs['testEnum'];
238
239 $args->write($this->protocol);
240
241 $actual = $this->transport->read(Fixtures::$bufsize);
242 $expected = TJSONProtocolFixtures::$testArgsJSON['testEnum'];
243
244 $this->assertEquals($expected, $actual);
245 }
246
247 public function testTypedefWrite()
248 {
249 $args = new \ThriftTest\ThriftTest_testTypedef_args();
250 $args->thing = Fixtures::$testArgs['testTypedef'];
251
252 $args->write($this->protocol);
253
254 $actual = $this->transport->read(Fixtures::$bufsize);
255 $expected = TJSONProtocolFixtures::$testArgsJSON['testTypedef'];
256
257 $this->assertEquals($expected, $actual);
258 }
259
260 /**
261 * READ TESTS
262 */
263 public function testVoidRead()
264 {
265 $this->transport->write(
266 TJSONProtocolFixtures::$testArgsJSON['testVoid']
267 );
268 $args = new \ThriftTest\ThriftTest_testVoid_args();
269 $args->read($this->protocol);
270 }
271
272 public function testString1Read()
273 {
274 $this->transport->write(
275 TJSONProtocolFixtures::$testArgsJSON['testString1']
276 );
277 $args = new \ThriftTest\ThriftTest_testString_args();
278 $args->read($this->protocol);
279
280 $actual = $args->thing;
281 $expected = Fixtures::$testArgs['testString1'];
282
283 $this->assertEquals($expected, $actual);
284 }
285
286 public function testString2Read()
287 {
288 $this->transport->write(
289 TJSONProtocolFixtures::$testArgsJSON['testString2']
290 );
291 $args = new \ThriftTest\ThriftTest_testString_args();
292 $args->read($this->protocol);
293
294 $actual = $args->thing;
295 $expected = Fixtures::$testArgs['testString2'];
296
297 $this->assertEquals($expected, $actual);
298 }
299
300 public function testString3Write()
301 {
302 $args = new \ThriftTest\ThriftTest_testString_args();
303 $args->thing = Fixtures::$testArgs['testString3'];
304 $args->write($this->protocol);
305
306 $actual = $this->transport->read(Fixtures::$bufsize);
307 $expected = TJSONProtocolFixtures::$testArgsJSON['testString3'];
308
309 $this->assertEquals($expected, $actual);
310 }
311
312 public function testString4Write()
313 {
314 $args = new \ThriftTest\ThriftTest_testString_args();
315 $args->thing = Fixtures::$testArgs['testUnicodeStringWithNonBMP'];
316 $args->write($this->protocol);
317
318 $actual = $this->transport->read(Fixtures::$bufsize);
319 $expected = TJSONProtocolFixtures::$testArgsJSON['testUnicodeStringWithNonBMP'];
320
321 $this->assertEquals($expected, $actual);
322 }
323
324 public function testDoubleRead()
325 {
326 $this->transport->write(
327 TJSONProtocolFixtures::$testArgsJSON['testDouble']
328 );
329 $args = new \ThriftTest\ThriftTest_testDouble_args();
330 $args->read($this->protocol);
331
332 $actual = $args->thing;
333 $expected = Fixtures::$testArgs['testDouble'];
334
335 $this->assertEquals($expected, $actual);
336 }
337
338 public function testByteRead()
339 {
340 $this->transport->write(
341 TJSONProtocolFixtures::$testArgsJSON['testByte']
342 );
343 $args = new \ThriftTest\ThriftTest_testByte_args();
344 $args->read($this->protocol);
345
346 $actual = $args->thing;
347 $expected = Fixtures::$testArgs['testByte'];
348
349 $this->assertEquals($expected, $actual);
350 }
351
352 public function testI32Read()
353 {
354 $this->transport->write(
355 TJSONProtocolFixtures::$testArgsJSON['testI32']
356 );
357 $args = new \ThriftTest\ThriftTest_testI32_args();
358 $args->read($this->protocol);
359
360 $actual = $args->thing;
361 $expected = Fixtures::$testArgs['testI32'];
362
363 $this->assertEquals($expected, $actual);
364 }
365
366 public function testI64Read()
367 {
368 $this->transport->write(
369 TJSONProtocolFixtures::$testArgsJSON['testI64']
370 );
371 $args = new \ThriftTest\ThriftTest_testI64_args();
372 $args->read($this->protocol);
373
374 $actual = $args->thing;
375 $expected = Fixtures::$testArgs['testI64'];
376
377 $this->assertEquals($expected, $actual);
378 }
379
380 public function testStructRead()
381 {
382 $this->transport->write(
383 TJSONProtocolFixtures::$testArgsJSON['testStruct']
384 );
385 $args = new \ThriftTest\ThriftTest_testStruct_args();
386 $args->read($this->protocol);
387
388 $actual = $args->thing;
389 $expected = Fixtures::$testArgs['testStruct'];
390
391 $this->assertEquals($expected, $actual);
392 }
393
394 public function testNestRead()
395 {
396 $this->transport->write(
397 TJSONProtocolFixtures::$testArgsJSON['testNest']
398 );
399 $args = new \ThriftTest\ThriftTest_testNest_args();
400 $args->read($this->protocol);
401
402 $actual = $args->thing;
403 $expected = Fixtures::$testArgs['testNest'];
404
405 $this->assertEquals($expected, $actual);
406 }
407
408 public function testMapRead()
409 {
410 $this->transport->write(
411 TJSONProtocolFixtures::$testArgsJSON['testMap']
412 );
413 $args = new \ThriftTest\ThriftTest_testMap_args();
414 $args->read($this->protocol);
415
416 $actual = $args->thing;
417 $expected = Fixtures::$testArgs['testMap'];
418
419 $this->assertEquals($expected, $actual);
420 }
421
422 public function testStringMapRead()
423 {
424 $this->transport->write(
425 TJSONProtocolFixtures::$testArgsJSON['testStringMap']
426 );
427 $args = new \ThriftTest\ThriftTest_testStringMap_args();
428 $args->read($this->protocol);
429
430 $actual = $args->thing;
431 $expected = Fixtures::$testArgs['testStringMap'];
432
433 $this->assertEquals($expected, $actual);
434 }
435
436 public function testSetRead()
437 {
438 $this->transport->write(
439 TJSONProtocolFixtures::$testArgsJSON['testSet']
440 );
441 $args = new \ThriftTest\ThriftTest_testSet_args();
442 $args->read($this->protocol);
443
444 $actual = $args->thing;
445 $expected = Fixtures::$testArgs['testSet'];
446
447 $this->assertEquals($expected, $actual);
448 }
449
450 public function testListRead()
451 {
452 $this->transport->write(
453 TJSONProtocolFixtures::$testArgsJSON['testList']
454 );
455 $args = new \ThriftTest\ThriftTest_testList_args();
456 $args->read($this->protocol);
457
458 $actual = $args->thing;
459 $expected = Fixtures::$testArgs['testList'];
460
461 $this->assertEquals($expected, $actual);
462 }
463
464 public function testEnumRead()
465 {
466 $this->transport->write(
467 TJSONProtocolFixtures::$testArgsJSON['testEnum']
468 );
469 $args = new \ThriftTest\ThriftTest_testEnum_args();
470 $args->read($this->protocol);
471
472 $actual = $args->thing;
473 $expected = Fixtures::$testArgs['testEnum'];
474
475 $this->assertEquals($expected, $actual);
476 }
477
478 public function testTypedefRead()
479 {
480 $this->transport->write(
481 TJSONProtocolFixtures::$testArgsJSON['testTypedef']
482 );
483 $args = new \ThriftTest\ThriftTest_testTypedef_args();
484 $args->read($this->protocol);
485
486 $actual = $args->thing;
487 $expected = Fixtures::$testArgs['testTypedef'];
488
489 $this->assertEquals($expected, $actual);
490 }
491
492 public function testMapMapRead()
493 {
494 $this->transport->write(
495 TJSONProtocolFixtures::$testArgsJSON['testMapMap']
496 );
497 $result = new \ThriftTest\ThriftTest_testMapMap_result();
498 $result->read($this->protocol);
499
500 $actual = $result->success;
501 $expected = Fixtures::$testArgs['testMapMapExpectedResult'];
502
503 $this->assertEquals($expected, $actual);
504 }
505
506 public function testInsanityRead()
507 {
508 $this->transport->write(
509 TJSONProtocolFixtures::$testArgsJSON['testInsanity']
510 );
511 $result = new \ThriftTest\ThriftTest_testInsanity_result();
512 $result->read($this->protocol);
513
514 $actual = $result->success;
515 $expected = Fixtures::$testArgs['testInsanityExpectedResult'];
516
517 $this->assertEquals($expected, $actual);
518 }
519}