• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3require_once('generated/NoNamespaceEnum.php');
4require_once('generated/NoNamespaceMessage.php');
5require_once('test_base.php');
6require_once('test_util.php');
7
8use Google\Protobuf\Internal\RepeatedField;
9use Google\Protobuf\Internal\MapField;
10use Google\Protobuf\Internal\GPBType;
11use Bar\TestLegacyMessage;
12use Bar\TestLegacyMessage_NestedEnum;
13use Bar\TestLegacyMessage_NestedMessage;
14use Foo\TestEnum;
15use Foo\TestIncludeNamespaceMessage;
16use Foo\TestIncludePrefixMessage;
17use Foo\TestMessage;
18use Foo\TestMessage\Sub;
19use Foo\TestMessage_Sub;
20use Foo\TestMessage\NestedEnum;
21use Foo\TestReverseFieldOrder;
22use Foo\testLowerCaseMessage;
23use Foo\testLowerCaseEnum;
24use PBEmpty\PBEcho\TestEmptyPackage;
25use Php\Test\TestNamespace;
26
27class GeneratedClassTest extends TestBase
28{
29
30    #########################################################
31    # Test field accessors.
32    #########################################################
33
34    public function testSetterGetter()
35    {
36        $m = new TestMessage();
37        $m->setOptionalInt32(1);
38        $this->assertSame(1, $m->getOptionalInt32());
39    }
40
41    #########################################################
42    # Test int32 field.
43    #########################################################
44
45    public function testInt32Field()
46    {
47        $m = new TestMessage();
48
49        // Set integer.
50        $m->setOptionalInt32(MAX_INT32);
51        $this->assertSame(MAX_INT32, $m->getOptionalInt32());
52        $m->setOptionalInt32(MIN_INT32);
53        $this->assertSame(MIN_INT32, $m->getOptionalInt32());
54
55        // Set float.
56        $m->setOptionalInt32(1.1);
57        $this->assertSame(1, $m->getOptionalInt32());
58        $m->setOptionalInt32(MAX_INT32_FLOAT);
59        $this->assertSame(MAX_INT32, $m->getOptionalInt32());
60        $m->setOptionalInt32(MIN_INT32_FLOAT);
61        $this->assertSame(MIN_INT32, $m->getOptionalInt32());
62
63        // Set string.
64        $m->setOptionalInt32('2');
65        $this->assertSame(2, $m->getOptionalInt32());
66        $m->setOptionalInt32('3.1');
67        $this->assertSame(3, $m->getOptionalInt32());
68        $m->setOptionalInt32(MAX_INT32_STRING);
69        $this->assertSame(MAX_INT32, $m->getOptionalInt32());
70        $m->setOptionalInt32(MIN_INT32_STRING);
71        $this->assertSame(MIN_INT32, $m->getOptionalInt32());
72    }
73
74    #########################################################
75    # Test optional int32 field.
76    #########################################################
77
78    public function testOptionalInt32Field()
79    {
80        $m = new TestMessage();
81
82        $this->assertFalse($m->hasTrueOptionalInt32());
83        $this->assertSame(0, $m->getTrueOptionalInt32());
84
85        // Set integer.
86        $m->setTrueOptionalInt32(MAX_INT32);
87        $this->assertTrue($m->hasTrueOptionalInt32());
88        $this->assertSame(MAX_INT32, $m->getTrueOptionalInt32());
89
90        // Clear integer.
91        $m->clearTrueOptionalInt32();
92        $this->assertFalse($m->hasTrueOptionalInt32());
93        $this->assertSame(0, $m->getTrueOptionalInt32());
94    }
95
96    #########################################################
97    # Test uint32 field.
98    #########################################################
99
100    public function testUint32Field()
101    {
102        $m = new TestMessage();
103
104        // Set integer.
105        $m->setOptionalUint32(MAX_UINT32);
106        $this->assertSame(-1, $m->getOptionalUint32());
107        $m->setOptionalUint32(-1);
108        $this->assertSame(-1, $m->getOptionalUint32());
109        $m->setOptionalUint32(MIN_UINT32);
110        $this->assertSame(MIN_INT32, $m->getOptionalUint32());
111
112        // Set float.
113        $m->setOptionalUint32(1.1);
114        $this->assertSame(1, $m->getOptionalUint32());
115        $m->setOptionalUint32(MAX_UINT32_FLOAT);
116        $this->assertSame(-1, $m->getOptionalUint32());
117        $m->setOptionalUint32(-1.0);
118        $this->assertSame(-1, $m->getOptionalUint32());
119        $m->setOptionalUint32(MIN_UINT32_FLOAT);
120        $this->assertSame(MIN_INT32, $m->getOptionalUint32());
121
122        // Set string.
123        $m->setOptionalUint32('2');
124        $this->assertSame(2, $m->getOptionalUint32());
125        $m->setOptionalUint32('3.1');
126        $this->assertSame(3, $m->getOptionalUint32());
127        $m->setOptionalUint32(MAX_UINT32_STRING);
128        $this->assertSame(-1, $m->getOptionalUint32());
129        $m->setOptionalUint32('-1.0');
130        $this->assertSame(-1, $m->getOptionalUint32());
131        $m->setOptionalUint32(MIN_UINT32_STRING);
132        $this->assertSame(MIN_INT32, $m->getOptionalUint32());
133    }
134
135    #########################################################
136    # Test int64 field.
137    #########################################################
138
139    public function testInt64Field()
140    {
141        $m = new TestMessage();
142
143        // Set integer.
144        $m->setOptionalInt64(MAX_INT64);
145        $this->assertSame(MAX_INT64, $m->getOptionalInt64());
146        $m->setOptionalInt64(MIN_INT64);
147        $this->assertEquals(MIN_INT64, $m->getOptionalInt64());
148
149        // Set float.
150        $m->setOptionalInt64(1.1);
151        if (PHP_INT_SIZE == 4) {
152            $this->assertSame('1', $m->getOptionalInt64());
153        } else {
154            $this->assertSame(1, $m->getOptionalInt64());
155        }
156
157        // Set string.
158        $m->setOptionalInt64('2');
159        if (PHP_INT_SIZE == 4) {
160            $this->assertSame('2', $m->getOptionalInt64());
161        } else {
162            $this->assertSame(2, $m->getOptionalInt64());
163        }
164
165        $m->setOptionalInt64('3.1');
166        if (PHP_INT_SIZE == 4) {
167            $this->assertSame('3', $m->getOptionalInt64());
168        } else {
169            $this->assertSame(3, $m->getOptionalInt64());
170        }
171
172        $m->setOptionalInt64(MAX_INT64_STRING);
173        if (PHP_INT_SIZE == 4) {
174            $this->assertSame(MAX_INT64_STRING, $m->getOptionalInt64());
175        } else {
176            $this->assertSame(MAX_INT64, $m->getOptionalInt64());
177        }
178
179        $m->setOptionalInt64(MIN_INT64_STRING);
180        if (PHP_INT_SIZE == 4) {
181            $this->assertSame(MIN_INT64_STRING, $m->getOptionalInt64());
182        } else {
183            $this->assertSame(MIN_INT64, $m->getOptionalInt64());
184        }
185    }
186
187    #########################################################
188    # Test uint64 field.
189    #########################################################
190
191    public function testUint64Field()
192    {
193        $m = new TestMessage();
194
195        // Set integer.
196        $m->setOptionalUint64(MAX_UINT64);
197        if (PHP_INT_SIZE == 4) {
198            $this->assertSame(MAX_UINT64_STRING, $m->getOptionalUint64());
199        } else {
200            $this->assertSame(MAX_UINT64, $m->getOptionalUint64());
201        }
202
203        // Set float.
204        $m->setOptionalUint64(1.1);
205        if (PHP_INT_SIZE == 4) {
206            $this->assertSame('1', $m->getOptionalUint64());
207        } else {
208            $this->assertSame(1, $m->getOptionalUint64());
209        }
210
211        // Set string.
212        $m->setOptionalUint64('2');
213        if (PHP_INT_SIZE == 4) {
214            $this->assertSame('2', $m->getOptionalUint64());
215        } else {
216            $this->assertSame(2, $m->getOptionalUint64());
217        }
218
219        $m->setOptionalUint64('3.1');
220        if (PHP_INT_SIZE == 4) {
221            $this->assertSame('3', $m->getOptionalUint64());
222        } else {
223            $this->assertSame(3, $m->getOptionalUint64());
224        }
225
226        $m->setOptionalUint64(MAX_UINT64_STRING);
227        if (PHP_INT_SIZE == 4) {
228            $this->assertSame(MAX_UINT64_STRING, $m->getOptionalUint64());
229        } else {
230            $this->assertSame(MAX_UINT64, $m->getOptionalUint64());
231        }
232    }
233
234    #########################################################
235    # Test enum field.
236    #########################################################
237
238    public function testEnumField()
239    {
240        $m = new TestMessage();
241
242        // Set enum.
243        $m->setOptionalEnum(TestEnum::ONE);
244        $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
245
246        // Set integer.
247        $m->setOptionalEnum(1);
248        $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
249
250        // Set float.
251        $m->setOptionalEnum(1.1);
252        $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
253
254        // Set string.
255        $m->setOptionalEnum("1");
256        $this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
257
258        // Test Enum methods
259        $this->assertEquals('ONE', TestEnum::name(1));
260        $this->assertEquals(1, TestEnum::value('ONE'));
261    }
262
263    /**
264     * @expectedException UnexpectedValueException
265     * @expectedExceptionMessage Enum Foo\TestEnum has no name defined for value -1
266     */
267    public function testInvalidEnumValueThrowsException()
268    {
269        TestEnum::name(-1);
270    }
271
272    /**
273     * @expectedException UnexpectedValueException
274     * @expectedExceptionMessage Enum Foo\TestEnum has no value defined for name DOES_NOT_EXIST
275     */
276    public function testInvalidEnumNameThrowsException()
277    {
278        TestEnum::value('DOES_NOT_EXIST');
279    }
280
281    public function testNestedEnum()
282    {
283        $m = new TestMessage();
284        $m->setOptionalNestedEnum(NestedEnum::ZERO);
285        $this->assertTrue(true);
286    }
287
288    public function testLegacyNestedEnum()
289    {
290        $m = new TestMessage();
291        $m->setOptionalNestedEnum(\Foo\TestMessage_NestedEnum::ZERO);
292        $this->assertTrue(true);
293    }
294
295    public function testLegacyTypehintWithNestedEnums()
296    {
297        $this->legacyEnum(new TestLegacyMessage\NestedEnum);
298    }
299
300    private function legacyEnum(TestLegacyMessage_NestedEnum $enum)
301    {
302        // If we made it here without a PHP Fatal error, the typehint worked
303        $this->assertTrue(true);
304    }
305
306    #########################################################
307    # Test float field.
308    #########################################################
309
310    public function testFloatField()
311    {
312        $m = new TestMessage();
313
314        // Set integer.
315        $m->setOptionalFloat(1);
316        $this->assertEquals(1.0, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
317
318        // Set float.
319        $m->setOptionalFloat(1.1);
320        $this->assertEquals(1.1, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
321
322        // Set string.
323        $m->setOptionalFloat('2');
324        $this->assertEquals(2.0, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
325        $m->setOptionalFloat('3.1');
326        $this->assertEquals(3.1, $m->getOptionalFloat(), '', MAX_FLOAT_DIFF);
327    }
328
329    #########################################################
330    # Test double field.
331    #########################################################
332
333    public function testDoubleField()
334    {
335        $m = new TestMessage();
336
337        // Set integer.
338        $m->setOptionalDouble(1);
339        $this->assertEquals(1.0, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
340
341        // Set float.
342        $m->setOptionalDouble(1.1);
343        $this->assertEquals(1.1, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
344
345        // Set string.
346        $m->setOptionalDouble('2');
347        $this->assertEquals(2.0, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
348        $m->setOptionalDouble('3.1');
349        $this->assertEquals(3.1, $m->getOptionalDouble(), '', MAX_FLOAT_DIFF);
350    }
351
352    #########################################################
353    # Test bool field.
354    #########################################################
355
356    public function testBoolField()
357    {
358        $m = new TestMessage();
359
360        // Set bool.
361        $m->setOptionalBool(true);
362        $this->assertSame(true, $m->getOptionalBool());
363
364        // Set integer.
365        $m->setOptionalBool(-1);
366        $this->assertSame(true, $m->getOptionalBool());
367
368        // Set float.
369        $m->setOptionalBool(1.1);
370        $this->assertSame(true, $m->getOptionalBool());
371
372        // Set string.
373        $m->setOptionalBool('');
374        $this->assertSame(false, $m->getOptionalBool());
375    }
376
377    #########################################################
378    # Test string field.
379    #########################################################
380
381    public function testStringField()
382    {
383        $m = new TestMessage();
384
385        // Set string.
386        $m->setOptionalString('abc');
387        $this->assertSame('abc', $m->getOptionalString());
388
389        // Set integer.
390        $m->setOptionalString(1);
391        $this->assertSame('1', $m->getOptionalString());
392
393        // Set double.
394        $m->setOptionalString(1.1);
395        $this->assertSame('1.1', $m->getOptionalString());
396
397        // Set bool.
398        $m->setOptionalString(true);
399        $this->assertSame('1', $m->getOptionalString());
400    }
401
402    #########################################################
403    # Test bytes field.
404    #########################################################
405
406    public function testBytesField()
407    {
408        $m = new TestMessage();
409
410        // Set string.
411        $m->setOptionalBytes('abc');
412        $this->assertSame('abc', $m->getOptionalBytes());
413
414        // Set integer.
415        $m->setOptionalBytes(1);
416        $this->assertSame('1', $m->getOptionalBytes());
417
418        // Set double.
419        $m->setOptionalBytes(1.1);
420        $this->assertSame('1.1', $m->getOptionalBytes());
421
422        // Set bool.
423        $m->setOptionalBytes(true);
424        $this->assertSame('1', $m->getOptionalBytes());
425    }
426
427      public function testBytesFieldInvalidUTF8Success()
428      {
429          $m = new TestMessage();
430          $hex = hex2bin("ff");
431          $m->setOptionalBytes($hex);
432          $this->assertTrue(true);
433      }
434
435    #########################################################
436    # Test message field.
437    #########################################################
438
439    public function testMessageField()
440    {
441        $m = new TestMessage();
442
443        $sub_m = new Sub();
444        $sub_m->setA(1);
445        $m->setOptionalMessage($sub_m);
446        $this->assertSame(1, $m->getOptionalMessage()->getA());
447
448        $null = null;
449        $m->setOptionalMessage($null);
450        $this->assertNull($m->getOptionalMessage());
451    }
452
453    public function testLegacyMessageField()
454    {
455        $m = new TestMessage();
456
457        $sub_m = new TestMessage_Sub();
458        $sub_m->setA(1);
459        $m->setOptionalMessage($sub_m);
460        $this->assertSame(1, $m->getOptionalMessage()->getA());
461
462        $null = null;
463        $m->setOptionalMessage($null);
464        $this->assertNull($m->getOptionalMessage());
465    }
466
467    public function testLegacyTypehintWithNestedMessages()
468    {
469        $this->legacyMessage(new TestLegacyMessage\NestedMessage);
470    }
471
472    private function legacyMessage(TestLegacyMessage_NestedMessage $sub)
473    {
474        // If we made it here without a PHP Fatal error, the typehint worked
475        $this->assertTrue(true);
476    }
477
478    #########################################################
479    # Test repeated field.
480    #########################################################
481
482    public function testRepeatedField()
483    {
484        $m = new TestMessage();
485
486        $repeated_int32 = new RepeatedField(GPBType::INT32);
487        $m->setRepeatedInt32($repeated_int32);
488        $this->assertSame($repeated_int32, $m->getRepeatedInt32());
489    }
490
491    public function testRepeatedFieldViaArray()
492    {
493        $m = new TestMessage();
494
495        $arr = array();
496        $m->setRepeatedInt32($arr);
497        $this->assertSame(0, count($m->getRepeatedInt32()));
498
499        $arr = array(1, 2.1, "3");
500        $m->setRepeatedInt32($arr);
501        $this->assertTrue($m->getRepeatedInt32() instanceof RepeatedField);
502        $this->assertSame("Google\Protobuf\Internal\RepeatedField",
503                          get_class($m->getRepeatedInt32()));
504        $this->assertSame(3, count($m->getRepeatedInt32()));
505        $this->assertSame(1, $m->getRepeatedInt32()[0]);
506        $this->assertSame(2, $m->getRepeatedInt32()[1]);
507        $this->assertSame(3, $m->getRepeatedInt32()[2]);
508        $this->assertFalse($arr instanceof RepeatedField);
509    }
510
511    #########################################################
512    # Test map field.
513    #########################################################
514
515    public function testMapField()
516    {
517        $m = new TestMessage();
518
519        $map_int32_int32 = new MapField(GPBType::INT32, GPBType::INT32);
520        $m->setMapInt32Int32($map_int32_int32);
521        $this->assertSame($map_int32_int32, $m->getMapInt32Int32());
522    }
523
524    public function testMapFieldViaArray()
525    {
526        $m = new TestMessage();
527
528        $dict = array();
529        $m->setMapInt32Int32($dict);
530        $this->assertSame(0, count($m->getMapInt32Int32()));
531
532        $dict = array(5 => 5, 6.1 => 6.1, "7" => "7");
533        $m->setMapInt32Int32($dict);
534        $this->assertTrue($m->getMapInt32Int32() instanceof MapField);
535        $this->assertSame(3, count($m->getMapInt32Int32()));
536        $this->assertSame(5, $m->getMapInt32Int32()[5]);
537        $this->assertSame(6, $m->getMapInt32Int32()[6]);
538        $this->assertSame(7, $m->getMapInt32Int32()[7]);
539        $this->assertFalse($dict instanceof MapField);
540    }
541
542    #########################################################
543    # Test oneof field.
544    #########################################################
545
546    public function testOneofField() {
547        $m = new TestMessage();
548
549        $this->assertSame("", $m->getMyOneof());
550
551        $m->setOneofInt32(1);
552        $this->assertSame(1, $m->getOneofInt32());
553        $this->assertSame(0.0, $m->getOneofFloat());
554        $this->assertSame('', $m->getOneofString());
555        $this->assertSame(NULL, $m->getOneofMessage());
556        $this->assertSame("oneof_int32", $m->getMyOneof());
557
558        $m->setOneofFloat(2.0);
559        $this->assertSame(0, $m->getOneofInt32());
560        $this->assertSame(2.0, $m->getOneofFloat());
561        $this->assertSame('', $m->getOneofString());
562        $this->assertSame(NULL, $m->getOneofMessage());
563        $this->assertSame("oneof_float", $m->getMyOneof());
564
565        $m->setOneofString('abc');
566        $this->assertSame(0, $m->getOneofInt32());
567        $this->assertSame(0.0, $m->getOneofFloat());
568        $this->assertSame('abc', $m->getOneofString());
569        $this->assertSame(NULL, $m->getOneofMessage());
570        $this->assertSame("oneof_string", $m->getMyOneof());
571
572        $sub_m = new Sub();
573        $sub_m->setA(1);
574        $m->setOneofMessage($sub_m);
575        $this->assertSame(0, $m->getOneofInt32());
576        $this->assertSame(0.0, $m->getOneofFloat());
577        $this->assertSame('', $m->getOneofString());
578        $this->assertSame(1, $m->getOneofMessage()->getA());
579        $this->assertSame("oneof_message", $m->getMyOneof());
580    }
581
582    #########################################################
583    # Test clear method.
584    #########################################################
585
586    public function testMessageClear()
587    {
588        $m = new TestMessage();
589        $this->setFields($m);
590        $this->expectFields($m);
591        $m->clear();
592        $this->expectEmptyFields($m);
593    }
594
595    #########################################################
596    # Test mergeFrom method.
597    #########################################################
598
599    public function testMessageMergeFrom()
600    {
601        $m = new TestMessage();
602        $this->setFields($m);
603        $this->expectFields($m);
604        $arr = $m->getOptionalMessage()->getB();
605        $arr[] = 1;
606
607        $n = new TestMessage();
608
609        // Singular
610        $n->setOptionalInt32(100);
611        $sub1 = new Sub();
612        $sub1->setA(101);
613
614        $b = $sub1->getB();
615        $b[] = 102;
616        $sub1->setB($b);
617
618        $n->setOptionalMessage($sub1);
619
620        // Repeated
621        $repeatedInt32 = $n->getRepeatedInt32();
622        $repeatedInt32[] = 200;
623        $n->setRepeatedInt32($repeatedInt32);
624
625        $repeatedString = $n->getRepeatedString();
626        $repeatedString[] = 'abc';
627        $n->setRepeatedString($repeatedString);
628
629        $sub2 = new Sub();
630        $sub2->setA(201);
631        $repeatedMessage = $n->getRepeatedMessage();
632        $repeatedMessage[] = $sub2;
633        $n->setRepeatedMessage($repeatedMessage);
634
635        // Map
636        $mapInt32Int32 = $n->getMapInt32Int32();
637        $mapInt32Int32[1] = 300;
638        $mapInt32Int32[-62] = 301;
639        $n->setMapInt32Int32($mapInt32Int32);
640
641        $mapStringString = $n->getMapStringString();
642        $mapStringString['def'] = 'def';
643        $n->setMapStringString($mapStringString);
644
645        $mapInt32Message = $n->getMapInt32Message();
646        $mapInt32Message[1] = new Sub();
647        $mapInt32Message[1]->setA(302);
648        $mapInt32Message[2] = new Sub();
649        $mapInt32Message[2]->setA(303);
650        $n->setMapInt32Message($mapInt32Message);
651
652        $m->mergeFrom($n);
653
654        $this->assertSame(100, $m->getOptionalInt32());
655        $this->assertSame(42, $m->getOptionalUint32());
656        $this->assertSame(101, $m->getOptionalMessage()->getA());
657        $this->assertSame(2, count($m->getOptionalMessage()->getB()));
658        $this->assertSame(1, $m->getOptionalMessage()->getB()[0]);
659        $this->assertSame(102, $m->getOptionalMessage()->getB()[1]);
660
661        $this->assertSame(3, count($m->getRepeatedInt32()));
662        $this->assertSame(200, $m->getRepeatedInt32()[2]);
663        $this->assertSame(2, count($m->getRepeatedUint32()));
664        $this->assertSame(3, count($m->getRepeatedString()));
665        $this->assertSame('abc', $m->getRepeatedString()[2]);
666        $this->assertSame(3, count($m->getRepeatedMessage()));
667        $this->assertSame(201, $m->getRepeatedMessage()[2]->getA());
668
669        $this->assertSame(2, count($m->getMapInt32Int32()));
670        $this->assertSame(300, $m->getMapInt32Int32()[1]);
671        $this->assertSame(301, $m->getMapInt32Int32()[-62]);
672        $this->assertSame(1, count($m->getMapUint32Uint32()));
673        $this->assertSame(2, count($m->getMapStringString()));
674        $this->assertSame('def', $m->getMapStringString()['def']);
675
676        $this->assertSame(2, count($m->getMapInt32Message()));
677        $this->assertSame(302, $m->getMapInt32Message()[1]->getA());
678        $this->assertSame(303, $m->getMapInt32Message()[2]->getA());
679
680        $this->assertSame("", $m->getMyOneof());
681
682        // Check sub-messages are copied by value.
683        $n->getOptionalMessage()->setA(-101);
684        $this->assertSame(101, $m->getOptionalMessage()->getA());
685
686        $repeatedMessage = $n->getRepeatedMessage();
687        $repeatedMessage[0]->setA(-201);
688        $n->setRepeatedMessage($repeatedMessage);
689        $this->assertSame(201, $m->getRepeatedMessage()[2]->getA());
690
691        $mapInt32Message = $n->getMapInt32Message();
692        $mapInt32Message[1]->setA(-302);
693        $n->setMapInt32Message($mapInt32Message);
694
695        $this->assertSame(302, $m->getMapInt32Message()[1]->getA());
696
697        // Test merge oneof.
698        $m = new TestMessage();
699
700        $n = new TestMessage();
701        $n->setOneofInt32(1);
702        $m->mergeFrom($n);
703        $this->assertSame(1, $m->getOneofInt32());
704
705        $sub = new Sub();
706        $n->setOneofMessage($sub);
707        $n->getOneofMessage()->setA(400);
708        $m->mergeFrom($n);
709        $this->assertSame(400, $m->getOneofMessage()->getA());
710        $n->getOneofMessage()->setA(-400);
711        $this->assertSame(400, $m->getOneofMessage()->getA());
712
713        // Test all fields
714        $m = new TestMessage();
715        $n = new TestMessage();
716        $this->setFields($m);
717        $n->mergeFrom($m);
718        $this->expectFields($n);
719    }
720
721    #########################################################
722    # Test message/enum without namespace.
723    #########################################################
724
725    public function testMessageWithoutNamespace()
726    {
727        $m = new TestMessage();
728        $n = new NoNameSpaceMessage();
729        $m->setOptionalNoNamespaceMessage($n);
730        $repeatedNoNamespaceMessage = $m->getRepeatedNoNamespaceMessage();
731        $repeatedNoNamespaceMessage[] = new NoNameSpaceMessage();
732        $m->setRepeatedNoNamespaceMessage($repeatedNoNamespaceMessage);
733
734        // test nested messages
735        $sub = new NoNamespaceMessage\NestedMessage();
736        $n->setNestedMessage($sub);
737
738        $this->assertTrue(true);
739    }
740
741    public function testEnumWithoutNamespace()
742    {
743        $m = new TestMessage();
744        $m->setOptionalNoNamespaceEnum(NoNameSpaceEnum::VALUE_A);
745        $repeatedNoNamespaceEnum = $m->getRepeatedNoNamespaceEnum();
746        $repeatedNoNamespaceEnum[] = NoNameSpaceEnum::VALUE_A;
747        $m->setRepeatedNoNamespaceEnum($repeatedNoNamespaceEnum);
748        $this->assertTrue(true);
749    }
750
751    #########################################################
752    # Test message with given namespace.
753    #########################################################
754
755    public function testNestedMessagesAndEnums()
756    {
757        $m = new TestMessage();
758        $n = new TestMessage\Sub();
759        $m->setOptionalMessage($n);
760        $m->setOptionalNestedEnum(TestMessage\NestedEnum::ZERO);
761        $this->assertSame($n, $m->getOptionalMessage());
762        $this->assertSame(TestMessage\NestedEnum::ZERO, $m->getOptionalNestedEnum());
763    }
764
765    public function testMessagesAndEnumsWithPrefix()
766    {
767        // Test message prefix
768        $m = new TestIncludePrefixMessage();
769        $n = new PrefixTestPrefix();
770        $n->setA(1);
771        $m->setPrefixMessage($n);
772        $this->assertSame(1, $m->getPrefixMessage()->getA());
773
774        // Test nested message prefix
775        $o = new PrefixTestPrefix();
776        $p = new PrefixTestPrefix\PrefixNestedMessage();
777        $o->setNestedMessage($p);
778        $o->setNestedEnum(PrefixTestPrefix\PrefixNestedEnum::ZERO);
779        $this->assertSame($p, $o->getNestedMessage());
780        $this->assertSame(PrefixTestPrefix\PrefixNestedEnum::ZERO, $o->getNestedEnum());
781    }
782
783    public function testMessagesAndEnumsWithPhpNamespace()
784    {
785        $m = new TestNamespace();
786        $n = new TestNamespace\NestedMessage();
787        $m->setNestedMessage($n);
788        $m->setNestedEnum(TestNamespace\NestedEnum::ZERO);
789        $this->assertSame($n, $m->getNestedMessage());
790        $this->assertSame(TestNamespace\NestedEnum::ZERO, $m->getNestedEnum());
791    }
792
793    public function testMesssagesAndEnumsWithEmptyPhpNamespace()
794    {
795        $m = new TestEmptyNamespace();
796        $n = new TestEmptyNamespace\NestedMessage();
797        $m->setNestedMessage($n);
798        $m->setNestedEnum(TestEmptyNamespace\NestedEnum::ZERO);
799        $this->assertSame($n, $m->getNestedMessage());
800        $this->assertSame(TestEmptyNamespace\NestedEnum::ZERO, $m->getNestedEnum());
801    }
802
803    public function testMessagesAndEnumsWithNoNamespace()
804    {
805        $m = new NoNamespaceMessage();
806        $n = new NoNamespaceMessage\NestedMessage();
807        $m->setNestedMessage($n);
808        $m->setNestedEnum(NoNamespaceMessage\NestedEnum::ZERO);
809        $this->assertSame($n, $m->getNestedMessage());
810        $this->assertSame(NoNamespaceMessage\NestedEnum::ZERO, $m->getNestedEnum());
811    }
812
813    public function testReservedWordsInPackageName()
814    {
815        $m = new TestEmptyPackage();
816        $n = new TestEmptyPackage\NestedMessage();
817        $m->setNestedMessage($n);
818        $m->setNestedEnum(TestEmptyPackage\NestedEnum::ZERO);
819        $this->assertSame($n, $m->getNestedMessage());
820        $this->assertSame(TestEmptyPackage\NestedEnum::ZERO, $m->getNestedEnum());
821    }
822
823    public function testReservedWordsInNamespace()
824    {
825        $m = new TestNamespace();
826        $n = new TestNamespace\PBEmpty();
827        $o = new TestNamespace\PBEmpty\NestedMessage();
828        $n->setNestedMessage($o);
829        $n->setNestedEnum(TestNamespace\PBEmpty\NestedEnum::ZERO);
830        $m->setReservedName($n);
831        $this->assertSame($n, $m->getReservedName());
832        $this->assertSame($o, $n->getNestedMessage());
833        $this->assertSame(
834            TestNamespace\PBEmpty\NestedEnum::ZERO,
835            $n->getNestedEnum()
836        );
837    }
838
839    #########################################################
840    # Test prefix for reserved words.
841    #########################################################
842
843    public function testPrefixForReservedWords()
844    {
845        $m = new \Foo\TestMessage\PBEmpty();
846        $m = new \Foo\PBEmpty();
847        $m = new \PrefixEmpty();
848        $m = new \Foo\PBARRAY();
849
850        $m = new \Lower\PBabstract();
851        $m = new \Lower\PBand();
852        $m = new \Lower\PBarray();
853        $m = new \Lower\PBas();
854        $m = new \Lower\PBbreak();
855        $m = new \Lower\PBcallable();
856        $m = new \Lower\PBcase();
857        $m = new \Lower\PBcatch();
858        $m = new \Lower\PBclass();
859        $m = new \Lower\PBclone();
860        $m = new \Lower\PBconst();
861        $m = new \Lower\PBcontinue();
862        $m = new \Lower\PBdeclare();
863        $m = new \Lower\PBdefault();
864        $m = new \Lower\PBdie();
865        $m = new \Lower\PBdo();
866        $m = new \Lower\PBecho();
867        $m = new \Lower\PBelse();
868        $m = new \Lower\PBelseif();
869        $m = new \Lower\PBempty();
870        $m = new \Lower\PBenddeclare();
871        $m = new \Lower\PBendfor();
872        $m = new \Lower\PBendforeach();
873        $m = new \Lower\PBendif();
874        $m = new \Lower\PBendswitch();
875        $m = new \Lower\PBendwhile();
876        $m = new \Lower\PBeval();
877        $m = new \Lower\PBexit();
878        $m = new \Lower\PBextends();
879        $m = new \Lower\PBfinal();
880        $m = new \Lower\PBfor();
881        $m = new \Lower\PBforeach();
882        $m = new \Lower\PBfunction();
883        $m = new \Lower\PBglobal();
884        $m = new \Lower\PBgoto();
885        $m = new \Lower\PBif();
886        $m = new \Lower\PBimplements();
887        $m = new \Lower\PBinclude();
888        $m = new \Lower\PBinclude_once();
889        $m = new \Lower\PBinstanceof();
890        $m = new \Lower\PBinsteadof();
891        $m = new \Lower\PBinterface();
892        $m = new \Lower\PBisset();
893        $m = new \Lower\PBlist();
894        $m = new \Lower\PBnamespace();
895        $m = new \Lower\PBnew();
896        $m = new \Lower\PBor();
897        $m = new \Lower\PBprint();
898        $m = new \Lower\PBprivate();
899        $m = new \Lower\PBprotected();
900        $m = new \Lower\PBpublic();
901        $m = new \Lower\PBrequire();
902        $m = new \Lower\PBrequire_once();
903        $m = new \Lower\PBreturn();
904        $m = new \Lower\PBstatic();
905        $m = new \Lower\PBswitch();
906        $m = new \Lower\PBthrow();
907        $m = new \Lower\PBtrait();
908        $m = new \Lower\PBtry();
909        $m = new \Lower\PBunset();
910        $m = new \Lower\PBuse();
911        $m = new \Lower\PBvar();
912        $m = new \Lower\PBwhile();
913        $m = new \Lower\PBxor();
914        $m = new \Lower\PBint();
915        $m = new \Lower\PBfloat();
916        $m = new \Lower\PBbool();
917        $m = new \Lower\PBstring();
918        $m = new \Lower\PBtrue();
919        $m = new \Lower\PBfalse();
920        $m = new \Lower\PBnull();
921        $m = new \Lower\PBvoid();
922        $m = new \Lower\PBiterable();
923
924        $m = new \Upper\PBABSTRACT();
925        $m = new \Upper\PBAND();
926        $m = new \Upper\PBARRAY();
927        $m = new \Upper\PBAS();
928        $m = new \Upper\PBBREAK();
929        $m = new \Upper\PBCALLABLE();
930        $m = new \Upper\PBCASE();
931        $m = new \Upper\PBCATCH();
932        $m = new \Upper\PBCLASS();
933        $m = new \Upper\PBCLONE();
934        $m = new \Upper\PBCONST();
935        $m = new \Upper\PBCONTINUE();
936        $m = new \Upper\PBDECLARE();
937        $m = new \Upper\PBDEFAULT();
938        $m = new \Upper\PBDIE();
939        $m = new \Upper\PBDO();
940        $m = new \Upper\PBECHO();
941        $m = new \Upper\PBELSE();
942        $m = new \Upper\PBELSEIF();
943        $m = new \Upper\PBEMPTY();
944        $m = new \Upper\PBENDDECLARE();
945        $m = new \Upper\PBENDFOR();
946        $m = new \Upper\PBENDFOREACH();
947        $m = new \Upper\PBENDIF();
948        $m = new \Upper\PBENDSWITCH();
949        $m = new \Upper\PBENDWHILE();
950        $m = new \Upper\PBEVAL();
951        $m = new \Upper\PBEXIT();
952        $m = new \Upper\PBEXTENDS();
953        $m = new \Upper\PBFINAL();
954        $m = new \Upper\PBFOR();
955        $m = new \Upper\PBFOREACH();
956        $m = new \Upper\PBFUNCTION();
957        $m = new \Upper\PBGLOBAL();
958        $m = new \Upper\PBGOTO();
959        $m = new \Upper\PBIF();
960        $m = new \Upper\PBIMPLEMENTS();
961        $m = new \Upper\PBINCLUDE();
962        $m = new \Upper\PBINCLUDE_ONCE();
963        $m = new \Upper\PBINSTANCEOF();
964        $m = new \Upper\PBINSTEADOF();
965        $m = new \Upper\PBINTERFACE();
966        $m = new \Upper\PBISSET();
967        $m = new \Upper\PBLIST();
968        $m = new \Upper\PBNAMESPACE();
969        $m = new \Upper\PBNEW();
970        $m = new \Upper\PBOR();
971        $m = new \Upper\PBPRINT();
972        $m = new \Upper\PBPRIVATE();
973        $m = new \Upper\PBPROTECTED();
974        $m = new \Upper\PBPUBLIC();
975        $m = new \Upper\PBREQUIRE();
976        $m = new \Upper\PBREQUIRE_ONCE();
977        $m = new \Upper\PBRETURN();
978        $m = new \Upper\PBSTATIC();
979        $m = new \Upper\PBSWITCH();
980        $m = new \Upper\PBTHROW();
981        $m = new \Upper\PBTRAIT();
982        $m = new \Upper\PBTRY();
983        $m = new \Upper\PBUNSET();
984        $m = new \Upper\PBUSE();
985        $m = new \Upper\PBVAR();
986        $m = new \Upper\PBWHILE();
987        $m = new \Upper\PBXOR();
988        $m = new \Upper\PBINT();
989        $m = new \Upper\PBFLOAT();
990        $m = new \Upper\PBBOOL();
991        $m = new \Upper\PBSTRING();
992        $m = new \Upper\PBTRUE();
993        $m = new \Upper\PBFALSE();
994        $m = new \Upper\PBNULL();
995        $m = new \Upper\PBVOID();
996        $m = new \Upper\PBITERABLE();
997
998        $m = new \Lower_enum\PBabstract();
999        $m = new \Lower_enum\PBand();
1000        $m = new \Lower_enum\PBarray();
1001        $m = new \Lower_enum\PBas();
1002        $m = new \Lower_enum\PBbreak();
1003        $m = new \Lower_enum\PBcallable();
1004        $m = new \Lower_enum\PBcase();
1005        $m = new \Lower_enum\PBcatch();
1006        $m = new \Lower_enum\PBclass();
1007        $m = new \Lower_enum\PBclone();
1008        $m = new \Lower_enum\PBconst();
1009        $m = new \Lower_enum\PBcontinue();
1010        $m = new \Lower_enum\PBdeclare();
1011        $m = new \Lower_enum\PBdefault();
1012        $m = new \Lower_enum\PBdie();
1013        $m = new \Lower_enum\PBdo();
1014        $m = new \Lower_enum\PBecho();
1015        $m = new \Lower_enum\PBelse();
1016        $m = new \Lower_enum\PBelseif();
1017        $m = new \Lower_enum\PBempty();
1018        $m = new \Lower_enum\PBenddeclare();
1019        $m = new \Lower_enum\PBendfor();
1020        $m = new \Lower_enum\PBendforeach();
1021        $m = new \Lower_enum\PBendif();
1022        $m = new \Lower_enum\PBendswitch();
1023        $m = new \Lower_enum\PBendwhile();
1024        $m = new \Lower_enum\PBeval();
1025        $m = new \Lower_enum\PBexit();
1026        $m = new \Lower_enum\PBextends();
1027        $m = new \Lower_enum\PBfinal();
1028        $m = new \Lower_enum\PBfor();
1029        $m = new \Lower_enum\PBforeach();
1030        $m = new \Lower_enum\PBfunction();
1031        $m = new \Lower_enum\PBglobal();
1032        $m = new \Lower_enum\PBgoto();
1033        $m = new \Lower_enum\PBif();
1034        $m = new \Lower_enum\PBimplements();
1035        $m = new \Lower_enum\PBinclude();
1036        $m = new \Lower_enum\PBinclude_once();
1037        $m = new \Lower_enum\PBinstanceof();
1038        $m = new \Lower_enum\PBinsteadof();
1039        $m = new \Lower_enum\PBinterface();
1040        $m = new \Lower_enum\PBisset();
1041        $m = new \Lower_enum\PBlist();
1042        $m = new \Lower_enum\PBnamespace();
1043        $m = new \Lower_enum\PBnew();
1044        $m = new \Lower_enum\PBor();
1045        $m = new \Lower_enum\PBprint();
1046        $m = new \Lower_enum\PBprivate();
1047        $m = new \Lower_enum\PBprotected();
1048        $m = new \Lower_enum\PBpublic();
1049        $m = new \Lower_enum\PBrequire();
1050        $m = new \Lower_enum\PBrequire_once();
1051        $m = new \Lower_enum\PBreturn();
1052        $m = new \Lower_enum\PBstatic();
1053        $m = new \Lower_enum\PBswitch();
1054        $m = new \Lower_enum\PBthrow();
1055        $m = new \Lower_enum\PBtrait();
1056        $m = new \Lower_enum\PBtry();
1057        $m = new \Lower_enum\PBunset();
1058        $m = new \Lower_enum\PBuse();
1059        $m = new \Lower_enum\PBvar();
1060        $m = new \Lower_enum\PBwhile();
1061        $m = new \Lower_enum\PBxor();
1062        $m = new \Lower_enum\PBint();
1063        $m = new \Lower_enum\PBfloat();
1064        $m = new \Lower_enum\PBbool();
1065        $m = new \Lower_enum\PBstring();
1066        $m = new \Lower_enum\PBtrue();
1067        $m = new \Lower_enum\PBfalse();
1068        $m = new \Lower_enum\PBnull();
1069        $m = new \Lower_enum\PBvoid();
1070        $m = new \Lower_enum\PBiterable();
1071
1072        $m = new \Upper_enum\PBABSTRACT();
1073        $m = new \Upper_enum\PBAND();
1074        $m = new \Upper_enum\PBARRAY();
1075        $m = new \Upper_enum\PBAS();
1076        $m = new \Upper_enum\PBBREAK();
1077        $m = new \Upper_enum\PBCALLABLE();
1078        $m = new \Upper_enum\PBCASE();
1079        $m = new \Upper_enum\PBCATCH();
1080        $m = new \Upper_enum\PBCLASS();
1081        $m = new \Upper_enum\PBCLONE();
1082        $m = new \Upper_enum\PBCONST();
1083        $m = new \Upper_enum\PBCONTINUE();
1084        $m = new \Upper_enum\PBDECLARE();
1085        $m = new \Upper_enum\PBDEFAULT();
1086        $m = new \Upper_enum\PBDIE();
1087        $m = new \Upper_enum\PBDO();
1088        $m = new \Upper_enum\PBECHO();
1089        $m = new \Upper_enum\PBELSE();
1090        $m = new \Upper_enum\PBELSEIF();
1091        $m = new \Upper_enum\PBEMPTY();
1092        $m = new \Upper_enum\PBENDDECLARE();
1093        $m = new \Upper_enum\PBENDFOR();
1094        $m = new \Upper_enum\PBENDFOREACH();
1095        $m = new \Upper_enum\PBENDIF();
1096        $m = new \Upper_enum\PBENDSWITCH();
1097        $m = new \Upper_enum\PBENDWHILE();
1098        $m = new \Upper_enum\PBEVAL();
1099        $m = new \Upper_enum\PBEXIT();
1100        $m = new \Upper_enum\PBEXTENDS();
1101        $m = new \Upper_enum\PBFINAL();
1102        $m = new \Upper_enum\PBFOR();
1103        $m = new \Upper_enum\PBFOREACH();
1104        $m = new \Upper_enum\PBFUNCTION();
1105        $m = new \Upper_enum\PBGLOBAL();
1106        $m = new \Upper_enum\PBGOTO();
1107        $m = new \Upper_enum\PBIF();
1108        $m = new \Upper_enum\PBIMPLEMENTS();
1109        $m = new \Upper_enum\PBINCLUDE();
1110        $m = new \Upper_enum\PBINCLUDE_ONCE();
1111        $m = new \Upper_enum\PBINSTANCEOF();
1112        $m = new \Upper_enum\PBINSTEADOF();
1113        $m = new \Upper_enum\PBINTERFACE();
1114        $m = new \Upper_enum\PBISSET();
1115        $m = new \Upper_enum\PBLIST();
1116        $m = new \Upper_enum\PBNAMESPACE();
1117        $m = new \Upper_enum\PBNEW();
1118        $m = new \Upper_enum\PBOR();
1119        $m = new \Upper_enum\PBPRINT();
1120        $m = new \Upper_enum\PBPRIVATE();
1121        $m = new \Upper_enum\PBPROTECTED();
1122        $m = new \Upper_enum\PBPUBLIC();
1123        $m = new \Upper_enum\PBREQUIRE();
1124        $m = new \Upper_enum\PBREQUIRE_ONCE();
1125        $m = new \Upper_enum\PBRETURN();
1126        $m = new \Upper_enum\PBSTATIC();
1127        $m = new \Upper_enum\PBSWITCH();
1128        $m = new \Upper_enum\PBTHROW();
1129        $m = new \Upper_enum\PBTRAIT();
1130        $m = new \Upper_enum\PBTRY();
1131        $m = new \Upper_enum\PBUNSET();
1132        $m = new \Upper_enum\PBUSE();
1133        $m = new \Upper_enum\PBVAR();
1134        $m = new \Upper_enum\PBWHILE();
1135        $m = new \Upper_enum\PBXOR();
1136        $m = new \Upper_enum\PBINT();
1137        $m = new \Upper_enum\PBFLOAT();
1138        $m = new \Upper_enum\PBBOOL();
1139        $m = new \Upper_enum\PBSTRING();
1140        $m = new \Upper_enum\PBTRUE();
1141        $m = new \Upper_enum\PBFALSE();
1142        $m = new \Upper_enum\PBNULL();
1143        $m = new \Upper_enum\PBVOID();
1144        $m = new \Upper_enum\PBITERABLE();
1145
1146        $m = \Lower_enum_value\NotAllowed::PBabstract;
1147        $m = \Lower_enum_value\NotAllowed::PBand;
1148        $m = \Lower_enum_value\NotAllowed::PBarray;
1149        $m = \Lower_enum_value\NotAllowed::PBas;
1150        $m = \Lower_enum_value\NotAllowed::PBbreak;
1151        $m = \Lower_enum_value\NotAllowed::PBcallable;
1152        $m = \Lower_enum_value\NotAllowed::PBcase;
1153        $m = \Lower_enum_value\NotAllowed::PBcatch;
1154        $m = \Lower_enum_value\NotAllowed::PBclass;
1155        $m = \Lower_enum_value\NotAllowed::PBclone;
1156        $m = \Lower_enum_value\NotAllowed::PBconst;
1157        $m = \Lower_enum_value\NotAllowed::PBcontinue;
1158        $m = \Lower_enum_value\NotAllowed::PBdeclare;
1159        $m = \Lower_enum_value\NotAllowed::PBdefault;
1160        $m = \Lower_enum_value\NotAllowed::PBdie;
1161        $m = \Lower_enum_value\NotAllowed::PBdo;
1162        $m = \Lower_enum_value\NotAllowed::PBecho;
1163        $m = \Lower_enum_value\NotAllowed::PBelse;
1164        $m = \Lower_enum_value\NotAllowed::PBelseif;
1165        $m = \Lower_enum_value\NotAllowed::PBempty;
1166        $m = \Lower_enum_value\NotAllowed::PBenddeclare;
1167        $m = \Lower_enum_value\NotAllowed::PBendfor;
1168        $m = \Lower_enum_value\NotAllowed::PBendforeach;
1169        $m = \Lower_enum_value\NotAllowed::PBendif;
1170        $m = \Lower_enum_value\NotAllowed::PBendswitch;
1171        $m = \Lower_enum_value\NotAllowed::PBendwhile;
1172        $m = \Lower_enum_value\NotAllowed::PBeval;
1173        $m = \Lower_enum_value\NotAllowed::PBexit;
1174        $m = \Lower_enum_value\NotAllowed::PBextends;
1175        $m = \Lower_enum_value\NotAllowed::PBfinal;
1176        $m = \Lower_enum_value\NotAllowed::PBfor;
1177        $m = \Lower_enum_value\NotAllowed::PBforeach;
1178        $m = \Lower_enum_value\NotAllowed::PBfunction;
1179        $m = \Lower_enum_value\NotAllowed::PBglobal;
1180        $m = \Lower_enum_value\NotAllowed::PBgoto;
1181        $m = \Lower_enum_value\NotAllowed::PBif;
1182        $m = \Lower_enum_value\NotAllowed::PBimplements;
1183        $m = \Lower_enum_value\NotAllowed::PBinclude;
1184        $m = \Lower_enum_value\NotAllowed::PBinclude_once;
1185        $m = \Lower_enum_value\NotAllowed::PBinstanceof;
1186        $m = \Lower_enum_value\NotAllowed::PBinsteadof;
1187        $m = \Lower_enum_value\NotAllowed::PBinterface;
1188        $m = \Lower_enum_value\NotAllowed::PBisset;
1189        $m = \Lower_enum_value\NotAllowed::PBlist;
1190        $m = \Lower_enum_value\NotAllowed::PBnamespace;
1191        $m = \Lower_enum_value\NotAllowed::PBnew;
1192        $m = \Lower_enum_value\NotAllowed::PBor;
1193        $m = \Lower_enum_value\NotAllowed::PBprint;
1194        $m = \Lower_enum_value\NotAllowed::PBprivate;
1195        $m = \Lower_enum_value\NotAllowed::PBprotected;
1196        $m = \Lower_enum_value\NotAllowed::PBpublic;
1197        $m = \Lower_enum_value\NotAllowed::PBrequire;
1198        $m = \Lower_enum_value\NotAllowed::PBrequire_once;
1199        $m = \Lower_enum_value\NotAllowed::PBreturn;
1200        $m = \Lower_enum_value\NotAllowed::PBstatic;
1201        $m = \Lower_enum_value\NotAllowed::PBswitch;
1202        $m = \Lower_enum_value\NotAllowed::PBthrow;
1203        $m = \Lower_enum_value\NotAllowed::PBtrait;
1204        $m = \Lower_enum_value\NotAllowed::PBtry;
1205        $m = \Lower_enum_value\NotAllowed::PBunset;
1206        $m = \Lower_enum_value\NotAllowed::PBuse;
1207        $m = \Lower_enum_value\NotAllowed::PBvar;
1208        $m = \Lower_enum_value\NotAllowed::PBwhile;
1209        $m = \Lower_enum_value\NotAllowed::PBxor;
1210        $m = \Lower_enum_value\NotAllowed::int;
1211        $m = \Lower_enum_value\NotAllowed::float;
1212        $m = \Lower_enum_value\NotAllowed::bool;
1213        $m = \Lower_enum_value\NotAllowed::string;
1214        $m = \Lower_enum_value\NotAllowed::true;
1215        $m = \Lower_enum_value\NotAllowed::false;
1216        $m = \Lower_enum_value\NotAllowed::null;
1217        $m = \Lower_enum_value\NotAllowed::void;
1218        $m = \Lower_enum_value\NotAllowed::iterable;
1219
1220        $m = \Upper_enum_value\NotAllowed::PBABSTRACT;
1221        $m = \Upper_enum_value\NotAllowed::PBAND;
1222        $m = \Upper_enum_value\NotAllowed::PBARRAY;
1223        $m = \Upper_enum_value\NotAllowed::PBAS;
1224        $m = \Upper_enum_value\NotAllowed::PBBREAK;
1225        $m = \Upper_enum_value\NotAllowed::PBCALLABLE;
1226        $m = \Upper_enum_value\NotAllowed::PBCASE;
1227        $m = \Upper_enum_value\NotAllowed::PBCATCH;
1228        $m = \Upper_enum_value\NotAllowed::PBCLASS;
1229        $m = \Upper_enum_value\NotAllowed::PBCLONE;
1230        $m = \Upper_enum_value\NotAllowed::PBCONST;
1231        $m = \Upper_enum_value\NotAllowed::PBCONTINUE;
1232        $m = \Upper_enum_value\NotAllowed::PBDECLARE;
1233        $m = \Upper_enum_value\NotAllowed::PBDEFAULT;
1234        $m = \Upper_enum_value\NotAllowed::PBDIE;
1235        $m = \Upper_enum_value\NotAllowed::PBDO;
1236        $m = \Upper_enum_value\NotAllowed::PBECHO;
1237        $m = \Upper_enum_value\NotAllowed::PBELSE;
1238        $m = \Upper_enum_value\NotAllowed::PBELSEIF;
1239        $m = \Upper_enum_value\NotAllowed::PBEMPTY;
1240        $m = \Upper_enum_value\NotAllowed::PBENDDECLARE;
1241        $m = \Upper_enum_value\NotAllowed::PBENDFOR;
1242        $m = \Upper_enum_value\NotAllowed::PBENDFOREACH;
1243        $m = \Upper_enum_value\NotAllowed::PBENDIF;
1244        $m = \Upper_enum_value\NotAllowed::PBENDSWITCH;
1245        $m = \Upper_enum_value\NotAllowed::PBENDWHILE;
1246        $m = \Upper_enum_value\NotAllowed::PBEVAL;
1247        $m = \Upper_enum_value\NotAllowed::PBEXIT;
1248        $m = \Upper_enum_value\NotAllowed::PBEXTENDS;
1249        $m = \Upper_enum_value\NotAllowed::PBFINAL;
1250        $m = \Upper_enum_value\NotAllowed::PBFOR;
1251        $m = \Upper_enum_value\NotAllowed::PBFOREACH;
1252        $m = \Upper_enum_value\NotAllowed::PBFUNCTION;
1253        $m = \Upper_enum_value\NotAllowed::PBGLOBAL;
1254        $m = \Upper_enum_value\NotAllowed::PBGOTO;
1255        $m = \Upper_enum_value\NotAllowed::PBIF;
1256        $m = \Upper_enum_value\NotAllowed::PBIMPLEMENTS;
1257        $m = \Upper_enum_value\NotAllowed::PBINCLUDE;
1258        $m = \Upper_enum_value\NotAllowed::PBINCLUDE_ONCE;
1259        $m = \Upper_enum_value\NotAllowed::PBINSTANCEOF;
1260        $m = \Upper_enum_value\NotAllowed::PBINSTEADOF;
1261        $m = \Upper_enum_value\NotAllowed::PBINTERFACE;
1262        $m = \Upper_enum_value\NotAllowed::PBISSET;
1263        $m = \Upper_enum_value\NotAllowed::PBLIST;
1264        $m = \Upper_enum_value\NotAllowed::PBNAMESPACE;
1265        $m = \Upper_enum_value\NotAllowed::PBNEW;
1266        $m = \Upper_enum_value\NotAllowed::PBOR;
1267        $m = \Upper_enum_value\NotAllowed::PBPRINT;
1268        $m = \Upper_enum_value\NotAllowed::PBPRIVATE;
1269        $m = \Upper_enum_value\NotAllowed::PBPROTECTED;
1270        $m = \Upper_enum_value\NotAllowed::PBPUBLIC;
1271        $m = \Upper_enum_value\NotAllowed::PBREQUIRE;
1272        $m = \Upper_enum_value\NotAllowed::PBREQUIRE_ONCE;
1273        $m = \Upper_enum_value\NotAllowed::PBRETURN;
1274        $m = \Upper_enum_value\NotAllowed::PBSTATIC;
1275        $m = \Upper_enum_value\NotAllowed::PBSWITCH;
1276        $m = \Upper_enum_value\NotAllowed::PBTHROW;
1277        $m = \Upper_enum_value\NotAllowed::PBTRAIT;
1278        $m = \Upper_enum_value\NotAllowed::PBTRY;
1279        $m = \Upper_enum_value\NotAllowed::PBUNSET;
1280        $m = \Upper_enum_value\NotAllowed::PBUSE;
1281        $m = \Upper_enum_value\NotAllowed::PBVAR;
1282        $m = \Upper_enum_value\NotAllowed::PBWHILE;
1283        $m = \Upper_enum_value\NotAllowed::PBXOR;
1284        $m = \Upper_enum_value\NotAllowed::INT;
1285        $m = \Upper_enum_value\NotAllowed::FLOAT;
1286        $m = \Upper_enum_value\NotAllowed::BOOL;
1287        $m = \Upper_enum_value\NotAllowed::STRING;
1288        $m = \Upper_enum_value\NotAllowed::TRUE;
1289        $m = \Upper_enum_value\NotAllowed::FALSE;
1290        $m = \Upper_enum_value\NotAllowed::NULL;
1291        $m = \Upper_enum_value\NotAllowed::VOID;
1292        $m = \Upper_enum_value\NotAllowed::ITERABLE;
1293
1294        $this->assertTrue(true);
1295    }
1296
1297    #########################################################
1298    # Test fluent setters.
1299    #########################################################
1300
1301    public function testFluentSetters()
1302    {
1303        $m = (new TestMessage())
1304            ->setOptionalInt32(1)
1305            ->setOptionalUInt32(2);
1306        $this->assertSame(1, $m->getOptionalInt32());
1307        $this->assertSame(2, $m->getOptionalUInt32());
1308    }
1309
1310    #########################################################
1311    # Test Reverse Field Order.
1312    #########################################################
1313
1314    public function testReverseFieldOrder()
1315    {
1316        $m = new TestReverseFieldOrder();
1317        $m->setB("abc");
1318        $this->assertSame("abc", $m->getB());
1319        $this->assertNotSame("abc", $m->getA());
1320    }
1321
1322    #########################################################
1323    # Test Reverse Field Order.
1324    #########################################################
1325
1326    public function testLowerCase()
1327    {
1328        $m = new testLowerCaseMessage();
1329        $n = testLowerCaseEnum::VALUE;
1330        $this->assertTrue(true);
1331    }
1332
1333    #########################################################
1334    # Test Array Constructor.
1335    #########################################################
1336
1337    public function testArrayConstructor()
1338    {
1339        $m = new TestMessage([
1340            'optional_int32' => -42,
1341            'optional_int64' => -43,
1342            'optional_uint32' => 42,
1343            'optional_uint64' => 43,
1344            'optional_sint32' => -44,
1345            'optional_sint64' => -45,
1346            'optional_fixed32' => 46,
1347            'optional_fixed64' => 47,
1348            'optional_sfixed32' => -46,
1349            'optional_sfixed64' => -47,
1350            'optional_float' => 1.5,
1351            'optional_double' => 1.6,
1352            'optional_bool' => true,
1353            'optional_string' => 'a',
1354            'optional_bytes' => 'bbbb',
1355            'optional_enum' => TestEnum::ONE,
1356            'optional_message' => new Sub([
1357                'a' => 33
1358            ]),
1359            'repeated_int32' => [-42, -52],
1360            'repeated_int64' => [-43, -53],
1361            'repeated_uint32' => [42, 52],
1362            'repeated_uint64' => [43, 53],
1363            'repeated_sint32' => [-44, -54],
1364            'repeated_sint64' => [-45, -55],
1365            'repeated_fixed32' => [46, 56],
1366            'repeated_fixed64' => [47, 57],
1367            'repeated_sfixed32' => [-46, -56],
1368            'repeated_sfixed64' => [-47, -57],
1369            'repeated_float' => [1.5, 2.5],
1370            'repeated_double' => [1.6, 2.6],
1371            'repeated_bool' => [true, false],
1372            'repeated_string' => ['a', 'c'],
1373            'repeated_bytes' => ['bbbb', 'dddd'],
1374            'repeated_enum' => [TestEnum::ZERO, TestEnum::ONE],
1375            'repeated_message' => [new Sub(['a' => 34]),
1376                                   new Sub(['a' => 35])],
1377            'map_int32_int32' => [-62 => -62],
1378            'map_int64_int64' => [-63 => -63],
1379            'map_uint32_uint32' => [62 => 62],
1380            'map_uint64_uint64' => [63 => 63],
1381            'map_sint32_sint32' => [-64 => -64],
1382            'map_sint64_sint64' => [-65 => -65],
1383            'map_fixed32_fixed32' => [66 => 66],
1384            'map_fixed64_fixed64' => [67 => 67],
1385            'map_sfixed32_sfixed32' => [-68 => -68],
1386            'map_sfixed64_sfixed64' => [-69 => -69],
1387            'map_int32_float' => [1 => 3.5],
1388            'map_int32_double' => [1 => 3.6],
1389            'map_bool_bool' => [true => true],
1390            'map_string_string' => ['e' => 'e'],
1391            'map_int32_bytes' => [1 => 'ffff'],
1392            'map_int32_enum' => [1 => TestEnum::ONE],
1393            'map_int32_message' => [1 => new Sub(['a' => 36])],
1394        ]);
1395
1396        TestUtil::assertTestMessage($m);
1397        $this->assertTrue(true);
1398    }
1399
1400    public function testReferenceInArrayConstructor()
1401    {
1402        $keys = [[
1403                    'optional_bool' => true,
1404                    'repeated_bool' => [true],
1405                    'map_bool_bool' => [true => true],
1406                    'optional_double' => 1.0,
1407                    'repeated_double' => [1.0],
1408                    'map_int32_double' => [1 => 1.0],
1409                    'optional_int32' => 1,
1410                    'repeated_int32' => [1],
1411                    'map_int32_int32' => [1 => 1],
1412                    'optional_string' => 'a',
1413                    'repeated_string' => ['a'],
1414                    'map_string_string' => ['a' => 'a'],
1415                    'optional_message' => ['a' => 1],
1416                    'repeated_message' => [['a' => 1]],
1417                    'map_int32_message' => [1 => ['a' => 1]],
1418                ]];
1419
1420        foreach ($keys as &$key) {
1421            foreach ($key as $id => &$value) {
1422                if ($id === 'repeated_bool') {
1423                    foreach ($value as &$element) {
1424                    }
1425                }
1426                if ($id === 'map_bool_bool') {
1427                    foreach ($value as $mapKey => &$element) {
1428                    }
1429                }
1430                if ($id === 'repeated_double') {
1431                    foreach ($value as &$element) {
1432                    }
1433                }
1434                if ($id === 'map_int32_double') {
1435                    foreach ($value as $mapKey => &$element) {
1436                    }
1437                }
1438                if ($id === 'repeated_int32') {
1439                    foreach ($value as &$element) {
1440                    }
1441                }
1442                if ($id === 'map_int32_int32') {
1443                    foreach ($value as $mapKey => &$element) {
1444                    }
1445                }
1446                if ($id === 'repeated_string') {
1447                    foreach ($value as &$element) {
1448                    }
1449                }
1450                if ($id === 'map_string_string') {
1451                    foreach ($value as $mapKey => &$element) {
1452                    }
1453                }
1454                if ($id === 'optional_message') {
1455                    $value = new Sub($value);
1456                }
1457                if ($id === 'repeated_message') {
1458                    foreach ($value as &$element) {
1459                        $element = new Sub($element);
1460                    }
1461                }
1462                if ($id === 'map_int32_message') {
1463                    foreach ($value as $mapKey => &$element) {
1464                        $element = new Sub($element);
1465                    }
1466                }
1467            }
1468            $key = new TestMessage($key);
1469        }
1470    }
1471
1472    public function testOneofMessageInArrayConstructor()
1473    {
1474        $m = new TestMessage([
1475            'oneof_message' => new Sub(),
1476        ]);
1477        $this->assertSame('oneof_message', $m->getMyOneof());
1478        $this->assertNotNull($m->getOneofMessage());
1479    }
1480
1481    public function testOneofStringInArrayConstructor()
1482    {
1483        $m = new TestMessage([
1484            'oneof_string' => 'abc',
1485        ]);
1486    }
1487
1488    #########################################################
1489    # Test message equals.
1490    #########################################################
1491
1492    public function testMessageEquals()
1493    {
1494        $m = new TestMessage();
1495        TestUtil::setTestMessage($m);
1496        $n = new TestMessage();
1497        TestUtil::setTestMessage($n);
1498        $this->assertEquals($m, $n);
1499    }
1500
1501    #########################################################
1502    # Test reference of value
1503    #########################################################
1504
1505    public function testValueIsReference()
1506    {
1507        // Bool element
1508        $values = [true];
1509        array_walk($values, function (&$value) {});
1510        $m = new TestMessage();
1511        $m->setOptionalBool($values[0]);
1512
1513        // Int32 element
1514        $values = [1];
1515        array_walk($values, function (&$value) {});
1516        $m = new TestMessage();
1517        $m->setOptionalInt32($values[0]);
1518
1519        // Double element
1520        $values = [1.0];
1521        array_walk($values, function (&$value) {});
1522        $m = new TestMessage();
1523        $m->setOptionalDouble($values[0]);
1524
1525        // String element
1526        $values = ['a'];
1527        array_walk($values, function (&$value) {});
1528        $m = new TestMessage();
1529        $m->setOptionalString($values[0]);
1530    }
1531
1532    #########################################################
1533    # Test no segfault when error happens
1534    #########################################################
1535
1536    function throwIntendedException()
1537    {
1538        throw new Exception('Intended');
1539    }
1540
1541    /**
1542     * @expectedException Exception
1543     */
1544    public function testNoSegfaultWithError()
1545    {
1546        new TestMessage(['optional_int32' => $this->throwIntendedException()]);
1547    }
1548
1549    public function testNoExceptionWithVarDump()
1550    {
1551        $m = new Sub(['a' => 1]);
1552        /*
1553         * This line currently segfaults on macOS with:
1554         *
1555         *    frame #0: 0x00000001029936cc xdebug.so`xdebug_zend_hash_is_recursive + 4
1556         *    frame #1: 0x00000001029a6736 xdebug.so`xdebug_var_export_text_ansi + 1006
1557         *    frame #2: 0x00000001029a715d xdebug.so`xdebug_get_zval_value_text_ansi + 273
1558         *    frame #3: 0x000000010298a441 xdebug.so`zif_xdebug_var_dump + 297
1559         *    frame #4: 0x000000010298d558 xdebug.so`xdebug_execute_internal + 640
1560         *    frame #5: 0x000000010046d47f php`ZEND_DO_FCALL_SPEC_RETVAL_UNUSED_HANDLER + 364
1561         *    frame #6: 0x000000010043cabc php`execute_ex + 44
1562         *    frame #7: 0x000000010298d151 xdebug.so`xdebug_execute_ex + 1662
1563         *    frame #8: 0x000000010046d865 php`ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER + 426
1564         *
1565         * The value we are passing to var_dump() appears to be corrupt somehow.
1566         */
1567        /* var_dump($m); */
1568    }
1569}
1570