WellKnownTest.php 12 KB

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