well_known_test.php 2.8 KB

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