generated_class_test.php 53 KB

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