public function testAddStream() { $this->assertCount(0, $this->collection); $stream1 = Stream::create(); $stream2 = Stream::create(); $this->collection[] = $stream1; $this->collection->add($stream2); $this->assertCount(2, $this->collection); $this->assertEquals([$stream1, $stream2], $this->collection->getArrayCopy()); }
public function testWriteStream() { $source = Stream::create(); $target = Stream::create(); $writer = new StreamWriter($this->config); $writer->writeVarint($source, WireFormat::getFieldKey(1, WireFormat::WIRE_FIXED64)); $writer->writeDouble($source, 123456789.12345); $source->seek(0); $writer->writeStream($target, $source); $this->assertEquals((string) $source, (string) $target); }
public function testUnserializeMessage() { $class = FooStub_MessageSerializerTest::CLASS; $config = new Configuration(); $serializer = new MessageSerializer($config); $stream = Stream::create(); FooStub_MessageSerializerTest::$calls = []; $this->assertInstanceOf($class, $serializer->unserialize($class, $stream)); $this->assertCount(1, FooStub_MessageSerializerTest::$calls); $this->assertSame($stream, FooStub_MessageSerializerTest::$calls[0][0]); $this->assertSame($config, FooStub_MessageSerializerTest::$calls[0][1]); }
/** * @param \Protobuf\Message $message * @param integer $level * * @return \Protobuf\Stream */ public function encodeMessage(Message $message, $level = 0) { $reflect = new ReflectionClass($message); $properties = $reflect->getProperties(ReflectionProperty::IS_PROTECTED); $indent = str_repeat(' ', $level); $stream = Stream::create(); foreach ($properties as $property) { $property->setAccessible(true); $name = $property->getName(); $value = $property->getValue($message); if ($value === null) { continue; } if (!is_array($value) && !$value instanceof Traversable) { if (!$value instanceof Message) { $item = $this->encodeValue($value); $buffer = $indent . $name . ': ' . $item . PHP_EOL; $stream->write($buffer, strlen($buffer)); continue; } $innerStream = $this->encodeMessage($value, $level + 1); $beginMessage = $indent . $name . ' {' . PHP_EOL; $endMessage = $indent . '}' . PHP_EOL; $stream->write($beginMessage, strlen($beginMessage)); $stream->writeStream($innerStream, $innerStream->getSize()); $stream->write($endMessage, strlen($endMessage)); continue; } foreach ($value as $val) { // Skip nullified repeated values if ($val == null) { continue; } if (!$val instanceof Message) { $item = $this->encodeValue($val); $buffer = $indent . $name . ': ' . $item . PHP_EOL; $stream->write($buffer, strlen($buffer)); continue; } $innerStream = $this->encodeMessage($val, $level + 1); $beginMessage = $indent . $name . ' {' . PHP_EOL; $endMessage = $indent . '}' . PHP_EOL; $stream->write($beginMessage, strlen($beginMessage)); $stream->writeStream($innerStream, $innerStream->getSize()); $stream->write($endMessage, strlen($endMessage)); } } $stream->seek(0); return $stream; }
/** * @return \Protobuf\Stream */ protected function getStdinStream() { $handle = fopen('php://stdin', 'r'); $stream = Stream::create(); $counter = 0; stream_set_blocking($handle, false); while (!feof($handle) && $counter++ < 10) { $buffer = fread($handle, 1024); $length = mb_strlen($buffer, '8bit'); if ($length > 0) { $stream->write($buffer, $length); $counter = 0; continue; } usleep(1000); } $stream->seek(0); fclose($handle); return $stream; }
/** * @expectedException RuntimeException * @expectedExceptionMessage Unsupported wire type '-1' while reading unknown field. */ public function testReadUnknownWireFormatException() { $stream = Stream::create($this->getProtoContent('simple.bin')); $reader = new StreamReader($this->config); $reader->readUnknown($stream, -1); }
/** * @dataProvider providerByteStream */ public function testComputeByteStreamSize($value) { $stream = Stream::create(); $this->writer->writeByteStream($stream, $value); $streamSize = $stream->getSize(); $actualSize = $this->calculator->computeByteStreamSize($value); $this->assertEquals($streamSize, $actualSize); }
/** * Create a write context. * * @return \Protobuf\WriteContext */ public function createWriteContext() { $stream = Stream::create(); $writer = $this->getStreamWriter(); $sizeContext = $this->createComputeSizeContext(); $context = new WriteContext($stream, $writer, $sizeContext); return $context; }
/** * @expectedException \RuntimeException * @expectedExceptionMessage Failed to write stream with 3 bytes */ public function testWriteStreamException() { $source = Stream::create(); $target = Stream::create(); $target->writeStream($source, 3); }