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 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 23 | use PHPUnit\Framework\TestCase; |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 24 | use stdClass; |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 25 | |
Robert Lu | 68707d9 | 2018-01-17 19:40:39 +0800 | [diff] [blame^] | 26 | require __DIR__ . '/../../../../vendor/autoload.php'; |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 27 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 28 | /** |
| 29 | * @runTestsInSeparateProcesses |
| 30 | */ |
| 31 | class JsonSerializeTest extends TestCase |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 32 | { |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 33 | protected function setUp() |
| 34 | { |
| 35 | if (version_compare(phpversion(), '5.4', '<')) { |
| 36 | $this->markTestSkipped('Requires PHP 5.4 or newer!'); |
| 37 | } |
Robert Lu | 68707d9 | 2018-01-17 19:40:39 +0800 | [diff] [blame^] | 38 | /** @var \Composer\Autoload\ClassLoader $loader */ |
| 39 | $loader = require __DIR__ . '/../../../../vendor/autoload.php'; |
| 40 | $loader->addPsr4('', __DIR__ . '/../packages/phpjs'); |
Stig Bakken | d6ca81b | 2015-07-24 01:41:33 +0200 | [diff] [blame] | 41 | } |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 42 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 43 | 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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 48 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 49 | public function testStringsAndInts() |
| 50 | { |
| 51 | $input = array( |
| 52 | 'string_thing' => 'foo', |
| 53 | 'i64_thing' => 1234567890, |
| 54 | ); |
| 55 | $xtruct = new \ThriftTest\Xtruct($input); |
Roger Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 56 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 57 | // 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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 63 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 64 | 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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 72 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 73 | $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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 79 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 80 | 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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 91 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 92 | 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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 100 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 101 | 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 Geyer | 89cffc6 | 2015-05-05 21:10:50 +0200 | [diff] [blame] | 108 | |
Robert Lu | 12f124c | 2018-01-25 23:19:41 +0800 | [diff] [blame] | 109 | 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 Meier | 964082a | 2014-10-08 23:28:09 +0200 | [diff] [blame] | 116 | } |