Skip to content

Commit

Permalink
Include tests for encoding/decoding extended fields
Browse files Browse the repository at this point in the history
  • Loading branch information
drslump committed Sep 19, 2012
1 parent 4be121d commit b875c82
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/Protobuf.Codec.Binary.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
include_once __DIR__ . '/protos/complex.php';
include_once __DIR__ . '/protos/repeated.php';
include_once __DIR__ . '/protos/addressbook.php';
include_once __DIR__ . '/protos/extension.php';

// Include some hamcrest matchers manually since they are not included by default
// TODO: Fix spec4php to include them
Expand All @@ -30,6 +31,7 @@
$W->bin_repeated_string = file_get_contents(__DIR__ . '/protos/repeated-string.bin');
$W->bin_repeated_int32 = file_get_contents(__DIR__ . '/protos/repeated-int32.bin');
$W->bin_repeated_nested = file_get_contents(__DIR__ . '/protos/repeated-nested.bin');
$W->bin_ext = file_get_contents(__DIR__ . '/protos/extension.bin');
end;
describe "serialize"
Expand Down Expand Up @@ -198,6 +200,14 @@
end.
it 'a message with extended fields'
$ext = new Tests\ExtA();
$ext->first = 'FIRST';
$ext['tests\ExtB\second'] = 'SECOND';
$bin = Protobuf::encode($ext);
$bin should eq $W->bin_ext but not be false;
end
end;
describe "unserialize"
Expand Down Expand Up @@ -234,6 +244,12 @@
$complex->getPerson(0)->getPhone(1)->number should eq '55512321312';
end.
it 'a message with extended fields'
$ext = Protobuf::decode('Tests\ExtA', $W->bin_ext);
$ext->first should eq 'FIRST';
$ext['tests\ExtB\second'] should eq 'SECOND';
end
end;
describe "multi codec"
Expand Down
2 changes: 2 additions & 0 deletions tests/protos/extension.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

FIRSTSECOND
106 changes: 106 additions & 0 deletions tests/protos/extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin @package_version@
// Source: extension.proto
// Date: 2012-09-19 15:03:23

namespace tests {

class ExtA extends \DrSlump\Protobuf\Message {

/** @var string */
public $first = null;


/** @var \Closure[] */
protected static $__extensions = array();

public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'tests.ExtA');

// OPTIONAL STRING first = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "first";
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$descriptor->addField($f);

foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}

return $descriptor;
}

/**
* Check if <first> has a value
*
* @return boolean
*/
public function hasFirst(){
return $this->_has(1);
}

/**
* Clear <first> value
*
* @return \tests\ExtA
*/
public function clearFirst(){
return $this->_clear(1);
}

/**
* Get <first> value
*
* @return string
*/
public function getFirst(){
return $this->_get(1);
}

/**
* Set <first> value
*
* @param string $value
* @return \tests\ExtA
*/
public function setFirst( $value){
return $this->_set(1, $value);
}
}
}

namespace tests {

class ExtB extends \DrSlump\Protobuf\Message {


/** @var \Closure[] */
protected static $__extensions = array();

public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'tests.ExtB');

foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}

return $descriptor;
}
}
}

namespace {
\tests\ExtA::extension(function(){
// OPTIONAL STRING tests\ExtB\second = 2
$f = new \DrSlump\Protobuf\Field();
$f->number = 2;
$f->name = "tests\ExtB\second";
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
return $f;
});
}
12 changes: 12 additions & 0 deletions tests/protos/extension.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tests;

message ExtA {
optional string first = 1;
extensions 2 to 3;
}

message ExtB {
extend ExtA {
optional string second = 2;
}
}

0 comments on commit b875c82

Please sign in to comment.