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