diff options
author | Paul Yang <TeBoring@users.noreply.github.com> | 2017-10-05 21:03:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-05 21:03:57 -0700 |
commit | 77f64bb7779ec2195f9bc4dc82497d12c18fc6b7 (patch) | |
tree | 0c1b7683a15ecd6fb597a05aaaae08cf4420107e /php/tests/well_known_test.php | |
parent | cd5f49d0942e19a5854a325941918fca02fdb409 (diff) |
Add well known types to php runtime. (#3697)
* Add well known types to php runtime.
* Fix php7.0 tests
* No longer generate empty.proto in test as it has been included in
runtime.
* Fix zts build
* Clean code
* Rename g_p_b_empty to empty.
* Don't generate code for empty.proto in compatibility test
* Fix 32-bit
* Fix mac build
* Fix Makefile.am to add new files
Diffstat (limited to 'php/tests/well_known_test.php')
-rw-r--r-- | php/tests/well_known_test.php | 280 |
1 files changed, 276 insertions, 4 deletions
diff --git a/php/tests/well_known_test.php b/php/tests/well_known_test.php index 690ce5f5..a7760685 100644 --- a/php/tests/well_known_test.php +++ b/php/tests/well_known_test.php @@ -3,18 +3,43 @@ require_once('test_base.php'); require_once('test_util.php'); -use Google\Protobuf\GPBEmpty; +use Foo\TestMessage; use Google\Protobuf\Any; +use Google\Protobuf\Api; +use Google\Protobuf\BoolValue; +use Google\Protobuf\BytesValue; +use Google\Protobuf\DoubleValue; use Google\Protobuf\Duration; +use Google\Protobuf\Enum; +use Google\Protobuf\EnumValue; +use Google\Protobuf\Field; +use Google\Protobuf\FieldMask; +use Google\Protobuf\Field_Cardinality; +use Google\Protobuf\Field_Kind; +use Google\Protobuf\FloatValue; +use Google\Protobuf\GPBEmpty; +use Google\Protobuf\Int32Value; +use Google\Protobuf\Int64Value; +use Google\Protobuf\ListValue; +use Google\Protobuf\Method; +use Google\Protobuf\Mixin; +use Google\Protobuf\NullValue; +use Google\Protobuf\Option; +use Google\Protobuf\SourceContext; +use Google\Protobuf\StringValue; +use Google\Protobuf\Struct; +use Google\Protobuf\Syntax; use Google\Protobuf\Timestamp; - -use Foo\TestMessage; +use Google\Protobuf\Type; +use Google\Protobuf\UInt32Value; +use Google\Protobuf\UInt64Value; +use Google\Protobuf\Value; class NotMessage {} class WellKnownTest extends TestBase { - public function testNone() + public function testEmpty() { $msg = new GPBEmpty(); } @@ -89,6 +114,191 @@ class WellKnownTest extends TestBase { $any->unpack(); } + public function testApi() + { + $m = new Api(); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setMethods([new Method()]); + $this->assertSame(1, count($m->getMethods())); + + $m->setOptions([new Option()]); + $this->assertSame(1, count($m->getOptions())); + + $m->setVersion("a"); + $this->assertSame("a", $m->getVersion()); + + $m->setSourceContext(new SourceContext()); + $this->assertFalse(is_null($m->getSourceContext())); + + $m->setMixins([new Mixin()]); + $this->assertSame(1, count($m->getMixins())); + + $m->setSyntax(Syntax::SYNTAX_PROTO2); + $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax()); + + $m = new Method(); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setRequestTypeUrl("a"); + $this->assertSame("a", $m->getRequestTypeUrl()); + + $m->setRequestStreaming(true); + $this->assertSame(true, $m->getRequestStreaming()); + + $m->setResponseTypeUrl("a"); + $this->assertSame("a", $m->getResponseTypeUrl()); + + $m->setResponseStreaming(true); + $this->assertSame(true, $m->getResponseStreaming()); + + $m->setOptions([new Option()]); + $this->assertSame(1, count($m->getOptions())); + + $m = new Mixin(); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setRoot("a"); + $this->assertSame("a", $m->getRoot()); + } + + public function testEnum() + { + $m = new Enum(); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setEnumvalue([new EnumValue()]); + $this->assertSame(1, count($m->getEnumvalue())); + + $m->setOptions([new Option()]); + $this->assertSame(1, count($m->getOptions())); + + $m->setSourceContext(new SourceContext()); + $this->assertFalse(is_null($m->getSourceContext())); + + $m->setSyntax(Syntax::SYNTAX_PROTO2); + $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax()); + } + + public function testEnumValue() + { + $m = new EnumValue(); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setNumber(1); + $this->assertSame(1, $m->getNumber()); + + $m->setOptions([new Option()]); + $this->assertSame(1, count($m->getOptions())); + } + + public function testField() + { + $m = new Field(); + + $m->setKind(Field_Kind::TYPE_DOUBLE); + $this->assertSame(Field_Kind::TYPE_DOUBLE, $m->getKind()); + + $m->setCardinality(Field_Cardinality::CARDINALITY_OPTIONAL); + $this->assertSame(Field_Cardinality::CARDINALITY_OPTIONAL, $m->getCardinality()); + + $m->setNumber(1); + $this->assertSame(1, $m->getNumber()); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setTypeUrl("a"); + $this->assertSame("a", $m->getTypeUrl()); + + $m->setOneofIndex(1); + $this->assertSame(1, $m->getOneofIndex()); + + $m->setPacked(true); + $this->assertSame(true, $m->getPacked()); + + $m->setOptions([new Option()]); + $this->assertSame(1, count($m->getOptions())); + + $m->setJsonName("a"); + $this->assertSame("a", $m->getJsonName()); + + $m->setDefaultValue("a"); + $this->assertSame("a", $m->getDefaultValue()); + } + + public function testFieldMask() + { + $m = new FieldMask(); + $m->setPaths(["a"]); + $this->assertSame(1, count($m->getPaths())); + } + + public function testOption() + { + $m = new Option(); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setValue(new Any()); + $this->assertFalse(is_null($m->getValue())); + } + + public function testSourceContext() + { + $m = new SourceContext(); + $m->setFileName("a"); + $this->assertSame("a", $m->getFileName()); + } + + public function testStruct() + { + $m = new ListValue(); + $m->setValues([new Value()]); + $this->assertSame(1, count($m->getValues())); + + $m = new Value(); + + $m->setNullValue(NullValue::NULL_VALUE); + $this->assertSame(NullValue::NULL_VALUE, $m->getNullValue()); + $this->assertSame("null_value", $m->getKind()); + + $m->setNumberValue(1.0); + $this->assertSame(1.0, $m->getNumberValue()); + $this->assertSame("number_value", $m->getKind()); + + $m->setStringValue("a"); + $this->assertSame("a", $m->getStringValue()); + $this->assertSame("string_value", $m->getKind()); + + $m->setBoolValue(true); + $this->assertSame(true, $m->getBoolValue()); + $this->assertSame("bool_value", $m->getKind()); + + $m->setStructValue(new Struct()); + $this->assertFalse(is_null($m->getStructValue())); + $this->assertSame("struct_value", $m->getKind()); + + $m->setListValue(new ListValue()); + $this->assertFalse(is_null($m->getListValue())); + $this->assertSame("list_value", $m->getKind()); + + $m = new Struct(); + $m->setFields(array("a"=>new Value())); + $this->assertSame(1, count($m->getFields())); + } + public function testTimestamp() { $timestamp = new Timestamp(); @@ -109,6 +319,29 @@ class WellKnownTest extends TestBase { $this->assertSame($from->format('U'), $to->format('U')); } + public function testType() + { + $m = new Type(); + + $m->setName("a"); + $this->assertSame("a", $m->getName()); + + $m->setFields([new Field()]); + $this->assertSame(1, count($m->getFields())); + + $m->setOneofs(["a"]); + $this->assertSame(1, count($m->getOneofs())); + + $m->setOptions([new Option()]); + $this->assertSame(1, count($m->getOptions())); + + $m->setSourceContext(new SourceContext()); + $this->assertFalse(is_null($m->getSourceContext())); + + $m->setSyntax(Syntax::SYNTAX_PROTO2); + $this->assertSame(Syntax::SYNTAX_PROTO2, $m->getSyntax()); + } + public function testDuration() { $duration = new Duration(); @@ -118,4 +351,43 @@ class WellKnownTest extends TestBase { $this->assertEquals(1, $duration->getSeconds()); $this->assertSame(2, $duration->getNanos()); } + + public function testWrappers() + { + $m = new DoubleValue(); + $m->setValue(1.0); + $this->assertSame(1.0, $m->getValue()); + + $m = new FloatValue(); + $m->setValue(1.0); + $this->assertSame(1.0, $m->getValue()); + + $m = new Int64Value(); + $m->setValue(1); + $this->assertEquals(1, $m->getValue()); + + $m = new UInt64Value(); + $m->setValue(1); + $this->assertEquals(1, $m->getValue()); + + $m = new Int32Value(); + $m->setValue(1); + $this->assertSame(1, $m->getValue()); + + $m = new UInt32Value(); + $m->setValue(1); + $this->assertSame(1, $m->getValue()); + + $m = new BoolValue(); + $m->setValue(true); + $this->assertSame(true, $m->getValue()); + + $m = new StringValue(); + $m->setValue("a"); + $this->assertSame("a", $m->getValue()); + + $m = new BytesValue(); + $m->setValue("a"); + $this->assertSame("a", $m->getValue()); + } } |