From c0da95f75ea418b39b02ff4528ca9926cc246a8c Mon Sep 17 00:00:00 2001 From: FabioBatSilva Date: Wed, 21 Sep 2016 19:47:34 -0400 Subject: [PATCH] #12 - Rewind stream before writing --- src/Binary/StreamWriter.php | 1 + tests/SerializeMessageTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Binary/StreamWriter.php b/src/Binary/StreamWriter.php index 6170768..235af8d 100644 --- a/src/Binary/StreamWriter.php +++ b/src/Binary/StreamWriter.php @@ -278,6 +278,7 @@ public function writeByteStream(Stream $stream, Stream $value) { $length = $value->getSize(); + $value->seek(0); $this->writeVarint($stream, $length); $stream->writeStream($value, $length); } diff --git a/tests/SerializeMessageTest.php b/tests/SerializeMessageTest.php index 0d93813..410e527 100644 --- a/tests/SerializeMessageTest.php +++ b/tests/SerializeMessageTest.php @@ -56,6 +56,34 @@ public function testWriteSimpleMessage() $this->assertSerializedMessageSize($expected, $simple); } + public function testWriteSimpleMessageTwice() + { + $simple = new Simple(); + + $simple->setBool(true); + $simple->setBytes("bar"); + $simple->setString("foo"); + $simple->setFloat(12345.123); + $simple->setUint32(123456789); + $simple->setInt32(-123456789); + $simple->setFixed32(123456789); + $simple->setSint32(-123456789); + $simple->setSfixed32(-123456789); + $simple->setDouble(123456789.12345); + $simple->setInt64(-123456789123456789); + $simple->setUint64(123456789123456789); + $simple->setFixed64(123456789123456789); + $simple->setSint64(-123456789123456789); + $simple->setSfixed64(-123456789123456789); + + $expected = $this->getProtoContent('simple.bin'); + $actual1 = $simple->toStream(); + $actual2 = $simple->toStream(); + + $this->assertEquals($expected, (string) $actual1); + $this->assertEquals($expected, (string) $actual2); + } + public function testWriteRepeatedString() { $repeated = new Repeated();