Debian Codegraph @

lib/php/test/Integration/Lib/Serializer/JsonSerializeTest.php

raw
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
22 namespace Test\Thrift\Integration\Lib\Serializer;
23
24 use PHPUnit\Framework\TestCase;
25 use stdClass;
26 use Thrift\ClassLoader\ThriftClassLoader;
27
28 /***
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,nsglobal="Json" -r --out ./Resources/packages/phpjs ./Resources/ThriftTest.thrift
31 */
32 class JsonSerializeTest extends TestCase
33 {
34 public function testEmptyStruct()
35 {
36 $empty = new \Json\ThriftTest\EmptyStruct(array('non_existing_key' => 'bar'));
37 $this->assertEquals(new stdClass(), json_decode(json_encode($empty)));
38 }
39
40 public function testStringsAndInts()
41 {
42 $input = array(
43 'string_thing' => 'foo',
44 'i64_thing' => 1234567890,
45 );
46 $xtruct = new \Json\ThriftTest\Xtruct($input);
47
48 // Xtruct's 'i32_thing' and 'byte_thing' fields should not be present here!
49 $expected = new stdClass();
50 $expected->string_thing = $input['string_thing'];
51 $expected->i64_thing = $input['i64_thing'];
52 $this->assertEquals($expected, json_decode(json_encode($xtruct)));
53 }
54
55 public function testNestedStructs()
56 {
57 $xtruct2 = new \Json\ThriftTest\Xtruct2(array(
58 'byte_thing' => 42,
59 'struct_thing' => new \Json\ThriftTest\Xtruct(array(
60 'i32_thing' => 123456,
61 )),
62 ));
63
64 $expected = new stdClass();
65 $expected->byte_thing = $xtruct2->byte_thing;
66 $expected->struct_thing = new stdClass();
67 $expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing;
68 $this->assertEquals($expected, json_decode(json_encode($xtruct2)));
69 }
70
71 public function testInsanity()
72 {
73 $xinput = array('string_thing' => 'foo');
74 $xtruct = new \Json\ThriftTest\Xtruct($xinput);
75 $insanity = new \Json\ThriftTest\Insanity(array(
76 'xtructs' => array($xtruct, $xtruct, $xtruct)
77 ));
78 $expected = new stdClass();
79 $expected->xtructs = array((object)$xinput, (object)$xinput, (object)$xinput);
80 $this->assertEquals($expected, json_decode(json_encode($insanity)));
81 }
82
83 public function testNestedLists()
84 {
85 $bonk = new \Json\ThriftTest\Bonk(array('message' => 'foo'));
86 $nested = new \Json\ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk)))));
87 $expected = new stdClass();
88 $expected->bonk = array(array(array((object)array('message' => 'foo'))));
89 $this->assertEquals($expected, json_decode(json_encode($nested)));
90 }
91
92 public function testMaps()
93 {
94 $intmap = new \Json\ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
95 $emptymap = new \Json\ThriftTest\ThriftTest_testMap_args([]);
96 $this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
97 $this->assertEquals('{}', json_encode($emptymap));
98 }
99
100 public function testScalarTypes()
101 {
102 $b = new \Json\ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
103 $this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
104 $s = new \Json\ThriftTest\StructA(['s' => 42]);
105 $this->assertEquals('{"s":"42"}', json_encode($s));
106 }
107 }