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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * @package thrift.test
24 namespace Test\Thrift\Protocol
;
26 use PHPUnit\Framework\TestCase
;
27 use Test\Thrift\Fixtures
;
28 use Thrift\Protocol\TJSONProtocol
;
29 use Thrift\Transport\TMemoryBuffer
;
31 require __DIR__
. '/../../../../vendor/autoload.php';
34 * This test suite depends on running the compiler against the
35 * standard ThriftTest.thrift file:
37 * lib/php/test$ ../../../compiler/cpp/thrift --gen php -r \
38 * --out ./packages ../../../test/ThriftTest.thrift
40 * @runTestsInSeparateProcesses
42 class TJSONProtocolTest
extends TestCase
47 public static function setUpBeforeClass()
49 /** @var \Composer\Autoload\ClassLoader $loader */
50 $loader = require __DIR__
. '/../../../../vendor/autoload.php';
51 $loader->addPsr4('', __DIR__
. '/../packages/php');
53 Fixtures
::populateTestArgs();
54 TJSONProtocolFixtures
::populateTestArgsJSON();
57 public function setUp()
59 $this->transport
= new TMemoryBuffer();
60 $this->protocol
= new TJSONProtocol($this->transport
);
61 $this->transport
->open();
67 public function testVoidWrite()
69 $args = new \ThriftTest\
ThriftTest_testVoid_args();
70 $args->write($this->protocol
);
72 $actual = $this->transport
->read(Fixtures
::$bufsize);
73 $expected = TJSONProtocolFixtures
::$testArgsJSON['testVoid'];
75 $this->assertEquals($expected, $actual);
78 public function testString1Write()
80 $args = new \ThriftTest\
ThriftTest_testString_args();
81 $args->thing
= Fixtures
::$testArgs['testString1'];
82 $args->write($this->protocol
);
84 $actual = $this->transport
->read(Fixtures
::$bufsize);
85 $expected = TJSONProtocolFixtures
::$testArgsJSON['testString1'];
87 $this->assertEquals($expected, $actual);
90 public function testString2Write()
92 $args = new \ThriftTest\
ThriftTest_testString_args();
93 $args->thing
= Fixtures
::$testArgs['testString2'];
94 $args->write($this->protocol
);
96 $actual = $this->transport
->read(Fixtures
::$bufsize);
97 $expected = TJSONProtocolFixtures
::$testArgsJSON['testString2'];
99 $this->assertEquals($expected, $actual);
102 public function testDoubleWrite()
104 $args = new \ThriftTest\
ThriftTest_testDouble_args();
105 $args->thing
= Fixtures
::$testArgs['testDouble'];
106 $args->write($this->protocol
);
108 $actual = $this->transport
->read(Fixtures
::$bufsize);
109 $expected = TJSONProtocolFixtures
::$testArgsJSON['testDouble'];
111 $this->assertEquals($expected, $actual);
114 public function testByteWrite()
116 $args = new \ThriftTest\
ThriftTest_testByte_args();
117 $args->thing
= Fixtures
::$testArgs['testByte'];
118 $args->write($this->protocol
);
120 $actual = $this->transport
->read(Fixtures
::$bufsize);
121 $expected = TJSONProtocolFixtures
::$testArgsJSON['testByte'];
123 $this->assertEquals($expected, $actual);
126 public function testI32Write()
128 $args = new \ThriftTest\
ThriftTest_testI32_args();
129 $args->thing
= Fixtures
::$testArgs['testI32'];
130 $args->write($this->protocol
);
132 $actual = $this->transport
->read(Fixtures
::$bufsize);
133 $expected = TJSONProtocolFixtures
::$testArgsJSON['testI32'];
135 $this->assertEquals($expected, $actual);
138 public function testI64Write()
140 $args = new \ThriftTest\
ThriftTest_testI64_args();
141 $args->thing
= Fixtures
::$testArgs['testI64'];
142 $args->write($this->protocol
);
144 $actual = $this->transport
->read(Fixtures
::$bufsize);
145 $expected = TJSONProtocolFixtures
::$testArgsJSON['testI64'];
147 $this->assertEquals($expected, $actual);
150 public function testStructWrite()
152 $args = new \ThriftTest\
ThriftTest_testStruct_args();
153 $args->thing
= Fixtures
::$testArgs['testStruct'];
155 $args->write($this->protocol
);
157 $actual = $this->transport
->read(Fixtures
::$bufsize);
158 $expected = TJSONProtocolFixtures
::$testArgsJSON['testStruct'];
160 $this->assertEquals($expected, $actual);
163 public function testNestWrite()
165 $args = new \ThriftTest\
ThriftTest_testNest_args();
166 $args->thing
= Fixtures
::$testArgs['testNest'];
168 $args->write($this->protocol
);
170 $actual = $this->transport
->read(Fixtures
::$bufsize);
171 $expected = TJSONProtocolFixtures
::$testArgsJSON['testNest'];
173 $this->assertEquals($expected, $actual);
176 public function testMapWrite()
178 $args = new \ThriftTest\
ThriftTest_testMap_args();
179 $args->thing
= Fixtures
::$testArgs['testMap'];
181 $args->write($this->protocol
);
183 $actual = $this->transport
->read(Fixtures
::$bufsize);
184 $expected = TJSONProtocolFixtures
::$testArgsJSON['testMap'];
186 $this->assertEquals($expected, $actual);
189 public function testStringMapWrite()
191 $args = new \ThriftTest\
ThriftTest_testStringMap_args();
192 $args->thing
= Fixtures
::$testArgs['testStringMap'];
194 $args->write($this->protocol
);
196 $actual = $this->transport
->read(Fixtures
::$bufsize);
197 $expected = TJSONProtocolFixtures
::$testArgsJSON['testStringMap'];
200 * The $actual returns unescaped string.
201 * It is required to to decode then encode it again
202 * to get the expected escaped unicode.
204 $this->assertEquals($expected, json_encode(json_decode($actual)));
207 public function testSetWrite()
209 $args = new \ThriftTest\
ThriftTest_testSet_args();
210 $args->thing
= Fixtures
::$testArgs['testSet'];
212 $args->write($this->protocol
);
214 $actual = $this->transport
->read(Fixtures
::$bufsize);
215 $expected = TJSONProtocolFixtures
::$testArgsJSON['testSet'];
217 $this->assertEquals($expected, $actual);
220 public function testListWrite()
222 $args = new \ThriftTest\
ThriftTest_testList_args();
223 $args->thing
= Fixtures
::$testArgs['testList'];
225 $args->write($this->protocol
);
227 $actual = $this->transport
->read(Fixtures
::$bufsize);
228 $expected = TJSONProtocolFixtures
::$testArgsJSON['testList'];
230 $this->assertEquals($expected, $actual);
233 public function testEnumWrite()
235 $args = new \ThriftTest\
ThriftTest_testEnum_args();
236 $args->thing
= Fixtures
::$testArgs['testEnum'];
238 $args->write($this->protocol
);
240 $actual = $this->transport
->read(Fixtures
::$bufsize);
241 $expected = TJSONProtocolFixtures
::$testArgsJSON['testEnum'];
243 $this->assertEquals($expected, $actual);
246 public function testTypedefWrite()
248 $args = new \ThriftTest\
ThriftTest_testTypedef_args();
249 $args->thing
= Fixtures
::$testArgs['testTypedef'];
251 $args->write($this->protocol
);
253 $actual = $this->transport
->read(Fixtures
::$bufsize);
254 $expected = TJSONProtocolFixtures
::$testArgsJSON['testTypedef'];
256 $this->assertEquals($expected, $actual);
262 public function testVoidRead()
264 $this->transport
->write(
265 TJSONProtocolFixtures
::$testArgsJSON['testVoid']
267 $args = new \ThriftTest\
ThriftTest_testVoid_args();
268 $args->read($this->protocol
);
271 public function testString1Read()
273 $this->transport
->write(
274 TJSONProtocolFixtures
::$testArgsJSON['testString1']
276 $args = new \ThriftTest\
ThriftTest_testString_args();
277 $args->read($this->protocol
);
279 $actual = $args->thing
;
280 $expected = Fixtures
::$testArgs['testString1'];
282 $this->assertEquals($expected, $actual);
285 public function testString2Read()
287 $this->transport
->write(
288 TJSONProtocolFixtures
::$testArgsJSON['testString2']
290 $args = new \ThriftTest\
ThriftTest_testString_args();
291 $args->read($this->protocol
);
293 $actual = $args->thing
;
294 $expected = Fixtures
::$testArgs['testString2'];
296 $this->assertEquals($expected, $actual);
299 public function testString3Write()
301 $args = new \ThriftTest\
ThriftTest_testString_args();
302 $args->thing
= Fixtures
::$testArgs['testString3'];
303 $args->write($this->protocol
);
305 $actual = $this->transport
->read(Fixtures
::$bufsize);
306 $expected = TJSONProtocolFixtures
::$testArgsJSON['testString3'];
308 $this->assertEquals($expected, $actual);
311 public function testString4Write()
313 $args = new \ThriftTest\
ThriftTest_testString_args();
314 $args->thing
= Fixtures
::$testArgs['testUnicodeStringWithNonBMP'];
315 $args->write($this->protocol
);
317 $actual = $this->transport
->read(Fixtures
::$bufsize);
318 $expected = TJSONProtocolFixtures
::$testArgsJSON['testUnicodeStringWithNonBMP'];
320 $this->assertEquals($expected, $actual);
323 public function testDoubleRead()
325 $this->transport
->write(
326 TJSONProtocolFixtures
::$testArgsJSON['testDouble']
328 $args = new \ThriftTest\
ThriftTest_testDouble_args();
329 $args->read($this->protocol
);
331 $actual = $args->thing
;
332 $expected = Fixtures
::$testArgs['testDouble'];
334 $this->assertEquals($expected, $actual);
337 public function testByteRead()
339 $this->transport
->write(
340 TJSONProtocolFixtures
::$testArgsJSON['testByte']
342 $args = new \ThriftTest\
ThriftTest_testByte_args();
343 $args->read($this->protocol
);
345 $actual = $args->thing
;
346 $expected = Fixtures
::$testArgs['testByte'];
348 $this->assertEquals($expected, $actual);
351 public function testI32Read()
353 $this->transport
->write(
354 TJSONProtocolFixtures
::$testArgsJSON['testI32']
356 $args = new \ThriftTest\
ThriftTest_testI32_args();
357 $args->read($this->protocol
);
359 $actual = $args->thing
;
360 $expected = Fixtures
::$testArgs['testI32'];
362 $this->assertEquals($expected, $actual);
365 public function testI64Read()
367 $this->transport
->write(
368 TJSONProtocolFixtures
::$testArgsJSON['testI64']
370 $args = new \ThriftTest\
ThriftTest_testI64_args();
371 $args->read($this->protocol
);
373 $actual = $args->thing
;
374 $expected = Fixtures
::$testArgs['testI64'];
376 $this->assertEquals($expected, $actual);
379 public function testStructRead()
381 $this->transport
->write(
382 TJSONProtocolFixtures
::$testArgsJSON['testStruct']
384 $args = new \ThriftTest\
ThriftTest_testStruct_args();
385 $args->read($this->protocol
);
387 $actual = $args->thing
;
388 $expected = Fixtures
::$testArgs['testStruct'];
390 $this->assertEquals($expected, $actual);
393 public function testNestRead()
395 $this->transport
->write(
396 TJSONProtocolFixtures
::$testArgsJSON['testNest']
398 $args = new \ThriftTest\
ThriftTest_testNest_args();
399 $args->read($this->protocol
);
401 $actual = $args->thing
;
402 $expected = Fixtures
::$testArgs['testNest'];
404 $this->assertEquals($expected, $actual);
407 public function testMapRead()
409 $this->transport
->write(
410 TJSONProtocolFixtures
::$testArgsJSON['testMap']
412 $args = new \ThriftTest\
ThriftTest_testMap_args();
413 $args->read($this->protocol
);
415 $actual = $args->thing
;
416 $expected = Fixtures
::$testArgs['testMap'];
418 $this->assertEquals($expected, $actual);
421 public function testStringMapRead()
423 $this->transport
->write(
424 TJSONProtocolFixtures
::$testArgsJSON['testStringMap']
426 $args = new \ThriftTest\
ThriftTest_testStringMap_args();
427 $args->read($this->protocol
);
429 $actual = $args->thing
;
430 $expected = Fixtures
::$testArgs['testStringMap'];
432 $this->assertEquals($expected, $actual);
435 public function testSetRead()
437 $this->transport
->write(
438 TJSONProtocolFixtures
::$testArgsJSON['testSet']
440 $args = new \ThriftTest\
ThriftTest_testSet_args();
441 $args->read($this->protocol
);
443 $actual = $args->thing
;
444 $expected = Fixtures
::$testArgs['testSet'];
446 $this->assertEquals($expected, $actual);
449 public function testListRead()
451 $this->transport
->write(
452 TJSONProtocolFixtures
::$testArgsJSON['testList']
454 $args = new \ThriftTest\
ThriftTest_testList_args();
455 $args->read($this->protocol
);
457 $actual = $args->thing
;
458 $expected = Fixtures
::$testArgs['testList'];
460 $this->assertEquals($expected, $actual);
463 public function testEnumRead()
465 $this->transport
->write(
466 TJSONProtocolFixtures
::$testArgsJSON['testEnum']
468 $args = new \ThriftTest\
ThriftTest_testEnum_args();
469 $args->read($this->protocol
);
471 $actual = $args->thing
;
472 $expected = Fixtures
::$testArgs['testEnum'];
474 $this->assertEquals($expected, $actual);
477 public function testTypedefRead()
479 $this->transport
->write(
480 TJSONProtocolFixtures
::$testArgsJSON['testTypedef']
482 $args = new \ThriftTest\
ThriftTest_testTypedef_args();
483 $args->read($this->protocol
);
485 $actual = $args->thing
;
486 $expected = Fixtures
::$testArgs['testTypedef'];
488 $this->assertEquals($expected, $actual);
491 public function testMapMapRead()
493 $this->transport
->write(
494 TJSONProtocolFixtures
::$testArgsJSON['testMapMap']
496 $result = new \ThriftTest\
ThriftTest_testMapMap_result();
497 $result->read($this->protocol
);
499 $actual = $result->success
;
500 $expected = Fixtures
::$testArgs['testMapMapExpectedResult'];
502 $this->assertEquals($expected, $actual);
505 public function testInsanityRead()
507 $this->transport
->write(
508 TJSONProtocolFixtures
::$testArgsJSON['testInsanity']
510 $result = new \ThriftTest\
ThriftTest_testInsanity_result();
511 $result->read($this->protocol
);
513 $actual = $result->success
;
514 $expected = Fixtures
::$testArgs['testInsanityExpectedResult'];
516 $this->assertEquals($expected, $actual);