blob: 66e4d5e376fcfa1bdaef73801fdeb22daa37e533 [file] [log] [blame]
Roger Meier964082a2014-10-08 23:28:09 +02001<?php
Volodymyr Panivko8e828c02024-02-19 11:34:48 +01002
Roger Meier964082a2014-10-08 23:28:09 +02003/*
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
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010022namespace Test\Thrift\Unit;
Roger Meier964082a2014-10-08 23:28:09 +020023
Robert Lu12f124c2018-01-25 23:19:41 +080024use PHPUnit\Framework\TestCase;
Roger Meier964082a2014-10-08 23:28:09 +020025use stdClass;
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010026use Thrift\ClassLoader\ThriftClassLoader;
Roger Meier964082a2014-10-08 23:28:09 +020027
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010028/***
29 * This test suite depends on running the compiler against the ./Resources/ThriftTest.thrift file:
30 * lib/php/test$ ../../../compiler/cpp/thrift --gen php:json -r --out ./Resources/packages/phpjs ./Resources/ThriftTest.thrift
Robert Lu12f124c2018-01-25 23:19:41 +080031 */
32class JsonSerializeTest extends TestCase
Roger Meier964082a2014-10-08 23:28:09 +020033{
vladimir.panivkof6927022024-02-24 17:12:10 +010034 protected function setUp(): void
Robert Lu12f124c2018-01-25 23:19:41 +080035 {
Volodymyr Panivko8e828c02024-02-19 11:34:48 +010036 $loader = new ThriftClassLoader();
37 $loader->registerNamespace('ThriftTest', __DIR__ . '/../Resources/packages/phpjs');
38 $loader->registerDefinition('ThriftTest', __DIR__ . '/../Resources/packages/phpjs');
39 $loader->register();
Stig Bakkend6ca81b2015-07-24 01:41:33 +020040 }
Roger Meier964082a2014-10-08 23:28:09 +020041
Robert Lu12f124c2018-01-25 23:19:41 +080042 public function testEmptyStruct()
43 {
44 $empty = new \ThriftTest\EmptyStruct(array('non_existing_key' => 'bar'));
45 $this->assertEquals(new stdClass(), json_decode(json_encode($empty)));
46 }
Roger Meier964082a2014-10-08 23:28:09 +020047
Robert Lu12f124c2018-01-25 23:19:41 +080048 public function testStringsAndInts()
49 {
50 $input = array(
51 'string_thing' => 'foo',
52 'i64_thing' => 1234567890,
53 );
54 $xtruct = new \ThriftTest\Xtruct($input);
Roger Meier964082a2014-10-08 23:28:09 +020055
Robert Lu12f124c2018-01-25 23:19:41 +080056 // Xtruct's 'i32_thing' and 'byte_thing' fields should not be present here!
57 $expected = new stdClass();
58 $expected->string_thing = $input['string_thing'];
59 $expected->i64_thing = $input['i64_thing'];
60 $this->assertEquals($expected, json_decode(json_encode($xtruct)));
61 }
Roger Meier964082a2014-10-08 23:28:09 +020062
Robert Lu12f124c2018-01-25 23:19:41 +080063 public function testNestedStructs()
64 {
65 $xtruct2 = new \ThriftTest\Xtruct2(array(
66 'byte_thing' => 42,
67 'struct_thing' => new \ThriftTest\Xtruct(array(
68 'i32_thing' => 123456,
69 )),
70 ));
Roger Meier964082a2014-10-08 23:28:09 +020071
Robert Lu12f124c2018-01-25 23:19:41 +080072 $expected = new stdClass();
73 $expected->byte_thing = $xtruct2->byte_thing;
74 $expected->struct_thing = new stdClass();
75 $expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing;
76 $this->assertEquals($expected, json_decode(json_encode($xtruct2)));
77 }
Roger Meier964082a2014-10-08 23:28:09 +020078
Robert Lu12f124c2018-01-25 23:19:41 +080079 public function testInsanity()
80 {
81 $xinput = array('string_thing' => 'foo');
82 $xtruct = new \ThriftTest\Xtruct($xinput);
83 $insanity = new \ThriftTest\Insanity(array(
84 'xtructs' => array($xtruct, $xtruct, $xtruct)
85 ));
86 $expected = new stdClass();
87 $expected->xtructs = array((object)$xinput, (object)$xinput, (object)$xinput);
88 $this->assertEquals($expected, json_decode(json_encode($insanity)));
89 }
Roger Meier964082a2014-10-08 23:28:09 +020090
Robert Lu12f124c2018-01-25 23:19:41 +080091 public function testNestedLists()
92 {
93 $bonk = new \ThriftTest\Bonk(array('message' => 'foo'));
94 $nested = new \ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk)))));
95 $expected = new stdClass();
96 $expected->bonk = array(array(array((object)array('message' => 'foo'))));
97 $this->assertEquals($expected, json_decode(json_encode($nested)));
98 }
Roger Meier964082a2014-10-08 23:28:09 +020099
Robert Lu12f124c2018-01-25 23:19:41 +0800100 public function testMaps()
101 {
102 $intmap = new \ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
103 $emptymap = new \ThriftTest\ThriftTest_testMap_args([]);
104 $this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
105 $this->assertEquals('{}', json_encode($emptymap));
106 }
Jens Geyer89cffc62015-05-05 21:10:50 +0200107
Robert Lu12f124c2018-01-25 23:19:41 +0800108 public function testScalarTypes()
109 {
110 $b = new \ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
111 $this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
112 $s = new \ThriftTest\StructA(['s' => 42]);
113 $this->assertEquals('{"s":"42"}', json_encode($s));
114 }
Roger Meier964082a2014-10-08 23:28:09 +0200115}