diff options
Diffstat (limited to 'php/tests')
-rw-r--r-- | php/tests/descriptors_test.php | 246 | ||||
-rw-r--r-- | php/tests/generated_class_test.php | 13 | ||||
-rw-r--r-- | php/tests/memory_leak_test.php | 1 | ||||
-rw-r--r-- | php/tests/proto/test.proto | 5 | ||||
-rw-r--r-- | php/tests/proto/test_descriptors.proto | 35 | ||||
-rwxr-xr-x | php/tests/test.sh | 2 |
6 files changed, 301 insertions, 1 deletions
diff --git a/php/tests/descriptors_test.php b/php/tests/descriptors_test.php new file mode 100644 index 00000000..17e8a4f2 --- /dev/null +++ b/php/tests/descriptors_test.php @@ -0,0 +1,246 @@ +<?php + +require_once('generated/Descriptors/TestDescriptorsEnum.php'); +require_once('generated/Descriptors/TestDescriptorsMessage.php'); +require_once('test_base.php'); +require_once('test_util.php'); + +use Google\Protobuf\DescriptorPool; +use Google\Protobuf\Internal\RepeatedField; +use Google\Protobuf\Internal\MapField; +use Descriptors\TestDescriptorsEnum; +use Descriptors\TestDescriptorsMessage; +use Descriptors\TestDescriptorsMessage_Sub; + +class DescriptorsTest extends TestBase +{ + + // Redefine these here for compatibility with c extension + const GPBLABEL_OPTIONAL = 1; + const GPBLABEL_REQUIRED = 2; + const GPBLABEL_REPEATED = 3; + + const GPBTYPE_DOUBLE = 1; + const GPBTYPE_FLOAT = 2; + const GPBTYPE_INT64 = 3; + const GPBTYPE_UINT64 = 4; + const GPBTYPE_INT32 = 5; + const GPBTYPE_FIXED64 = 6; + const GPBTYPE_FIXED32 = 7; + const GPBTYPE_BOOL = 8; + const GPBTYPE_STRING = 9; + const GPBTYPE_GROUP = 10; + const GPBTYPE_MESSAGE = 11; + const GPBTYPE_BYTES = 12; + const GPBTYPE_UINT32 = 13; + const GPBTYPE_ENUM = 14; + const GPBTYPE_SFIXED32 = 15; + const GPBTYPE_SFIXED64 = 16; + const GPBTYPE_SINT32 = 17; + const GPBTYPE_SINT64 = 18; + + ######################################################### + # Test descriptor pool. + ######################################################### + + public function testDescriptorPool() + { + $pool = DescriptorPool::getGeneratedPool(); + + $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); + $this->assertInstanceOf('\Google\Protobuf\Descriptor', $desc); + + $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum())); + $this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $enumDesc); + } + + public function testDescriptorPoolIncorrectArgs() + { + $pool = DescriptorPool::getGeneratedPool(); + + $desc = $pool->getDescriptorByClassName('NotAClass'); + $this->assertNull($desc); + + $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsEnum())); + $this->assertNull($desc); + + $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsMessage())); + $this->assertNull($enumDesc); + } + + ######################################################### + # Test descriptor. + ######################################################### + + public function testDescriptor() + { + $pool = DescriptorPool::getGeneratedPool(); + $class = get_class(new TestDescriptorsMessage()); + $this->assertSame('Descriptors\TestDescriptorsMessage', $class); + $desc = $pool->getDescriptorByClassName($class); + + $this->assertSame('descriptors.TestDescriptorsMessage', $desc->getFullName()); + $this->assertSame($class, $desc->getClass()); + + $this->assertInstanceOf('\Google\Protobuf\FieldDescriptor', $desc->getField(0)); + $this->assertSame(7, $desc->getFieldCount()); + + $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); + $this->assertSame(1, $desc->getOneofDeclCount()); + } + + ######################################################### + # Test enum descriptor. + ######################################################### + + public function testEnumDescriptor() + { + // WARNINIG - we need to do this so that TestDescriptorsEnum is registered!!? + new TestDescriptorsMessage(); + + $pool = DescriptorPool::getGeneratedPool(); + + $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum())); + + // Build map of enum values + $enumDescMap = []; + for ($i = 0; $i < $enumDesc->getValueCount(); $i++) { + $enumValueDesc = $enumDesc->getValue($i); + $this->assertInstanceOf('\Google\Protobuf\EnumValueDescriptor', $enumValueDesc); + $enumDescMap[$enumValueDesc->getNumber()] = $enumValueDesc->getName(); + } + + $this->assertSame('ZERO', $enumDescMap[0]); + $this->assertSame('ONE', $enumDescMap[1]); + + $this->assertSame(2, $enumDesc->getValueCount()); + } + + ######################################################### + # Test field descriptor. + ######################################################### + + public function testFieldDescriptor() + { + $pool = DescriptorPool::getGeneratedPool(); + $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); + + $fieldDescMap = $this->buildFieldMap($desc); + + // Optional int field + $fieldDesc = $fieldDescMap[1]; + $this->assertSame('optional_int32', $fieldDesc->getName()); + $this->assertSame(1, $fieldDesc->getNumber()); + $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); + $this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType()); + $this->assertFalse($fieldDesc->isMap()); + + // Optional enum field + $fieldDesc = $fieldDescMap[16]; + $this->assertSame('optional_enum', $fieldDesc->getName()); + $this->assertSame(16, $fieldDesc->getNumber()); + $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); + $this->assertSame(self::GPBTYPE_ENUM, $fieldDesc->getType()); + $this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $fieldDesc->getEnumType()); + $this->assertFalse($fieldDesc->isMap()); + + // Optional message field + $fieldDesc = $fieldDescMap[17]; + $this->assertSame('optional_message', $fieldDesc->getName()); + $this->assertSame(17, $fieldDesc->getNumber()); + $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); + $this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType()); + $this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType()); + $this->assertFalse($fieldDesc->isMap()); + + // Repeated int field + $fieldDesc = $fieldDescMap[31]; + $this->assertSame('repeated_int32', $fieldDesc->getName()); + $this->assertSame(31, $fieldDesc->getNumber()); + $this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel()); + $this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType()); + $this->assertFalse($fieldDesc->isMap()); + + // Repeated message field + $fieldDesc = $fieldDescMap[47]; + $this->assertSame('repeated_message', $fieldDesc->getName()); + $this->assertSame(47, $fieldDesc->getNumber()); + $this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel()); + $this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType()); + $this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType()); + $this->assertFalse($fieldDesc->isMap()); + + // Oneof int field + // Tested further in testOneofDescriptor() + $fieldDesc = $fieldDescMap[51]; + $this->assertSame('oneof_int32', $fieldDesc->getName()); + $this->assertSame(51, $fieldDesc->getNumber()); + $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); + $this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType()); + $this->assertFalse($fieldDesc->isMap()); + + // Map int-enum field + $fieldDesc = $fieldDescMap[71]; + $this->assertSame('map_int32_enum', $fieldDesc->getName()); + $this->assertSame(71, $fieldDesc->getNumber()); + $this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel()); + $this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType()); + $this->assertTrue($fieldDesc->isMap()); + $mapDesc = $fieldDesc->getMessageType(); + $this->assertSame('descriptors.TestDescriptorsMessage.MapInt32EnumEntry', $mapDesc->getFullName()); + $this->assertSame(self::GPBTYPE_INT32, $mapDesc->getField(0)->getType()); + $this->assertSame(self::GPBTYPE_ENUM, $mapDesc->getField(1)->getType()); + } + + /** + * @expectedException \Exception + */ + public function testFieldDescriptorEnumException() + { + $pool = DescriptorPool::getGeneratedPool(); + $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); + $fieldDesc = $desc->getField(0); + $fieldDesc->getEnumType(); + } + + /** + * @expectedException \Exception + */ + public function testFieldDescriptorMessageException() + { + $pool = DescriptorPool::getGeneratedPool(); + $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); + $fieldDesc = $desc->getField(0); + $fieldDesc->getMessageType(); + } + + ######################################################### + # Test oneof descriptor. + ######################################################### + + public function testOneofDescriptor() + { + $pool = DescriptorPool::getGeneratedPool(); + $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); + + $fieldDescMap = $this->buildFieldMap($desc); + $fieldDesc = $fieldDescMap[51]; + + $oneofDesc = $desc->getOneofDecl(0); + + $this->assertSame('my_oneof', $oneofDesc->getName()); + $fieldDescFromOneof = $oneofDesc->getField(0); + $this->assertSame($fieldDesc, $fieldDescFromOneof); + $this->assertSame(1, $oneofDesc->getFieldCount()); + } + + private function buildFieldMap($desc) + { + $fieldDescMap = []; + for ($i = 0; $i < $desc->getFieldCount(); $i++) { + $fieldDesc = $desc->getField($i); + $fieldDescMap[$fieldDesc->getNumber()] = $fieldDesc; + } + return $fieldDescMap; + } +} diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php index 56e3be20..86e68683 100644 --- a/php/tests/generated_class_test.php +++ b/php/tests/generated_class_test.php @@ -13,6 +13,7 @@ use Foo\TestIncludeNamespaceMessage; use Foo\TestIncludePrefixMessage; use Foo\TestMessage; use Foo\TestMessage_Sub; +use Foo\TestReverseFieldOrder; use Php\Test\TestNamespace; class GeneratedClassTest extends TestBase @@ -702,4 +703,16 @@ class GeneratedClassTest extends TestBase $this->assertSame(1, $m->getOptionalInt32()); $this->assertSame(2, $m->getOptionalUInt32()); } + + ######################################################### + # Test Reverse Field Order. + ######################################################### + + public function testReverseFieldOrder() + { + $m = new TestReverseFieldOrder(); + $m->setB("abc"); + $this->assertSame("abc", $m->getB()); + $this->assertNotSame("abc", $m->getA()); + } } diff --git a/php/tests/memory_leak_test.php b/php/tests/memory_leak_test.php index faa1833d..a92694d0 100644 --- a/php/tests/memory_leak_test.php +++ b/php/tests/memory_leak_test.php @@ -21,6 +21,7 @@ require_once('generated/Foo/TestMessage_Sub.php'); require_once('generated/Foo/TestPackedMessage.php'); require_once('generated/Foo/TestPhpDoc.php'); require_once('generated/Foo/TestRandomFieldOrder.php'); +require_once('generated/Foo/TestReverseFieldOrder.php'); require_once('generated/Foo/TestUnpackedMessage.php'); require_once('generated/GPBMetadata/Proto/Test.php'); require_once('generated/GPBMetadata/Proto/TestEmptyPhpNamespace.php'); diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto index d81f66f5..a90f3d1d 100644 --- a/php/tests/proto/test.proto +++ b/php/tests/proto/test.proto @@ -187,3 +187,8 @@ message TestRandomFieldOrder { int64 tag13 = 150; string tag14 = 160; } + +message TestReverseFieldOrder { + repeated int32 a = 2; + string b = 1; +} diff --git a/php/tests/proto/test_descriptors.proto b/php/tests/proto/test_descriptors.proto new file mode 100644 index 00000000..d42aec7c --- /dev/null +++ b/php/tests/proto/test_descriptors.proto @@ -0,0 +1,35 @@ +syntax = "proto3"; + +package descriptors; + +message TestDescriptorsMessage { + int32 optional_int32 = 1; + TestDescriptorsEnum optional_enum = 16; + Sub optional_message = 17; + + // Repeated + repeated int32 repeated_int32 = 31; + repeated Sub repeated_message = 47; + + oneof my_oneof { + int32 oneof_int32 = 51; + } + + map<int32, EnumSub> map_int32_enum = 71; + + message Sub { + int32 a = 1; + repeated int32 b = 2; + } + + enum EnumSub { + ZERO = 0; + ONE = 1; + } +} + +enum TestDescriptorsEnum { + ZERO = 0; + ONE = 1; +} + diff --git a/php/tests/test.sh b/php/tests/test.sh index b640c143..c35372d3 100755 --- a/php/tests/test.sh +++ b/php/tests/test.sh @@ -8,7 +8,7 @@ set -e phpize && ./configure CFLAGS='-g -O0' && make popd -tests=( array_test.php encode_decode_test.php generated_class_test.php generated_phpdoc_test.php map_field_test.php well_known_test.php generated_service_test.php ) +tests=( array_test.php encode_decode_test.php generated_class_test.php generated_phpdoc_test.php map_field_test.php well_known_test.php generated_service_test.php descriptors_test.php ) for t in "${tests[@]}" do |