well_known_test.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. require_once('test_base.php');
  3. require_once('test_util.php');
  4. use Foo\TestMessage;
  5. use Google\Protobuf\Any;
  6. use Google\Protobuf\Api;
  7. use Google\Protobuf\BoolValue;
  8. use Google\Protobuf\BytesValue;
  9. use Google\Protobuf\DoubleValue;
  10. use Google\Protobuf\Duration;
  11. use Google\Protobuf\Enum;
  12. use Google\Protobuf\EnumValue;
  13. use Google\Protobuf\Field;
  14. use Google\Protobuf\FieldMask;
  15. use Google\Protobuf\Field\Cardinality;
  16. use Google\Protobuf\Field\Kind;
  17. use Google\Protobuf\FloatValue;
  18. use Google\Protobuf\GPBEmpty;
  19. use Google\Protobuf\Int32Value;
  20. use Google\Protobuf\Int64Value;
  21. use Google\Protobuf\ListValue;
  22. use Google\Protobuf\Method;
  23. use Google\Protobuf\Mixin;
  24. use Google\Protobuf\NullValue;
  25. use Google\Protobuf\Option;
  26. use Google\Protobuf\SourceContext;
  27. use Google\Protobuf\StringValue;
  28. use Google\Protobuf\Struct;
  29. use Google\Protobuf\Syntax;
  30. use Google\Protobuf\Timestamp;
  31. use Google\Protobuf\Type;
  32. use Google\Protobuf\UInt32Value;
  33. use Google\Protobuf\UInt64Value;
  34. use Google\Protobuf\Value;
  35. class NotMessage {}
  36. class WellKnownTest extends TestBase {
  37. public function testEmpty()
  38. {
  39. $msg = new GPBEmpty();
  40. }
  41. public function testImportDescriptorProto()
  42. {
  43. $msg = new TestImportDescriptorProto();
  44. }
  45. public function testAny()
  46. {
  47. // Create embed message
  48. $embed = new TestMessage();
  49. $this->setFields($embed);
  50. $data = $embed->serializeToString();
  51. // Set any via normal setter.
  52. $any = new Any();
  53. $this->assertSame(
  54. $any, $any->setTypeUrl("type.googleapis.com/foo.TestMessage"));
  55. $this->assertSame("type.googleapis.com/foo.TestMessage",
  56. $any->getTypeUrl());
  57. $this->assertSame($any, $any->setValue($data));
  58. $this->assertSame($data, $any->getValue());
  59. // Test unpack.
  60. $msg = $any->unpack();
  61. $this->assertTrue($msg instanceof TestMessage);
  62. $this->expectFields($msg);
  63. // Test pack.
  64. $any = new Any();
  65. $any->pack($embed);
  66. $this->assertSame($data, $any->getValue());
  67. $this->assertSame("type.googleapis.com/foo.TestMessage", $any->getTypeUrl());
  68. // Test is.
  69. $this->assertTrue($any->is(TestMessage::class));
  70. $this->assertFalse($any->is(Any::class));
  71. }
  72. /**
  73. * @expectedException Exception
  74. */
  75. public function testAnyUnpackInvalidTypeUrl()
  76. {
  77. $any = new Any();
  78. $any->setTypeUrl("invalid");
  79. $any->unpack();
  80. }
  81. /**
  82. * @expectedException Exception
  83. */
  84. public function testAnyUnpackMessageNotAdded()
  85. {
  86. $any = new Any();
  87. $any->setTypeUrl("type.googleapis.com/MessageNotAdded");
  88. $any->unpack();
  89. }
  90. /**
  91. * @expectedException Exception
  92. */
  93. public function testAnyUnpackDecodeError()
  94. {
  95. $any = new Any();
  96. $any->setTypeUrl("type.googleapis.com/foo.TestMessage");
  97. $any->setValue("abc");
  98. $any->unpack();
  99. }
  100. public function testApi()
  101. {
  102. $m = new Api();
  103. $m->setName("a");
  104. $this->assertSame("a", $m->getName());
  105. $m->setMethods([new Method()]);
  106. $this->assertSame(1, count($m->getMethods()));
  107. $m->setOptions([new Option()]);
  108. $this->assertSame(1, count($m->getOptions()));
  109. $m->setVersion("a");
  110. $this->assertSame("a", $m->getVersion());
  111. $m->setSourceContext(new SourceContext());
  112. $this->assertFalse(is_null($m->getSourceContext()));
  113. $m->setMixins([new Mixin()]);
  114. $this->assertSame(1, count($m->getMixins()));
  115. $m->setSyntax(Syntax::SYNTAX_PROTO2);
  116. $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax());
  117. $m = new Method();
  118. $m->setName("a");
  119. $this->assertSame("a", $m->getName());
  120. $m->setRequestTypeUrl("a");
  121. $this->assertSame("a", $m->getRequestTypeUrl());
  122. $m->setRequestStreaming(true);
  123. $this->assertSame(true, $m->getRequestStreaming());
  124. $m->setResponseTypeUrl("a");
  125. $this->assertSame("a", $m->getResponseTypeUrl());
  126. $m->setResponseStreaming(true);
  127. $this->assertSame(true, $m->getResponseStreaming());
  128. $m->setOptions([new Option()]);
  129. $this->assertSame(1, count($m->getOptions()));
  130. $m = new Mixin();
  131. $m->setName("a");
  132. $this->assertSame("a", $m->getName());
  133. $m->setRoot("a");
  134. $this->assertSame("a", $m->getRoot());
  135. }
  136. public function testEnum()
  137. {
  138. $m = new Enum();
  139. $m->setName("a");
  140. $this->assertSame("a", $m->getName());
  141. $m->setEnumvalue([new EnumValue()]);
  142. $this->assertSame(1, count($m->getEnumvalue()));
  143. $m->setOptions([new Option()]);
  144. $this->assertSame(1, count($m->getOptions()));
  145. $m->setSourceContext(new SourceContext());
  146. $this->assertFalse(is_null($m->getSourceContext()));
  147. $m->setSyntax(Syntax::SYNTAX_PROTO2);
  148. $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax());
  149. }
  150. public function testEnumValue()
  151. {
  152. $m = new EnumValue();
  153. $m->setName("a");
  154. $this->assertSame("a", $m->getName());
  155. $m->setNumber(1);
  156. $this->assertSame(1, $m->getNumber());
  157. $m->setOptions([new Option()]);
  158. $this->assertSame(1, count($m->getOptions()));
  159. }
  160. public function testField()
  161. {
  162. $m = new Field();
  163. $m->setKind(Kind::TYPE_DOUBLE);
  164. $this->assertSame(Kind::TYPE_DOUBLE, $m->getKind());
  165. $m->setCardinality(Cardinality::CARDINALITY_OPTIONAL);
  166. $this->assertSame(Cardinality::CARDINALITY_OPTIONAL, $m->getCardinality());
  167. $m->setNumber(1);
  168. $this->assertSame(1, $m->getNumber());
  169. $m->setName("a");
  170. $this->assertSame("a", $m->getName());
  171. $m->setTypeUrl("a");
  172. $this->assertSame("a", $m->getTypeUrl());
  173. $m->setOneofIndex(1);
  174. $this->assertSame(1, $m->getOneofIndex());
  175. $m->setPacked(true);
  176. $this->assertSame(true, $m->getPacked());
  177. $m->setOptions([new Option()]);
  178. $this->assertSame(1, count($m->getOptions()));
  179. $m->setJsonName("a");
  180. $this->assertSame("a", $m->getJsonName());
  181. $m->setDefaultValue("a");
  182. $this->assertSame("a", $m->getDefaultValue());
  183. }
  184. public function testFieldMask()
  185. {
  186. $m = new FieldMask();
  187. $m->setPaths(["a"]);
  188. $this->assertSame(1, count($m->getPaths()));
  189. }
  190. public function testOption()
  191. {
  192. $m = new Option();
  193. $m->setName("a");
  194. $this->assertSame("a", $m->getName());
  195. $m->setValue(new Any());
  196. $this->assertFalse(is_null($m->getValue()));
  197. }
  198. public function testSourceContext()
  199. {
  200. $m = new SourceContext();
  201. $m->setFileName("a");
  202. $this->assertSame("a", $m->getFileName());
  203. }
  204. public function testStruct()
  205. {
  206. $m = new ListValue();
  207. $m->setValues([new Value()]);
  208. $this->assertSame(1, count($m->getValues()));
  209. $m = new Value();
  210. $m->setNullValue(NullValue::NULL_VALUE);
  211. $this->assertSame(NullValue::NULL_VALUE, $m->getNullValue());
  212. $this->assertSame("null_value", $m->getKind());
  213. $m->setNumberValue(1.0);
  214. $this->assertSame(1.0, $m->getNumberValue());
  215. $this->assertSame("number_value", $m->getKind());
  216. $m->setStringValue("a");
  217. $this->assertSame("a", $m->getStringValue());
  218. $this->assertSame("string_value", $m->getKind());
  219. $m->setBoolValue(true);
  220. $this->assertSame(true, $m->getBoolValue());
  221. $this->assertSame("bool_value", $m->getKind());
  222. $m->setStructValue(new Struct());
  223. $this->assertFalse(is_null($m->getStructValue()));
  224. $this->assertSame("struct_value", $m->getKind());
  225. $m->setListValue(new ListValue());
  226. $this->assertFalse(is_null($m->getListValue()));
  227. $this->assertSame("list_value", $m->getKind());
  228. $m = new Struct();
  229. $m->setFields(array("a"=>new Value()));
  230. $this->assertSame(1, count($m->getFields()));
  231. }
  232. public function testTimestamp()
  233. {
  234. $timestamp = new Timestamp();
  235. $timestamp->setSeconds(1);
  236. $timestamp->setNanos(2);
  237. $this->assertEquals(1, $timestamp->getSeconds());
  238. $this->assertSame(2, $timestamp->getNanos());
  239. date_default_timezone_set('UTC');
  240. $from = new DateTime('2011-01-01T15:03:01.012345UTC');
  241. $timestamp->fromDateTime($from);
  242. $this->assertEquals($from->format('U'), $timestamp->getSeconds());
  243. $this->assertSame(0, $timestamp->getNanos());
  244. $to = $timestamp->toDateTime();
  245. $this->assertSame(\DateTime::class, get_class($to));
  246. $this->assertSame($from->format('U'), $to->format('U'));
  247. }
  248. public function testType()
  249. {
  250. $m = new Type();
  251. $m->setName("a");
  252. $this->assertSame("a", $m->getName());
  253. $m->setFields([new Field()]);
  254. $this->assertSame(1, count($m->getFields()));
  255. $m->setOneofs(["a"]);
  256. $this->assertSame(1, count($m->getOneofs()));
  257. $m->setOptions([new Option()]);
  258. $this->assertSame(1, count($m->getOptions()));
  259. $m->setSourceContext(new SourceContext());
  260. $this->assertFalse(is_null($m->getSourceContext()));
  261. $m->setSyntax(Syntax::SYNTAX_PROTO2);
  262. $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax());
  263. }
  264. public function testDuration()
  265. {
  266. $duration = new Duration();
  267. $duration->setSeconds(1);
  268. $duration->setNanos(2);
  269. $this->assertEquals(1, $duration->getSeconds());
  270. $this->assertSame(2, $duration->getNanos());
  271. }
  272. public function testWrappers()
  273. {
  274. $m = new DoubleValue();
  275. $m->setValue(1.0);
  276. $this->assertSame(1.0, $m->getValue());
  277. $m = new FloatValue();
  278. $m->setValue(1.0);
  279. $this->assertSame(1.0, $m->getValue());
  280. $m = new Int64Value();
  281. $m->setValue(1);
  282. $this->assertEquals(1, $m->getValue());
  283. $m = new UInt64Value();
  284. $m->setValue(1);
  285. $this->assertEquals(1, $m->getValue());
  286. $m = new Int32Value();
  287. $m->setValue(1);
  288. $this->assertSame(1, $m->getValue());
  289. $m = new UInt32Value();
  290. $m->setValue(1);
  291. $this->assertSame(1, $m->getValue());
  292. $m = new BoolValue();
  293. $m->setValue(true);
  294. $this->assertSame(true, $m->getValue());
  295. $m = new StringValue();
  296. $m->setValue("a");
  297. $this->assertSame("a", $m->getValue());
  298. $m = new BytesValue();
  299. $m->setValue("a");
  300. $this->assertSame("a", $m->getValue());
  301. }
  302. }