well_known_test.php 11 KB

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