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