generated_class_test.php 56 KB

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