Автор: Fabio B. Silva (fabio.bat.silva@gmail.com)
 /**
  * Set 'encrypted_signature' value
  *
  * @param \Protobuf\Stream $value
  */
 public function setEncryptedSignature($value = null)
 {
     if ($value !== null && !$value instanceof \Protobuf\Stream) {
         $value = \Protobuf\Stream::wrap($value);
     }
     $this->encrypted_signature = $value;
 }
 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());
 }
Пример #3
0
 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]);
 }
Пример #5
0
 public function testSimpleMessageMergeNullComparison()
 {
     $simple1 = new Simple();
     $simple2 = new Simple();
     $bytes = Stream::wrap("bar");
     $simple1->setBool(false);
     $simple1->setFloat(0.0);
     $simple1->setUint32(0);
     $simple2->merge($simple1);
     $this->assertSame(false, $simple2->getBool());
     $this->assertSame(0.0, $simple2->getFloat());
     $this->assertSame(0, $simple2->getUint32());
 }
Пример #6
0
 /**
  * @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;
 }
Пример #7
0
 public function testSimpleMessageClear()
 {
     $simple = new Simple();
     $simple->setBool(true);
     $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->setBytes(Stream::wrap("bar"));
     $simple->setSfixed64(-123456789123456789);
     $this->assertSame(true, $simple->getBool());
     $this->assertSame("foo", $simple->getString());
     $this->assertSame(12345.123, $simple->getFloat());
     $this->assertSame(123456789, $simple->getUint32());
     $this->assertSame(-123456789, $simple->getInt32());
     $this->assertSame(123456789, $simple->getFixed32());
     $this->assertSame(-123456789, $simple->getSint32());
     $this->assertSame(-123456789, $simple->getSfixed32());
     $this->assertSame(123456789.12345, $simple->getDouble());
     $this->assertSame(-123456789123456789, $simple->getInt64());
     $this->assertSame(123456789123456789, $simple->getUint64());
     $this->assertSame(123456789123456789, $simple->getFixed64());
     $this->assertSame(-123456789123456789, $simple->getSint64());
     $this->assertSame(-123456789123456789, $simple->getSfixed64());
     $this->assertInstanceOf('Protobuf\\Stream', $simple->getBytes());
     $simple->clear();
     $this->assertNull($simple->getBool());
     $this->assertNull($simple->getString());
     $this->assertNull($simple->getFloat());
     $this->assertNull($simple->getUint32());
     $this->assertNull($simple->getInt32());
     $this->assertNull($simple->getFixed32());
     $this->assertNull($simple->getSint32());
     $this->assertNull($simple->getSfixed32());
     $this->assertNull($simple->getDouble());
     $this->assertNull($simple->getInt64());
     $this->assertNull($simple->getUint64());
     $this->assertNull($simple->getFixed64());
     $this->assertNull($simple->getSint64());
     $this->assertNull($simple->getSfixed64());
     $this->assertNull($simple->getBytes());
 }
Пример #8
0
 public function testGenerateSimpleMessage()
 {
     $binaryRequest = $this->getFixtureFileContent('compiler/generator-request-simple.bin');
     $expectedContent = $this->getFixtureFileContent('Simple.tpl');
     $compiler = new Compiler($this->logger);
     $binaryResponse = $compiler->compile(Stream::wrap($binaryRequest));
     $response = CodeGeneratorResponse::fromStream($binaryResponse);
     $this->assertInstanceOf('google\\protobuf\\compiler\\CodeGeneratorResponse', $response);
     $this->assertInstanceOf('Protobuf\\Collection', $response->getFileList());
     $this->assertCount(1, $response->getFileList());
     $file = $response->getFileList()[0];
     $this->assertInstanceOf('google\\protobuf\\compiler\\CodeGeneratorResponse\\File', $file);
     $this->assertEquals('ProtobufCompilerTest/Protos/Simple.php', $file->getName());
     $this->assertEquals($expectedContent, $file->getContent());
 }
Пример #9
0
 /**
  * @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;
 }
Пример #10
0
 /**
  * Set 'session_hash' value
  *
  * @param \Protobuf\Stream $value
  */
 public function setSessionHash($value = null)
 {
     if ($value !== null && !$value instanceof \Protobuf\Stream) {
         $value = \Protobuf\Stream::wrap($value);
     }
     $this->session_hash = $value;
 }
