generated_class_test.php 49 KB

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