test_base.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. use Foo\TestMessage;
  3. use Foo\TestEnum;
  4. use Foo\TestMessage\Sub;
  5. class TestBase extends \PHPUnit\Framework\TestCase
  6. {
  7. public function setFields(TestMessage $m)
  8. {
  9. TestUtil::setTestMessage($m);
  10. }
  11. /**
  12. * Polyfill for phpunit6.
  13. */
  14. static public function assertStringContains($needle, $haystack)
  15. {
  16. if (function_exists('PHPUnit\Framework\assertStringContainsString')) {
  17. parent::assertStringContainsString($needle, $haystack);
  18. } else {
  19. parent::assertContains($needle, $haystack);
  20. }
  21. }
  22. /**
  23. * Polyfill for phpunit6.
  24. */
  25. static public function assertFloatEquals($expected, $actual, $delta)
  26. {
  27. if (function_exists('PHPUnit\Framework\assertEqualsWithDelta')) {
  28. parent::assertEqualsWithDelta($expected, $actual, $delta);
  29. } else {
  30. parent::assertEquals($expected, $actual, '', $delta);
  31. }
  32. }
  33. public function setFields2(TestMessage $m)
  34. {
  35. TestUtil::setTestMessage2($m);
  36. }
  37. public function expectFields(TestMessage $m)
  38. {
  39. $this->assertSame(-42, $m->getOptionalInt32());
  40. $this->assertSame(42, $m->getOptionalUint32());
  41. $this->assertSame(-44, $m->getOptionalSint32());
  42. $this->assertSame(46, $m->getOptionalFixed32());
  43. $this->assertSame(-46, $m->getOptionalSfixed32());
  44. $this->assertSame(1.5, $m->getOptionalFloat());
  45. $this->assertSame(1.6, $m->getOptionalDouble());
  46. $this->assertSame(true, $m->getOptionalBool());
  47. $this->assertSame('a', $m->getOptionalString());
  48. $this->assertSame('bbbb', $m->getOptionalBytes());
  49. $this->assertSame(TestEnum::ONE, $m->getOptionalEnum());
  50. $this->assertSame(33, $m->getOptionalMessage()->getA());
  51. if (PHP_INT_SIZE == 4) {
  52. $this->assertSame('-43', $m->getOptionalInt64());
  53. $this->assertSame('43', $m->getOptionalUint64());
  54. $this->assertSame('-45', $m->getOptionalSint64());
  55. $this->assertSame('47', $m->getOptionalFixed64());
  56. $this->assertSame('-47', $m->getOptionalSfixed64());
  57. } else {
  58. $this->assertSame(-43, $m->getOptionalInt64());
  59. $this->assertSame(43, $m->getOptionalUint64());
  60. $this->assertSame(-45, $m->getOptionalSint64());
  61. $this->assertSame(47, $m->getOptionalFixed64());
  62. $this->assertSame(-47, $m->getOptionalSfixed64());
  63. }
  64. $this->assertEquals(-42, $m->getRepeatedInt32()[0]);
  65. $this->assertEquals(42, $m->getRepeatedUint32()[0]);
  66. $this->assertEquals(-43, $m->getRepeatedInt64()[0]);
  67. $this->assertEquals(43, $m->getRepeatedUint64()[0]);
  68. $this->assertEquals(-44, $m->getRepeatedSint32()[0]);
  69. $this->assertEquals(-45, $m->getRepeatedSint64()[0]);
  70. $this->assertEquals(46, $m->getRepeatedFixed32()[0]);
  71. $this->assertEquals(47, $m->getRepeatedFixed64()[0]);
  72. $this->assertEquals(-46, $m->getRepeatedSfixed32()[0]);
  73. $this->assertEquals(-47, $m->getRepeatedSfixed64()[0]);
  74. $this->assertEquals(1.5, $m->getRepeatedFloat()[0]);
  75. $this->assertEquals(1.6, $m->getRepeatedDouble()[0]);
  76. $this->assertEquals(true, $m->getRepeatedBool()[0]);
  77. $this->assertEquals('a', $m->getRepeatedString()[0]);
  78. $this->assertEquals('bbbb', $m->getRepeatedBytes()[0]);
  79. $this->assertEquals(34, $m->getRepeatedMessage()[0]->GetA());
  80. $this->assertEquals(-52, $m->getRepeatedInt32()[1]);
  81. $this->assertEquals(52, $m->getRepeatedUint32()[1]);
  82. $this->assertEquals(-53, $m->getRepeatedInt64()[1]);
  83. $this->assertEquals(53, $m->getRepeatedUint64()[1]);
  84. $this->assertEquals(-54, $m->getRepeatedSint32()[1]);
  85. $this->assertEquals(-55, $m->getRepeatedSint64()[1]);
  86. $this->assertEquals(56, $m->getRepeatedFixed32()[1]);
  87. $this->assertEquals(57, $m->getRepeatedFixed64()[1]);
  88. $this->assertEquals(-56, $m->getRepeatedSfixed32()[1]);
  89. $this->assertEquals(-57, $m->getRepeatedSfixed64()[1]);
  90. $this->assertEquals(2.5, $m->getRepeatedFloat()[1]);
  91. $this->assertEquals(2.6, $m->getRepeatedDouble()[1]);
  92. $this->assertEquals(false, $m->getRepeatedBool()[1]);
  93. $this->assertEquals('c', $m->getRepeatedString()[1]);
  94. $this->assertEquals('dddd', $m->getRepeatedBytes()[1]);
  95. $this->assertEquals(35, $m->getRepeatedMessage()[1]->GetA());
  96. if (PHP_INT_SIZE == 4) {
  97. $this->assertEquals('-63', $m->getMapInt64Int64()['-63']);
  98. $this->assertEquals('63', $m->getMapUint64Uint64()['63']);
  99. $this->assertEquals('-65', $m->getMapSint64Sint64()['-65']);
  100. $this->assertEquals('67', $m->getMapFixed64Fixed64()['67']);
  101. $this->assertEquals('-69', $m->getMapSfixed64Sfixed64()['-69']);
  102. } else {
  103. $this->assertEquals(-63, $m->getMapInt64Int64()[-63]);
  104. $this->assertEquals(63, $m->getMapUint64Uint64()[63]);
  105. $this->assertEquals(-65, $m->getMapSint64Sint64()[-65]);
  106. $this->assertEquals(67, $m->getMapFixed64Fixed64()[67]);
  107. $this->assertEquals(-69, $m->getMapSfixed64Sfixed64()[-69]);
  108. }
  109. $this->assertEquals(-62, $m->getMapInt32Int32()[-62]);
  110. $this->assertEquals(62, $m->getMapUint32Uint32()[62]);
  111. $this->assertEquals(-64, $m->getMapSint32Sint32()[-64]);
  112. $this->assertEquals(66, $m->getMapFixed32Fixed32()[66]);
  113. $this->assertEquals(-68, $m->getMapSfixed32Sfixed32()[-68]);
  114. $this->assertEquals(3.5, $m->getMapInt32Float()[1]);
  115. $this->assertEquals(3.6, $m->getMapInt32Double()[1]);
  116. $this->assertEquals(true , $m->getMapBoolBool()[true]);
  117. $this->assertEquals('e', $m->getMapStringString()['e']);
  118. $this->assertEquals('ffff', $m->getMapInt32Bytes()[1]);
  119. $this->assertEquals(TestEnum::ONE, $m->getMapInt32Enum()[1]);
  120. $this->assertEquals(36, $m->getMapInt32Message()[1]->GetA());
  121. }
  122. // Test message merged from setFields and setFields2.
  123. public function expectFieldsMerged(TestMessage $m)
  124. {
  125. $this->assertSame(-144, $m->getOptionalSint32());
  126. $this->assertSame(146, $m->getOptionalFixed32());
  127. $this->assertSame(-146, $m->getOptionalSfixed32());
  128. $this->assertSame(11.5, $m->getOptionalFloat());
  129. $this->assertSame(11.6, $m->getOptionalDouble());
  130. $this->assertSame(true, $m->getOptionalBool());
  131. $this->assertSame('aa', $m->getOptionalString());
  132. $this->assertSame('bb', $m->getOptionalBytes());
  133. $this->assertSame(133, $m->getOptionalMessage()->getA());
  134. if (PHP_INT_SIZE == 4) {
  135. $this->assertSame('-143', $m->getOptionalInt64());
  136. $this->assertSame('143', $m->getOptionalUint64());
  137. $this->assertSame('-145', $m->getOptionalSint64());
  138. $this->assertSame('147', $m->getOptionalFixed64());
  139. $this->assertSame('-147', $m->getOptionalSfixed64());
  140. } else {
  141. $this->assertSame(-143, $m->getOptionalInt64());
  142. $this->assertSame(143, $m->getOptionalUint64());
  143. $this->assertSame(-145, $m->getOptionalSint64());
  144. $this->assertSame(147, $m->getOptionalFixed64());
  145. $this->assertSame(-147, $m->getOptionalSfixed64());
  146. }
  147. $this->assertEquals(-42, $m->getRepeatedInt32()[0]);
  148. $this->assertEquals(42, $m->getRepeatedUint32()[0]);
  149. $this->assertEquals(-43, $m->getRepeatedInt64()[0]);
  150. $this->assertEquals(43, $m->getRepeatedUint64()[0]);
  151. $this->assertEquals(-44, $m->getRepeatedSint32()[0]);
  152. $this->assertEquals(-45, $m->getRepeatedSint64()[0]);
  153. $this->assertEquals(46, $m->getRepeatedFixed32()[0]);
  154. $this->assertEquals(47, $m->getRepeatedFixed64()[0]);
  155. $this->assertEquals(-46, $m->getRepeatedSfixed32()[0]);
  156. $this->assertEquals(-47, $m->getRepeatedSfixed64()[0]);
  157. $this->assertEquals(1.5, $m->getRepeatedFloat()[0]);
  158. $this->assertEquals(1.6, $m->getRepeatedDouble()[0]);
  159. $this->assertEquals(true, $m->getRepeatedBool()[0]);
  160. $this->assertEquals('a', $m->getRepeatedString()[0]);
  161. $this->assertEquals('bbbb', $m->getRepeatedBytes()[0]);
  162. $this->assertEquals(TestEnum::ZERO, $m->getRepeatedEnum()[0]);
  163. $this->assertEquals(34, $m->getRepeatedMessage()[0]->GetA());
  164. $this->assertEquals(-52, $m->getRepeatedInt32()[1]);
  165. $this->assertEquals(52, $m->getRepeatedUint32()[1]);
  166. $this->assertEquals(-53, $m->getRepeatedInt64()[1]);
  167. $this->assertEquals(53, $m->getRepeatedUint64()[1]);
  168. $this->assertEquals(-54, $m->getRepeatedSint32()[1]);
  169. $this->assertEquals(-55, $m->getRepeatedSint64()[1]);
  170. $this->assertEquals(56, $m->getRepeatedFixed32()[1]);
  171. $this->assertEquals(57, $m->getRepeatedFixed64()[1]);
  172. $this->assertEquals(-56, $m->getRepeatedSfixed32()[1]);
  173. $this->assertEquals(-57, $m->getRepeatedSfixed64()[1]);
  174. $this->assertEquals(2.5, $m->getRepeatedFloat()[1]);
  175. $this->assertEquals(2.6, $m->getRepeatedDouble()[1]);
  176. $this->assertEquals(false, $m->getRepeatedBool()[1]);
  177. $this->assertEquals('c', $m->getRepeatedString()[1]);
  178. $this->assertEquals('dddd', $m->getRepeatedBytes()[1]);
  179. $this->assertEquals(TestEnum::ONE, $m->getRepeatedEnum()[1]);
  180. $this->assertEquals(35, $m->getRepeatedMessage()[1]->GetA());
  181. $this->assertEquals(-142, $m->getRepeatedInt32()[2]);
  182. $this->assertEquals(142, $m->getRepeatedUint32()[2]);
  183. $this->assertEquals(-143, $m->getRepeatedInt64()[2]);
  184. $this->assertEquals(143, $m->getRepeatedUint64()[2]);
  185. $this->assertEquals(-144, $m->getRepeatedSint32()[2]);
  186. $this->assertEquals(-145, $m->getRepeatedSint64()[2]);
  187. $this->assertEquals(146, $m->getRepeatedFixed32()[2]);
  188. $this->assertEquals(147, $m->getRepeatedFixed64()[2]);
  189. $this->assertEquals(-146, $m->getRepeatedSfixed32()[2]);
  190. $this->assertEquals(-147, $m->getRepeatedSfixed64()[2]);
  191. $this->assertEquals(11.5, $m->getRepeatedFloat()[2]);
  192. $this->assertEquals(11.6, $m->getRepeatedDouble()[2]);
  193. $this->assertEquals(false, $m->getRepeatedBool()[2]);
  194. $this->assertEquals('aa', $m->getRepeatedString()[2]);
  195. $this->assertEquals('bb', $m->getRepeatedBytes()[2]);
  196. $this->assertEquals(TestEnum::TWO, $m->getRepeatedEnum()[2]);
  197. $this->assertEquals(134, $m->getRepeatedMessage()[2]->GetA());
  198. if (PHP_INT_SIZE == 4) {
  199. $this->assertEquals('-163', $m->getMapInt64Int64()['-63']);
  200. $this->assertEquals('163', $m->getMapUint64Uint64()['63']);
  201. $this->assertEquals('-165', $m->getMapSint64Sint64()['-65']);
  202. $this->assertEquals('167', $m->getMapFixed64Fixed64()['67']);
  203. $this->assertEquals('-169', $m->getMapSfixed64Sfixed64()['-69']);
  204. } else {
  205. $this->assertEquals(-163, $m->getMapInt64Int64()[-63]);
  206. $this->assertEquals(163, $m->getMapUint64Uint64()[63]);
  207. $this->assertEquals(-165, $m->getMapSint64Sint64()[-65]);
  208. $this->assertEquals(167, $m->getMapFixed64Fixed64()[67]);
  209. $this->assertEquals(-169, $m->getMapSfixed64Sfixed64()[-69]);
  210. }
  211. $this->assertEquals(-162, $m->getMapInt32Int32()[-62]);
  212. $this->assertEquals(162, $m->getMapUint32Uint32()[62]);
  213. $this->assertEquals(-164, $m->getMapSint32Sint32()[-64]);
  214. $this->assertEquals(166, $m->getMapFixed32Fixed32()[66]);
  215. $this->assertEquals(-168, $m->getMapSfixed32Sfixed32()[-68]);
  216. $this->assertEquals(13.5, $m->getMapInt32Float()[1]);
  217. $this->assertEquals(13.6, $m->getMapInt32Double()[1]);
  218. $this->assertEquals(false , $m->getMapBoolBool()[true]);
  219. $this->assertEquals('ee', $m->getMapStringString()['e']);
  220. $this->assertEquals('ff', $m->getMapInt32Bytes()[1]);
  221. $this->assertEquals(TestEnum::TWO, $m->getMapInt32Enum()[1]);
  222. $this->assertEquals(136, $m->getMapInt32Message()[1]->GetA());
  223. if (PHP_INT_SIZE == 4) {
  224. $this->assertEquals('-163', $m->getMapInt64Int64()['-163']);
  225. $this->assertEquals('163', $m->getMapUint64Uint64()['163']);
  226. $this->assertEquals('-165', $m->getMapSint64Sint64()['-165']);
  227. $this->assertEquals('167', $m->getMapFixed64Fixed64()['167']);
  228. $this->assertEquals('-169', $m->getMapSfixed64Sfixed64()['-169']);
  229. } else {
  230. $this->assertEquals(-163, $m->getMapInt64Int64()[-163]);
  231. $this->assertEquals(163, $m->getMapUint64Uint64()[163]);
  232. $this->assertEquals(-165, $m->getMapSint64Sint64()[-165]);
  233. $this->assertEquals(167, $m->getMapFixed64Fixed64()[167]);
  234. $this->assertEquals(-169, $m->getMapSfixed64Sfixed64()[-169]);
  235. }
  236. $this->assertEquals(-162, $m->getMapInt32Int32()[-162]);
  237. $this->assertEquals(162, $m->getMapUint32Uint32()[162]);
  238. $this->assertEquals(-164, $m->getMapSint32Sint32()[-164]);
  239. $this->assertEquals(166, $m->getMapFixed32Fixed32()[166]);
  240. $this->assertEquals(-168, $m->getMapSfixed32Sfixed32()[-168]);
  241. $this->assertEquals(13.5, $m->getMapInt32Float()[2]);
  242. $this->assertEquals(13.6, $m->getMapInt32Double()[2]);
  243. $this->assertEquals(false , $m->getMapBoolBool()[false]);
  244. $this->assertEquals('ee', $m->getMapStringString()['ee']);
  245. $this->assertEquals('ff', $m->getMapInt32Bytes()[2]);
  246. $this->assertEquals(TestEnum::TWO, $m->getMapInt32Enum()[2]);
  247. $this->assertEquals(136, $m->getMapInt32Message()[2]->GetA());
  248. }
  249. public function expectEmptyFields(TestMessage $m)
  250. {
  251. $this->assertSame(0, $m->getOptionalInt32());
  252. $this->assertSame(0, $m->getOptionalUint32());
  253. $this->assertSame(0, $m->getOptionalSint32());
  254. $this->assertSame(0, $m->getOptionalFixed32());
  255. $this->assertSame(0, $m->getOptionalSfixed32());
  256. $this->assertSame(0.0, $m->getOptionalFloat());
  257. $this->assertSame(0.0, $m->getOptionalDouble());
  258. $this->assertSame(false, $m->getOptionalBool());
  259. $this->assertSame('', $m->getOptionalString());
  260. $this->assertSame('', $m->getOptionalBytes());
  261. $this->assertSame(0, $m->getOptionalEnum());
  262. $this->assertNull($m->getOptionalMessage());
  263. $this->assertNull($m->getOptionalIncludedMessage());
  264. $this->assertNull($m->getRecursive());
  265. if (PHP_INT_SIZE == 4) {
  266. $this->assertSame("0", $m->getOptionalInt64());
  267. $this->assertSame("0", $m->getOptionalUint64());
  268. $this->assertSame("0", $m->getOptionalSint64());
  269. $this->assertSame("0", $m->getOptionalFixed64());
  270. $this->assertSame("0", $m->getOptionalSfixed64());
  271. } else {
  272. $this->assertSame(0, $m->getOptionalInt64());
  273. $this->assertSame(0, $m->getOptionalUint64());
  274. $this->assertSame(0, $m->getOptionalSint64());
  275. $this->assertSame(0, $m->getOptionalFixed64());
  276. $this->assertSame(0, $m->getOptionalSfixed64());
  277. }
  278. $this->assertEquals(0, count($m->getRepeatedInt32()));
  279. $this->assertEquals(0, count($m->getRepeatedUint32()));
  280. $this->assertEquals(0, count($m->getRepeatedInt64()));
  281. $this->assertEquals(0, count($m->getRepeatedUint64()));
  282. $this->assertEquals(0, count($m->getRepeatedSint32()));
  283. $this->assertEquals(0, count($m->getRepeatedSint64()));
  284. $this->assertEquals(0, count($m->getRepeatedFixed32()));
  285. $this->assertEquals(0, count($m->getRepeatedFixed64()));
  286. $this->assertEquals(0, count($m->getRepeatedSfixed32()));
  287. $this->assertEquals(0, count($m->getRepeatedSfixed64()));
  288. $this->assertEquals(0, count($m->getRepeatedFloat()));
  289. $this->assertEquals(0, count($m->getRepeatedDouble()));
  290. $this->assertEquals(0, count($m->getRepeatedBool()));
  291. $this->assertEquals(0, count($m->getRepeatedString()));
  292. $this->assertEquals(0, count($m->getRepeatedBytes()));
  293. $this->assertEquals(0, count($m->getRepeatedEnum()));
  294. $this->assertEquals(0, count($m->getRepeatedMessage()));
  295. $this->assertEquals(0, count($m->getRepeatedRecursive()));
  296. $this->assertSame("", $m->getMyOneof());
  297. $this->assertSame(0, $m->getOneofInt32());
  298. $this->assertSame(0, $m->getOneofUint32());
  299. $this->assertSame(0, $m->getOneofSint32());
  300. $this->assertSame(0, $m->getOneofFixed32());
  301. $this->assertSame(0, $m->getOneofSfixed32());
  302. $this->assertSame(0.0, $m->getOneofFloat());
  303. $this->assertSame(0.0, $m->getOneofDouble());
  304. $this->assertSame(false, $m->getOneofBool());
  305. $this->assertSame('', $m->getOneofString());
  306. $this->assertSame('', $m->getOneofBytes());
  307. $this->assertSame(0, $m->getOneofEnum());
  308. $this->assertNull($m->getOptionalMessage());
  309. if (PHP_INT_SIZE == 4) {
  310. $this->assertSame("0", $m->getOneofInt64());
  311. $this->assertSame("0", $m->getOneofUint64());
  312. $this->assertSame("0", $m->getOneofSint64());
  313. $this->assertSame("0", $m->getOneofFixed64());
  314. $this->assertSame("0", $m->getOneofSfixed64());
  315. } else {
  316. $this->assertSame(0, $m->getOneofInt64());
  317. $this->assertSame(0, $m->getOneofUint64());
  318. $this->assertSame(0, $m->getOneofSint64());
  319. $this->assertSame(0, $m->getOneofFixed64());
  320. $this->assertSame(0, $m->getOneofSfixed64());
  321. }
  322. $this->assertEquals(0, count($m->getMapInt64Int64()));
  323. $this->assertEquals(0, count($m->getMapUint64Uint64()));
  324. $this->assertEquals(0, count($m->getMapSint64Sint64()));
  325. $this->assertEquals(0, count($m->getMapFixed64Fixed64()));
  326. $this->assertEquals(0, count($m->getMapInt32Int32()));
  327. $this->assertEquals(0, count($m->getMapUint32Uint32()));
  328. $this->assertEquals(0, count($m->getMapSint32Sint32()));
  329. $this->assertEquals(0, count($m->getMapFixed32Fixed32()));
  330. $this->assertEquals(0, count($m->getMapSfixed32Sfixed32()));
  331. $this->assertEquals(0, count($m->getMapSfixed64Sfixed64()));
  332. $this->assertEquals(0, count($m->getMapInt32Float()));
  333. $this->assertEquals(0, count($m->getMapInt32Double()));
  334. $this->assertEquals(0, count($m->getMapBoolBool()));
  335. $this->assertEquals(0, count($m->getMapStringString()));
  336. $this->assertEquals(0, count($m->getMapInt32Bytes()));
  337. $this->assertEquals(0, count($m->getMapInt32Enum()));
  338. $this->assertEquals(0, count($m->getMapInt32Message()));
  339. $this->assertEquals(0, count($m->getMapRecursive()));
  340. }
  341. // This test is to avoid the warning of no test by php unit.
  342. public function testNone()
  343. {
  344. $this->assertTrue(true);
  345. }
  346. }