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