From 85e27d7372d7e73a70d6b8cdd71cc54c0d4a3820 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Fri, 10 Jan 2020 18:26:30 +0000 Subject: [PATCH 1/2] Call register_class before getClass from desc --- php/ext/google/protobuf/def.c | 1 + php/tests/descriptors_test.php | 390 +++++++++++++++++---------------- php/tests/gdb_test.sh | 3 +- 3 files changed, 209 insertions(+), 185 deletions(-) diff --git a/php/ext/google/protobuf/def.c b/php/ext/google/protobuf/def.c index f10c37cd9bc2..7f5004ced147 100644 --- a/php/ext/google/protobuf/def.c +++ b/php/ext/google/protobuf/def.c @@ -132,6 +132,7 @@ static void descriptor_init_c_instance(Descriptor *desc TSRMLS_DC) { PHP_METHOD(Descriptor, getClass) { Descriptor* desc = UNBOX(Descriptor, getThis()); DescriptorInternal* intern = desc->intern; + register_class(intern, false TSRMLS_CC); #if PHP_MAJOR_VERSION < 7 const char* classname = intern->klass->name; #else diff --git a/php/tests/descriptors_test.php b/php/tests/descriptors_test.php index 93683b82fdfd..2ab24d4c7f18 100644 --- a/php/tests/descriptors_test.php +++ b/php/tests/descriptors_test.php @@ -11,6 +11,8 @@ use Descriptors\TestDescriptorsEnum; use Descriptors\TestDescriptorsMessage; use Descriptors\TestDescriptorsMessage\Sub; +use Foo\TestMessage; +use Bar\TestInclude; class DescriptorsTest extends TestBase { @@ -39,208 +41,228 @@ class DescriptorsTest extends TestBase const GPBTYPE_SINT32 = 17; const GPBTYPE_SINT64 = 18; - ######################################################### - # Test descriptor pool. - ######################################################### + # ######################################################### + # # Test descriptor pool. + # ######################################################### - public function testDescriptorPool() - { - $pool = DescriptorPool::getGeneratedPool(); + # public function testDescriptorPool() + # { + # $pool = DescriptorPool::getGeneratedPool(); - $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); - $this->assertInstanceOf('\Google\Protobuf\Descriptor', $desc); + # $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); - } + # $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum())); + # $this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $enumDesc); + # } - public function testDescriptorPoolIncorrectArgs() - { - $pool = DescriptorPool::getGeneratedPool(); + # public function testDescriptorPoolIncorrectArgs() + # { + # $pool = DescriptorPool::getGeneratedPool(); - $desc = $pool->getDescriptorByClassName('NotAClass'); - $this->assertNull($desc); + # $desc = $pool->getDescriptorByClassName('NotAClass'); + # $this->assertNull($desc); - $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsEnum())); - $this->assertNull($desc); + # $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsEnum())); + # $this->assertNull($desc); - $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsMessage())); - $this->assertNull($enumDesc); - } + # $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsMessage())); + # $this->assertNull($enumDesc); + # } - ######################################################### - # Test descriptor. - ######################################################### + # ######################################################### + # # Test descriptor. + # ######################################################### - public function testDescriptor() - { - $pool = DescriptorPool::getGeneratedPool(); - $class = get_class(new TestDescriptorsMessage()); - $this->assertSame('Descriptors\TestDescriptorsMessage', $class); - $desc = $pool->getDescriptorByClassName($class); + # 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->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\FieldDescriptor', $desc->getField(0)); + # $this->assertSame(7, $desc->getFieldCount()); - $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); - $this->assertSame(1, $desc->getOneofDeclCount()); - } + # $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); + # $this->assertSame(1, $desc->getOneofDeclCount()); + # } - ######################################################### - # Test enum descriptor. - ######################################################### - - public function testEnumDescriptor() + public function testDescriptorForIncludedMessage() { - // WARNINIG - we need to do this so that TestDescriptorsEnum is registered!!? - new TestDescriptorsMessage(); - $pool = DescriptorPool::getGeneratedPool(); + $class = get_class(new TestMessage()); + $this->assertSame('Foo\TestMessage', $class); + $desc = $pool->getDescriptorByClassName($class); + $fielddesc = $desc->getField(17); + $subdesc = $fielddesc->getMessageType(); + var_dump($subdesc->getClass()); - $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]; + # $this->assertSame('bar.TestInclude', $desc->getFullName()); + # $this->assertSame($class, $desc->getClass()); - $oneofDesc = $desc->getOneofDecl(0); + # $this->assertInstanceOf('\Google\Protobuf\FieldDescriptor', $desc->getField(0)); + # $this->assertSame(7, $desc->getFieldCount()); - $this->assertSame('my_oneof', $oneofDesc->getName()); - $fieldDescFromOneof = $oneofDesc->getField(0); - $this->assertSame($fieldDesc, $fieldDescFromOneof); - $this->assertSame(1, $oneofDesc->getFieldCount()); + # $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); + # $this->assertSame(1, $desc->getOneofDeclCount()); } - private function buildFieldMap($desc) - { - $fieldDescMap = []; - for ($i = 0; $i < $desc->getFieldCount(); $i++) { - $fieldDesc = $desc->getField($i); - $fieldDescMap[$fieldDesc->getNumber()] = $fieldDesc; - } - return $fieldDescMap; - } + # ######################################################### + # # 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/gdb_test.sh b/php/tests/gdb_test.sh index da5f3f3ac14b..0b297ba7218b 100755 --- a/php/tests/gdb_test.sh +++ b/php/tests/gdb_test.sh @@ -12,7 +12,8 @@ php -i | grep "Configuration" # phpunit` --bootstrap autoload.php tmp_test.php # # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php generated_class_test.php -gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php +# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php +gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php descriptors_test.php # # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php # From 055917e1e697273d34239920cafb31b254b4eda2 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Fri, 10 Jan 2020 18:57:30 +0000 Subject: [PATCH 2/2] Remove debug code --- php/tests/descriptors_test.php | 387 ++++++++++++++++----------------- php/tests/gdb_test.sh | 3 +- 2 files changed, 190 insertions(+), 200 deletions(-) diff --git a/php/tests/descriptors_test.php b/php/tests/descriptors_test.php index 2ab24d4c7f18..60a6292cb61d 100644 --- a/php/tests/descriptors_test.php +++ b/php/tests/descriptors_test.php @@ -41,55 +41,55 @@ class DescriptorsTest extends TestBase const GPBTYPE_SINT32 = 17; const GPBTYPE_SINT64 = 18; - # ######################################################### - # # Test descriptor pool. - # ######################################################### + ######################################################### + # Test descriptor pool. + ######################################################### - # public function testDescriptorPool() - # { - # $pool = DescriptorPool::getGeneratedPool(); + public function testDescriptorPool() + { + $pool = DescriptorPool::getGeneratedPool(); - # $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); - # $this->assertInstanceOf('\Google\Protobuf\Descriptor', $desc); + $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); - # } + $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum())); + $this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $enumDesc); + } - # public function testDescriptorPoolIncorrectArgs() - # { - # $pool = DescriptorPool::getGeneratedPool(); + public function testDescriptorPoolIncorrectArgs() + { + $pool = DescriptorPool::getGeneratedPool(); - # $desc = $pool->getDescriptorByClassName('NotAClass'); - # $this->assertNull($desc); + $desc = $pool->getDescriptorByClassName('NotAClass'); + $this->assertNull($desc); - # $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsEnum())); - # $this->assertNull($desc); + $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsEnum())); + $this->assertNull($desc); - # $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsMessage())); - # $this->assertNull($enumDesc); - # } + $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsMessage())); + $this->assertNull($enumDesc); + } - # ######################################################### - # # Test descriptor. - # ######################################################### + ######################################################### + # Test descriptor. + ######################################################### - # public function testDescriptor() - # { - # $pool = DescriptorPool::getGeneratedPool(); - # $class = get_class(new TestDescriptorsMessage()); - # $this->assertSame('Descriptors\TestDescriptorsMessage', $class); - # $desc = $pool->getDescriptorByClassName($class); + 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->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\FieldDescriptor', $desc->getField(0)); + $this->assertSame(7, $desc->getFieldCount()); - # $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); - # $this->assertSame(1, $desc->getOneofDeclCount()); - # } + $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); + $this->assertSame(1, $desc->getOneofDeclCount()); + } public function testDescriptorForIncludedMessage() { @@ -99,170 +99,161 @@ public function testDescriptorForIncludedMessage() $desc = $pool->getDescriptorByClassName($class); $fielddesc = $desc->getField(17); $subdesc = $fielddesc->getMessageType(); - var_dump($subdesc->getClass()); + $this->assertSame('Bar\TestInclude', $subdesc->getClass()); + } + + ######################################################### + # 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(); + } - # $this->assertSame('bar.TestInclude', $desc->getFullName()); - # $this->assertSame($class, $desc->getClass()); + /** + * @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]; - # $this->assertInstanceOf('\Google\Protobuf\FieldDescriptor', $desc->getField(0)); - # $this->assertSame(7, $desc->getFieldCount()); + $oneofDesc = $desc->getOneofDecl(0); - # $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); - # $this->assertSame(1, $desc->getOneofDeclCount()); + $this->assertSame('my_oneof', $oneofDesc->getName()); + $fieldDescFromOneof = $oneofDesc->getField(0); + $this->assertSame($fieldDesc, $fieldDescFromOneof); + $this->assertSame(1, $oneofDesc->getFieldCount()); } - # ######################################################### - # # 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; - # } + 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/gdb_test.sh b/php/tests/gdb_test.sh index 0b297ba7218b..da5f3f3ac14b 100755 --- a/php/tests/gdb_test.sh +++ b/php/tests/gdb_test.sh @@ -12,8 +12,7 @@ php -i | grep "Configuration" # phpunit` --bootstrap autoload.php tmp_test.php # # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php generated_class_test.php -# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php -gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php descriptors_test.php +gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php # # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php #