Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 1 | <?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 | |
| 21 | namespace Test\Thrift\JsonSerialize; |
| 22 | |
| 23 | use stdClass; |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 24 | use Thrift\ClassLoader\ThriftClassLoader; |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 25 | |
Robert Lu | bfba370 | 2017-11-03 12:27:31 +0800 | [diff] [blame] | 26 | require_once __DIR__.'/../../../../vendor/autoload.php'; |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 27 | |
| 28 | $loader = new ThriftClassLoader(); |
Robert Lu | bfba370 | 2017-11-03 12:27:31 +0800 | [diff] [blame] | 29 | $loader->registerDefinition('ThriftTest', __DIR__ . '/../packages/phpjs'); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 30 | $loader->register(); |
| 31 | |
| 32 | class JsonSerializeTest extends \PHPUnit_Framework_TestCase |
| 33 | { |
Stig Bakken | d6ca81b | 2015-07-24 01:41:33 +0200 | [diff] [blame] | 34 | protected function setUp() { |
| 35 | if (version_compare(phpversion(), '5.4', '<')) { |
| 36 | $this->markTestSkipped('Requires PHP 5.4 or newer!'); |
| 37 | } |
| 38 | } |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 39 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 40 | public function testEmptyStruct() |
| 41 | { |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 42 | $empty = new \ThriftTest\EmptyStruct(array('non_existing_key' => 'bar')); |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 43 | $this->assertEquals(new stdClass(), json_decode(json_encode($empty))); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 46 | public function testStringsAndInts() |
| 47 | { |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 48 | $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 Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 55 | $expected = new stdClass(); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 56 | $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 Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 61 | public function testNestedStructs() |
| 62 | { |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 63 | $xtruct2 = new \ThriftTest\Xtruct2(array( |
| 64 | 'byte_thing' => 42, |
| 65 | 'struct_thing' => new \ThriftTest\Xtruct(array( |
| 66 | 'i32_thing' => 123456, |
| 67 | )), |
| 68 | )); |
| 69 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 70 | $expected = new stdClass(); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 71 | $expected->byte_thing = $xtruct2->byte_thing; |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 72 | $expected->struct_thing = new stdClass(); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 73 | $expected->struct_thing->i32_thing = $xtruct2->struct_thing->i32_thing; |
| 74 | $this->assertEquals($expected, json_decode(json_encode($xtruct2))); |
| 75 | } |
| 76 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 77 | public function testInsanity() |
| 78 | { |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 79 | $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 Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 84 | $expected = new stdClass(); |
| 85 | $expected->xtructs = array((object) $xinput, (object) $xinput, (object) $xinput); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 86 | $this->assertEquals($expected, json_decode(json_encode($insanity))); |
| 87 | } |
| 88 | |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 89 | public function testNestedLists() |
| 90 | { |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 91 | $bonk = new \ThriftTest\Bonk(array('message' => 'foo')); |
| 92 | $nested = new \ThriftTest\NestedListsBonk(array('bonk' => array(array(array($bonk))))); |
Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 93 | $expected = new stdClass(); |
| 94 | $expected->bonk = array(array(array((object) array('message' => 'foo')))); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 95 | $this->assertEquals($expected, json_decode(json_encode($nested))); |
| 96 | } |
| 97 | |
Jens Geyer | 89cffc6 | 2015-05-05 21:10:50 +0200 | [diff] [blame] | 98 | 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 Bakken | d6ca81b | 2015-07-24 01:41:33 +0200 | [diff] [blame] | 106 | 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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 114 | } |