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