diff options
Diffstat (limited to 'php/tests/encode_decode_test.php')
-rw-r--r-- | php/tests/encode_decode_test.php | 81 |
1 files changed, 58 insertions, 23 deletions
diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index 3218aa63..94adf793 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -21,7 +21,7 @@ class EncodeDecodeTest extends TestBase $this->setFields($from); $this->expectFields($from); - $data = $from->encode(); + $data = $from->serializeToString(); $this->assertSame(bin2hex(TestUtil::getGoldenTestMessage()), bin2hex($data)); } @@ -29,7 +29,7 @@ class EncodeDecodeTest extends TestBase public function testDecode() { $to = new TestMessage(); - $to->decode(TestUtil::getGoldenTestMessage()); + $to->mergeFromString(TestUtil::getGoldenTestMessage()); $this->expectFields($to); } @@ -40,10 +40,10 @@ class EncodeDecodeTest extends TestBase $this->setFields($from); $this->expectFields($from); - $data = $from->encode(); + $data = $from->serializeToString(); $to = new TestMessage(); - $to->decode($data); + $to->mergeFromString($data); $this->expectFields($to); } @@ -52,10 +52,10 @@ class EncodeDecodeTest extends TestBase $from = new TestMessage(); $this->expectEmptyFields($from); - $data = $from->encode(); + $data = $from->serializeToString(); $to = new TestMessage(); - $to->decode($data); + $to->mergeFromString($data); $this->expectEmptyFields($to); } @@ -64,29 +64,29 @@ class EncodeDecodeTest extends TestBase $m = new TestMessage(); $m->setOneofInt32(1); - $data = $m->encode(); + $data = $m->serializeToString(); $n = new TestMessage(); - $n->decode($data); + $n->mergeFromString($data); $this->assertSame(1, $n->getOneofInt32()); $m->setOneofFloat(2.0); - $data = $m->encode(); + $data = $m->serializeToString(); $n = new TestMessage(); - $n->decode($data); + $n->mergeFromString($data); $this->assertSame(2.0, $n->getOneofFloat()); $m->setOneofString('abc'); - $data = $m->encode(); + $data = $m->serializeToString(); $n = new TestMessage(); - $n->decode($data); + $n->mergeFromString($data); $this->assertSame('abc', $n->getOneofString()); $sub_m = new TestMessage_Sub(); $sub_m->setA(1); $m->setOneofMessage($sub_m); - $data = $m->encode(); + $data = $m->serializeToString(); $n = new TestMessage(); - $n->decode($data); + $n->mergeFromString($data); $this->assertSame(1, $n->getOneofMessage()->getA()); } @@ -95,20 +95,20 @@ class EncodeDecodeTest extends TestBase $from = new TestPackedMessage(); TestUtil::setTestPackedMessage($from); $this->assertSame(TestUtil::getGoldenTestPackedMessage(), - $from->encode()); + $from->serializeToString()); } public function testPackedDecodePacked() { $to = new TestPackedMessage(); - $to->decode(TestUtil::getGoldenTestPackedMessage()); + $to->mergeFromString(TestUtil::getGoldenTestPackedMessage()); TestUtil::assertTestPackedMessage($to); } public function testPackedDecodeUnpacked() { $to = new TestPackedMessage(); - $to->decode(TestUtil::getGoldenTestUnpackedMessage()); + $to->mergeFromString(TestUtil::getGoldenTestUnpackedMessage()); TestUtil::assertTestPackedMessage($to); } @@ -117,20 +117,20 @@ class EncodeDecodeTest extends TestBase $from = new TestUnpackedMessage(); TestUtil::setTestPackedMessage($from); $this->assertSame(TestUtil::getGoldenTestUnpackedMessage(), - $from->encode()); + $from->serializeToString()); } public function testUnpackedDecodePacked() { $to = new TestUnpackedMessage(); - $to->decode(TestUtil::getGoldenTestPackedMessage()); + $to->mergeFromString(TestUtil::getGoldenTestPackedMessage()); TestUtil::assertTestPackedMessage($to); } public function testUnpackedDecodeUnpacked() { $to = new TestUnpackedMessage(); - $to->decode(TestUtil::getGoldenTestUnpackedMessage()); + $to->mergeFromString(TestUtil::getGoldenTestUnpackedMessage()); TestUtil::assertTestPackedMessage($to); } @@ -161,19 +161,54 @@ class EncodeDecodeTest extends TestBase $msg = new TestMessage(); foreach ($testVals as $original => $encoded) { $msg->setOptionalInt64($original); - $data = $msg->encode(); + $data = $msg->serializeToString(); $this->assertSame($encoded, bin2hex($data)); $msg->setOptionalInt64(0); - $msg->decode($data); + $msg->mergeFromString($data); $this->assertEquals($original, $msg->getOptionalInt64()); } } + public function testDecodeToExistingMessage() + { + $m1 = new TestMessage(); + $this->setFields($m1); + $this->expectFields($m1); + + $m2 = new TestMessage(); + $this->setFields2($m2); + $data = $m2->serializeToString(); + + $m1->mergeFromString($data); + $this->expectFieldsMerged($m1); + } + public function testDecodeFieldNonExist() { $data = hex2bin('c80501'); $m = new TestMessage(); - $m->decode($data); + $m->mergeFromString($data); + } + + public function testEncodeNegativeInt32() + { + $m = new TestMessage(); + $m->setOptionalInt32(-1); + $data = $m->serializeToString(); + $this->assertSame("08ffffffffffffffffff01", bin2hex($data)); + } + + public function testDecodeNegativeInt32() + { + $m = new TestMessage(); + $this->assertEquals(0, $m->getOptionalInt32()); + $m->mergeFromString(hex2bin("08ffffffffffffffffff01")); + $this->assertEquals(-1, $m->getOptionalInt32()); + + $m = new TestMessage(); + $this->assertEquals(0, $m->getOptionalInt32()); + $m->mergeFromString(hex2bin("08ffffffff0f")); + $this->assertEquals(-1, $m->getOptionalInt32()); } # TODO(teboring): Add test back when php implementation is ready for json |