Пример #11
0
 /**
  * Set 'request_message' value
  *
  * @param \Protobuf\Stream $value
  */
 public function setRequestMessage($value = null)
 {
     if ($value !== null && !$value instanceof \Protobuf\Stream) {
         $value = \Protobuf\Stream::wrap($value);
     }
     $this->request_message = $value;
 }
 public function testEncodeAndDecodeNestedMessageComparingWithProtoc()
 {
     $proto = 'complex';
     $complex = new Complex();
     $nested = new Complex\Nested();
     $input = 'nested { foo: "FOO" }';
     $class = 'ProtobufTest.Protos.Complex';
     $nested->setFoo('FOO');
     $complex->setNested($nested);
     $encoded = $complex->toStream();
     $expected = $this->executeProtoc($input, $class, $proto);
     $decoded = Complex::fromStream(Stream::wrap($expected));
     $this->assertInstanceOf(Complex::CLASS, $decoded);
     $this->assertInstanceOf(Complex\Nested::CLASS, $complex->getNested());
     $this->assertEquals(bin2hex($encoded), bin2hex($expected));
     $this->assertEquals($complex->getNested()->getFoo(), 'FOO');
 }
Пример #13
0
 /**
  * Set 'status' value
  *
  * @param \Protobuf\Stream $value
  */
 public function setStatus($value = null)
 {
     if ($value !== null && !$value instanceof \Protobuf\Stream) {
         $value = \Protobuf\Stream::wrap($value);
     }
     $this->status = $value;
 }
Пример #14
0
 /**
  * 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;
 }
Пример #15
0
 /**
  * Decode a 64bit double.
  *
  * @param \Protobuf\Stream $stream
  *
  * @return float
  */
 public function readDouble(Stream $stream)
 {
     $bytes = $stream->read(8);
     if ($this->isBigEndian) {
         $bytes = strrev($bytes);
     }
     list(, $result) = unpack('d', $bytes);
     return $result;
 }
Пример #16
0
 /**
  * @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);
 }
Пример #17
0
 /**
  * Add a new element to 'returns'
  *
  * @param \Protobuf\Stream $value
  */
 public function addReturns($value)
 {
     if ($this->returns === null) {
         $this->returns = new \Protobuf\StreamCollection();
     }
     $this->returns->add(\Protobuf\Stream::wrap($value));
 }
Пример #18
0
 /**
  * @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);
 }
Пример #19
0
 /**
  * Write the given stream.
  *
  * @param \Protobuf\Stream $stream
  * @param \Protobuf\Stream $value
  * @param int              $length
  */
 public function writeStream(Stream $stream, Stream $value, $length = null)
 {
     if ($length === null) {
         $length = $value->getSize();
     }
     $stream->writeStream($value, $length);
 }
Пример #20
0
 /**
  * Compute the number of bytes that would be needed to encode a stream of bytes.
  *
  * @param \Protobuf\Stream $value
  *
  * @return integer
  */
 public function computeByteStreamSize(Stream $value)
 {
     $length = $value->getSize();
     $size = $length + $this->computeVarintSize($length);
     return $size;
 }
Пример #21
0
 /**
  * Set 'response' value
  *
  * @param \Protobuf\Stream $value
  */
 public function setResponse($value = null)
 {
     if ($value !== null && !$value instanceof \Protobuf\Stream) {
         $value = \Protobuf\Stream::wrap($value);
     }
     $this->response = $value;
 }
Пример #22
0
 public function testUnknownFieldSet()
 {
     $binary = $this->getProtoContent('unknown.bin');
     $unrecognized = Unrecognized::fromStream(Stream::wrap($binary));
     $this->assertInstanceOf(Unrecognized::CLASS, $unrecognized);
     $this->assertInstanceOf('Protobuf\\UnknownFieldSet', $unrecognized->unknownFieldSet());
     $this->assertCount(15, $unrecognized->unknownFieldSet());
     $values = $unrecognized->unknownFieldSet();
     $this->assertInstanceOf('Protobuf\\Unknown', $values[1]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[2]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[3]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[4]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[5]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[6]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[7]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[8]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[9]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[12]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[13]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[15]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[16]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[17]);
     $this->assertInstanceOf('Protobuf\\Unknown', $values[18]);
     $this->assertEquals(4728057454355442093, $values[1]->value);
     $this->assertEquals(1178657918, $values[2]->value);
     $this->assertEquals(-123456789123456789, $values[3]->value);
     $this->assertEquals(123456789123456789, $values[4]->value);
     $this->assertEquals(-123456789, $values[5]->value);
     $this->assertEquals(123456789123456789, $values[6]->value);
     $this->assertEquals(123456789, $values[7]->value);
     $this->assertEquals(1, $values[8]->value);
     $this->assertEquals("foo", $values[9]->value);
     $this->assertEquals("bar", $values[12]->value);
     $this->assertEquals(123456789, $values[13]->value);
     $this->assertEquals(4171510507, $values[15]->value);
     $this->assertEquals(-123456789123456789, $values[16]->value);
     $this->assertEquals(246913577, $values[17]->value);
     $this->assertEquals(246913578246913577, $values[18]->value);
 }
Пример #23
0
 public function testCanSetSize()
 {
     $this->assertEquals(10, Stream::wrap('', 10)->getSize());
 }