well_known_test.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. require_once('test_base.php');
  3. require_once('test_util.php');
  4. use Google\Protobuf\GPBEmpty;
  5. use Google\Protobuf\Any;
  6. use Google\Protobuf\Duration;
  7. use Google\Protobuf\Timestamp;
  8. use Foo\TestMessage;
  9. class NotMessage {}
  10. class WellKnownTest extends TestBase {
  11. public function testNone()
  12. {
  13. $msg = new GPBEmpty();
  14. }
  15. public function testImportDescriptorProto()
  16. {
  17. $msg = new TestImportDescriptorProto();
  18. }
  19. public function testAny()
  20. {
  21. // Create embed message
  22. $embed = new TestMessage();
  23. $this->setFields($embed);
  24. $data = $embed->serializeToString();
  25. // Set any via normal setter.
  26. $any = new Any();
  27. $this->assertSame(
  28. $any, $any->setTypeUrl("type.googleapis.com/foo.TestMessage"));
  29. $this->assertSame("type.googleapis.com/foo.TestMessage",
  30. $any->getTypeUrl());
  31. $this->assertSame($any, $any->setValue($data));
  32. $this->assertSame($data, $any->getValue());
  33. // Test unpack.
  34. $msg = $any->unpack();
  35. $this->assertTrue($msg instanceof TestMessage);
  36. $this->expectFields($msg);
  37. // Test pack.
  38. $any = new Any();
  39. $any->pack($embed);
  40. $this->assertSame($data, $any->getValue());
  41. $this->assertSame("type.googleapis.com/foo.TestMessage", $any->getTypeUrl());
  42. // Test is.
  43. $this->assertTrue($any->is(TestMessage::class));
  44. $this->assertFalse($any->is(Any::class));
  45. }
  46. /**
  47. * @expectedException Exception
  48. */
  49. public function testAnyUnpackInvalidTypeUrl()
  50. {
  51. $any = new Any();
  52. $any->setTypeUrl("invalid");
  53. $any->unpack();
  54. }
  55. /**
  56. * @expectedException Exception
  57. */
  58. public function testAnyUnpackMessageNotAdded()
  59. {
  60. $any = new Any();
  61. $any->setTypeUrl("type.googleapis.com/MessageNotAdded");
  62. $any->unpack();
  63. }
  64. /**
  65. * @expectedException Exception
  66. */
  67. public function testAnyUnpackDecodeError()
  68. {
  69. $any = new Any();
  70. $any->setTypeUrl("type.googleapis.com/foo.TestMessage");
  71. $any->setValue("abc");
  72. $any->unpack();
  73. }
  74. public function testTimestamp()
  75. {
  76. $timestamp = new Timestamp();
  77. $timestamp->setSeconds(1);
  78. $timestamp->setNanos(2);
  79. $this->assertEquals(1, $timestamp->getSeconds());
  80. $this->assertSame(2, $timestamp->getNanos());
  81. date_default_timezone_set('UTC');
  82. $from = new DateTime('2011-01-01T15:03:01.012345UTC');
  83. $timestamp->fromDateTime($from);
  84. $this->assertEquals($from->format('U'), $timestamp->getSeconds());
  85. $this->assertSame(0, $timestamp->getNanos());
  86. $to = $timestamp->toDateTime();
  87. $this->assertSame(\DateTime::class, get_class($to));
  88. $this->assertSame($from->format('U'), $to->format('U'));
  89. }
  90. public function testDuration()
  91. {
  92. $duration = new Duration();
  93. $duration->setSeconds(1);
  94. $duration->setNanos(2);
  95. $this->assertEquals(1, $duration->getSeconds());
  96. $this->assertSame(2, $duration->getNanos());
  97. }
  98. }