generated_class_test.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. $this->assertSame('oneof_message', $m->getMyOneof());
  1293. $this->assertNotNull($m->getOneofMessage());
  1294. }
  1295. public function testOneofStringInArrayConstructor()
  1296. {
  1297. $m = new TestMessage([
  1298. 'oneof_string' => 'abc',
  1299. ]);
  1300. }
  1301. #########################################################
  1302. # Test message equals.
  1303. #########################################################
  1304. public function testMessageEquals()
  1305. {
  1306. $m = new TestMessage();
  1307. TestUtil::setTestMessage($m);
  1308. $n = new TestMessage();
  1309. TestUtil::setTestMessage($n);
  1310. $this->assertEquals($m, $n);
  1311. }
  1312. #########################################################
  1313. # Test reference of value
  1314. #########################################################
  1315. public function testValueIsReference()
  1316. {
  1317. // Bool element
  1318. $values = [true];
  1319. array_walk($values, function (&$value) {});
  1320. $m = new TestMessage();
  1321. $m->setOptionalBool($values[0]);
  1322. // Int32 element
  1323. $values = [1];
  1324. array_walk($values, function (&$value) {});
  1325. $m = new TestMessage();
  1326. $m->setOptionalInt32($values[0]);
  1327. // Double element
  1328. $values = [1.0];
  1329. array_walk($values, function (&$value) {});
  1330. $m = new TestMessage();
  1331. $m->setOptionalDouble($values[0]);
  1332. // String element
  1333. $values = ['a'];
  1334. array_walk($values, function (&$value) {});
  1335. $m = new TestMessage();
  1336. $m->setOptionalString($values[0]);
  1337. }
  1338. #########################################################
  1339. # Test no segfault when error happens
  1340. #########################################################
  1341. function throwIntendedException()
  1342. {
  1343. throw new Exception('Intended');
  1344. }
  1345. /**
  1346. * @expectedException Exception
  1347. */
  1348. public function testNoSegfaultWithError()
  1349. {
  1350. new TestMessage(['optional_int32' => $this->throwIntendedException()]);
  1351. }
  1352. public function testNoExceptionWithVarDump()
  1353. {
  1354. $m = new Sub(['a' => 1]);
  1355. var_dump($m);
  1356. }
  1357. }