GeneratedClassTest.php 66 KB

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