• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3use Foo\TestMessage;
4use Foo\TestEnum;
5use Foo\TestMessage\Sub;
6
7class TestBase extends \PHPUnit\Framework\TestCase
8{
9
10    public function setFields(TestMessage $m)
11    {
12        TestUtil::setTestMessage($m);
13    }
14
15    public function setFields2(TestMessage $m)
16    {
17        TestUtil::setTestMessage2($m);
18    }
19
20    public function expectFields(TestMessage $m)
21    {
22        $this->assertSame(-42,  $m->getOptionalInt32());
23        $this->assertSame(42,  $m->getOptionalUint32());
24        $this->assertSame(-44,  $m->getOptionalSint32());
25        $this->assertSame(46,   $m->getOptionalFixed32());
26        $this->assertSame(-46,  $m->getOptionalSfixed32());
27        $this->assertSame(1.5,  $m->getOptionalFloat());
28        $this->assertSame(1.6,  $m->getOptionalDouble());
29        $this->assertSame(true, $m->getOptionalBool());
30        $this->assertSame('a',  $m->getOptionalString());
31        $this->assertSame('bbbb',  $m->getOptionalBytes());
32        $this->assertSame(TestEnum::ONE, $m->getOptionalEnum());
33        $this->assertSame(33,   $m->getOptionalMessage()->getA());
34        if (PHP_INT_SIZE == 4) {
35            $this->assertSame('-43',  $m->getOptionalInt64());
36            $this->assertSame('43',   $m->getOptionalUint64());
37            $this->assertSame('-45',  $m->getOptionalSint64());
38            $this->assertSame('47',   $m->getOptionalFixed64());
39            $this->assertSame('-47',  $m->getOptionalSfixed64());
40        } else {
41            $this->assertSame(-43,  $m->getOptionalInt64());
42            $this->assertSame(43,   $m->getOptionalUint64());
43            $this->assertSame(-45,  $m->getOptionalSint64());
44            $this->assertSame(47,   $m->getOptionalFixed64());
45            $this->assertSame(-47,  $m->getOptionalSfixed64());
46        }
47
48        $this->assertEquals(-42,  $m->getRepeatedInt32()[0]);
49        $this->assertEquals(42,   $m->getRepeatedUint32()[0]);
50        $this->assertEquals(-43,  $m->getRepeatedInt64()[0]);
51        $this->assertEquals(43,   $m->getRepeatedUint64()[0]);
52        $this->assertEquals(-44,  $m->getRepeatedSint32()[0]);
53        $this->assertEquals(-45,  $m->getRepeatedSint64()[0]);
54        $this->assertEquals(46,   $m->getRepeatedFixed32()[0]);
55        $this->assertEquals(47,   $m->getRepeatedFixed64()[0]);
56        $this->assertEquals(-46,  $m->getRepeatedSfixed32()[0]);
57        $this->assertEquals(-47,  $m->getRepeatedSfixed64()[0]);
58        $this->assertEquals(1.5,  $m->getRepeatedFloat()[0]);
59        $this->assertEquals(1.6,  $m->getRepeatedDouble()[0]);
60        $this->assertEquals(true, $m->getRepeatedBool()[0]);
61        $this->assertEquals('a',  $m->getRepeatedString()[0]);
62        $this->assertEquals('bbbb',  $m->getRepeatedBytes()[0]);
63        $this->assertEquals(34,   $m->getRepeatedMessage()[0]->GetA());
64
65        $this->assertEquals(-52,   $m->getRepeatedInt32()[1]);
66        $this->assertEquals(52,    $m->getRepeatedUint32()[1]);
67        $this->assertEquals(-53,   $m->getRepeatedInt64()[1]);
68        $this->assertEquals(53,    $m->getRepeatedUint64()[1]);
69        $this->assertEquals(-54,   $m->getRepeatedSint32()[1]);
70        $this->assertEquals(-55,   $m->getRepeatedSint64()[1]);
71        $this->assertEquals(56,    $m->getRepeatedFixed32()[1]);
72        $this->assertEquals(57,    $m->getRepeatedFixed64()[1]);
73        $this->assertEquals(-56,   $m->getRepeatedSfixed32()[1]);
74        $this->assertEquals(-57,   $m->getRepeatedSfixed64()[1]);
75        $this->assertEquals(2.5,   $m->getRepeatedFloat()[1]);
76        $this->assertEquals(2.6,   $m->getRepeatedDouble()[1]);
77        $this->assertEquals(false, $m->getRepeatedBool()[1]);
78        $this->assertEquals('c',   $m->getRepeatedString()[1]);
79        $this->assertEquals('dddd',   $m->getRepeatedBytes()[1]);
80        $this->assertEquals(35,    $m->getRepeatedMessage()[1]->GetA());
81
82        if (PHP_INT_SIZE == 4) {
83            $this->assertEquals('-63', $m->getMapInt64Int64()['-63']);
84            $this->assertEquals('63',  $m->getMapUint64Uint64()['63']);
85            $this->assertEquals('-65', $m->getMapSint64Sint64()['-65']);
86            $this->assertEquals('67',  $m->getMapFixed64Fixed64()['67']);
87            $this->assertEquals('-69',  $m->getMapSfixed64Sfixed64()['-69']);
88        } else {
89            $this->assertEquals(-63, $m->getMapInt64Int64()[-63]);
90            $this->assertEquals(63,  $m->getMapUint64Uint64()[63]);
91            $this->assertEquals(-65, $m->getMapSint64Sint64()[-65]);
92            $this->assertEquals(67,  $m->getMapFixed64Fixed64()[67]);
93            $this->assertEquals(-69,  $m->getMapSfixed64Sfixed64()[-69]);
94        }
95        $this->assertEquals(-62, $m->getMapInt32Int32()[-62]);
96        $this->assertEquals(62,  $m->getMapUint32Uint32()[62]);
97        $this->assertEquals(-64, $m->getMapSint32Sint32()[-64]);
98        $this->assertEquals(66,  $m->getMapFixed32Fixed32()[66]);
99        $this->assertEquals(-68,  $m->getMapSfixed32Sfixed32()[-68]);
100        $this->assertEquals(3.5, $m->getMapInt32Float()[1]);
101        $this->assertEquals(3.6, $m->getMapInt32Double()[1]);
102        $this->assertEquals(true , $m->getMapBoolBool()[true]);
103        $this->assertEquals('e', $m->getMapStringString()['e']);
104        $this->assertEquals('ffff', $m->getMapInt32Bytes()[1]);
105        $this->assertEquals(TestEnum::ONE, $m->getMapInt32Enum()[1]);
106        $this->assertEquals(36, $m->getMapInt32Message()[1]->GetA());
107    }
108
109    // Test message merged from setFields and setFields2.
110    public function expectFieldsMerged(TestMessage $m)
111    {
112        $this->assertSame(-144,  $m->getOptionalSint32());
113        $this->assertSame(146,   $m->getOptionalFixed32());
114        $this->assertSame(-146,  $m->getOptionalSfixed32());
115        $this->assertSame(11.5,  $m->getOptionalFloat());
116        $this->assertSame(11.6,  $m->getOptionalDouble());
117        $this->assertSame(true, $m->getOptionalBool());
118        $this->assertSame('aa',  $m->getOptionalString());
119        $this->assertSame('bb',  $m->getOptionalBytes());
120        $this->assertSame(133,   $m->getOptionalMessage()->getA());
121        if (PHP_INT_SIZE == 4) {
122            $this->assertSame('-143',  $m->getOptionalInt64());
123            $this->assertSame('143',   $m->getOptionalUint64());
124            $this->assertSame('-145',  $m->getOptionalSint64());
125            $this->assertSame('147',   $m->getOptionalFixed64());
126            $this->assertSame('-147',  $m->getOptionalSfixed64());
127        } else {
128            $this->assertSame(-143,  $m->getOptionalInt64());
129            $this->assertSame(143,   $m->getOptionalUint64());
130            $this->assertSame(-145,  $m->getOptionalSint64());
131            $this->assertSame(147,   $m->getOptionalFixed64());
132            $this->assertSame(-147,  $m->getOptionalSfixed64());
133        }
134
135        $this->assertEquals(-42,  $m->getRepeatedInt32()[0]);
136        $this->assertEquals(42,   $m->getRepeatedUint32()[0]);
137        $this->assertEquals(-43,  $m->getRepeatedInt64()[0]);
138        $this->assertEquals(43,   $m->getRepeatedUint64()[0]);
139        $this->assertEquals(-44,  $m->getRepeatedSint32()[0]);
140        $this->assertEquals(-45,  $m->getRepeatedSint64()[0]);
141        $this->assertEquals(46,   $m->getRepeatedFixed32()[0]);
142        $this->assertEquals(47,   $m->getRepeatedFixed64()[0]);
143        $this->assertEquals(-46,  $m->getRepeatedSfixed32()[0]);
144        $this->assertEquals(-47,  $m->getRepeatedSfixed64()[0]);
145        $this->assertEquals(1.5,  $m->getRepeatedFloat()[0]);
146        $this->assertEquals(1.6,  $m->getRepeatedDouble()[0]);
147        $this->assertEquals(true, $m->getRepeatedBool()[0]);
148        $this->assertEquals('a',  $m->getRepeatedString()[0]);
149        $this->assertEquals('bbbb',  $m->getRepeatedBytes()[0]);
150        $this->assertEquals(TestEnum::ZERO,  $m->getRepeatedEnum()[0]);
151        $this->assertEquals(34,   $m->getRepeatedMessage()[0]->GetA());
152
153        $this->assertEquals(-52,   $m->getRepeatedInt32()[1]);
154        $this->assertEquals(52,    $m->getRepeatedUint32()[1]);
155        $this->assertEquals(-53,   $m->getRepeatedInt64()[1]);
156        $this->assertEquals(53,    $m->getRepeatedUint64()[1]);
157        $this->assertEquals(-54,   $m->getRepeatedSint32()[1]);
158        $this->assertEquals(-55,   $m->getRepeatedSint64()[1]);
159        $this->assertEquals(56,    $m->getRepeatedFixed32()[1]);
160        $this->assertEquals(57,    $m->getRepeatedFixed64()[1]);
161        $this->assertEquals(-56,   $m->getRepeatedSfixed32()[1]);
162        $this->assertEquals(-57,   $m->getRepeatedSfixed64()[1]);
163        $this->assertEquals(2.5,   $m->getRepeatedFloat()[1]);
164        $this->assertEquals(2.6,   $m->getRepeatedDouble()[1]);
165        $this->assertEquals(false, $m->getRepeatedBool()[1]);
166        $this->assertEquals('c',   $m->getRepeatedString()[1]);
167        $this->assertEquals('dddd',   $m->getRepeatedBytes()[1]);
168        $this->assertEquals(TestEnum::ONE,  $m->getRepeatedEnum()[1]);
169        $this->assertEquals(35,    $m->getRepeatedMessage()[1]->GetA());
170
171        $this->assertEquals(-142,  $m->getRepeatedInt32()[2]);
172        $this->assertEquals(142,   $m->getRepeatedUint32()[2]);
173        $this->assertEquals(-143,  $m->getRepeatedInt64()[2]);
174        $this->assertEquals(143,   $m->getRepeatedUint64()[2]);
175        $this->assertEquals(-144,  $m->getRepeatedSint32()[2]);
176        $this->assertEquals(-145,  $m->getRepeatedSint64()[2]);
177        $this->assertEquals(146,   $m->getRepeatedFixed32()[2]);
178        $this->assertEquals(147,   $m->getRepeatedFixed64()[2]);
179        $this->assertEquals(-146,  $m->getRepeatedSfixed32()[2]);
180        $this->assertEquals(-147,  $m->getRepeatedSfixed64()[2]);
181        $this->assertEquals(11.5,  $m->getRepeatedFloat()[2]);
182        $this->assertEquals(11.6,  $m->getRepeatedDouble()[2]);
183        $this->assertEquals(false, $m->getRepeatedBool()[2]);
184        $this->assertEquals('aa',  $m->getRepeatedString()[2]);
185        $this->assertEquals('bb',  $m->getRepeatedBytes()[2]);
186        $this->assertEquals(TestEnum::TWO,  $m->getRepeatedEnum()[2]);
187        $this->assertEquals(134,   $m->getRepeatedMessage()[2]->GetA());
188
189        if (PHP_INT_SIZE == 4) {
190            $this->assertEquals('-163', $m->getMapInt64Int64()['-63']);
191            $this->assertEquals('163',  $m->getMapUint64Uint64()['63']);
192            $this->assertEquals('-165', $m->getMapSint64Sint64()['-65']);
193            $this->assertEquals('167',  $m->getMapFixed64Fixed64()['67']);
194            $this->assertEquals('-169',  $m->getMapSfixed64Sfixed64()['-69']);
195        } else {
196            $this->assertEquals(-163, $m->getMapInt64Int64()[-63]);
197            $this->assertEquals(163,  $m->getMapUint64Uint64()[63]);
198            $this->assertEquals(-165, $m->getMapSint64Sint64()[-65]);
199            $this->assertEquals(167,  $m->getMapFixed64Fixed64()[67]);
200            $this->assertEquals(-169,  $m->getMapSfixed64Sfixed64()[-69]);
201        }
202        $this->assertEquals(-162, $m->getMapInt32Int32()[-62]);
203        $this->assertEquals(162,  $m->getMapUint32Uint32()[62]);
204        $this->assertEquals(-164, $m->getMapSint32Sint32()[-64]);
205        $this->assertEquals(166,  $m->getMapFixed32Fixed32()[66]);
206        $this->assertEquals(-168,  $m->getMapSfixed32Sfixed32()[-68]);
207        $this->assertEquals(13.5, $m->getMapInt32Float()[1]);
208        $this->assertEquals(13.6, $m->getMapInt32Double()[1]);
209        $this->assertEquals(false , $m->getMapBoolBool()[true]);
210        $this->assertEquals('ee', $m->getMapStringString()['e']);
211        $this->assertEquals('ff', $m->getMapInt32Bytes()[1]);
212        $this->assertEquals(TestEnum::TWO, $m->getMapInt32Enum()[1]);
213        $this->assertEquals(136, $m->getMapInt32Message()[1]->GetA());
214
215        if (PHP_INT_SIZE == 4) {
216            $this->assertEquals('-163', $m->getMapInt64Int64()['-163']);
217            $this->assertEquals('163',  $m->getMapUint64Uint64()['163']);
218            $this->assertEquals('-165', $m->getMapSint64Sint64()['-165']);
219            $this->assertEquals('167',  $m->getMapFixed64Fixed64()['167']);
220            $this->assertEquals('-169',  $m->getMapSfixed64Sfixed64()['-169']);
221        } else {
222            $this->assertEquals(-163, $m->getMapInt64Int64()[-163]);
223            $this->assertEquals(163,  $m->getMapUint64Uint64()[163]);
224            $this->assertEquals(-165, $m->getMapSint64Sint64()[-165]);
225            $this->assertEquals(167,  $m->getMapFixed64Fixed64()[167]);
226            $this->assertEquals(-169,  $m->getMapSfixed64Sfixed64()[-169]);
227        }
228        $this->assertEquals(-162, $m->getMapInt32Int32()[-162]);
229        $this->assertEquals(162,  $m->getMapUint32Uint32()[162]);
230        $this->assertEquals(-164, $m->getMapSint32Sint32()[-164]);
231        $this->assertEquals(166,  $m->getMapFixed32Fixed32()[166]);
232        $this->assertEquals(-168,  $m->getMapSfixed32Sfixed32()[-168]);
233        $this->assertEquals(13.5, $m->getMapInt32Float()[2]);
234        $this->assertEquals(13.6, $m->getMapInt32Double()[2]);
235        $this->assertEquals(false , $m->getMapBoolBool()[false]);
236        $this->assertEquals('ee', $m->getMapStringString()['ee']);
237        $this->assertEquals('ff', $m->getMapInt32Bytes()[2]);
238        $this->assertEquals(TestEnum::TWO, $m->getMapInt32Enum()[2]);
239        $this->assertEquals(136, $m->getMapInt32Message()[2]->GetA());
240    }
241
242    public function expectEmptyFields(TestMessage $m)
243    {
244        $this->assertSame(0,   $m->getOptionalInt32());
245        $this->assertSame(0,   $m->getOptionalUint32());
246        $this->assertSame(0,   $m->getOptionalSint32());
247        $this->assertSame(0,   $m->getOptionalFixed32());
248        $this->assertSame(0,   $m->getOptionalSfixed32());
249        $this->assertSame(0.0, $m->getOptionalFloat());
250        $this->assertSame(0.0, $m->getOptionalDouble());
251        $this->assertSame(false, $m->getOptionalBool());
252        $this->assertSame('',  $m->getOptionalString());
253        $this->assertSame('',  $m->getOptionalBytes());
254        $this->assertSame(0, $m->getOptionalEnum());
255        $this->assertNull($m->getOptionalMessage());
256        $this->assertNull($m->getOptionalIncludedMessage());
257        $this->assertNull($m->getRecursive());
258        if (PHP_INT_SIZE == 4) {
259            $this->assertSame("0", $m->getOptionalInt64());
260            $this->assertSame("0", $m->getOptionalUint64());
261            $this->assertSame("0", $m->getOptionalSint64());
262            $this->assertSame("0", $m->getOptionalFixed64());
263            $this->assertSame("0", $m->getOptionalSfixed64());
264        } else {
265            $this->assertSame(0, $m->getOptionalInt64());
266            $this->assertSame(0, $m->getOptionalUint64());
267            $this->assertSame(0, $m->getOptionalSint64());
268            $this->assertSame(0, $m->getOptionalFixed64());
269            $this->assertSame(0, $m->getOptionalSfixed64());
270        }
271
272        $this->assertEquals(0, count($m->getRepeatedInt32()));
273        $this->assertEquals(0, count($m->getRepeatedUint32()));
274        $this->assertEquals(0, count($m->getRepeatedInt64()));
275        $this->assertEquals(0, count($m->getRepeatedUint64()));
276        $this->assertEquals(0, count($m->getRepeatedSint32()));
277        $this->assertEquals(0, count($m->getRepeatedSint64()));
278        $this->assertEquals(0, count($m->getRepeatedFixed32()));
279        $this->assertEquals(0, count($m->getRepeatedFixed64()));
280        $this->assertEquals(0, count($m->getRepeatedSfixed32()));
281        $this->assertEquals(0, count($m->getRepeatedSfixed64()));
282        $this->assertEquals(0, count($m->getRepeatedFloat()));
283        $this->assertEquals(0, count($m->getRepeatedDouble()));
284        $this->assertEquals(0, count($m->getRepeatedBool()));
285        $this->assertEquals(0, count($m->getRepeatedString()));
286        $this->assertEquals(0, count($m->getRepeatedBytes()));
287        $this->assertEquals(0, count($m->getRepeatedEnum()));
288        $this->assertEquals(0, count($m->getRepeatedMessage()));
289        $this->assertEquals(0, count($m->getRepeatedRecursive()));
290
291        $this->assertSame("", $m->getMyOneof());
292        $this->assertSame(0,   $m->getOneofInt32());
293        $this->assertSame(0,   $m->getOneofUint32());
294        $this->assertSame(0,   $m->getOneofSint32());
295        $this->assertSame(0,   $m->getOneofFixed32());
296        $this->assertSame(0,   $m->getOneofSfixed32());
297        $this->assertSame(0.0, $m->getOneofFloat());
298        $this->assertSame(0.0, $m->getOneofDouble());
299        $this->assertSame(false, $m->getOneofBool());
300        $this->assertSame('',  $m->getOneofString());
301        $this->assertSame('',  $m->getOneofBytes());
302        $this->assertSame(0, $m->getOneofEnum());
303        $this->assertNull($m->getOptionalMessage());
304        if (PHP_INT_SIZE == 4) {
305            $this->assertSame("0", $m->getOneofInt64());
306            $this->assertSame("0", $m->getOneofUint64());
307            $this->assertSame("0", $m->getOneofSint64());
308            $this->assertSame("0", $m->getOneofFixed64());
309            $this->assertSame("0", $m->getOneofSfixed64());
310        } else {
311            $this->assertSame(0, $m->getOneofInt64());
312            $this->assertSame(0, $m->getOneofUint64());
313            $this->assertSame(0, $m->getOneofSint64());
314            $this->assertSame(0, $m->getOneofFixed64());
315            $this->assertSame(0, $m->getOneofSfixed64());
316        }
317
318        $this->assertEquals(0, count($m->getMapInt64Int64()));
319        $this->assertEquals(0, count($m->getMapUint64Uint64()));
320        $this->assertEquals(0, count($m->getMapSint64Sint64()));
321        $this->assertEquals(0, count($m->getMapFixed64Fixed64()));
322        $this->assertEquals(0, count($m->getMapInt32Int32()));
323        $this->assertEquals(0, count($m->getMapUint32Uint32()));
324        $this->assertEquals(0, count($m->getMapSint32Sint32()));
325        $this->assertEquals(0, count($m->getMapFixed32Fixed32()));
326        $this->assertEquals(0, count($m->getMapSfixed32Sfixed32()));
327        $this->assertEquals(0, count($m->getMapSfixed64Sfixed64()));
328        $this->assertEquals(0, count($m->getMapInt32Float()));
329        $this->assertEquals(0, count($m->getMapInt32Double()));
330        $this->assertEquals(0, count($m->getMapBoolBool()));
331        $this->assertEquals(0, count($m->getMapStringString()));
332        $this->assertEquals(0, count($m->getMapInt32Bytes()));
333        $this->assertEquals(0, count($m->getMapInt32Enum()));
334        $this->assertEquals(0, count($m->getMapInt32Message()));
335        $this->assertEquals(0, count($m->getMapRecursive()));
336    }
337
338  // This test is to avoid the warning of no test by php unit.
339  public function testNone()
340  {
341      $this->assertTrue(true);
342  }
343}
344