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