Skip to content

Commit b875c82

Browse files
committed
Include tests for encoding/decoding extended fields
1 parent 4be121d commit b875c82

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

tests/Protobuf.Codec.Binary.spec.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
include_once __DIR__ . '/protos/complex.php';
1313
include_once __DIR__ . '/protos/repeated.php';
1414
include_once __DIR__ . '/protos/addressbook.php';
15+
include_once __DIR__ . '/protos/extension.php';
1516

1617
// Include some hamcrest matchers manually since they are not included by default
1718
// TODO: Fix spec4php to include them
@@ -30,6 +31,7 @@
3031
$W->bin_repeated_string = file_get_contents(__DIR__ . '/protos/repeated-string.bin');
3132
$W->bin_repeated_int32 = file_get_contents(__DIR__ . '/protos/repeated-int32.bin');
3233
$W->bin_repeated_nested = file_get_contents(__DIR__ . '/protos/repeated-nested.bin');
34+
$W->bin_ext = file_get_contents(__DIR__ . '/protos/extension.bin');
3335
end;
3436
3537
describe "serialize"
@@ -198,6 +200,14 @@
198200
199201
end.
200202
203+
it 'a message with extended fields'
204+
$ext = new Tests\ExtA();
205+
$ext->first = 'FIRST';
206+
$ext['tests\ExtB\second'] = 'SECOND';
207+
$bin = Protobuf::encode($ext);
208+
$bin should eq $W->bin_ext but not be false;
209+
end
210+
201211
end;
202212
203213
describe "unserialize"
@@ -234,6 +244,12 @@
234244
$complex->getPerson(0)->getPhone(1)->number should eq '55512321312';
235245
end.
236246
247+
it 'a message with extended fields'
248+
$ext = Protobuf::decode('Tests\ExtA', $W->bin_ext);
249+
$ext->first should eq 'FIRST';
250+
$ext['tests\ExtB\second'] should eq 'SECOND';
251+
end
252+
237253
end;
238254
239255
describe "multi codec"

tests/protos/extension.bin

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
FIRSTSECOND

tests/protos/extension.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin @package_version@
3+
// Source: extension.proto
4+
// Date: 2012-09-19 15:03:23
5+
6+
namespace tests {
7+
8+
class ExtA extends \DrSlump\Protobuf\Message {
9+
10+
/** @var string */
11+
public $first = null;
12+
13+
14+
/** @var \Closure[] */
15+
protected static $__extensions = array();
16+
17+
public static function descriptor()
18+
{
19+
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'tests.ExtA');
20+
21+
// OPTIONAL STRING first = 1
22+
$f = new \DrSlump\Protobuf\Field();
23+
$f->number = 1;
24+
$f->name = "first";
25+
$f->type = \DrSlump\Protobuf::TYPE_STRING;
26+
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
27+
$descriptor->addField($f);
28+
29+
foreach (self::$__extensions as $cb) {
30+
$descriptor->addField($cb(), true);
31+
}
32+
33+
return $descriptor;
34+
}
35+
36+
/**
37+
* Check if <first> has a value
38+
*
39+
* @return boolean
40+
*/
41+
public function hasFirst(){
42+
return $this->_has(1);
43+
}
44+
45+
/**
46+
* Clear <first> value
47+
*
48+
* @return \tests\ExtA
49+
*/
50+
public function clearFirst(){
51+
return $this->_clear(1);
52+
}
53+
54+
/**
55+
* Get <first> value
56+
*
57+
* @return string
58+
*/
59+
public function getFirst(){
60+
return $this->_get(1);
61+
}
62+
63+
/**
64+
* Set <first> value
65+
*
66+
* @param string $value
67+
* @return \tests\ExtA
68+
*/
69+
public function setFirst( $value){
70+
return $this->_set(1, $value);
71+
}
72+
}
73+
}
74+
75+
namespace tests {
76+
77+
class ExtB extends \DrSlump\Protobuf\Message {
78+
79+
80+
/** @var \Closure[] */
81+
protected static $__extensions = array();
82+
83+
public static function descriptor()
84+
{
85+
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'tests.ExtB');
86+
87+
foreach (self::$__extensions as $cb) {
88+
$descriptor->addField($cb(), true);
89+
}
90+
91+
return $descriptor;
92+
}
93+
}
94+
}
95+
96+
namespace {
97+
\tests\ExtA::extension(function(){
98+
// OPTIONAL STRING tests\ExtB\second = 2
99+
$f = new \DrSlump\Protobuf\Field();
100+
$f->number = 2;
101+
$f->name = "tests\ExtB\second";
102+
$f->type = \DrSlump\Protobuf::TYPE_STRING;
103+
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
104+
return $f;
105+
});
106+
}

tests/protos/extension.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package tests;
2+
3+
message ExtA {
4+
optional string first = 1;
5+
extensions 2 to 3;
6+
}
7+
8+
message ExtB {
9+
extend ExtA {
10+
optional string second = 2;
11+
}
12+
}

0 commit comments

Comments
 (0)