blob: 8c6459560322757c1c2928a7d3434a759f0d23a4 [file] [log] [blame]
Roger Meier964082a2014-10-08 23:28:09 +02001<?php
2/*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 */
20
21namespace Test\Thrift\JsonSerialize;
22
Robert Lu12f124c2018-01-25 23:19:41 +080023use PHPUnit\Framework\TestCase;
Roger Meier964082a2014-10-08 23:28:09 +020024use stdClass;
Roger Meier964082a2014-10-08 23:28:09 +020025use Thrift\ClassLoader\ThriftClassLoader;
Roger Meier964082a2014-10-08 23:28:09 +020026
Robert Lu12f124c2018-01-25 23:19:41 +080027require_once __DIR__ . '/../../../../vendor/autoload.php';
Roger Meier964082a2014-10-08 23:28:09 +020028
Robert Lu12f124c2018-01-25 23:19:41 +080029/**
30 * @runTestsInSeparateProcesses
31 */
32class JsonSerializeTest extends TestCase
Roger Meier964082a2014-10-08 23:28:09 +020033{
Robert Lu12f124c2018-01-25 23:19:41 +080034 protected function setUp()
35 {
36 if (version_compare(phpversion(), '5.4', '<')) {
37 $this->markTestSkipped('Requires PHP 5.4 or newer!');
38 }
39 $loader = new ThriftClassLoader();
40 $loader->registerDefinition('ThriftTest', __DIR__ . '/../packages/phpjs');
41 $loader->register();
Stig Bakkend6ca81b2015-07-24 01:41:33 +020042 }
Roger Meier964082a2014-10-08 23:28:09 +020043
Robert Lu12f124c2018-01-25 23:19:41 +080044 public function testEmptyStruct()
45 {
46 $empty = new \ThriftTest\EmptyStruct(array('non_existing_key' => 'bar'));
47 $this->assertEquals(new stdClass(), json_decode(json_encode($empty)));
48 }
Roger Meier964082a2014-10-08 23:28:09 +020049
Robert Lu12f124c2018-01-25 23:19:41 +080050 public function testStringsAndInts()
51 {
52 $input = array(
53 'string_thing' => 'foo',
54 'i64_thing' => 1234567890,
55 );
56 $xtruct = new \ThriftTest\Xtruct($input);
Roger Meier964082a2014-10-08 23:28:09 +020057
Robert Lu12f124c2018-01-25 23:19:41 +080058 // Xtruct's 'i32_thing' and 'byte_thing' fields should not be present here!
59 $expected = new stdClass();
60 $expected->string_thing = $input['string_thing'];
61 $expected->i64_thing = $input['i64_thing'];
62 $this->assertEquals($expected, json_decode(json_encode($xtruct)));
63 }
Roger Meier964082a2014-10-08 23:28:09 +020064
Robert Lu12f124c2018-01-25 23:19:41 +080065 public function testNestedStructs()
66 {
67 $xtruct2 = new \ThriftTest\Xtruct2(array(
68 'byte_thing' => 42,
69 'struct_thing' => new \ThriftTest\Xtruct(array(
70 'i32_thing' => 123456,
71 )),
72 ));
Roger Meier964082a2014-10-08 23:28:09 +020073
Robert Lu12f124c2018-01-25 23:19:41 +080074 $expected = new stdClass();
75 $expected->byte_thing = $xtruct2->byte_thing;
76 $expected->struct_thing = new stdClass();
77 $expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing;
78 $this->assertEquals($expected, json_decode(json_encode($xtruct2)));
79 }
Roger Meier964082a2014-10-08 23:28:09 +020080
Robert Lu12f124c2018-01-25 23:19:41 +080081 public function testInsanity()
82 {
83 $xinput = array('string_thing' => 'foo');
84 $xtruct = new \ThriftTest\Xtruct($xinput);
85 $insanity = new \ThriftTest\Insanity(array(
86 'xtructs' => array($xtruct, $xtruct, $xtruct)
87 ));
88 $expected = new stdClass();
89 $expected->xtructs = array((object)$xinput, (object)$xinput, (object)$xinput);
90 $this->assertEquals($expected, json_decode(json_encode($insanity)));
91 }
Roger Meier964082a2014-10-08 23:28:09 +020092
Robert Lu12f124c2018-01-25 23:19:41 +080093 public function testNestedLists()
94 {
95 $bonk = new \ThriftTest\Bonk(array('message' => 'foo'));
96 $nested = new \ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk)))));
97 $expected = new stdClass();
98 $expected->bonk = array(array(array((object)array('message' => 'foo'))));
99 $this->assertEquals($expected, json_decode(json_encode($nested)));
100 }
Roger Meier964082a2014-10-08 23:28:09 +0200101
Robert Lu12f124c2018-01-25 23:19:41 +0800102 public function testMaps()
103 {
104 $intmap = new \ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
105 $emptymap = new \ThriftTest\ThriftTest_testMap_args([]);
106 $this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
107 $this->assertEquals('{}', json_encode($emptymap));
108 }
Jens Geyer89cffc62015-05-05 21:10:50 +0200109
Robert Lu12f124c2018-01-25 23:19:41 +0800110 public function testScalarTypes()
111 {
112 $b = new \ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
113 $this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
114 $s = new \ThriftTest\StructA(['s' => 42]);
115 $this->assertEquals('{"s":"42"}', json_encode($s));
116 }
Roger Meier964082a2014-10-08 23:28:09 +0200117}