well_known_test.php 11 KB

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