blob: c6686525fac755a3d9424045bb69ce01d9cf9565 [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 +020025
Robert Lu68707d92018-01-17 19:40:39 +080026require __DIR__ . '/../../../../vendor/autoload.php';
Roger Meier964082a2014-10-08 23:28:09 +020027
Robert Lu12f124c2018-01-25 23:19:41 +080028/**
29 * @runTestsInSeparateProcesses
30 */
31class JsonSerializeTest extends TestCase
Roger Meier964082a2014-10-08 23:28:09 +020032{
Robert Lu12f124c2018-01-25 23:19:41 +080033 protected function setUp()
34 {
35 if (version_compare(phpversion(), '5.4', '<')) {
36 $this->markTestSkipped('Requires PHP 5.4 or newer!');
37 }
Robert Lu68707d92018-01-17 19:40:39 +080038 /** @var \Composer\Autoload\ClassLoader $loader */
39 $loader = require __DIR__ . '/../../../../vendor/autoload.php';
40 $loader->addPsr4('', __DIR__ . '/../packages/phpjs');
Stig Bakkend6ca81b2015-07-24 01:41:33 +020041 }
Roger Meier964082a2014-10-08 23:28:09 +020042
Robert Lu12f124c2018-01-25 23:19:41 +080043 public function testEmptyStruct()
44 {
45 $empty = new \ThriftTest\EmptyStruct(array('non_existing_key' => 'bar'));
46 $this->assertEquals(new stdClass(), json_decode(json_encode($empty)));
47 }
Roger Meier964082a2014-10-08 23:28:09 +020048
Robert Lu12f124c2018-01-25 23:19:41 +080049 public function testStringsAndInts()
50 {
51 $input = array(
52 'string_thing' => 'foo',
53 'i64_thing' => 1234567890,
54 );
55 $xtruct = new \ThriftTest\Xtruct($input);
Roger Meier964082a2014-10-08 23:28:09 +020056
Robert Lu12f124c2018-01-25 23:19:41 +080057 // Xtruct's 'i32_thing' and 'byte_thing' fields should not be present here!
58 $expected = new stdClass();
59 $expected->string_thing = $input['string_thing'];
60 $expected->i64_thing = $input['i64_thing'];
61 $this->assertEquals($expected, json_decode(json_encode($xtruct)));
62 }
Roger Meier964082a2014-10-08 23:28:09 +020063
Robert Lu12f124c2018-01-25 23:19:41 +080064 public function testNestedStructs()
65 {
66 $xtruct2 = new \ThriftTest\Xtruct2(array(
67 'byte_thing' => 42,
68 'struct_thing' => new \ThriftTest\Xtruct(array(
69 'i32_thing' => 123456,
70 )),
71 ));
Roger Meier964082a2014-10-08 23:28:09 +020072
Robert Lu12f124c2018-01-25 23:19:41 +080073 $expected = new stdClass();
74 $expected->byte_thing = $xtruct2->byte_thing;
75 $expected->struct_thing = new stdClass();
76 $expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing;
77 $this->assertEquals($expected, json_decode(json_encode($xtruct2)));
78 }
Roger Meier964082a2014-10-08 23:28:09 +020079
Robert Lu12f124c2018-01-25 23:19:41 +080080 public function testInsanity()
81 {
82 $xinput = array('string_thing' => 'foo');
83 $xtruct = new \ThriftTest\Xtruct($xinput);
84 $insanity = new \ThriftTest\Insanity(array(
85 'xtructs' => array($xtruct, $xtruct, $xtruct)
86 ));
87 $expected = new stdClass();
88 $expected->xtructs = array((object)$xinput, (object)$xinput, (object)$xinput);
89 $this->assertEquals($expected, json_decode(json_encode($insanity)));
90 }
Roger Meier964082a2014-10-08 23:28:09 +020091
Robert Lu12f124c2018-01-25 23:19:41 +080092 public function testNestedLists()
93 {
94 $bonk = new \ThriftTest\Bonk(array('message' => 'foo'));
95 $nested = new \ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk)))));
96 $expected = new stdClass();
97 $expected->bonk = array(array(array((object)array('message' => 'foo'))));
98 $this->assertEquals($expected, json_decode(json_encode($nested)));
99 }
Roger Meier964082a2014-10-08 23:28:09 +0200100
Robert Lu12f124c2018-01-25 23:19:41 +0800101 public function testMaps()
102 {
103 $intmap = new \ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
104 $emptymap = new \ThriftTest\ThriftTest_testMap_args([]);
105 $this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
106 $this->assertEquals('{}', json_encode($emptymap));
107 }
Jens Geyer89cffc62015-05-05 21:10:50 +0200108
Robert Lu12f124c2018-01-25 23:19:41 +0800109 public function testScalarTypes()
110 {
111 $b = new \ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
112 $this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
113 $s = new \ThriftTest\StructA(['s' => 42]);
114 $this->assertEquals('{"s":"42"}', json_encode($s));
115 }
Roger Meier964082a2014-10-08 23:28:09 +0200116}