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