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