/** * @dataProvider simpleMessageProvider */ public function testDecodeSimpleMessageComparingTypesWithProtoc($field, $value) { $escaped = $value; $proto = 'simple'; $getter = 'get' . ucfirst($field); $class = 'ProtobufTest.Protos.Simple'; if (is_string($value)) { $escaped = '"' . $value . '"'; } if ($value instanceof \Protobuf\Stream) { $tell = $value->tell(); $escaped = '"' . $value . '"'; $value->seek($tell); } $binary = $this->executeProtoc("{$field}: {$escaped}", $class, $proto); $message = Simple::fromStream(Stream::wrap($binary)); $result = $message->{$getter}(); // Hack the comparison for float precision if (is_float($value)) { $precision = strlen($value) - strpos($value, '.'); $result = round($result, $precision); } if ($result instanceof \Protobuf\Stream) { $result = (string) $result; } $this->assertEquals($value, $result, "Decoding {$field} with value {$value}"); }
public function testReadSimpleMessage() { $binary = $this->getProtoContent('simple.bin'); $simple = Simple::fromStream($binary); $this->assertInstanceOf(Simple::CLASS, $simple); $this->assertInstanceOf(Stream::CLASS, $simple->getBytes()); $this->assertInternalType('bool', $simple->getBool()); $this->assertInternalType('string', $simple->getString()); $this->assertInternalType('float', $simple->getFloat(), '', 0.0001); $this->assertInternalType('integer', $simple->getUint32()); $this->assertInternalType('integer', $simple->getInt32()); $this->assertInternalType('integer', $simple->getFixed32()); $this->assertInternalType('integer', $simple->getSint32()); $this->assertInternalType('integer', $simple->getSfixed32()); $this->assertInternalType('float', $simple->getDouble()); $this->assertInternalType('integer', $simple->getInt64()); $this->assertInternalType('integer', $simple->getUint64()); $this->assertInternalType('integer', $simple->getFixed64()); $this->assertInternalType('integer', $simple->getSint64()); $this->assertInternalType('integer', $simple->getSfixed64()); $this->assertEquals(true, $simple->getBool()); $this->assertEquals("bar", $simple->getBytes()); $this->assertEquals("foo", $simple->getString()); $this->assertEquals(12345.123, $simple->getFloat(), '', 0.0001); $this->assertEquals(123456789, $simple->getUint32()); $this->assertEquals(-123456789, $simple->getInt32()); $this->assertEquals(123456789, $simple->getFixed32()); $this->assertEquals(-123456789, $simple->getSint32()); $this->assertEquals(-123456789, $simple->getSfixed32()); $this->assertEquals(123456789.12345, $simple->getDouble()); $this->assertEquals(-123456789123456789, $simple->getInt64()); $this->assertEquals(123456789123456789, $simple->getUint64()); $this->assertEquals(123456789123456789, $simple->getFixed64()); $this->assertEquals(-123456789123456789, $simple->getSint64()); $this->assertEquals(-123456789123456789, $simple->getSfixed64()); }
public function testFormatSimple() { $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.txt'); $actual = $this->textFormat->encodeMessage($simple); $this->assertEquals($expected, (string) $actual); }