blob: dfb34c3b7fc284b4be3eb28adae815379ba7bf87 [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
23use stdClass;
Roger Meier964082a2014-10-08 23:28:09 +020024use Thrift\ClassLoader\ThriftClassLoader;
Roger Meier964082a2014-10-08 23:28:09 +020025
Robert Lubfba3702017-11-03 12:27:31 +080026require_once __DIR__.'/../../../../vendor/autoload.php';
Roger Meier964082a2014-10-08 23:28:09 +020027
28$loader = new ThriftClassLoader();
Robert Lubfba3702017-11-03 12:27:31 +080029$loader->registerDefinition('ThriftTest', __DIR__ . '/../packages/phpjs');
Roger Meier964082a2014-10-08 23:28:09 +020030$loader->register();
31
32class JsonSerializeTest extends \PHPUnit_Framework_TestCase
33{
Stig Bakkend6ca81b2015-07-24 01:41:33 +020034 protected function setUp() {
35 if (version_compare(phpversion(), '5.4', '<')) {
36 $this->markTestSkipped('Requires PHP 5.4 or newer!');
37 }
38 }
Roger Meier964082a2014-10-08 23:28:09 +020039
Roger Thomas6fb59232014-11-04 10:09:23 +000040 public function testEmptyStruct()
41 {
Roger Meier964082a2014-10-08 23:28:09 +020042 $empty = new \ThriftTest\EmptyStruct(array('non_existing_key' => 'bar'));
Roger Thomas6fb59232014-11-04 10:09:23 +000043 $this->assertEquals(new stdClass(), json_decode(json_encode($empty)));
Roger Meier964082a2014-10-08 23:28:09 +020044 }
45
Roger Thomas6fb59232014-11-04 10:09:23 +000046 public function testStringsAndInts()
47 {
Roger Meier964082a2014-10-08 23:28:09 +020048 $input = array(
49 'string_thing' => 'foo',
50 'i64_thing' => 1234567890,
51 );
52 $xtruct = new \ThriftTest\Xtruct($input);
53
54 // Xtruct's 'i32_thing' and 'byte_thing' fields should not be present here!
Roger Thomas6fb59232014-11-04 10:09:23 +000055 $expected = new stdClass();
Roger Meier964082a2014-10-08 23:28:09 +020056 $expected->string_thing = $input['string_thing'];
57 $expected->i64_thing = $input['i64_thing'];
58 $this->assertEquals($expected, json_decode(json_encode($xtruct)));
59 }
60
Roger Thomas6fb59232014-11-04 10:09:23 +000061 public function testNestedStructs()
62 {
Roger Meier964082a2014-10-08 23:28:09 +020063 $xtruct2 = new \ThriftTest\Xtruct2(array(
64 'byte_thing' => 42,
65 'struct_thing' => new \ThriftTest\Xtruct(array(
66 'i32_thing' => 123456,
67 )),
68 ));
69
Roger Thomas6fb59232014-11-04 10:09:23 +000070 $expected = new stdClass();
Roger Meier964082a2014-10-08 23:28:09 +020071 $expected->byte_thing = $xtruct2->byte_thing;
Roger Thomas6fb59232014-11-04 10:09:23 +000072 $expected->struct_thing = new stdClass();
Roger Meier964082a2014-10-08 23:28:09 +020073 $expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing;
74 $this->assertEquals($expected, json_decode(json_encode($xtruct2)));
75 }
76
Roger Thomas6fb59232014-11-04 10:09:23 +000077 public function testInsanity()
78 {
Roger Meier964082a2014-10-08 23:28:09 +020079 $xinput = array('string_thing' => 'foo');
80 $xtruct = new \ThriftTest\Xtruct($xinput);
81 $insanity = new \ThriftTest\Insanity(array(
82 'xtructs' => array($xtruct, $xtruct, $xtruct)
83 ));
Roger Thomas6fb59232014-11-04 10:09:23 +000084 $expected = new stdClass();
85 $expected->xtructs = array((object) $xinput, (object) $xinput, (object) $xinput);
Roger Meier964082a2014-10-08 23:28:09 +020086 $this->assertEquals($expected, json_decode(json_encode($insanity)));
87 }
88
Roger Thomas6fb59232014-11-04 10:09:23 +000089 public function testNestedLists()
90 {
Roger Meier964082a2014-10-08 23:28:09 +020091 $bonk = new \ThriftTest\Bonk(array('message' => 'foo'));
92 $nested = new \ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk)))));
Roger Thomas6fb59232014-11-04 10:09:23 +000093 $expected = new stdClass();
94 $expected->bonk = array(array(array((object) array('message' => 'foo'))));
Roger Meier964082a2014-10-08 23:28:09 +020095 $this->assertEquals($expected, json_decode(json_encode($nested)));
96 }
97
Jens Geyer89cffc62015-05-05 21:10:50 +020098 public function testMaps()
99 {
100 $intmap = new \ThriftTest\ThriftTest_testMap_args(['thing' => [0 => 'zero']]);
101 $emptymap = new \ThriftTest\ThriftTest_testMap_args([]);
102 $this->assertEquals('{"thing":{"0":"zero"}}', json_encode($intmap));
103 $this->assertEquals('{}', json_encode($emptymap));
104 }
105
Stig Bakkend6ca81b2015-07-24 01:41:33 +0200106 public function testScalarTypes()
107 {
108 $b = new \ThriftTest\Bools(['im_true' => '1', 'im_false' => '0']);
109 $this->assertEquals('{"im_true":true,"im_false":false}', json_encode($b));
110 $s = new \ThriftTest\StructA(['s' => 42]);
111 $this->assertEquals('{"s":"42"}', json_encode($s));
112 }
113
Roger Meier964082a2014-10-08 23:28:09 +0200114}