1ffe3c632Sopenharmony_ci<?php 2ffe3c632Sopenharmony_ci 3ffe3c632Sopenharmony_cirequire_once('test_base.php'); 4ffe3c632Sopenharmony_cirequire_once('test_util.php'); 5ffe3c632Sopenharmony_ci 6ffe3c632Sopenharmony_ciuse Google\Protobuf\RepeatedField; 7ffe3c632Sopenharmony_ciuse Google\Protobuf\GPBType; 8ffe3c632Sopenharmony_ciuse Foo\TestInt32Value; 9ffe3c632Sopenharmony_ciuse Foo\TestInt64Value; 10ffe3c632Sopenharmony_ciuse Foo\TestUInt32Value; 11ffe3c632Sopenharmony_ciuse Foo\TestUInt64Value; 12ffe3c632Sopenharmony_ciuse Foo\TestBoolValue; 13ffe3c632Sopenharmony_ciuse Foo\TestStringValue; 14ffe3c632Sopenharmony_ciuse Foo\TestBytesValue; 15ffe3c632Sopenharmony_ciuse Foo\TestAny; 16ffe3c632Sopenharmony_ciuse Foo\TestEnum; 17ffe3c632Sopenharmony_ciuse Foo\TestMessage; 18ffe3c632Sopenharmony_ciuse Foo\TestMessage\Sub; 19ffe3c632Sopenharmony_ciuse Foo\TestPackedMessage; 20ffe3c632Sopenharmony_ciuse Foo\TestRandomFieldOrder; 21ffe3c632Sopenharmony_ciuse Foo\TestUnpackedMessage; 22ffe3c632Sopenharmony_ciuse Google\Protobuf\Any; 23ffe3c632Sopenharmony_ciuse Google\Protobuf\DoubleValue; 24ffe3c632Sopenharmony_ciuse Google\Protobuf\FieldMask; 25ffe3c632Sopenharmony_ciuse Google\Protobuf\FloatValue; 26ffe3c632Sopenharmony_ciuse Google\Protobuf\Int32Value; 27ffe3c632Sopenharmony_ciuse Google\Protobuf\UInt32Value; 28ffe3c632Sopenharmony_ciuse Google\Protobuf\Int64Value; 29ffe3c632Sopenharmony_ciuse Google\Protobuf\UInt64Value; 30ffe3c632Sopenharmony_ciuse Google\Protobuf\BoolValue; 31ffe3c632Sopenharmony_ciuse Google\Protobuf\StringValue; 32ffe3c632Sopenharmony_ciuse Google\Protobuf\BytesValue; 33ffe3c632Sopenharmony_ciuse Google\Protobuf\Value; 34ffe3c632Sopenharmony_ciuse Google\Protobuf\ListValue; 35ffe3c632Sopenharmony_ciuse Google\Protobuf\Struct; 36ffe3c632Sopenharmony_ciuse Google\Protobuf\GPBEmpty; 37ffe3c632Sopenharmony_ci 38ffe3c632Sopenharmony_ciclass EncodeDecodeTest extends TestBase 39ffe3c632Sopenharmony_ci{ 40ffe3c632Sopenharmony_ci public function testDecodeJsonSimple() 41ffe3c632Sopenharmony_ci { 42ffe3c632Sopenharmony_ci $m = new TestMessage(); 43ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"optionalInt32\":1}"); 44ffe3c632Sopenharmony_ci $this->assertEquals(1, $m->getOptionalInt32()); 45ffe3c632Sopenharmony_ci } 46ffe3c632Sopenharmony_ci 47ffe3c632Sopenharmony_ci public function testDecodeTopLevelBoolValue() 48ffe3c632Sopenharmony_ci { 49ffe3c632Sopenharmony_ci $m = new BoolValue(); 50ffe3c632Sopenharmony_ci 51ffe3c632Sopenharmony_ci $m->mergeFromJsonString("true"); 52ffe3c632Sopenharmony_ci $this->assertEquals(true, $m->getValue()); 53ffe3c632Sopenharmony_ci 54ffe3c632Sopenharmony_ci $m->mergeFromJsonString("false"); 55ffe3c632Sopenharmony_ci $this->assertEquals(false, $m->getValue()); 56ffe3c632Sopenharmony_ci } 57ffe3c632Sopenharmony_ci 58ffe3c632Sopenharmony_ci public function testEncodeTopLevelBoolValue() 59ffe3c632Sopenharmony_ci { 60ffe3c632Sopenharmony_ci $m = new BoolValue(); 61ffe3c632Sopenharmony_ci $m->setValue(true); 62ffe3c632Sopenharmony_ci $this->assertSame("true", $m->serializeToJsonString()); 63ffe3c632Sopenharmony_ci } 64ffe3c632Sopenharmony_ci 65ffe3c632Sopenharmony_ci public function testDecodeTopLevelDoubleValue() 66ffe3c632Sopenharmony_ci { 67ffe3c632Sopenharmony_ci $m = new DoubleValue(); 68ffe3c632Sopenharmony_ci $m->mergeFromJsonString("1.5"); 69ffe3c632Sopenharmony_ci $this->assertEquals(1.5, $m->getValue()); 70ffe3c632Sopenharmony_ci } 71ffe3c632Sopenharmony_ci 72ffe3c632Sopenharmony_ci public function testEncodeTopLevelDoubleValue() 73ffe3c632Sopenharmony_ci { 74ffe3c632Sopenharmony_ci $m = new DoubleValue(); 75ffe3c632Sopenharmony_ci $m->setValue(1.5); 76ffe3c632Sopenharmony_ci $this->assertSame("1.5", $m->serializeToJsonString()); 77ffe3c632Sopenharmony_ci } 78ffe3c632Sopenharmony_ci 79ffe3c632Sopenharmony_ci public function testDecodeTopLevelFloatValue() 80ffe3c632Sopenharmony_ci { 81ffe3c632Sopenharmony_ci $m = new FloatValue(); 82ffe3c632Sopenharmony_ci $m->mergeFromJsonString("1.5"); 83ffe3c632Sopenharmony_ci $this->assertEquals(1.5, $m->getValue()); 84ffe3c632Sopenharmony_ci } 85ffe3c632Sopenharmony_ci 86ffe3c632Sopenharmony_ci public function testEncodeTopLevelFloatValue() 87ffe3c632Sopenharmony_ci { 88ffe3c632Sopenharmony_ci $m = new FloatValue(); 89ffe3c632Sopenharmony_ci $m->setValue(1.5); 90ffe3c632Sopenharmony_ci $this->assertSame("1.5", $m->serializeToJsonString()); 91ffe3c632Sopenharmony_ci } 92ffe3c632Sopenharmony_ci 93ffe3c632Sopenharmony_ci public function testDecodeTopLevelInt32Value() 94ffe3c632Sopenharmony_ci { 95ffe3c632Sopenharmony_ci $m = new Int32Value(); 96ffe3c632Sopenharmony_ci $m->mergeFromJsonString("1"); 97ffe3c632Sopenharmony_ci $this->assertEquals(1, $m->getValue()); 98ffe3c632Sopenharmony_ci } 99ffe3c632Sopenharmony_ci 100ffe3c632Sopenharmony_ci public function testEncodeTopLevelInt32Value() 101ffe3c632Sopenharmony_ci { 102ffe3c632Sopenharmony_ci $m = new Int32Value(); 103ffe3c632Sopenharmony_ci $m->setValue(1); 104ffe3c632Sopenharmony_ci $this->assertSame("1", $m->serializeToJsonString()); 105ffe3c632Sopenharmony_ci } 106ffe3c632Sopenharmony_ci 107ffe3c632Sopenharmony_ci public function testDecodeRepeatedInt32Value() 108ffe3c632Sopenharmony_ci { 109ffe3c632Sopenharmony_ci $m = new TestInt32Value(); 110ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"repeated_field\":[12345]}"); 111ffe3c632Sopenharmony_ci $this->assertSame(12345, $m->getRepeatedField()[0]->getValue()); 112ffe3c632Sopenharmony_ci } 113ffe3c632Sopenharmony_ci 114ffe3c632Sopenharmony_ci public function testDecodeTopLevelUInt32Value() 115ffe3c632Sopenharmony_ci { 116ffe3c632Sopenharmony_ci $m = new UInt32Value(); 117ffe3c632Sopenharmony_ci $m->mergeFromJsonString("1"); 118ffe3c632Sopenharmony_ci $this->assertEquals(1, $m->getValue()); 119ffe3c632Sopenharmony_ci } 120ffe3c632Sopenharmony_ci 121ffe3c632Sopenharmony_ci public function testEncodeTopLevelUInt32Value() 122ffe3c632Sopenharmony_ci { 123ffe3c632Sopenharmony_ci $m = new UInt32Value(); 124ffe3c632Sopenharmony_ci $m->setValue(1); 125ffe3c632Sopenharmony_ci $this->assertSame("1", $m->serializeToJsonString()); 126ffe3c632Sopenharmony_ci } 127ffe3c632Sopenharmony_ci 128ffe3c632Sopenharmony_ci public function testDecodeTopLevelInt64Value() 129ffe3c632Sopenharmony_ci { 130ffe3c632Sopenharmony_ci $m = new Int64Value(); 131ffe3c632Sopenharmony_ci $m->mergeFromJsonString("1"); 132ffe3c632Sopenharmony_ci $this->assertEquals(1, $m->getValue()); 133ffe3c632Sopenharmony_ci } 134ffe3c632Sopenharmony_ci 135ffe3c632Sopenharmony_ci public function testDecodeTopLevelInt64ValueAsString() 136ffe3c632Sopenharmony_ci { 137ffe3c632Sopenharmony_ci $m = new Int64Value(); 138ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"1\""); 139ffe3c632Sopenharmony_ci $this->assertEquals(1, $m->getValue()); 140ffe3c632Sopenharmony_ci } 141ffe3c632Sopenharmony_ci 142ffe3c632Sopenharmony_ci public function testEncodeTopLevelInt64Value() 143ffe3c632Sopenharmony_ci { 144ffe3c632Sopenharmony_ci $m = new Int64Value(); 145ffe3c632Sopenharmony_ci $m->setValue(1); 146ffe3c632Sopenharmony_ci $this->assertSame("\"1\"", $m->serializeToJsonString()); 147ffe3c632Sopenharmony_ci } 148ffe3c632Sopenharmony_ci 149ffe3c632Sopenharmony_ci public function testDecodeTopLevelUInt64Value() 150ffe3c632Sopenharmony_ci { 151ffe3c632Sopenharmony_ci $m = new UInt64Value(); 152ffe3c632Sopenharmony_ci $m->mergeFromJsonString("1"); 153ffe3c632Sopenharmony_ci $this->assertEquals(1, $m->getValue()); 154ffe3c632Sopenharmony_ci } 155ffe3c632Sopenharmony_ci 156ffe3c632Sopenharmony_ci public function testDecodeTopLevelUInt64ValueAsString() 157ffe3c632Sopenharmony_ci { 158ffe3c632Sopenharmony_ci $m = new UInt64Value(); 159ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"1\""); 160ffe3c632Sopenharmony_ci $this->assertEquals(1, $m->getValue()); 161ffe3c632Sopenharmony_ci } 162ffe3c632Sopenharmony_ci 163ffe3c632Sopenharmony_ci public function testEncodeTopLevelUInt64Value() 164ffe3c632Sopenharmony_ci { 165ffe3c632Sopenharmony_ci $m = new UInt64Value(); 166ffe3c632Sopenharmony_ci $m->setValue(1); 167ffe3c632Sopenharmony_ci $this->assertSame("\"1\"", $m->serializeToJsonString()); 168ffe3c632Sopenharmony_ci } 169ffe3c632Sopenharmony_ci 170ffe3c632Sopenharmony_ci public function testDecodeTopLevelStringValue() 171ffe3c632Sopenharmony_ci { 172ffe3c632Sopenharmony_ci $m = new StringValue(); 173ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"a\""); 174ffe3c632Sopenharmony_ci $this->assertSame("a", $m->getValue()); 175ffe3c632Sopenharmony_ci } 176ffe3c632Sopenharmony_ci 177ffe3c632Sopenharmony_ci public function testEncodeTopLevelStringValue() 178ffe3c632Sopenharmony_ci { 179ffe3c632Sopenharmony_ci $m = new StringValue(); 180ffe3c632Sopenharmony_ci $m->setValue("a"); 181ffe3c632Sopenharmony_ci $this->assertSame("\"a\"", $m->serializeToJsonString()); 182ffe3c632Sopenharmony_ci } 183ffe3c632Sopenharmony_ci 184ffe3c632Sopenharmony_ci public function testDecodeRepeatedStringValue() 185ffe3c632Sopenharmony_ci { 186ffe3c632Sopenharmony_ci $m = new TestStringValue(); 187ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"repeated_field\":[\"a\"]}"); 188ffe3c632Sopenharmony_ci $this->assertSame("a", $m->getRepeatedField()[0]->getValue()); 189ffe3c632Sopenharmony_ci } 190ffe3c632Sopenharmony_ci 191ffe3c632Sopenharmony_ci public function testDecodeMapStringValue() 192ffe3c632Sopenharmony_ci { 193ffe3c632Sopenharmony_ci $m = new TestStringValue(); 194ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"map_field\":{\"1\": \"a\"}}"); 195ffe3c632Sopenharmony_ci $this->assertSame("a", $m->getMapField()[1]->getValue()); 196ffe3c632Sopenharmony_ci } 197ffe3c632Sopenharmony_ci 198ffe3c632Sopenharmony_ci public function testDecodeTopLevelBytesValue() 199ffe3c632Sopenharmony_ci { 200ffe3c632Sopenharmony_ci $m = new BytesValue(); 201ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"YQ==\""); 202ffe3c632Sopenharmony_ci $this->assertSame("a", $m->getValue()); 203ffe3c632Sopenharmony_ci } 204ffe3c632Sopenharmony_ci 205ffe3c632Sopenharmony_ci public function testEncodeTopLevelBytesValue() 206ffe3c632Sopenharmony_ci { 207ffe3c632Sopenharmony_ci $m = new BytesValue(); 208ffe3c632Sopenharmony_ci $m->setValue("a"); 209ffe3c632Sopenharmony_ci $this->assertSame("\"YQ==\"", $m->serializeToJsonString()); 210ffe3c632Sopenharmony_ci } 211ffe3c632Sopenharmony_ci 212ffe3c632Sopenharmony_ci public function generateRandomString($length = 10) { 213ffe3c632Sopenharmony_ci $randomString = str_repeat("+", $length); 214ffe3c632Sopenharmony_ci for ($i = 0; $i < $length; $i++) { 215ffe3c632Sopenharmony_ci $randomString[$i] = rand(0, 255); 216ffe3c632Sopenharmony_ci } 217ffe3c632Sopenharmony_ci return $randomString; 218ffe3c632Sopenharmony_ci } 219ffe3c632Sopenharmony_ci 220ffe3c632Sopenharmony_ci public function testEncodeTopLevelLongBytesValue() 221ffe3c632Sopenharmony_ci { 222ffe3c632Sopenharmony_ci $m = new BytesValue(); 223ffe3c632Sopenharmony_ci $data = $this->generateRandomString(12007); 224ffe3c632Sopenharmony_ci $m->setValue($data); 225ffe3c632Sopenharmony_ci $expected = "\"" . base64_encode($data) . "\""; 226ffe3c632Sopenharmony_ci $this->assertSame(strlen($expected), strlen($m->serializeToJsonString())); 227ffe3c632Sopenharmony_ci } 228ffe3c632Sopenharmony_ci 229ffe3c632Sopenharmony_ci public function testEncode() 230ffe3c632Sopenharmony_ci { 231ffe3c632Sopenharmony_ci $from = new TestMessage(); 232ffe3c632Sopenharmony_ci $this->expectEmptyFields($from); 233ffe3c632Sopenharmony_ci $this->setFields($from); 234ffe3c632Sopenharmony_ci $this->expectFields($from); 235ffe3c632Sopenharmony_ci 236ffe3c632Sopenharmony_ci $data = $from->serializeToString(); 237ffe3c632Sopenharmony_ci $this->assertSame(bin2hex(TestUtil::getGoldenTestMessage()), 238ffe3c632Sopenharmony_ci bin2hex($data)); 239ffe3c632Sopenharmony_ci } 240ffe3c632Sopenharmony_ci 241ffe3c632Sopenharmony_ci public function testDecode() 242ffe3c632Sopenharmony_ci { 243ffe3c632Sopenharmony_ci $to = new TestMessage(); 244ffe3c632Sopenharmony_ci $to->mergeFromString(TestUtil::getGoldenTestMessage()); 245ffe3c632Sopenharmony_ci $this->expectFields($to); 246ffe3c632Sopenharmony_ci } 247ffe3c632Sopenharmony_ci 248ffe3c632Sopenharmony_ci public function testEncodeDecode() 249ffe3c632Sopenharmony_ci { 250ffe3c632Sopenharmony_ci $from = new TestMessage(); 251ffe3c632Sopenharmony_ci $this->expectEmptyFields($from); 252ffe3c632Sopenharmony_ci $this->setFields($from); 253ffe3c632Sopenharmony_ci $this->expectFields($from); 254ffe3c632Sopenharmony_ci 255ffe3c632Sopenharmony_ci $data = $from->serializeToString(); 256ffe3c632Sopenharmony_ci 257ffe3c632Sopenharmony_ci $to = new TestMessage(); 258ffe3c632Sopenharmony_ci $to->mergeFromString($data); 259ffe3c632Sopenharmony_ci $this->expectFields($to); 260ffe3c632Sopenharmony_ci } 261ffe3c632Sopenharmony_ci 262ffe3c632Sopenharmony_ci public function testEncodeDecodeEmpty() 263ffe3c632Sopenharmony_ci { 264ffe3c632Sopenharmony_ci $from = new TestMessage(); 265ffe3c632Sopenharmony_ci $this->expectEmptyFields($from); 266ffe3c632Sopenharmony_ci 267ffe3c632Sopenharmony_ci $data = $from->serializeToString(); 268ffe3c632Sopenharmony_ci 269ffe3c632Sopenharmony_ci $to = new TestMessage(); 270ffe3c632Sopenharmony_ci $to->mergeFromString($data); 271ffe3c632Sopenharmony_ci $this->expectEmptyFields($to); 272ffe3c632Sopenharmony_ci } 273ffe3c632Sopenharmony_ci 274ffe3c632Sopenharmony_ci public function testEncodeDecodeOneof() 275ffe3c632Sopenharmony_ci { 276ffe3c632Sopenharmony_ci $m = new TestMessage(); 277ffe3c632Sopenharmony_ci 278ffe3c632Sopenharmony_ci $m->setOneofInt32(1); 279ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 280ffe3c632Sopenharmony_ci $n = new TestMessage(); 281ffe3c632Sopenharmony_ci $n->mergeFromString($data); 282ffe3c632Sopenharmony_ci $this->assertSame(1, $n->getOneofInt32()); 283ffe3c632Sopenharmony_ci 284ffe3c632Sopenharmony_ci $m->setOneofFloat(2.0); 285ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 286ffe3c632Sopenharmony_ci $n = new TestMessage(); 287ffe3c632Sopenharmony_ci $n->mergeFromString($data); 288ffe3c632Sopenharmony_ci $this->assertSame(2.0, $n->getOneofFloat()); 289ffe3c632Sopenharmony_ci 290ffe3c632Sopenharmony_ci $m->setOneofString('abc'); 291ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 292ffe3c632Sopenharmony_ci $n = new TestMessage(); 293ffe3c632Sopenharmony_ci $n->mergeFromString($data); 294ffe3c632Sopenharmony_ci $this->assertSame('abc', $n->getOneofString()); 295ffe3c632Sopenharmony_ci 296ffe3c632Sopenharmony_ci $sub_m = new Sub(); 297ffe3c632Sopenharmony_ci $sub_m->setA(1); 298ffe3c632Sopenharmony_ci $m->setOneofMessage($sub_m); 299ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 300ffe3c632Sopenharmony_ci $n = new TestMessage(); 301ffe3c632Sopenharmony_ci $n->mergeFromString($data); 302ffe3c632Sopenharmony_ci $this->assertSame(1, $n->getOneofMessage()->getA()); 303ffe3c632Sopenharmony_ci 304ffe3c632Sopenharmony_ci // Encode default value 305ffe3c632Sopenharmony_ci $m->setOneofEnum(TestEnum::ZERO); 306ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 307ffe3c632Sopenharmony_ci $n = new TestMessage(); 308ffe3c632Sopenharmony_ci $n->mergeFromString($data); 309ffe3c632Sopenharmony_ci $this->assertSame("oneof_enum", $n->getMyOneof()); 310ffe3c632Sopenharmony_ci $this->assertSame(TestEnum::ZERO, $n->getOneofEnum()); 311ffe3c632Sopenharmony_ci 312ffe3c632Sopenharmony_ci $m->setOneofString(""); 313ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 314ffe3c632Sopenharmony_ci $n = new TestMessage(); 315ffe3c632Sopenharmony_ci $n->mergeFromString($data); 316ffe3c632Sopenharmony_ci $this->assertSame("oneof_string", $n->getMyOneof()); 317ffe3c632Sopenharmony_ci $this->assertSame("", $n->getOneofString()); 318ffe3c632Sopenharmony_ci 319ffe3c632Sopenharmony_ci $sub_m = new Sub(); 320ffe3c632Sopenharmony_ci $m->setOneofMessage($sub_m); 321ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 322ffe3c632Sopenharmony_ci $n = new TestMessage(); 323ffe3c632Sopenharmony_ci $n->mergeFromString($data); 324ffe3c632Sopenharmony_ci $this->assertSame("oneof_message", $n->getMyOneof()); 325ffe3c632Sopenharmony_ci $this->assertFalse(is_null($n->getOneofMessage())); 326ffe3c632Sopenharmony_ci 327ffe3c632Sopenharmony_ci } 328ffe3c632Sopenharmony_ci 329ffe3c632Sopenharmony_ci public function testEncodeDecodeOptional() 330ffe3c632Sopenharmony_ci { 331ffe3c632Sopenharmony_ci $m = new TestMessage(); 332ffe3c632Sopenharmony_ci $this->assertFalse($m->hasTrueOptionalInt32()); 333ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 334ffe3c632Sopenharmony_ci $this->assertSame("", $data); 335ffe3c632Sopenharmony_ci 336ffe3c632Sopenharmony_ci $m->setTrueOptionalInt32(0); 337ffe3c632Sopenharmony_ci $this->assertTrue($m->hasTrueOptionalInt32()); 338ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 339ffe3c632Sopenharmony_ci $this->assertNotSame("", $data); 340ffe3c632Sopenharmony_ci 341ffe3c632Sopenharmony_ci $m2 = new TestMessage(); 342ffe3c632Sopenharmony_ci $m2->mergeFromString($data); 343ffe3c632Sopenharmony_ci $this->assertTrue($m2->hasTrueOptionalInt32()); 344ffe3c632Sopenharmony_ci $this->assertSame(0, $m2->getTrueOptionalInt32()); 345ffe3c632Sopenharmony_ci } 346ffe3c632Sopenharmony_ci 347ffe3c632Sopenharmony_ci public function testJsonEncodeDecodeOptional() 348ffe3c632Sopenharmony_ci { 349ffe3c632Sopenharmony_ci $m = new TestMessage(); 350ffe3c632Sopenharmony_ci $this->assertFalse($m->hasTrueOptionalInt32()); 351ffe3c632Sopenharmony_ci $data = $m->serializeToJsonString(); 352ffe3c632Sopenharmony_ci $this->assertSame("{}", $data); 353ffe3c632Sopenharmony_ci 354ffe3c632Sopenharmony_ci $m->setTrueOptionalInt32(0); 355ffe3c632Sopenharmony_ci $this->assertTrue($m->hasTrueOptionalInt32()); 356ffe3c632Sopenharmony_ci $data = $m->serializeToJsonString(); 357ffe3c632Sopenharmony_ci $this->assertNotSame("{}", $data); 358ffe3c632Sopenharmony_ci 359ffe3c632Sopenharmony_ci $m2 = new TestMessage(); 360ffe3c632Sopenharmony_ci $m2->mergeFromJsonString($data); 361ffe3c632Sopenharmony_ci $this->assertTrue($m2->hasTrueOptionalInt32()); 362ffe3c632Sopenharmony_ci $this->assertSame(0, $m2->getTrueOptionalInt32()); 363ffe3c632Sopenharmony_ci } 364ffe3c632Sopenharmony_ci 365ffe3c632Sopenharmony_ci public function testJsonEncodeDecodeOneof() 366ffe3c632Sopenharmony_ci { 367ffe3c632Sopenharmony_ci $m = new TestMessage(); 368ffe3c632Sopenharmony_ci 369ffe3c632Sopenharmony_ci $m->setOneofEnum(TestEnum::ONE); 370ffe3c632Sopenharmony_ci $data = $m->serializeToJsonString(); 371ffe3c632Sopenharmony_ci $n = new TestMessage(); 372ffe3c632Sopenharmony_ci $n->mergeFromJsonString($data); 373ffe3c632Sopenharmony_ci $this->assertSame("oneof_enum", $n->getMyOneof()); 374ffe3c632Sopenharmony_ci $this->assertSame(TestEnum::ONE, $n->getOneofEnum()); 375ffe3c632Sopenharmony_ci 376ffe3c632Sopenharmony_ci $m->setOneofString("a"); 377ffe3c632Sopenharmony_ci $data = $m->serializeToJsonString(); 378ffe3c632Sopenharmony_ci $n = new TestMessage(); 379ffe3c632Sopenharmony_ci $n->mergeFromJsonString($data); 380ffe3c632Sopenharmony_ci $this->assertSame("oneof_string", $n->getMyOneof()); 381ffe3c632Sopenharmony_ci $this->assertSame("a", $n->getOneofString()); 382ffe3c632Sopenharmony_ci 383ffe3c632Sopenharmony_ci $m->setOneofBytes("bbbb"); 384ffe3c632Sopenharmony_ci $data = $m->serializeToJsonString(); 385ffe3c632Sopenharmony_ci $n = new TestMessage(); 386ffe3c632Sopenharmony_ci $n->mergeFromJsonString($data); 387ffe3c632Sopenharmony_ci $this->assertSame("oneof_bytes", $n->getMyOneof()); 388ffe3c632Sopenharmony_ci $this->assertSame("bbbb", $n->getOneofBytes()); 389ffe3c632Sopenharmony_ci 390ffe3c632Sopenharmony_ci $sub_m = new Sub(); 391ffe3c632Sopenharmony_ci $m->setOneofMessage($sub_m); 392ffe3c632Sopenharmony_ci $data = $m->serializeToJsonString(); 393ffe3c632Sopenharmony_ci $n = new TestMessage(); 394ffe3c632Sopenharmony_ci $n->mergeFromJsonString($data); 395ffe3c632Sopenharmony_ci $this->assertSame("oneof_message", $n->getMyOneof()); 396ffe3c632Sopenharmony_ci $this->assertFalse(is_null($n->getOneofMessage())); 397ffe3c632Sopenharmony_ci } 398ffe3c632Sopenharmony_ci 399ffe3c632Sopenharmony_ci public function testPackedEncode() 400ffe3c632Sopenharmony_ci { 401ffe3c632Sopenharmony_ci $from = new TestPackedMessage(); 402ffe3c632Sopenharmony_ci TestUtil::setTestPackedMessage($from); 403ffe3c632Sopenharmony_ci $this->assertSame(TestUtil::getGoldenTestPackedMessage(), 404ffe3c632Sopenharmony_ci $from->serializeToString()); 405ffe3c632Sopenharmony_ci } 406ffe3c632Sopenharmony_ci 407ffe3c632Sopenharmony_ci public function testPackedDecodePacked() 408ffe3c632Sopenharmony_ci { 409ffe3c632Sopenharmony_ci $to = new TestPackedMessage(); 410ffe3c632Sopenharmony_ci $to->mergeFromString(TestUtil::getGoldenTestPackedMessage()); 411ffe3c632Sopenharmony_ci TestUtil::assertTestPackedMessage($to); 412ffe3c632Sopenharmony_ci $this->assertTrue(true); 413ffe3c632Sopenharmony_ci } 414ffe3c632Sopenharmony_ci 415ffe3c632Sopenharmony_ci public function testPackedDecodeUnpacked() 416ffe3c632Sopenharmony_ci { 417ffe3c632Sopenharmony_ci $to = new TestPackedMessage(); 418ffe3c632Sopenharmony_ci $to->mergeFromString(TestUtil::getGoldenTestUnpackedMessage()); 419ffe3c632Sopenharmony_ci TestUtil::assertTestPackedMessage($to); 420ffe3c632Sopenharmony_ci $this->assertTrue(true); 421ffe3c632Sopenharmony_ci } 422ffe3c632Sopenharmony_ci 423ffe3c632Sopenharmony_ci public function testUnpackedEncode() 424ffe3c632Sopenharmony_ci { 425ffe3c632Sopenharmony_ci $from = new TestUnpackedMessage(); 426ffe3c632Sopenharmony_ci TestUtil::setTestPackedMessage($from); 427ffe3c632Sopenharmony_ci $this->assertSame(TestUtil::getGoldenTestUnpackedMessage(), 428ffe3c632Sopenharmony_ci $from->serializeToString()); 429ffe3c632Sopenharmony_ci } 430ffe3c632Sopenharmony_ci 431ffe3c632Sopenharmony_ci public function testUnpackedDecodePacked() 432ffe3c632Sopenharmony_ci { 433ffe3c632Sopenharmony_ci $to = new TestUnpackedMessage(); 434ffe3c632Sopenharmony_ci $to->mergeFromString(TestUtil::getGoldenTestPackedMessage()); 435ffe3c632Sopenharmony_ci TestUtil::assertTestPackedMessage($to); 436ffe3c632Sopenharmony_ci $this->assertTrue(true); 437ffe3c632Sopenharmony_ci } 438ffe3c632Sopenharmony_ci 439ffe3c632Sopenharmony_ci public function testUnpackedDecodeUnpacked() 440ffe3c632Sopenharmony_ci { 441ffe3c632Sopenharmony_ci $to = new TestUnpackedMessage(); 442ffe3c632Sopenharmony_ci $to->mergeFromString(TestUtil::getGoldenTestUnpackedMessage()); 443ffe3c632Sopenharmony_ci TestUtil::assertTestPackedMessage($to); 444ffe3c632Sopenharmony_ci $this->assertTrue(true); 445ffe3c632Sopenharmony_ci } 446ffe3c632Sopenharmony_ci 447ffe3c632Sopenharmony_ci public function testDecodeInt64() 448ffe3c632Sopenharmony_ci { 449ffe3c632Sopenharmony_ci // Read 64 testing 450ffe3c632Sopenharmony_ci $testVals = array( 451ffe3c632Sopenharmony_ci '10' => '100a', 452ffe3c632Sopenharmony_ci '100' => '1064', 453ffe3c632Sopenharmony_ci '800' => '10a006', 454ffe3c632Sopenharmony_ci '6400' => '108032', 455ffe3c632Sopenharmony_ci '70400' => '1080a604', 456ffe3c632Sopenharmony_ci '774400' => '1080a22f', 457ffe3c632Sopenharmony_ci '9292800' => '108098b704', 458ffe3c632Sopenharmony_ci '74342400' => '1080c0b923', 459ffe3c632Sopenharmony_ci '743424000' => '108080bfe202', 460ffe3c632Sopenharmony_ci '8177664000' => '108080b5bb1e', 461ffe3c632Sopenharmony_ci '65421312000' => '108080a8dbf301', 462ffe3c632Sopenharmony_ci '785055744000' => '108080e0c7ec16', 463ffe3c632Sopenharmony_ci '9420668928000' => '10808080dd969202', 464ffe3c632Sopenharmony_ci '103627358208000' => '10808080fff9c717', 465ffe3c632Sopenharmony_ci '1139900940288000' => '10808080f5bd978302', 466ffe3c632Sopenharmony_ci '13678811283456000' => '10808080fce699a618', 467ffe3c632Sopenharmony_ci '109430490267648000' => '10808080e0b7ceb1c201', 468ffe3c632Sopenharmony_ci '984874412408832000' => '10808080e0f5c1bed50d', 469ffe3c632Sopenharmony_ci ); 470ffe3c632Sopenharmony_ci 471ffe3c632Sopenharmony_ci $msg = new TestMessage(); 472ffe3c632Sopenharmony_ci foreach ($testVals as $original => $encoded) { 473ffe3c632Sopenharmony_ci $msg->setOptionalInt64($original); 474ffe3c632Sopenharmony_ci $data = $msg->serializeToString(); 475ffe3c632Sopenharmony_ci $this->assertSame($encoded, bin2hex($data)); 476ffe3c632Sopenharmony_ci $msg->setOptionalInt64(0); 477ffe3c632Sopenharmony_ci $msg->mergeFromString($data); 478ffe3c632Sopenharmony_ci $this->assertEquals($original, $msg->getOptionalInt64()); 479ffe3c632Sopenharmony_ci } 480ffe3c632Sopenharmony_ci } 481ffe3c632Sopenharmony_ci 482ffe3c632Sopenharmony_ci public function testDecodeToExistingMessage() 483ffe3c632Sopenharmony_ci { 484ffe3c632Sopenharmony_ci $m1 = new TestMessage(); 485ffe3c632Sopenharmony_ci $this->setFields($m1); 486ffe3c632Sopenharmony_ci $this->expectFields($m1); 487ffe3c632Sopenharmony_ci 488ffe3c632Sopenharmony_ci $m2 = new TestMessage(); 489ffe3c632Sopenharmony_ci $this->setFields2($m2); 490ffe3c632Sopenharmony_ci $data = $m2->serializeToString(); 491ffe3c632Sopenharmony_ci 492ffe3c632Sopenharmony_ci $m1->mergeFromString($data); 493ffe3c632Sopenharmony_ci $this->expectFieldsMerged($m1); 494ffe3c632Sopenharmony_ci } 495ffe3c632Sopenharmony_ci 496ffe3c632Sopenharmony_ci public function testDecodeFieldNonExist() 497ffe3c632Sopenharmony_ci { 498ffe3c632Sopenharmony_ci $data = hex2bin('c80501'); 499ffe3c632Sopenharmony_ci $m = new TestMessage(); 500ffe3c632Sopenharmony_ci $m->mergeFromString($data); 501ffe3c632Sopenharmony_ci $this->assertTrue(true); 502ffe3c632Sopenharmony_ci } 503ffe3c632Sopenharmony_ci 504ffe3c632Sopenharmony_ci public function testEncodeNegativeInt32() 505ffe3c632Sopenharmony_ci { 506ffe3c632Sopenharmony_ci $m = new TestMessage(); 507ffe3c632Sopenharmony_ci $m->setOptionalInt32(-1); 508ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 509ffe3c632Sopenharmony_ci $this->assertSame("08ffffffffffffffffff01", bin2hex($data)); 510ffe3c632Sopenharmony_ci } 511ffe3c632Sopenharmony_ci 512ffe3c632Sopenharmony_ci public function testDecodeNegativeInt32() 513ffe3c632Sopenharmony_ci { 514ffe3c632Sopenharmony_ci $m = new TestMessage(); 515ffe3c632Sopenharmony_ci $this->assertEquals(0, $m->getOptionalInt32()); 516ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin("08ffffffffffffffffff01")); 517ffe3c632Sopenharmony_ci $this->assertEquals(-1, $m->getOptionalInt32()); 518ffe3c632Sopenharmony_ci 519ffe3c632Sopenharmony_ci $m = new TestMessage(); 520ffe3c632Sopenharmony_ci $this->assertEquals(0, $m->getOptionalInt32()); 521ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin("08ffffffff0f")); 522ffe3c632Sopenharmony_ci $this->assertEquals(-1, $m->getOptionalInt32()); 523ffe3c632Sopenharmony_ci } 524ffe3c632Sopenharmony_ci 525ffe3c632Sopenharmony_ci public function testRandomFieldOrder() 526ffe3c632Sopenharmony_ci { 527ffe3c632Sopenharmony_ci $m = new TestRandomFieldOrder(); 528ffe3c632Sopenharmony_ci $data = $m->serializeToString(); 529ffe3c632Sopenharmony_ci $this->assertSame("", $data); 530ffe3c632Sopenharmony_ci } 531ffe3c632Sopenharmony_ci 532ffe3c632Sopenharmony_ci /** 533ffe3c632Sopenharmony_ci * @expectedException Exception 534ffe3c632Sopenharmony_ci */ 535ffe3c632Sopenharmony_ci public function testDecodeInvalidInt32() 536ffe3c632Sopenharmony_ci { 537ffe3c632Sopenharmony_ci $m = new TestMessage(); 538ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('08')); 539ffe3c632Sopenharmony_ci } 540ffe3c632Sopenharmony_ci 541ffe3c632Sopenharmony_ci /** 542ffe3c632Sopenharmony_ci * @expectedException Exception 543ffe3c632Sopenharmony_ci */ 544ffe3c632Sopenharmony_ci public function testDecodeInvalidSubMessage() 545ffe3c632Sopenharmony_ci { 546ffe3c632Sopenharmony_ci $m = new TestMessage(); 547ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('9A010108')); 548ffe3c632Sopenharmony_ci } 549ffe3c632Sopenharmony_ci 550ffe3c632Sopenharmony_ci /** 551ffe3c632Sopenharmony_ci * @expectedException Exception 552ffe3c632Sopenharmony_ci */ 553ffe3c632Sopenharmony_ci public function testDecodeInvalidInt64() 554ffe3c632Sopenharmony_ci { 555ffe3c632Sopenharmony_ci $m = new TestMessage(); 556ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('10')); 557ffe3c632Sopenharmony_ci } 558ffe3c632Sopenharmony_ci 559ffe3c632Sopenharmony_ci /** 560ffe3c632Sopenharmony_ci * @expectedException Exception 561ffe3c632Sopenharmony_ci */ 562ffe3c632Sopenharmony_ci public function testDecodeInvalidUInt32() 563ffe3c632Sopenharmony_ci { 564ffe3c632Sopenharmony_ci $m = new TestMessage(); 565ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('18')); 566ffe3c632Sopenharmony_ci } 567ffe3c632Sopenharmony_ci 568ffe3c632Sopenharmony_ci /** 569ffe3c632Sopenharmony_ci * @expectedException Exception 570ffe3c632Sopenharmony_ci */ 571ffe3c632Sopenharmony_ci public function testDecodeInvalidUInt64() 572ffe3c632Sopenharmony_ci { 573ffe3c632Sopenharmony_ci $m = new TestMessage(); 574ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('20')); 575ffe3c632Sopenharmony_ci } 576ffe3c632Sopenharmony_ci 577ffe3c632Sopenharmony_ci /** 578ffe3c632Sopenharmony_ci * @expectedException Exception 579ffe3c632Sopenharmony_ci */ 580ffe3c632Sopenharmony_ci public function testDecodeInvalidSInt32() 581ffe3c632Sopenharmony_ci { 582ffe3c632Sopenharmony_ci $m = new TestMessage(); 583ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('28')); 584ffe3c632Sopenharmony_ci } 585ffe3c632Sopenharmony_ci 586ffe3c632Sopenharmony_ci /** 587ffe3c632Sopenharmony_ci * @expectedException Exception 588ffe3c632Sopenharmony_ci */ 589ffe3c632Sopenharmony_ci public function testDecodeInvalidSInt64() 590ffe3c632Sopenharmony_ci { 591ffe3c632Sopenharmony_ci $m = new TestMessage(); 592ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('30')); 593ffe3c632Sopenharmony_ci } 594ffe3c632Sopenharmony_ci 595ffe3c632Sopenharmony_ci /** 596ffe3c632Sopenharmony_ci * @expectedException Exception 597ffe3c632Sopenharmony_ci */ 598ffe3c632Sopenharmony_ci public function testDecodeInvalidFixed32() 599ffe3c632Sopenharmony_ci { 600ffe3c632Sopenharmony_ci $m = new TestMessage(); 601ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('3D')); 602ffe3c632Sopenharmony_ci } 603ffe3c632Sopenharmony_ci 604ffe3c632Sopenharmony_ci /** 605ffe3c632Sopenharmony_ci * @expectedException Exception 606ffe3c632Sopenharmony_ci */ 607ffe3c632Sopenharmony_ci public function testDecodeInvalidFixed64() 608ffe3c632Sopenharmony_ci { 609ffe3c632Sopenharmony_ci $m = new TestMessage(); 610ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('41')); 611ffe3c632Sopenharmony_ci } 612ffe3c632Sopenharmony_ci 613ffe3c632Sopenharmony_ci /** 614ffe3c632Sopenharmony_ci * @expectedException Exception 615ffe3c632Sopenharmony_ci */ 616ffe3c632Sopenharmony_ci public function testDecodeInvalidSFixed32() 617ffe3c632Sopenharmony_ci { 618ffe3c632Sopenharmony_ci $m = new TestMessage(); 619ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('4D')); 620ffe3c632Sopenharmony_ci } 621ffe3c632Sopenharmony_ci 622ffe3c632Sopenharmony_ci /** 623ffe3c632Sopenharmony_ci * @expectedException Exception 624ffe3c632Sopenharmony_ci */ 625ffe3c632Sopenharmony_ci public function testDecodeInvalidSFixed64() 626ffe3c632Sopenharmony_ci { 627ffe3c632Sopenharmony_ci $m = new TestMessage(); 628ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('51')); 629ffe3c632Sopenharmony_ci } 630ffe3c632Sopenharmony_ci 631ffe3c632Sopenharmony_ci /** 632ffe3c632Sopenharmony_ci * @expectedException Exception 633ffe3c632Sopenharmony_ci */ 634ffe3c632Sopenharmony_ci public function testDecodeInvalidFloat() 635ffe3c632Sopenharmony_ci { 636ffe3c632Sopenharmony_ci $m = new TestMessage(); 637ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('5D')); 638ffe3c632Sopenharmony_ci } 639ffe3c632Sopenharmony_ci 640ffe3c632Sopenharmony_ci /** 641ffe3c632Sopenharmony_ci * @expectedException Exception 642ffe3c632Sopenharmony_ci */ 643ffe3c632Sopenharmony_ci public function testDecodeInvalidDouble() 644ffe3c632Sopenharmony_ci { 645ffe3c632Sopenharmony_ci $m = new TestMessage(); 646ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('61')); 647ffe3c632Sopenharmony_ci } 648ffe3c632Sopenharmony_ci 649ffe3c632Sopenharmony_ci /** 650ffe3c632Sopenharmony_ci * @expectedException Exception 651ffe3c632Sopenharmony_ci */ 652ffe3c632Sopenharmony_ci public function testDecodeInvalidBool() 653ffe3c632Sopenharmony_ci { 654ffe3c632Sopenharmony_ci $m = new TestMessage(); 655ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('68')); 656ffe3c632Sopenharmony_ci } 657ffe3c632Sopenharmony_ci 658ffe3c632Sopenharmony_ci /** 659ffe3c632Sopenharmony_ci * @expectedException Exception 660ffe3c632Sopenharmony_ci */ 661ffe3c632Sopenharmony_ci public function testDecodeInvalidStringLengthMiss() 662ffe3c632Sopenharmony_ci { 663ffe3c632Sopenharmony_ci $m = new TestMessage(); 664ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('72')); 665ffe3c632Sopenharmony_ci } 666ffe3c632Sopenharmony_ci 667ffe3c632Sopenharmony_ci /** 668ffe3c632Sopenharmony_ci * @expectedException Exception 669ffe3c632Sopenharmony_ci */ 670ffe3c632Sopenharmony_ci public function testDecodeInvalidStringDataMiss() 671ffe3c632Sopenharmony_ci { 672ffe3c632Sopenharmony_ci $m = new TestMessage(); 673ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('7201')); 674ffe3c632Sopenharmony_ci } 675ffe3c632Sopenharmony_ci 676ffe3c632Sopenharmony_ci /** 677ffe3c632Sopenharmony_ci * @expectedException Exception 678ffe3c632Sopenharmony_ci */ 679ffe3c632Sopenharmony_ci public function testDecodeInvalidBytesLengthMiss() 680ffe3c632Sopenharmony_ci { 681ffe3c632Sopenharmony_ci $m = new TestMessage(); 682ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('7A')); 683ffe3c632Sopenharmony_ci } 684ffe3c632Sopenharmony_ci 685ffe3c632Sopenharmony_ci /** 686ffe3c632Sopenharmony_ci * @expectedException Exception 687ffe3c632Sopenharmony_ci */ 688ffe3c632Sopenharmony_ci public function testDecodeInvalidBytesDataMiss() 689ffe3c632Sopenharmony_ci { 690ffe3c632Sopenharmony_ci $m = new TestMessage(); 691ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('7A01')); 692ffe3c632Sopenharmony_ci } 693ffe3c632Sopenharmony_ci 694ffe3c632Sopenharmony_ci /** 695ffe3c632Sopenharmony_ci * @expectedException Exception 696ffe3c632Sopenharmony_ci */ 697ffe3c632Sopenharmony_ci public function testDecodeInvalidEnum() 698ffe3c632Sopenharmony_ci { 699ffe3c632Sopenharmony_ci $m = new TestMessage(); 700ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('8001')); 701ffe3c632Sopenharmony_ci } 702ffe3c632Sopenharmony_ci 703ffe3c632Sopenharmony_ci /** 704ffe3c632Sopenharmony_ci * @expectedException Exception 705ffe3c632Sopenharmony_ci */ 706ffe3c632Sopenharmony_ci public function testDecodeInvalidMessageLengthMiss() 707ffe3c632Sopenharmony_ci { 708ffe3c632Sopenharmony_ci $m = new TestMessage(); 709ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('8A01')); 710ffe3c632Sopenharmony_ci } 711ffe3c632Sopenharmony_ci 712ffe3c632Sopenharmony_ci /** 713ffe3c632Sopenharmony_ci * @expectedException Exception 714ffe3c632Sopenharmony_ci */ 715ffe3c632Sopenharmony_ci public function testDecodeInvalidMessageDataMiss() 716ffe3c632Sopenharmony_ci { 717ffe3c632Sopenharmony_ci $m = new TestMessage(); 718ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('8A0101')); 719ffe3c632Sopenharmony_ci } 720ffe3c632Sopenharmony_ci 721ffe3c632Sopenharmony_ci /** 722ffe3c632Sopenharmony_ci * @expectedException Exception 723ffe3c632Sopenharmony_ci */ 724ffe3c632Sopenharmony_ci public function testDecodeInvalidPackedMessageLength() 725ffe3c632Sopenharmony_ci { 726ffe3c632Sopenharmony_ci $m = new TestPackedMessage(); 727ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin('D205')); 728ffe3c632Sopenharmony_ci } 729ffe3c632Sopenharmony_ci 730ffe3c632Sopenharmony_ci public function testUnknown() 731ffe3c632Sopenharmony_ci { 732ffe3c632Sopenharmony_ci // Test preserve unknown for varint. 733ffe3c632Sopenharmony_ci $m = new TestMessage(); 734ffe3c632Sopenharmony_ci $from = hex2bin('F80601'); // TODO(teboring): Add a util to encode 735ffe3c632Sopenharmony_ci // varint for better readability 736ffe3c632Sopenharmony_ci $m->mergeFromString($from); 737ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 738ffe3c632Sopenharmony_ci $this->assertSame(bin2hex($from), bin2hex($to)); 739ffe3c632Sopenharmony_ci 740ffe3c632Sopenharmony_ci // Test preserve unknown for 64-bit. 741ffe3c632Sopenharmony_ci $m = new TestMessage(); 742ffe3c632Sopenharmony_ci $from = hex2bin('F9060000000000000000'); 743ffe3c632Sopenharmony_ci $m->mergeFromString($from); 744ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 745ffe3c632Sopenharmony_ci $this->assertSame(bin2hex($from), bin2hex($to)); 746ffe3c632Sopenharmony_ci 747ffe3c632Sopenharmony_ci // Test preserve unknown for length delimited. 748ffe3c632Sopenharmony_ci $m = new TestMessage(); 749ffe3c632Sopenharmony_ci $from = hex2bin('FA0600'); 750ffe3c632Sopenharmony_ci $m->mergeFromString($from); 751ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 752ffe3c632Sopenharmony_ci $this->assertSame(bin2hex($from), bin2hex($to)); 753ffe3c632Sopenharmony_ci 754ffe3c632Sopenharmony_ci // Test preserve unknown for 32-bit. 755ffe3c632Sopenharmony_ci $m = new TestMessage(); 756ffe3c632Sopenharmony_ci $from = hex2bin('FD0600000000'); 757ffe3c632Sopenharmony_ci $m->mergeFromString($from); 758ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 759ffe3c632Sopenharmony_ci $this->assertSame(bin2hex($from), bin2hex($to)); 760ffe3c632Sopenharmony_ci 761ffe3c632Sopenharmony_ci // Test discard unknown in message. 762ffe3c632Sopenharmony_ci $m = new TestMessage(); 763ffe3c632Sopenharmony_ci $from = hex2bin('F80601'); 764ffe3c632Sopenharmony_ci $m->mergeFromString($from); 765ffe3c632Sopenharmony_ci $m->discardUnknownFields(); 766ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 767ffe3c632Sopenharmony_ci $this->assertSame("", bin2hex($to)); 768ffe3c632Sopenharmony_ci 769ffe3c632Sopenharmony_ci // Test discard unknown for singular message field. 770ffe3c632Sopenharmony_ci $m = new TestMessage(); 771ffe3c632Sopenharmony_ci $from = hex2bin('8A0103F80601'); 772ffe3c632Sopenharmony_ci $m->mergeFromString($from); 773ffe3c632Sopenharmony_ci $m->discardUnknownFields(); 774ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 775ffe3c632Sopenharmony_ci $this->assertSame("8a0100", bin2hex($to)); 776ffe3c632Sopenharmony_ci 777ffe3c632Sopenharmony_ci // Test discard unknown for repeated message field. 778ffe3c632Sopenharmony_ci $m = new TestMessage(); 779ffe3c632Sopenharmony_ci $from = hex2bin('FA0203F80601'); 780ffe3c632Sopenharmony_ci $m->mergeFromString($from); 781ffe3c632Sopenharmony_ci $m->discardUnknownFields(); 782ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 783ffe3c632Sopenharmony_ci $this->assertSame("fa0200", bin2hex($to)); 784ffe3c632Sopenharmony_ci 785ffe3c632Sopenharmony_ci // Test discard unknown for map message value field. 786ffe3c632Sopenharmony_ci $m = new TestMessage(); 787ffe3c632Sopenharmony_ci $from = hex2bin("BA050708011203F80601"); 788ffe3c632Sopenharmony_ci $m->mergeFromString($from); 789ffe3c632Sopenharmony_ci $m->discardUnknownFields(); 790ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 791ffe3c632Sopenharmony_ci $this->assertSame("ba050408011200", bin2hex($to)); 792ffe3c632Sopenharmony_ci 793ffe3c632Sopenharmony_ci // Test discard unknown for singular message field. 794ffe3c632Sopenharmony_ci $m = new TestMessage(); 795ffe3c632Sopenharmony_ci $from = hex2bin('9A0403F80601'); 796ffe3c632Sopenharmony_ci $m->mergeFromString($from); 797ffe3c632Sopenharmony_ci $m->discardUnknownFields(); 798ffe3c632Sopenharmony_ci $to = $m->serializeToString(); 799ffe3c632Sopenharmony_ci $this->assertSame("9a0400", bin2hex($to)); 800ffe3c632Sopenharmony_ci } 801ffe3c632Sopenharmony_ci 802ffe3c632Sopenharmony_ci public function testJsonUnknown() 803ffe3c632Sopenharmony_ci { 804ffe3c632Sopenharmony_ci // Test unknown number 805ffe3c632Sopenharmony_ci $m = new TestMessage(); 806ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":1, 807ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 808ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 809ffe3c632Sopenharmony_ci 810ffe3c632Sopenharmony_ci // Test unknown bool 811ffe3c632Sopenharmony_ci $m = new TestMessage(); 812ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":true, 813ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 814ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 815ffe3c632Sopenharmony_ci 816ffe3c632Sopenharmony_ci // Test unknown string 817ffe3c632Sopenharmony_ci $m = new TestMessage(); 818ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":\"abc\", 819ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 820ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 821ffe3c632Sopenharmony_ci 822ffe3c632Sopenharmony_ci // Test unknown null 823ffe3c632Sopenharmony_ci $m = new TestMessage(); 824ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":null, 825ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 826ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 827ffe3c632Sopenharmony_ci 828ffe3c632Sopenharmony_ci // Test unknown array 829ffe3c632Sopenharmony_ci $m = new TestMessage(); 830ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[], 831ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 832ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 833ffe3c632Sopenharmony_ci 834ffe3c632Sopenharmony_ci // Test unknown number array 835ffe3c632Sopenharmony_ci $m = new TestMessage(); 836ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[1], 837ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 838ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 839ffe3c632Sopenharmony_ci 840ffe3c632Sopenharmony_ci // Test unknown bool array 841ffe3c632Sopenharmony_ci $m = new TestMessage(); 842ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[true], 843ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 844ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 845ffe3c632Sopenharmony_ci 846ffe3c632Sopenharmony_ci // Test unknown string array 847ffe3c632Sopenharmony_ci $m = new TestMessage(); 848ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[\"a\"], 849ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 850ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 851ffe3c632Sopenharmony_ci 852ffe3c632Sopenharmony_ci // Test unknown null array 853ffe3c632Sopenharmony_ci $m = new TestMessage(); 854ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[null], 855ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 856ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 857ffe3c632Sopenharmony_ci 858ffe3c632Sopenharmony_ci // Test unknown array array 859ffe3c632Sopenharmony_ci $m = new TestMessage(); 860ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[[]], 861ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 862ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 863ffe3c632Sopenharmony_ci 864ffe3c632Sopenharmony_ci // Test unknown object array 865ffe3c632Sopenharmony_ci $m = new TestMessage(); 866ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[{}], 867ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 868ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 869ffe3c632Sopenharmony_ci 870ffe3c632Sopenharmony_ci // Test unknown double value array 871ffe3c632Sopenharmony_ci $m = new TestMessage(); 872ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":[1, 2], 873ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 874ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 875ffe3c632Sopenharmony_ci 876ffe3c632Sopenharmony_ci // Test unknown object 877ffe3c632Sopenharmony_ci $m = new TestMessage(); 878ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{}, 879ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 880ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 881ffe3c632Sopenharmony_ci 882ffe3c632Sopenharmony_ci // Test unknown number object 883ffe3c632Sopenharmony_ci $m = new TestMessage(); 884ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{\"a\":1}, 885ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 886ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 887ffe3c632Sopenharmony_ci 888ffe3c632Sopenharmony_ci // Test unknown bool object 889ffe3c632Sopenharmony_ci $m = new TestMessage(); 890ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{\"a\":true}, 891ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 892ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 893ffe3c632Sopenharmony_ci 894ffe3c632Sopenharmony_ci // Test unknown string object 895ffe3c632Sopenharmony_ci $m = new TestMessage(); 896ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{\"a\":\"a\"}, 897ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 898ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 899ffe3c632Sopenharmony_ci 900ffe3c632Sopenharmony_ci // Test unknown null object 901ffe3c632Sopenharmony_ci $m = new TestMessage(); 902ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{\"a\":null}, 903ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 904ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 905ffe3c632Sopenharmony_ci 906ffe3c632Sopenharmony_ci // Test unknown array object 907ffe3c632Sopenharmony_ci $m = new TestMessage(); 908ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{\"a\":[]}, 909ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 910ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 911ffe3c632Sopenharmony_ci 912ffe3c632Sopenharmony_ci // Test unknown object object 913ffe3c632Sopenharmony_ci $m = new TestMessage(); 914ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{\"a\":{}}, 915ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 916ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 917ffe3c632Sopenharmony_ci 918ffe3c632Sopenharmony_ci // Test unknown double value object 919ffe3c632Sopenharmony_ci $m = new TestMessage(); 920ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"unknown\":{\"a\":1, \"b\":1}, 921ffe3c632Sopenharmony_ci \"optionalInt32\":1}", true); 922ffe3c632Sopenharmony_ci $this->assertSame(1, $m->getOptionalInt32()); 923ffe3c632Sopenharmony_ci 924ffe3c632Sopenharmony_ci // Test unknown enum value 925ffe3c632Sopenharmony_ci $m = new TestMessage(); 926ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"optionalEnum\":\"UNKNOWN\"}", true); 927ffe3c632Sopenharmony_ci $this->assertSame(0, $m->getOptionalEnum()); 928ffe3c632Sopenharmony_ci } 929ffe3c632Sopenharmony_ci 930ffe3c632Sopenharmony_ci public function testJsonEncode() 931ffe3c632Sopenharmony_ci { 932ffe3c632Sopenharmony_ci $from = new TestMessage(); 933ffe3c632Sopenharmony_ci $this->setFields($from); 934ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 935ffe3c632Sopenharmony_ci $to = new TestMessage(); 936ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 937ffe3c632Sopenharmony_ci $this->expectFields($to); 938ffe3c632Sopenharmony_ci } 939ffe3c632Sopenharmony_ci 940ffe3c632Sopenharmony_ci public function testDecodeDuration() 941ffe3c632Sopenharmony_ci { 942ffe3c632Sopenharmony_ci $m = new Google\Protobuf\Duration(); 943ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"1234.5678s\""); 944ffe3c632Sopenharmony_ci $this->assertEquals(1234, $m->getSeconds()); 945ffe3c632Sopenharmony_ci $this->assertEquals(567800000, $m->getNanos()); 946ffe3c632Sopenharmony_ci } 947ffe3c632Sopenharmony_ci 948ffe3c632Sopenharmony_ci public function testEncodeDuration() 949ffe3c632Sopenharmony_ci { 950ffe3c632Sopenharmony_ci $m = new Google\Protobuf\Duration(); 951ffe3c632Sopenharmony_ci $m->setSeconds(1234); 952ffe3c632Sopenharmony_ci $m->setNanos(999999999); 953ffe3c632Sopenharmony_ci $this->assertEquals("\"1234.999999999s\"", $m->serializeToJsonString()); 954ffe3c632Sopenharmony_ci } 955ffe3c632Sopenharmony_ci 956ffe3c632Sopenharmony_ci public function testDecodeTimestamp() 957ffe3c632Sopenharmony_ci { 958ffe3c632Sopenharmony_ci $m = new Google\Protobuf\Timestamp(); 959ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"2000-01-01T00:00:00.123456789Z\""); 960ffe3c632Sopenharmony_ci $this->assertEquals(946684800, $m->getSeconds()); 961ffe3c632Sopenharmony_ci $this->assertEquals(123456789, $m->getNanos()); 962ffe3c632Sopenharmony_ci } 963ffe3c632Sopenharmony_ci 964ffe3c632Sopenharmony_ci public function testEncodeTimestamp() 965ffe3c632Sopenharmony_ci { 966ffe3c632Sopenharmony_ci $m = new Google\Protobuf\Timestamp(); 967ffe3c632Sopenharmony_ci $m->setSeconds(946684800); 968ffe3c632Sopenharmony_ci $m->setNanos(123456789); 969ffe3c632Sopenharmony_ci $this->assertEquals("\"2000-01-01T00:00:00.123456789Z\"", 970ffe3c632Sopenharmony_ci $m->serializeToJsonString()); 971ffe3c632Sopenharmony_ci } 972ffe3c632Sopenharmony_ci 973ffe3c632Sopenharmony_ci public function testDecodeTopLevelValue() 974ffe3c632Sopenharmony_ci { 975ffe3c632Sopenharmony_ci $m = new Value(); 976ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"a\""); 977ffe3c632Sopenharmony_ci $this->assertSame("a", $m->getStringValue()); 978ffe3c632Sopenharmony_ci 979ffe3c632Sopenharmony_ci $m = new Value(); 980ffe3c632Sopenharmony_ci $m->mergeFromJsonString("1.5"); 981ffe3c632Sopenharmony_ci $this->assertSame(1.5, $m->getNumberValue()); 982ffe3c632Sopenharmony_ci 983ffe3c632Sopenharmony_ci $m = new Value(); 984ffe3c632Sopenharmony_ci $m->mergeFromJsonString("true"); 985ffe3c632Sopenharmony_ci $this->assertSame(true, $m->getBoolValue()); 986ffe3c632Sopenharmony_ci 987ffe3c632Sopenharmony_ci $m = new Value(); 988ffe3c632Sopenharmony_ci $m->mergeFromJsonString("null"); 989ffe3c632Sopenharmony_ci $this->assertSame("null_value", $m->getKind()); 990ffe3c632Sopenharmony_ci 991ffe3c632Sopenharmony_ci $m = new Value(); 992ffe3c632Sopenharmony_ci $m->mergeFromJsonString("[1]"); 993ffe3c632Sopenharmony_ci $this->assertSame("list_value", $m->getKind()); 994ffe3c632Sopenharmony_ci 995ffe3c632Sopenharmony_ci $m = new Value(); 996ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"a\":1}"); 997ffe3c632Sopenharmony_ci $this->assertSame("struct_value", $m->getKind()); 998ffe3c632Sopenharmony_ci } 999ffe3c632Sopenharmony_ci 1000ffe3c632Sopenharmony_ci public function testEncodeTopLevelValue() 1001ffe3c632Sopenharmony_ci { 1002ffe3c632Sopenharmony_ci $m = new Value(); 1003ffe3c632Sopenharmony_ci $m->setStringValue("a"); 1004ffe3c632Sopenharmony_ci $this->assertSame("\"a\"", $m->serializeToJsonString()); 1005ffe3c632Sopenharmony_ci 1006ffe3c632Sopenharmony_ci $m = new Value(); 1007ffe3c632Sopenharmony_ci $m->setNumberValue(1.5); 1008ffe3c632Sopenharmony_ci $this->assertSame("1.5", $m->serializeToJsonString()); 1009ffe3c632Sopenharmony_ci 1010ffe3c632Sopenharmony_ci $m = new Value(); 1011ffe3c632Sopenharmony_ci $m->setBoolValue(true); 1012ffe3c632Sopenharmony_ci $this->assertSame("true", $m->serializeToJsonString()); 1013ffe3c632Sopenharmony_ci 1014ffe3c632Sopenharmony_ci $m = new Value(); 1015ffe3c632Sopenharmony_ci $m->setNullValue(0); 1016ffe3c632Sopenharmony_ci $this->assertSame("null", $m->serializeToJsonString()); 1017ffe3c632Sopenharmony_ci } 1018ffe3c632Sopenharmony_ci 1019ffe3c632Sopenharmony_ci public function testDecodeTopLevelListValue() 1020ffe3c632Sopenharmony_ci { 1021ffe3c632Sopenharmony_ci $m = new ListValue(); 1022ffe3c632Sopenharmony_ci $m->mergeFromJsonString("[1]"); 1023ffe3c632Sopenharmony_ci $this->assertSame(1.0, $m->getValues()[0]->getNumberValue()); 1024ffe3c632Sopenharmony_ci } 1025ffe3c632Sopenharmony_ci 1026ffe3c632Sopenharmony_ci public function testEncodeTopLevelListValue() 1027ffe3c632Sopenharmony_ci { 1028ffe3c632Sopenharmony_ci $m = new ListValue(); 1029ffe3c632Sopenharmony_ci $arr = $m->getValues(); 1030ffe3c632Sopenharmony_ci $sub = new Value(); 1031ffe3c632Sopenharmony_ci $sub->setNumberValue(1.5); 1032ffe3c632Sopenharmony_ci $arr[] = $sub; 1033ffe3c632Sopenharmony_ci $this->assertSame("[1.5]", $m->serializeToJsonString()); 1034ffe3c632Sopenharmony_ci } 1035ffe3c632Sopenharmony_ci 1036ffe3c632Sopenharmony_ci public function testEncodeEmptyListValue() 1037ffe3c632Sopenharmony_ci { 1038ffe3c632Sopenharmony_ci $m = new Struct(); 1039ffe3c632Sopenharmony_ci $m->setFields(['test' => (new Value())->setListValue(new ListValue())]); 1040ffe3c632Sopenharmony_ci $this->assertSame('{"test":[]}', $m->serializeToJsonString()); 1041ffe3c632Sopenharmony_ci } 1042ffe3c632Sopenharmony_ci 1043ffe3c632Sopenharmony_ci public function testDecodeTopLevelStruct() 1044ffe3c632Sopenharmony_ci { 1045ffe3c632Sopenharmony_ci $m = new Struct(); 1046ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"a\":{\"b\":1}}"); 1047ffe3c632Sopenharmony_ci $this->assertSame(1.0, $m->getFields()["a"] 1048ffe3c632Sopenharmony_ci ->getStructValue() 1049ffe3c632Sopenharmony_ci ->getFields()["b"]->getNumberValue()); 1050ffe3c632Sopenharmony_ci } 1051ffe3c632Sopenharmony_ci 1052ffe3c632Sopenharmony_ci public function testEncodeTopLevelStruct() 1053ffe3c632Sopenharmony_ci { 1054ffe3c632Sopenharmony_ci $m = new Struct(); 1055ffe3c632Sopenharmony_ci $map = $m->getFields(); 1056ffe3c632Sopenharmony_ci $sub = new Value(); 1057ffe3c632Sopenharmony_ci $sub->setNumberValue(1.5); 1058ffe3c632Sopenharmony_ci $map["a"] = $sub; 1059ffe3c632Sopenharmony_ci $this->assertSame("{\"a\":1.5}", $m->serializeToJsonString()); 1060ffe3c632Sopenharmony_ci } 1061ffe3c632Sopenharmony_ci 1062ffe3c632Sopenharmony_ci public function testEncodeEmptyStruct() 1063ffe3c632Sopenharmony_ci { 1064ffe3c632Sopenharmony_ci $m = new Struct(); 1065ffe3c632Sopenharmony_ci $m->setFields(['test' => (new Value())->setStructValue(new Struct())]); 1066ffe3c632Sopenharmony_ci $this->assertSame('{"test":{}}', $m->serializeToJsonString()); 1067ffe3c632Sopenharmony_ci } 1068ffe3c632Sopenharmony_ci 1069ffe3c632Sopenharmony_ci public function testDecodeTopLevelAny() 1070ffe3c632Sopenharmony_ci { 1071ffe3c632Sopenharmony_ci // Make sure packed message has been created at least once. 1072ffe3c632Sopenharmony_ci $packed = new TestMessage(); 1073ffe3c632Sopenharmony_ci 1074ffe3c632Sopenharmony_ci $m1 = new Any(); 1075ffe3c632Sopenharmony_ci $m1->mergeFromJsonString( 1076ffe3c632Sopenharmony_ci "{\"optionalInt32\": 1, " . 1077ffe3c632Sopenharmony_ci "\"@type\":\"type.googleapis.com/foo.TestMessage\"}"); 1078ffe3c632Sopenharmony_ci $this->assertSame("type.googleapis.com/foo.TestMessage", 1079ffe3c632Sopenharmony_ci $m1->getTypeUrl()); 1080ffe3c632Sopenharmony_ci $this->assertSame("0801", bin2hex($m1->getValue())); 1081ffe3c632Sopenharmony_ci 1082ffe3c632Sopenharmony_ci $m2 = new Any(); 1083ffe3c632Sopenharmony_ci $m2->mergeFromJsonString( 1084ffe3c632Sopenharmony_ci "{\"@type\":\"type.googleapis.com/foo.TestMessage\", " . 1085ffe3c632Sopenharmony_ci "\"optionalInt32\": 1}"); 1086ffe3c632Sopenharmony_ci $this->assertSame("type.googleapis.com/foo.TestMessage", 1087ffe3c632Sopenharmony_ci $m2->getTypeUrl()); 1088ffe3c632Sopenharmony_ci $this->assertSame("0801", bin2hex($m2->getValue())); 1089ffe3c632Sopenharmony_ci 1090ffe3c632Sopenharmony_ci $m3 = new Any(); 1091ffe3c632Sopenharmony_ci $m3->mergeFromJsonString( 1092ffe3c632Sopenharmony_ci "{\"optionalInt32\": 1, " . 1093ffe3c632Sopenharmony_ci "\"@type\":\"type.googleapis.com/foo.TestMessage\", " . 1094ffe3c632Sopenharmony_ci "\"optionalInt64\": 2}"); 1095ffe3c632Sopenharmony_ci $this->assertSame("type.googleapis.com/foo.TestMessage", 1096ffe3c632Sopenharmony_ci $m3->getTypeUrl()); 1097ffe3c632Sopenharmony_ci $this->assertSame("08011002", bin2hex($m3->getValue())); 1098ffe3c632Sopenharmony_ci } 1099ffe3c632Sopenharmony_ci 1100ffe3c632Sopenharmony_ci public function testDecodeAny() 1101ffe3c632Sopenharmony_ci { 1102ffe3c632Sopenharmony_ci // Make sure packed message has been created at least once. 1103ffe3c632Sopenharmony_ci $packed = new TestMessage(); 1104ffe3c632Sopenharmony_ci 1105ffe3c632Sopenharmony_ci $m1 = new TestAny(); 1106ffe3c632Sopenharmony_ci $m1->mergeFromJsonString( 1107ffe3c632Sopenharmony_ci "{\"any\": {\"optionalInt32\": 1, " . 1108ffe3c632Sopenharmony_ci "\"@type\":\"type.googleapis.com/foo.TestMessage\"}}"); 1109ffe3c632Sopenharmony_ci $this->assertSame("type.googleapis.com/foo.TestMessage", 1110ffe3c632Sopenharmony_ci $m1->getAny()->getTypeUrl()); 1111ffe3c632Sopenharmony_ci $this->assertSame("0801", bin2hex($m1->getAny()->getValue())); 1112ffe3c632Sopenharmony_ci 1113ffe3c632Sopenharmony_ci $m2 = new TestAny(); 1114ffe3c632Sopenharmony_ci $m2->mergeFromJsonString( 1115ffe3c632Sopenharmony_ci "{\"any\":{\"@type\":\"type.googleapis.com/foo.TestMessage\", " . 1116ffe3c632Sopenharmony_ci "\"optionalInt32\": 1}}"); 1117ffe3c632Sopenharmony_ci $this->assertSame("type.googleapis.com/foo.TestMessage", 1118ffe3c632Sopenharmony_ci $m2->getAny()->getTypeUrl()); 1119ffe3c632Sopenharmony_ci $this->assertSame("0801", bin2hex($m2->getAny()->getValue())); 1120ffe3c632Sopenharmony_ci 1121ffe3c632Sopenharmony_ci $m3 = new TestAny(); 1122ffe3c632Sopenharmony_ci $m3->mergeFromJsonString( 1123ffe3c632Sopenharmony_ci "{\"any\":{\"optionalInt32\": 1, " . 1124ffe3c632Sopenharmony_ci "\"@type\":\"type.googleapis.com/foo.TestMessage\", " . 1125ffe3c632Sopenharmony_ci "\"optionalInt64\": 2}}"); 1126ffe3c632Sopenharmony_ci $this->assertSame("type.googleapis.com/foo.TestMessage", 1127ffe3c632Sopenharmony_ci $m3->getAny()->getTypeUrl()); 1128ffe3c632Sopenharmony_ci $this->assertSame("08011002", bin2hex($m3->getAny()->getValue())); 1129ffe3c632Sopenharmony_ci } 1130ffe3c632Sopenharmony_ci 1131ffe3c632Sopenharmony_ci public function testDecodeAnyWithWellKnownPacked() 1132ffe3c632Sopenharmony_ci { 1133ffe3c632Sopenharmony_ci // Make sure packed message has been created at least once. 1134ffe3c632Sopenharmony_ci $packed = new Int32Value(); 1135ffe3c632Sopenharmony_ci 1136ffe3c632Sopenharmony_ci $m1 = new TestAny(); 1137ffe3c632Sopenharmony_ci $m1->mergeFromJsonString( 1138ffe3c632Sopenharmony_ci "{\"any\":" . 1139ffe3c632Sopenharmony_ci " {\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . 1140ffe3c632Sopenharmony_ci " \"value\":1}}"); 1141ffe3c632Sopenharmony_ci $this->assertSame("type.googleapis.com/google.protobuf.Int32Value", 1142ffe3c632Sopenharmony_ci $m1->getAny()->getTypeUrl()); 1143ffe3c632Sopenharmony_ci $this->assertSame("0801", bin2hex($m1->getAny()->getValue())); 1144ffe3c632Sopenharmony_ci } 1145ffe3c632Sopenharmony_ci 1146ffe3c632Sopenharmony_ci /** 1147ffe3c632Sopenharmony_ci * @expectedException Exception 1148ffe3c632Sopenharmony_ci */ 1149ffe3c632Sopenharmony_ci public function testDecodeAnyWithUnknownPacked() 1150ffe3c632Sopenharmony_ci { 1151ffe3c632Sopenharmony_ci $m = new TestAny(); 1152ffe3c632Sopenharmony_ci $m->mergeFromJsonString( 1153ffe3c632Sopenharmony_ci "{\"any\":" . 1154ffe3c632Sopenharmony_ci " {\"@type\":\"type.googleapis.com/unknown\"," . 1155ffe3c632Sopenharmony_ci " \"value\":1}}"); 1156ffe3c632Sopenharmony_ci } 1157ffe3c632Sopenharmony_ci 1158ffe3c632Sopenharmony_ci public function testEncodeTopLevelAny() 1159ffe3c632Sopenharmony_ci { 1160ffe3c632Sopenharmony_ci // Test a normal message. 1161ffe3c632Sopenharmony_ci $packed = new TestMessage(); 1162ffe3c632Sopenharmony_ci $packed->setOptionalInt32(123); 1163ffe3c632Sopenharmony_ci $packed->setOptionalString("abc"); 1164ffe3c632Sopenharmony_ci 1165ffe3c632Sopenharmony_ci $m = new Any(); 1166ffe3c632Sopenharmony_ci $m->pack($packed); 1167ffe3c632Sopenharmony_ci $expected1 = 1168ffe3c632Sopenharmony_ci "{\"@type\":\"type.googleapis.com/foo.TestMessage\"," . 1169ffe3c632Sopenharmony_ci "\"optional_int32\":123,\"optional_string\":\"abc\"}"; 1170ffe3c632Sopenharmony_ci $expected2 = 1171ffe3c632Sopenharmony_ci "{\"@type\":\"type.googleapis.com/foo.TestMessage\"," . 1172ffe3c632Sopenharmony_ci "\"optionalInt32\":123,\"optionalString\":\"abc\"}"; 1173ffe3c632Sopenharmony_ci $result = $m->serializeToJsonString(); 1174ffe3c632Sopenharmony_ci $this->assertTrue($expected1 === $result || $expected2 === $result); 1175ffe3c632Sopenharmony_ci 1176ffe3c632Sopenharmony_ci // Test a well known message. 1177ffe3c632Sopenharmony_ci $packed = new Int32Value(); 1178ffe3c632Sopenharmony_ci $packed->setValue(123); 1179ffe3c632Sopenharmony_ci 1180ffe3c632Sopenharmony_ci $m = new Any(); 1181ffe3c632Sopenharmony_ci $m->pack($packed); 1182ffe3c632Sopenharmony_ci $this->assertSame( 1183ffe3c632Sopenharmony_ci "{\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . 1184ffe3c632Sopenharmony_ci "\"value\":123}", 1185ffe3c632Sopenharmony_ci $m->serializeToJsonString()); 1186ffe3c632Sopenharmony_ci 1187ffe3c632Sopenharmony_ci // Test an Any message. 1188ffe3c632Sopenharmony_ci $outer = new Any(); 1189ffe3c632Sopenharmony_ci $outer->pack($m); 1190ffe3c632Sopenharmony_ci $this->assertSame( 1191ffe3c632Sopenharmony_ci "{\"@type\":\"type.googleapis.com/google.protobuf.Any\"," . 1192ffe3c632Sopenharmony_ci "\"value\":{\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . 1193ffe3c632Sopenharmony_ci "\"value\":123}}", 1194ffe3c632Sopenharmony_ci $outer->serializeToJsonString()); 1195ffe3c632Sopenharmony_ci 1196ffe3c632Sopenharmony_ci // Test a Timestamp message. 1197ffe3c632Sopenharmony_ci $packed = new Google\Protobuf\Timestamp(); 1198ffe3c632Sopenharmony_ci $packed->setSeconds(946684800); 1199ffe3c632Sopenharmony_ci $packed->setNanos(123456789); 1200ffe3c632Sopenharmony_ci $m = new Any(); 1201ffe3c632Sopenharmony_ci $m->pack($packed); 1202ffe3c632Sopenharmony_ci $this->assertSame( 1203ffe3c632Sopenharmony_ci "{\"@type\":\"type.googleapis.com/google.protobuf.Timestamp\"," . 1204ffe3c632Sopenharmony_ci "\"value\":\"2000-01-01T00:00:00.123456789Z\"}", 1205ffe3c632Sopenharmony_ci $m->serializeToJsonString()); 1206ffe3c632Sopenharmony_ci } 1207ffe3c632Sopenharmony_ci 1208ffe3c632Sopenharmony_ci public function testEncodeAnyWithDefaultWrapperMessagePacked() 1209ffe3c632Sopenharmony_ci { 1210ffe3c632Sopenharmony_ci $any = new Any(); 1211ffe3c632Sopenharmony_ci $any->pack(new TestInt32Value([ 1212ffe3c632Sopenharmony_ci 'field' => new Int32Value(['value' => 0]), 1213ffe3c632Sopenharmony_ci ])); 1214ffe3c632Sopenharmony_ci $this->assertSame( 1215ffe3c632Sopenharmony_ci "{\"@type\":\"type.googleapis.com/foo.TestInt32Value\"," . 1216ffe3c632Sopenharmony_ci "\"field\":0}", 1217ffe3c632Sopenharmony_ci $any->serializeToJsonString()); 1218ffe3c632Sopenharmony_ci } 1219ffe3c632Sopenharmony_ci 1220ffe3c632Sopenharmony_ci public function testDecodeTopLevelFieldMask() 1221ffe3c632Sopenharmony_ci { 1222ffe3c632Sopenharmony_ci $m = new TestMessage(); 1223ffe3c632Sopenharmony_ci $m->setMapStringString(['a'=>'abcdefg']); 1224ffe3c632Sopenharmony_ci $data1 = $m->serializeToJsonString(); 1225ffe3c632Sopenharmony_ci $n = new TestMessage(); 1226ffe3c632Sopenharmony_ci $n->mergeFromJsonString($data1); 1227ffe3c632Sopenharmony_ci $data2 = $n->serializeToJsonString(); 1228ffe3c632Sopenharmony_ci $this->assertSame($data1, $data2); 1229ffe3c632Sopenharmony_ci 1230ffe3c632Sopenharmony_ci $m = new FieldMask(); 1231ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"foo.barBaz,qux\""); 1232ffe3c632Sopenharmony_ci $this->assertSame("foo.bar_baz", $m->getPaths()[0]); 1233ffe3c632Sopenharmony_ci $this->assertSame("qux", $m->getPaths()[1]); 1234ffe3c632Sopenharmony_ci } 1235ffe3c632Sopenharmony_ci 1236ffe3c632Sopenharmony_ci public function testEncodeTopLevelFieldMask() 1237ffe3c632Sopenharmony_ci { 1238ffe3c632Sopenharmony_ci $m = new FieldMask(); 1239ffe3c632Sopenharmony_ci $m->setPaths(["foo.bar_baz", "qux"]); 1240ffe3c632Sopenharmony_ci $this->assertSame("\"foo.barBaz,qux\"", $m->serializeToJsonString()); 1241ffe3c632Sopenharmony_ci } 1242ffe3c632Sopenharmony_ci 1243ffe3c632Sopenharmony_ci public function testDecodeEmptyFieldMask() 1244ffe3c632Sopenharmony_ci { 1245ffe3c632Sopenharmony_ci $m = new FieldMask(); 1246ffe3c632Sopenharmony_ci $m->mergeFromJsonString("\"\""); 1247ffe3c632Sopenharmony_ci $this->assertEquals("", $m->serializeToString()); 1248ffe3c632Sopenharmony_ci } 1249ffe3c632Sopenharmony_ci 1250ffe3c632Sopenharmony_ci public function testJsonDecodeMapWithDefaultValueKey() 1251ffe3c632Sopenharmony_ci { 1252ffe3c632Sopenharmony_ci $m = new TestMessage(); 1253ffe3c632Sopenharmony_ci $m->getMapInt32Int32()[0] = 0; 1254ffe3c632Sopenharmony_ci $this->assertSame("{\"mapInt32Int32\":{\"0\":0}}", 1255ffe3c632Sopenharmony_ci $m->serializeToJsonString()); 1256ffe3c632Sopenharmony_ci 1257ffe3c632Sopenharmony_ci $m = new TestMessage(); 1258ffe3c632Sopenharmony_ci $m->getMapStringString()[""] = ""; 1259ffe3c632Sopenharmony_ci $this->assertSame("{\"mapStringString\":{\"\":\"\"}}", 1260ffe3c632Sopenharmony_ci $m->serializeToJsonString()); 1261ffe3c632Sopenharmony_ci } 1262ffe3c632Sopenharmony_ci 1263ffe3c632Sopenharmony_ci public function testJsonDecodeNumericStringMapKey() 1264ffe3c632Sopenharmony_ci { 1265ffe3c632Sopenharmony_ci $m = new TestMessage(); 1266ffe3c632Sopenharmony_ci $m->getMapStringString()["1"] = "1"; 1267ffe3c632Sopenharmony_ci $data = $m->serializeToJsonString(); 1268ffe3c632Sopenharmony_ci $this->assertSame("{\"mapStringString\":{\"1\":\"1\"}}", $data); 1269ffe3c632Sopenharmony_ci $n = new TestMessage(); 1270ffe3c632Sopenharmony_ci $n->mergeFromJsonString($data); 1271ffe3c632Sopenharmony_ci } 1272ffe3c632Sopenharmony_ci 1273ffe3c632Sopenharmony_ci public function testMessageMapNoValue() 1274ffe3c632Sopenharmony_ci { 1275ffe3c632Sopenharmony_ci $m = new TestMessage(); 1276ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin("CA0700")); 1277ffe3c632Sopenharmony_ci $m->serializeToString(); 1278ffe3c632Sopenharmony_ci $this->assertTrue(true); 1279ffe3c632Sopenharmony_ci } 1280ffe3c632Sopenharmony_ci 1281ffe3c632Sopenharmony_ci public function testAnyMapNoValue() 1282ffe3c632Sopenharmony_ci { 1283ffe3c632Sopenharmony_ci $m = new TestMessage(); 1284ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin("D20700")); 1285ffe3c632Sopenharmony_ci $m->serializeToString(); 1286ffe3c632Sopenharmony_ci $this->assertTrue(true); 1287ffe3c632Sopenharmony_ci } 1288ffe3c632Sopenharmony_ci 1289ffe3c632Sopenharmony_ci public function testListValueMapNoValue() 1290ffe3c632Sopenharmony_ci { 1291ffe3c632Sopenharmony_ci $m = new TestMessage(); 1292ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin("DA0700")); 1293ffe3c632Sopenharmony_ci $m->serializeToString(); 1294ffe3c632Sopenharmony_ci $this->assertTrue(true); 1295ffe3c632Sopenharmony_ci } 1296ffe3c632Sopenharmony_ci 1297ffe3c632Sopenharmony_ci public function testStructMapNoValue() 1298ffe3c632Sopenharmony_ci { 1299ffe3c632Sopenharmony_ci $m = new TestMessage(); 1300ffe3c632Sopenharmony_ci $m->mergeFromString(hex2bin("E20700")); 1301ffe3c632Sopenharmony_ci $m->serializeToString(); 1302ffe3c632Sopenharmony_ci $this->assertTrue(true); 1303ffe3c632Sopenharmony_ci } 1304ffe3c632Sopenharmony_ci 1305ffe3c632Sopenharmony_ci /** 1306ffe3c632Sopenharmony_ci * @dataProvider wrappersDataProvider 1307ffe3c632Sopenharmony_ci */ 1308ffe3c632Sopenharmony_ci public function testWrapperJsonDecodeAndGet( 1309ffe3c632Sopenharmony_ci $class, 1310ffe3c632Sopenharmony_ci $nonDefaultValue, 1311ffe3c632Sopenharmony_ci $nonDefaultValueData, 1312ffe3c632Sopenharmony_ci $defaultValue, 1313ffe3c632Sopenharmony_ci $defaultValueData 1314ffe3c632Sopenharmony_ci ) 1315ffe3c632Sopenharmony_ci { 1316ffe3c632Sopenharmony_ci // Singular with non-default 1317ffe3c632Sopenharmony_ci $m = new $class(); 1318ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"field\":" . $nonDefaultValueData . "}"); 1319ffe3c632Sopenharmony_ci $wrapper = $m->getField(); 1320ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $wrapper->getValue()); 1321ffe3c632Sopenharmony_ci 1322ffe3c632Sopenharmony_ci // Singular with default 1323ffe3c632Sopenharmony_ci $m = new $class(); 1324ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"field\":" . $defaultValueData . "}"); 1325ffe3c632Sopenharmony_ci $wrapper = $m->getField(); 1326ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $wrapper->getValue()); 1327ffe3c632Sopenharmony_ci 1328ffe3c632Sopenharmony_ci // Repeated with empty 1329ffe3c632Sopenharmony_ci $m = new $class(); 1330ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"repeated_field\":[]}"); 1331ffe3c632Sopenharmony_ci $repeatedWrapper = $m->getRepeatedField(); 1332ffe3c632Sopenharmony_ci $this->assertSame(0, count($repeatedWrapper)); 1333ffe3c632Sopenharmony_ci 1334ffe3c632Sopenharmony_ci // Repeated with non-default 1335ffe3c632Sopenharmony_ci $m = new $class(); 1336ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"repeated_field\":[" . $defaultValueData . "]}"); 1337ffe3c632Sopenharmony_ci $repeatedWrapper = $m->getRepeatedField(); 1338ffe3c632Sopenharmony_ci $this->assertSame(1, count($repeatedWrapper)); 1339ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $repeatedWrapper[0]->getValue()); 1340ffe3c632Sopenharmony_ci 1341ffe3c632Sopenharmony_ci // Repeated with default 1342ffe3c632Sopenharmony_ci $m = new $class(); 1343ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"repeated_field\":[" . $defaultValueData . "]}"); 1344ffe3c632Sopenharmony_ci $repeatedWrapper = $m->getRepeatedField(); 1345ffe3c632Sopenharmony_ci $this->assertSame(1, count($repeatedWrapper)); 1346ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $repeatedWrapper[0]->getValue()); 1347ffe3c632Sopenharmony_ci 1348ffe3c632Sopenharmony_ci // Oneof with non-default 1349ffe3c632Sopenharmony_ci $m = new $class(); 1350ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"oneof_field\":" . $nonDefaultValueData . "}"); 1351ffe3c632Sopenharmony_ci $wrapper = $m->getOneofField(); 1352ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $wrapper->getValue()); 1353ffe3c632Sopenharmony_ci $this->assertEquals("oneof_field", $m->getOneofFields()); 1354ffe3c632Sopenharmony_ci $this->assertEquals(0, $m->getInt32Field()); 1355ffe3c632Sopenharmony_ci 1356ffe3c632Sopenharmony_ci // Oneof with default 1357ffe3c632Sopenharmony_ci $m = new $class(); 1358ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"oneof_field\":" . $defaultValueData . "}"); 1359ffe3c632Sopenharmony_ci $wrapper = $m->getOneofField(); 1360ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $wrapper->getValue()); 1361ffe3c632Sopenharmony_ci $this->assertEquals("oneof_field", $m->getOneofFields()); 1362ffe3c632Sopenharmony_ci $this->assertEquals(0, $m->getInt32Field()); 1363ffe3c632Sopenharmony_ci } 1364ffe3c632Sopenharmony_ci 1365ffe3c632Sopenharmony_ci /** 1366ffe3c632Sopenharmony_ci * @dataProvider wrappersDataProvider 1367ffe3c632Sopenharmony_ci */ 1368ffe3c632Sopenharmony_ci public function testWrapperJsonDecodeAndGetUnwrapped( 1369ffe3c632Sopenharmony_ci $class, 1370ffe3c632Sopenharmony_ci $nonDefaultValue, 1371ffe3c632Sopenharmony_ci $nonDefaultValueData, 1372ffe3c632Sopenharmony_ci $defaultValue, 1373ffe3c632Sopenharmony_ci $defaultValueData 1374ffe3c632Sopenharmony_ci ) 1375ffe3c632Sopenharmony_ci { 1376ffe3c632Sopenharmony_ci // Singular with non-default 1377ffe3c632Sopenharmony_ci $m = new $class(); 1378ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"field\":" . $nonDefaultValueData . "}"); 1379ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $m->getFieldUnwrapped()); 1380ffe3c632Sopenharmony_ci 1381ffe3c632Sopenharmony_ci // Singular with default 1382ffe3c632Sopenharmony_ci $m = new $class(); 1383ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"field\":" . $defaultValueData . "}"); 1384ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $m->getFieldUnwrapped()); 1385ffe3c632Sopenharmony_ci 1386ffe3c632Sopenharmony_ci // Oneof with non-default 1387ffe3c632Sopenharmony_ci $m = new $class(); 1388ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"oneof_field\":" . $nonDefaultValueData . "}"); 1389ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $m->getOneofFieldUnwrapped()); 1390ffe3c632Sopenharmony_ci $this->assertEquals("oneof_field", $m->getOneofFields()); 1391ffe3c632Sopenharmony_ci $this->assertEquals(0, $m->getInt32Field()); 1392ffe3c632Sopenharmony_ci 1393ffe3c632Sopenharmony_ci // Oneof with default 1394ffe3c632Sopenharmony_ci $m = new $class(); 1395ffe3c632Sopenharmony_ci $m->mergeFromJsonString("{\"oneof_field\":" . $defaultValueData . "}"); 1396ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $m->getOneofFieldUnwrapped()); 1397ffe3c632Sopenharmony_ci $this->assertEquals("oneof_field", $m->getOneofFields()); 1398ffe3c632Sopenharmony_ci $this->assertEquals(0, $m->getInt32Field()); 1399ffe3c632Sopenharmony_ci } 1400ffe3c632Sopenharmony_ci 1401ffe3c632Sopenharmony_ci /** 1402ffe3c632Sopenharmony_ci * @dataProvider wrappersDataProvider 1403ffe3c632Sopenharmony_ci */ 1404ffe3c632Sopenharmony_ci public function testWrapperJsonDecodeEncode( 1405ffe3c632Sopenharmony_ci $class, 1406ffe3c632Sopenharmony_ci $nonDefaultValue, 1407ffe3c632Sopenharmony_ci $nonDefaultValueData, 1408ffe3c632Sopenharmony_ci $defaultValue, 1409ffe3c632Sopenharmony_ci $defaultValueData 1410ffe3c632Sopenharmony_ci ) 1411ffe3c632Sopenharmony_ci { 1412ffe3c632Sopenharmony_ci // Singular with non-default 1413ffe3c632Sopenharmony_ci $from = new $class(); 1414ffe3c632Sopenharmony_ci $to = new $class(); 1415ffe3c632Sopenharmony_ci $from->mergeFromJsonString("{\"field\":" . $nonDefaultValueData . "}"); 1416ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1417ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1418ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $to->getFieldUnwrapped()); 1419ffe3c632Sopenharmony_ci 1420ffe3c632Sopenharmony_ci // Singular with default 1421ffe3c632Sopenharmony_ci $from = new $class(); 1422ffe3c632Sopenharmony_ci $to = new $class(); 1423ffe3c632Sopenharmony_ci $from->mergeFromJsonString("{\"field\":" . $defaultValueData . "}"); 1424ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1425ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1426ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $to->getFieldUnwrapped()); 1427ffe3c632Sopenharmony_ci 1428ffe3c632Sopenharmony_ci // Oneof with non-default 1429ffe3c632Sopenharmony_ci $from = new $class(); 1430ffe3c632Sopenharmony_ci $to = new $class(); 1431ffe3c632Sopenharmony_ci $from->mergeFromJsonString("{\"oneof_field\":" . $nonDefaultValueData . "}"); 1432ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1433ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1434ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $to->getOneofFieldUnwrapped()); 1435ffe3c632Sopenharmony_ci 1436ffe3c632Sopenharmony_ci // Oneof with default 1437ffe3c632Sopenharmony_ci $from = new $class(); 1438ffe3c632Sopenharmony_ci $to = new $class(); 1439ffe3c632Sopenharmony_ci $from->mergeFromJsonString("{\"oneof_field\":" . $defaultValueData . "}"); 1440ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1441ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1442ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $to->getOneofFieldUnwrapped()); 1443ffe3c632Sopenharmony_ci } 1444ffe3c632Sopenharmony_ci 1445ffe3c632Sopenharmony_ci /** 1446ffe3c632Sopenharmony_ci * @dataProvider wrappersDataProvider 1447ffe3c632Sopenharmony_ci */ 1448ffe3c632Sopenharmony_ci public function testWrapperSetUnwrappedJsonEncode( 1449ffe3c632Sopenharmony_ci $class, 1450ffe3c632Sopenharmony_ci $nonDefaultValue, 1451ffe3c632Sopenharmony_ci $nonDefaultValueData, 1452ffe3c632Sopenharmony_ci $defaultValue, 1453ffe3c632Sopenharmony_ci $defaultValueData 1454ffe3c632Sopenharmony_ci ) 1455ffe3c632Sopenharmony_ci { 1456ffe3c632Sopenharmony_ci // Singular with non-default 1457ffe3c632Sopenharmony_ci $from = new $class(); 1458ffe3c632Sopenharmony_ci $to = new $class(); 1459ffe3c632Sopenharmony_ci $from->setFieldUnwrapped($nonDefaultValue); 1460ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1461ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1462ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $to->getFieldUnwrapped()); 1463ffe3c632Sopenharmony_ci 1464ffe3c632Sopenharmony_ci // Singular with default 1465ffe3c632Sopenharmony_ci $from = new $class(); 1466ffe3c632Sopenharmony_ci $to = new $class(); 1467ffe3c632Sopenharmony_ci $from->setFieldUnwrapped($defaultValue); 1468ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1469ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1470ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $to->getFieldUnwrapped()); 1471ffe3c632Sopenharmony_ci 1472ffe3c632Sopenharmony_ci // Oneof with non-default 1473ffe3c632Sopenharmony_ci $from = new $class(); 1474ffe3c632Sopenharmony_ci $to = new $class(); 1475ffe3c632Sopenharmony_ci $from->setOneofFieldUnwrapped($nonDefaultValue); 1476ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1477ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1478ffe3c632Sopenharmony_ci $this->assertEquals($nonDefaultValue, $to->getOneofFieldUnwrapped()); 1479ffe3c632Sopenharmony_ci 1480ffe3c632Sopenharmony_ci // Oneof with default 1481ffe3c632Sopenharmony_ci $from = new $class(); 1482ffe3c632Sopenharmony_ci $to = new $class(); 1483ffe3c632Sopenharmony_ci $from->setOneofFieldUnwrapped($defaultValue); 1484ffe3c632Sopenharmony_ci $data = $from->serializeToJsonString(); 1485ffe3c632Sopenharmony_ci $to->mergeFromJsonString($data); 1486ffe3c632Sopenharmony_ci $this->assertEquals($defaultValue, $to->getOneofFieldUnwrapped()); 1487ffe3c632Sopenharmony_ci } 1488ffe3c632Sopenharmony_ci 1489ffe3c632Sopenharmony_ci public function wrappersDataProvider() 1490ffe3c632Sopenharmony_ci { 1491ffe3c632Sopenharmony_ci return [ 1492ffe3c632Sopenharmony_ci [TestInt32Value::class, 1, "1", 0, "0"], 1493ffe3c632Sopenharmony_ci [TestStringValue::class, "a", "\"a\"", "", "\"\""], 1494ffe3c632Sopenharmony_ci ]; 1495ffe3c632Sopenharmony_ci } 1496ffe3c632Sopenharmony_ci} 1497