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