1ffe3c632Sopenharmony_ci<?php 2ffe3c632Sopenharmony_ci 3ffe3c632Sopenharmony_cirequire_once('generated/Descriptors/TestDescriptorsEnum.php'); 4ffe3c632Sopenharmony_cirequire_once('generated/Descriptors/TestDescriptorsMessage.php'); 5ffe3c632Sopenharmony_cirequire_once('test_base.php'); 6ffe3c632Sopenharmony_cirequire_once('test_util.php'); 7ffe3c632Sopenharmony_ci 8ffe3c632Sopenharmony_ciuse Google\Protobuf\DescriptorPool; 9ffe3c632Sopenharmony_ciuse Google\Protobuf\Internal\RepeatedField; 10ffe3c632Sopenharmony_ciuse Google\Protobuf\Internal\MapField; 11ffe3c632Sopenharmony_ciuse Descriptors\TestDescriptorsEnum; 12ffe3c632Sopenharmony_ciuse Descriptors\TestDescriptorsMessage; 13ffe3c632Sopenharmony_ciuse Descriptors\TestDescriptorsMessage\Sub; 14ffe3c632Sopenharmony_ciuse Foo\TestMessage; 15ffe3c632Sopenharmony_ciuse Bar\TestInclude; 16ffe3c632Sopenharmony_ci 17ffe3c632Sopenharmony_ciclass DescriptorsTest extends TestBase 18ffe3c632Sopenharmony_ci{ 19ffe3c632Sopenharmony_ci 20ffe3c632Sopenharmony_ci // Redefine these here for compatibility with c extension 21ffe3c632Sopenharmony_ci const GPBLABEL_OPTIONAL = 1; 22ffe3c632Sopenharmony_ci const GPBLABEL_REQUIRED = 2; 23ffe3c632Sopenharmony_ci const GPBLABEL_REPEATED = 3; 24ffe3c632Sopenharmony_ci 25ffe3c632Sopenharmony_ci const GPBTYPE_DOUBLE = 1; 26ffe3c632Sopenharmony_ci const GPBTYPE_FLOAT = 2; 27ffe3c632Sopenharmony_ci const GPBTYPE_INT64 = 3; 28ffe3c632Sopenharmony_ci const GPBTYPE_UINT64 = 4; 29ffe3c632Sopenharmony_ci const GPBTYPE_INT32 = 5; 30ffe3c632Sopenharmony_ci const GPBTYPE_FIXED64 = 6; 31ffe3c632Sopenharmony_ci const GPBTYPE_FIXED32 = 7; 32ffe3c632Sopenharmony_ci const GPBTYPE_BOOL = 8; 33ffe3c632Sopenharmony_ci const GPBTYPE_STRING = 9; 34ffe3c632Sopenharmony_ci const GPBTYPE_GROUP = 10; 35ffe3c632Sopenharmony_ci const GPBTYPE_MESSAGE = 11; 36ffe3c632Sopenharmony_ci const GPBTYPE_BYTES = 12; 37ffe3c632Sopenharmony_ci const GPBTYPE_UINT32 = 13; 38ffe3c632Sopenharmony_ci const GPBTYPE_ENUM = 14; 39ffe3c632Sopenharmony_ci const GPBTYPE_SFIXED32 = 15; 40ffe3c632Sopenharmony_ci const GPBTYPE_SFIXED64 = 16; 41ffe3c632Sopenharmony_ci const GPBTYPE_SINT32 = 17; 42ffe3c632Sopenharmony_ci const GPBTYPE_SINT64 = 18; 43ffe3c632Sopenharmony_ci 44ffe3c632Sopenharmony_ci ######################################################### 45ffe3c632Sopenharmony_ci # Test descriptor pool. 46ffe3c632Sopenharmony_ci ######################################################### 47ffe3c632Sopenharmony_ci 48ffe3c632Sopenharmony_ci public function testDescriptorPool() 49ffe3c632Sopenharmony_ci { 50ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 51ffe3c632Sopenharmony_ci 52ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); 53ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\Descriptor', $desc); 54ffe3c632Sopenharmony_ci 55ffe3c632Sopenharmony_ci $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum())); 56ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $enumDesc); 57ffe3c632Sopenharmony_ci } 58ffe3c632Sopenharmony_ci 59ffe3c632Sopenharmony_ci public function testDescriptorPoolIncorrectArgs() 60ffe3c632Sopenharmony_ci { 61ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 62ffe3c632Sopenharmony_ci 63ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName('NotAClass'); 64ffe3c632Sopenharmony_ci $this->assertNull($desc); 65ffe3c632Sopenharmony_ci 66ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsEnum())); 67ffe3c632Sopenharmony_ci $this->assertNull($desc); 68ffe3c632Sopenharmony_ci 69ffe3c632Sopenharmony_ci $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsMessage())); 70ffe3c632Sopenharmony_ci $this->assertNull($enumDesc); 71ffe3c632Sopenharmony_ci } 72ffe3c632Sopenharmony_ci 73ffe3c632Sopenharmony_ci ######################################################### 74ffe3c632Sopenharmony_ci # Test descriptor. 75ffe3c632Sopenharmony_ci ######################################################### 76ffe3c632Sopenharmony_ci 77ffe3c632Sopenharmony_ci public function testDescriptor() 78ffe3c632Sopenharmony_ci { 79ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 80ffe3c632Sopenharmony_ci $class = get_class(new TestDescriptorsMessage()); 81ffe3c632Sopenharmony_ci $this->assertSame('Descriptors\TestDescriptorsMessage', $class); 82ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName($class); 83ffe3c632Sopenharmony_ci 84ffe3c632Sopenharmony_ci $this->assertSame('descriptors.TestDescriptorsMessage', $desc->getFullName()); 85ffe3c632Sopenharmony_ci $this->assertSame($class, $desc->getClass()); 86ffe3c632Sopenharmony_ci 87ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\FieldDescriptor', $desc->getField(0)); 88ffe3c632Sopenharmony_ci $this->assertSame(7, $desc->getFieldCount()); 89ffe3c632Sopenharmony_ci 90ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\OneofDescriptor', $desc->getOneofDecl(0)); 91ffe3c632Sopenharmony_ci $this->assertSame(1, $desc->getOneofDeclCount()); 92ffe3c632Sopenharmony_ci } 93ffe3c632Sopenharmony_ci 94ffe3c632Sopenharmony_ci public function testDescriptorForIncludedMessage() 95ffe3c632Sopenharmony_ci { 96ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 97ffe3c632Sopenharmony_ci $class = get_class(new TestMessage()); 98ffe3c632Sopenharmony_ci $this->assertSame('Foo\TestMessage', $class); 99ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName($class); 100ffe3c632Sopenharmony_ci $fielddesc = $desc->getField(17); 101ffe3c632Sopenharmony_ci $subdesc = $fielddesc->getMessageType(); 102ffe3c632Sopenharmony_ci $this->assertSame('Bar\TestInclude', $subdesc->getClass()); 103ffe3c632Sopenharmony_ci } 104ffe3c632Sopenharmony_ci 105ffe3c632Sopenharmony_ci ######################################################### 106ffe3c632Sopenharmony_ci # Test enum descriptor. 107ffe3c632Sopenharmony_ci ######################################################### 108ffe3c632Sopenharmony_ci 109ffe3c632Sopenharmony_ci public function testEnumDescriptor() 110ffe3c632Sopenharmony_ci { 111ffe3c632Sopenharmony_ci // WARNINIG - we need to do this so that TestDescriptorsEnum is registered!!? 112ffe3c632Sopenharmony_ci new TestDescriptorsMessage(); 113ffe3c632Sopenharmony_ci 114ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 115ffe3c632Sopenharmony_ci 116ffe3c632Sopenharmony_ci $enumDesc = $pool->getEnumDescriptorByClassName(get_class(new TestDescriptorsEnum())); 117ffe3c632Sopenharmony_ci 118ffe3c632Sopenharmony_ci // Build map of enum values 119ffe3c632Sopenharmony_ci $enumDescMap = []; 120ffe3c632Sopenharmony_ci for ($i = 0; $i < $enumDesc->getValueCount(); $i++) { 121ffe3c632Sopenharmony_ci $enumValueDesc = $enumDesc->getValue($i); 122ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\EnumValueDescriptor', $enumValueDesc); 123ffe3c632Sopenharmony_ci $enumDescMap[$enumValueDesc->getNumber()] = $enumValueDesc->getName(); 124ffe3c632Sopenharmony_ci } 125ffe3c632Sopenharmony_ci 126ffe3c632Sopenharmony_ci $this->assertSame('ZERO', $enumDescMap[0]); 127ffe3c632Sopenharmony_ci $this->assertSame('ONE', $enumDescMap[1]); 128ffe3c632Sopenharmony_ci 129ffe3c632Sopenharmony_ci $this->assertSame(2, $enumDesc->getValueCount()); 130ffe3c632Sopenharmony_ci } 131ffe3c632Sopenharmony_ci 132ffe3c632Sopenharmony_ci ######################################################### 133ffe3c632Sopenharmony_ci # Test field descriptor. 134ffe3c632Sopenharmony_ci ######################################################### 135ffe3c632Sopenharmony_ci 136ffe3c632Sopenharmony_ci public function testFieldDescriptor() 137ffe3c632Sopenharmony_ci { 138ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 139ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); 140ffe3c632Sopenharmony_ci 141ffe3c632Sopenharmony_ci $fieldDescMap = $this->buildFieldMap($desc); 142ffe3c632Sopenharmony_ci 143ffe3c632Sopenharmony_ci // Optional int field 144ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[1]; 145ffe3c632Sopenharmony_ci $this->assertSame('optional_int32', $fieldDesc->getName()); 146ffe3c632Sopenharmony_ci $this->assertSame(1, $fieldDesc->getNumber()); 147ffe3c632Sopenharmony_ci $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); 148ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType()); 149ffe3c632Sopenharmony_ci $this->assertFalse($fieldDesc->isMap()); 150ffe3c632Sopenharmony_ci 151ffe3c632Sopenharmony_ci // Optional enum field 152ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[16]; 153ffe3c632Sopenharmony_ci $this->assertSame('optional_enum', $fieldDesc->getName()); 154ffe3c632Sopenharmony_ci $this->assertSame(16, $fieldDesc->getNumber()); 155ffe3c632Sopenharmony_ci $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); 156ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_ENUM, $fieldDesc->getType()); 157ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\EnumDescriptor', $fieldDesc->getEnumType()); 158ffe3c632Sopenharmony_ci $this->assertFalse($fieldDesc->isMap()); 159ffe3c632Sopenharmony_ci 160ffe3c632Sopenharmony_ci // Optional message field 161ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[17]; 162ffe3c632Sopenharmony_ci $this->assertSame('optional_message', $fieldDesc->getName()); 163ffe3c632Sopenharmony_ci $this->assertSame(17, $fieldDesc->getNumber()); 164ffe3c632Sopenharmony_ci $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); 165ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType()); 166ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType()); 167ffe3c632Sopenharmony_ci $this->assertFalse($fieldDesc->isMap()); 168ffe3c632Sopenharmony_ci 169ffe3c632Sopenharmony_ci // Repeated int field 170ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[31]; 171ffe3c632Sopenharmony_ci $this->assertSame('repeated_int32', $fieldDesc->getName()); 172ffe3c632Sopenharmony_ci $this->assertSame(31, $fieldDesc->getNumber()); 173ffe3c632Sopenharmony_ci $this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel()); 174ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType()); 175ffe3c632Sopenharmony_ci $this->assertFalse($fieldDesc->isMap()); 176ffe3c632Sopenharmony_ci 177ffe3c632Sopenharmony_ci // Repeated message field 178ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[47]; 179ffe3c632Sopenharmony_ci $this->assertSame('repeated_message', $fieldDesc->getName()); 180ffe3c632Sopenharmony_ci $this->assertSame(47, $fieldDesc->getNumber()); 181ffe3c632Sopenharmony_ci $this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel()); 182ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType()); 183ffe3c632Sopenharmony_ci $this->assertInstanceOf('\Google\Protobuf\Descriptor', $fieldDesc->getMessageType()); 184ffe3c632Sopenharmony_ci $this->assertFalse($fieldDesc->isMap()); 185ffe3c632Sopenharmony_ci 186ffe3c632Sopenharmony_ci // Oneof int field 187ffe3c632Sopenharmony_ci // Tested further in testOneofDescriptor() 188ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[51]; 189ffe3c632Sopenharmony_ci $this->assertSame('oneof_int32', $fieldDesc->getName()); 190ffe3c632Sopenharmony_ci $this->assertSame(51, $fieldDesc->getNumber()); 191ffe3c632Sopenharmony_ci $this->assertSame(self::GPBLABEL_OPTIONAL, $fieldDesc->getLabel()); 192ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_INT32, $fieldDesc->getType()); 193ffe3c632Sopenharmony_ci $this->assertFalse($fieldDesc->isMap()); 194ffe3c632Sopenharmony_ci 195ffe3c632Sopenharmony_ci // Map int-enum field 196ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[71]; 197ffe3c632Sopenharmony_ci $this->assertSame('map_int32_enum', $fieldDesc->getName()); 198ffe3c632Sopenharmony_ci $this->assertSame(71, $fieldDesc->getNumber()); 199ffe3c632Sopenharmony_ci $this->assertSame(self::GPBLABEL_REPEATED, $fieldDesc->getLabel()); 200ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_MESSAGE, $fieldDesc->getType()); 201ffe3c632Sopenharmony_ci $this->assertTrue($fieldDesc->isMap()); 202ffe3c632Sopenharmony_ci $mapDesc = $fieldDesc->getMessageType(); 203ffe3c632Sopenharmony_ci $this->assertSame('descriptors.TestDescriptorsMessage.MapInt32EnumEntry', $mapDesc->getFullName()); 204ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_INT32, $mapDesc->getField(0)->getType()); 205ffe3c632Sopenharmony_ci $this->assertSame(self::GPBTYPE_ENUM, $mapDesc->getField(1)->getType()); 206ffe3c632Sopenharmony_ci } 207ffe3c632Sopenharmony_ci 208ffe3c632Sopenharmony_ci /** 209ffe3c632Sopenharmony_ci * @expectedException \Exception 210ffe3c632Sopenharmony_ci */ 211ffe3c632Sopenharmony_ci public function testFieldDescriptorEnumException() 212ffe3c632Sopenharmony_ci { 213ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 214ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); 215ffe3c632Sopenharmony_ci $fieldDesc = $desc->getField(0); 216ffe3c632Sopenharmony_ci $fieldDesc->getEnumType(); 217ffe3c632Sopenharmony_ci } 218ffe3c632Sopenharmony_ci 219ffe3c632Sopenharmony_ci /** 220ffe3c632Sopenharmony_ci * @expectedException \Exception 221ffe3c632Sopenharmony_ci */ 222ffe3c632Sopenharmony_ci public function testFieldDescriptorMessageException() 223ffe3c632Sopenharmony_ci { 224ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 225ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); 226ffe3c632Sopenharmony_ci $fieldDesc = $desc->getField(0); 227ffe3c632Sopenharmony_ci $fieldDesc->getMessageType(); 228ffe3c632Sopenharmony_ci } 229ffe3c632Sopenharmony_ci 230ffe3c632Sopenharmony_ci ######################################################### 231ffe3c632Sopenharmony_ci # Test oneof descriptor. 232ffe3c632Sopenharmony_ci ######################################################### 233ffe3c632Sopenharmony_ci 234ffe3c632Sopenharmony_ci public function testOneofDescriptor() 235ffe3c632Sopenharmony_ci { 236ffe3c632Sopenharmony_ci $pool = DescriptorPool::getGeneratedPool(); 237ffe3c632Sopenharmony_ci $desc = $pool->getDescriptorByClassName(get_class(new TestDescriptorsMessage())); 238ffe3c632Sopenharmony_ci 239ffe3c632Sopenharmony_ci $fieldDescMap = $this->buildFieldMap($desc); 240ffe3c632Sopenharmony_ci $fieldDesc = $fieldDescMap[51]; 241ffe3c632Sopenharmony_ci 242ffe3c632Sopenharmony_ci $oneofDesc = $desc->getOneofDecl(0); 243ffe3c632Sopenharmony_ci 244ffe3c632Sopenharmony_ci $this->assertSame('my_oneof', $oneofDesc->getName()); 245ffe3c632Sopenharmony_ci $fieldDescFromOneof = $oneofDesc->getField(0); 246ffe3c632Sopenharmony_ci $this->assertSame($fieldDesc, $fieldDescFromOneof); 247ffe3c632Sopenharmony_ci $this->assertSame(1, $oneofDesc->getFieldCount()); 248ffe3c632Sopenharmony_ci } 249ffe3c632Sopenharmony_ci 250ffe3c632Sopenharmony_ci private function buildFieldMap($desc) 251ffe3c632Sopenharmony_ci { 252ffe3c632Sopenharmony_ci $fieldDescMap = []; 253ffe3c632Sopenharmony_ci for ($i = 0; $i < $desc->getFieldCount(); $i++) { 254ffe3c632Sopenharmony_ci $fieldDesc = $desc->getField($i); 255ffe3c632Sopenharmony_ci $fieldDescMap[$fieldDesc->getNumber()] = $fieldDesc; 256ffe3c632Sopenharmony_ci } 257ffe3c632Sopenharmony_ci return $fieldDescMap; 258ffe3c632Sopenharmony_ci } 259ffe3c632Sopenharmony_ci} 260