test_base.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. use Foo\TestMessage;
  3. use Foo\TestMessage_Sub;
  4. class TestBase extends PHPUnit_Framework_TestCase
  5. {
  6. public function setFields(TestMessage $m)
  7. {
  8. TestUtil::setTestMessage($m);
  9. }
  10. public function expectFields(TestMessage $m)
  11. {
  12. $this->assertSame(-44, $m->getOptionalSint32());
  13. $this->assertSame(46, $m->getOptionalFixed32());
  14. $this->assertSame(-46, $m->getOptionalSfixed32());
  15. $this->assertSame(1.5, $m->getOptionalFloat());
  16. $this->assertSame(1.6, $m->getOptionalDouble());
  17. $this->assertSame(true, $m->getOptionalBool());
  18. $this->assertSame('a', $m->getOptionalString());
  19. $this->assertSame('b', $m->getOptionalBytes());
  20. $this->assertSame(33, $m->getOptionalMessage()->getA());
  21. if (PHP_INT_SIZE == 4) {
  22. $this->assertSame('-43', $m->getOptionalInt64());
  23. $this->assertSame('43', $m->getOptionalUint64());
  24. $this->assertSame('-45', $m->getOptionalSint64());
  25. $this->assertSame('47', $m->getOptionalFixed64());
  26. $this->assertSame('-47', $m->getOptionalSfixed64());
  27. } else {
  28. $this->assertSame(-43, $m->getOptionalInt64());
  29. $this->assertSame(43, $m->getOptionalUint64());
  30. $this->assertSame(-45, $m->getOptionalSint64());
  31. $this->assertSame(47, $m->getOptionalFixed64());
  32. $this->assertSame(-47, $m->getOptionalSfixed64());
  33. }
  34. $this->assertEquals(-42, $m->getRepeatedInt32()[0]);
  35. $this->assertEquals(42, $m->getRepeatedUint32()[0]);
  36. $this->assertEquals(-43, $m->getRepeatedInt64()[0]);
  37. $this->assertEquals(43, $m->getRepeatedUint64()[0]);
  38. $this->assertEquals(-44, $m->getRepeatedSint32()[0]);
  39. $this->assertEquals(-45, $m->getRepeatedSint64()[0]);
  40. $this->assertEquals(46, $m->getRepeatedFixed32()[0]);
  41. $this->assertEquals(47, $m->getRepeatedFixed64()[0]);
  42. $this->assertEquals(-46, $m->getRepeatedSfixed32()[0]);
  43. $this->assertEquals(-47, $m->getRepeatedSfixed64()[0]);
  44. $this->assertEquals(1.5, $m->getRepeatedFloat()[0]);
  45. $this->assertEquals(1.6, $m->getRepeatedDouble()[0]);
  46. $this->assertEquals(true, $m->getRepeatedBool()[0]);
  47. $this->assertEquals('a', $m->getRepeatedString()[0]);
  48. $this->assertEquals('b', $m->getRepeatedBytes()[0]);
  49. $this->assertEquals(34, $m->getRepeatedMessage()[0]->GetA());
  50. $this->assertEquals(-52, $m->getRepeatedInt32()[1]);
  51. $this->assertEquals(52, $m->getRepeatedUint32()[1]);
  52. $this->assertEquals(-53, $m->getRepeatedInt64()[1]);
  53. $this->assertEquals(53, $m->getRepeatedUint64()[1]);
  54. $this->assertEquals(-54, $m->getRepeatedSint32()[1]);
  55. $this->assertEquals(-55, $m->getRepeatedSint64()[1]);
  56. $this->assertEquals(56, $m->getRepeatedFixed32()[1]);
  57. $this->assertEquals(57, $m->getRepeatedFixed64()[1]);
  58. $this->assertEquals(-56, $m->getRepeatedSfixed32()[1]);
  59. $this->assertEquals(-57, $m->getRepeatedSfixed64()[1]);
  60. $this->assertEquals(2.5, $m->getRepeatedFloat()[1]);
  61. $this->assertEquals(2.6, $m->getRepeatedDouble()[1]);
  62. $this->assertEquals(false, $m->getRepeatedBool()[1]);
  63. $this->assertEquals('c', $m->getRepeatedString()[1]);
  64. $this->assertEquals('d', $m->getRepeatedBytes()[1]);
  65. $this->assertEquals(35, $m->getRepeatedMessage()[1]->GetA());
  66. }
  67. public function expectEmptyFields(TestMessage $m)
  68. {
  69. $this->assertSame(0, $m->getOptionalInt32());
  70. $this->assertSame(0, $m->getOptionalUint32());
  71. $this->assertSame(0, $m->getOptionalSint32());
  72. $this->assertSame(0, $m->getOptionalFixed32());
  73. $this->assertSame(0, $m->getOptionalSfixed32());
  74. $this->assertSame(0.0, $m->getOptionalFloat());
  75. $this->assertSame(0.0, $m->getOptionalDouble());
  76. $this->assertSame(false, $m->getOptionalBool());
  77. $this->assertSame('', $m->getOptionalString());
  78. $this->assertSame('', $m->getOptionalBytes());
  79. $this->assertNull($m->getOptionalMessage());
  80. if (PHP_INT_SIZE == 4) {
  81. $this->assertSame("0", $m->getOptionalInt64());
  82. $this->assertSame("0", $m->getOptionalUint64());
  83. $this->assertSame("0", $m->getOptionalSint64());
  84. $this->assertSame("0", $m->getOptionalFixed64());
  85. $this->assertSame("0", $m->getOptionalSfixed64());
  86. } else {
  87. $this->assertSame(0, $m->getOptionalInt64());
  88. $this->assertSame(0, $m->getOptionalUint64());
  89. $this->assertSame(0, $m->getOptionalSint64());
  90. $this->assertSame(0, $m->getOptionalFixed64());
  91. $this->assertSame(0, $m->getOptionalSfixed64());
  92. }
  93. }
  94. // This test is to avoid the warning of no test by php unit.
  95. public function testNone()
  96. {
  97. }
  98. }