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