Skip to content

Commit

Permalink
Added getPbString() method to retrieve the PB file contents directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamamused committed Dec 13, 2010
1 parent e159f8d commit 2e7f32a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
14 changes: 9 additions & 5 deletions UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,22 @@ public function testUsage() {

# Serialize the data to a .pb format.
$tmp = dirname(__FILE__) . '/addressbook.pb';
$outfp = fopen($tmp, 'wb');

$outfp = fopen($tmp, 'wb');
$book->write($outfp);
fclose($outfp);

$this->assertTrue(filesize($tmp) > 0);

// check that the string output is correct
$string = $book->getPbString();
$this->assertEquals($string,file_get_contents($tmp));

// Read it into a new addressbook
$infp = fopen($tmp, 'rb');
$newBook = new Unit_Test_AddressBook($infp);
fclose($infp);
fclose($infp);

unlink($tmp);
unlink($tmp);

# Re-run some tests on the new book to ensure things match.
$this->assertEquals(4,$newBook->getPersonCount());
Expand All @@ -176,7 +178,9 @@ public function testUsage() {
$this->assertEquals($book->getPerson(3)->getPhone(0)->getType(),$newBook->getPerson(3)->getPhone(0)->getType());
$this->assertEquals($book->getPerson(3)->getPhone(1)->getNumber(),$newBook->getPerson(3)->getPhone(1)->getNumber());
$this->assertEquals($book->getPerson(3)->getPhone(1)->getType(),$newBook->getPerson(3)->getPhone(1)->getType());




}


Expand Down
17 changes: 17 additions & 0 deletions protoc-gen-php.cc
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,23 @@ void PHPCodeGenerator::PrintMessageWrite(io::Printer &printer, const Descriptor

printer.Outdent();
printer.Print("}\n");

// Write
printer.Print(
"\n"
"function getPbString() {\n"
);
printer.Indent();

printer.Print("$fp = fopen('php://memory', 'w+b');\n");
printer.Print("$this->write($fp);\n");
printer.Print("rewind($fp);\n");
printer.Print("$out = stream_get_contents($fp);\n");
printer.Print("fclose($fp);\n");
printer.Print("return $out;\n");
printer.Outdent();
printer.Print("}\n");

}

void PHPCodeGenerator::PrintMessageSize(io::Printer &printer, const Descriptor & message) const {
Expand Down

0 comments on commit 2e7f32a

Please sign in to comment.