예제 #1
0
 /**
  * test ResourceType class
  */
 public function testResourceType()
 {
     $exceptionThrown = false;
     try {
         ResourceType::getPrimitiveResourceType(TypeCode::VOID);
         $this->fail('An expected InvalidArgumentException for \'EdmPrimitiveType\' has not been raised');
     } catch (\InvalidArgumentException $exception) {
         $this->assertStringEndsWith('is not a valid EdmPrimitiveType Enum value', $exception->getMessage());
     }
     $int16ResType = new ResourceType(new Int16(), ResourceTypeKind::PRIMITIVE, 'Int16', 'Edm');
     try {
         $int32ResType = new ResourceType(new Int32(), ResourceTypeKind::PRIMITIVE, 'Int32', 'Edm', $int16ResType);
         $this->fail('An expected InvalidArgumentException for \'basetype\' has not been raised');
     } catch (\InvalidArgumentException $exception) {
         $this->AssertEquals('Primitive type cannot have base type', $exception->getMessage());
     }
     try {
         $int32ResType = new ResourceType(new Int32(), ResourceTypeKind::PRIMITIVE, 'Int32', 'Edm', null, true);
         $this->fail('An expected InvalidArgumentException for \'abstract\' has not been raised');
     } catch (\InvalidArgumentException $exception) {
         $this->AssertEquals('Primitive type cannot be abstract', $exception->getMessage());
     }
     try {
         $int32ResType = new ResourceType(null, ResourceTypeKind::PRIMITIVE, 'Int32', 'Edm');
         $this->fail('An expected InvalidArgumentException for \'IType\' has not been raised');
     } catch (\InvalidArgumentException $exception) {
         $this->assertStringEndsWith('should be an \'IType\' implementor instance', $exception->getMessage());
     }
     try {
         $customerResType = new ResourceType(null, ResourceTypeKind::ENTITY, 'Customer', 'Northwind');
         $this->fail('An expected InvalidArgumentException for \'ReflectionClass\' has not been raised');
     } catch (\InvalidArgumentException $exception) {
         $this->assertStringEndsWith('argument should be an \'ReflectionClass\' instance', $exception->getMessage());
     }
     $customerResType = new ResourceType(new \ReflectionClass('UnitTests\\POData\\Facets\\NorthWind1\\Customer2'), ResourceTypeKind::ENTITY, 'Customer', 'Northwind');
     $this->AssertEquals($customerResType->getName(), 'Customer');
     $this->AssertEquals($customerResType->getFullName(), 'Northwind.Customer');
     $this->assertTrue($customerResType->getInstanceType() instanceof \ReflectionClass);
     $this->AssertEquals($customerResType->getNamespace(), 'Northwind');
     $this->AssertEquals($customerResType->getResourceTypeKind(), ResourceTypeKind::ENTITY);
     $this->AssertEquals($customerResType->isMediaLinkEntry(), false);
     try {
         $customerResType->validateType();
         $this->fail('An expected InvalidOperationException for \'No key defined\' has not been raised');
     } catch (InvalidOperationException $exception) {
         $this->assertStringEndsWith('Please make sure the key properties are defined for this entity type', $exception->getMessage());
     }
     $int32ResourceType = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::INT32);
     $primitiveResourceType = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::STRING);
     $testProperty = new ResourceProperty('test', null, ResourcePropertyKind::PRIMITIVE, $primitiveResourceType);
     try {
         $int32ResourceType->addProperty($testProperty);
         $this->fail('An expected InvalidOperationException for \'property on primitive\' has not been raised');
     } catch (InvalidOperationException $exception) {
         $this->assertStringEndsWith('ResourceType instances with a ResourceTypeKind equal to \'Primitive\'', $exception->getMessage());
     }
     $stringResourceType = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::STRING);
     $customerIDPrimProperty = new ResourceProperty('CustomerID', null, ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::KEY, $stringResourceType);
     $customerNamePrimProperty = new ResourceProperty('CustomerName', null, ResourcePropertyKind::PRIMITIVE, $stringResourceType);
     $intResourceType = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::INT32);
     $ratingPrimProperty = new ResourceProperty('Rating', null, ResourcePropertyKind::PRIMITIVE, $intResourceType);
     $addressResType = new ResourceType(new \ReflectionClass('UnitTests\\POData\\Facets\\NorthWind1\\Address2'), ResourceTypeKind::COMPLEX, 'Address', 'Northwind');
     $booleanResourceType = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::BOOLEAN);
     $isPrimaryPrimProperty = new ResourceProperty('IsPrimary', null, ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::KEY, $booleanResourceType);
     try {
         $addressResType->addProperty($isPrimaryPrimProperty);
         $this->fail('An expected InvalidOperationException for \'Key on non-entity\' has not been raised');
     } catch (InvalidOperationException $exception) {
         $this->assertStringEndsWith('ResourceType instances with a ResourceTypeKind equal to \'EntityType\'', $exception->getMessage());
     }
     $booleanResourceType = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::BOOLEAN);
     $isPrimaryPrimProperty = new ResourceProperty('IsPrimary', null, ResourcePropertyKind::PRIMITIVE, $booleanResourceType);
     $addressResType->addProperty($isPrimaryPrimProperty);
     try {
         $addressResType->addProperty($isPrimaryPrimProperty);
         $this->fail('An expected InvalidArgumentException for \'Property duplication\' has not been raised');
     } catch (InvalidOperationException $exception) {
         $this->assertStringStartsWith('Property with same name \'IsPrimary\' already exists in type \'Address\'', $exception->getMessage());
     }
     try {
         $addressResType->setMediaLinkEntry(true);
         $this->fail('An expected InvalidOperationException for \'MLE on non-entity\' has not been raised');
     } catch (InvalidOperationException $exception) {
         $this->assertStringStartsWith('Cannot apply the HasStreamAttribute', $exception->getMessage());
     }
     $customerAdrComplexType = new ResourceProperty('Address', null, ResourcePropertyKind::COMPLEX_TYPE, $addressResType);
     $customerResType->addProperty($customerIDPrimProperty);
     $customerResType->addProperty($customerNamePrimProperty);
     $customerResType->addProperty($ratingPrimProperty);
     $customerResType->addProperty($customerAdrComplexType);
     $customerResType->validateType();
     $customerProperties = $customerResType->getPropertiesDeclaredOnThisType();
     $this->AssertEquals(count($customerProperties), 4);
     $customerAllProperties = $customerResType->getAllProperties();
     $this->AssertEquals(count($customerProperties), count($customerAllProperties));
     $keys = array('CustomerID', 'CustomerName', 'Rating', 'Address');
     $i = 0;
     foreach ($customerAllProperties as $key => $customerProperty) {
         $this->AssertEquals($key, $keys[$i++]);
     }
     $entityKeys = array('CustomerID');
     $customerKeyProperties = $customerResType->getKeyProperties();
     $i = 0;
     foreach ($customerKeyProperties as $key => $customerKeyProperty) {
         $this->AssertEquals($key, $entityKeys[$i++]);
     }
     $this->AssertEquals(count($customerResType->getETagProperties()), 0);
     $this->AssertEquals($customerResType->resolveProperty('PropNotExists'), null);
     $property = $customerResType->resolveProperty('CustomerName');
     $this->AssertNotEquals($property, null);
     $this->AssertEquals($property->getName(), 'CustomerName');
     $employeeResType = new ResourceType(new \ReflectionClass('UnitTests\\POData\\Facets\\NorthWind1\\Employee2'), ResourceTypeKind::ENTITY, 'Employee', 'Northwind');
     $stringResourceType = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::STRING);
     $employeeResType->addProperty(new ResourceProperty('EmployeeID', null, ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::KEY, $stringResourceType));
     $employeeResType->addProperty(new ResourceProperty('Emails', null, ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::BAG, $stringResourceType));
     $employeeResType->setMediaLinkEntry(true);
     $employeeResType->addNamedStream(new ResourceStreamInfo('ThumNail_64X64'));
     try {
         $employeeResType->addNamedStream(new ResourceStreamInfo('ThumNail_64X64'));
         $this->fail('An expected InvalidOperationException for \'named stream duplication\' has not been raised');
     } catch (InvalidOperationException $exception) {
         $this->assertStringStartsWith('Named stream with the name \'ThumNail_64X64\' already exists in type \'Employee\'', $exception->getMessage());
     }
     $this->AssertEquals($employeeResType->hasNamedStream(), true);
     $b = array();
     $this->AssertEquals($employeeResType->hasBagProperty($b), true);
     $namedStreams = $employeeResType->getAllNamedStreams();
     $this->AssertEquals(count($namedStreams), 1);
     $this->AssertTrue(array_key_exists('ThumNail_64X64', $namedStreams));
     $name = $employeeResType->tryResolveNamedStreamByName('ThumNail_64X64')->getName();
     $this->AssertEquals($name, 'ThumNail_64X64');
 }
예제 #2
0
 /**
  * Returns the etag for the given resource.
  * Note: This function will not add W\" prefix and " suffix, its callers
  * repsonsability.
  *
  * @param mixed        &$entryObject  Resource for which etag value needs to 
  *                                    be returned
  * @param ResourceType &$resourceType Resource type of the $entryObject
  * 
  * @return string|null ETag value for the given resource (with values encoded 
  *                     for use in a URI) there are etag properties, NULL if 
  *                     there is no etag property.
  */
 protected function getETagForEntry(&$entryObject, ResourceType &$resourceType)
 {
     $eTag = null;
     $comma = null;
     foreach ($resourceType->getETagProperties() as $eTagProperty) {
         $type = $eTagProperty->getInstanceType();
         self::assert(!is_null($type) && $type instanceof IType, '!is_null($type) && $type instanceof IType');
         $value = null;
         try {
             //TODO #88...also this seems like dupe work
             $reflectionProperty = new \ReflectionProperty($entryObject, $eTagProperty->getName());
             $value = $reflectionProperty->getValue($entryObject);
         } catch (\ReflectionException $reflectionException) {
             throw ODataException::createInternalServerError(Messages::failedToAccessProperty($eTagProperty->getName(), $resourceType->getName()));
         }
         if (is_null($value)) {
             $eTag = $eTag . $comma . 'null';
         } else {
             $eTag = $eTag . $comma . $type->convertToOData($value);
         }
         $comma = ',';
     }
     if (!is_null($eTag)) {
         // If eTag is made up of datetime or string properties then the above
         // IType::convertToOData will perform utf8 and url encode. But we don't
         // want this for eTag value.
         $eTag = urldecode(utf8_decode($eTag));
         return rtrim($eTag, ',');
     }
     return null;
 }
예제 #3
0
 /**
  * Returns the etag for the given resource.
  * 
  * @param mixed        $entryObject  Resource for which etag value
  *                                    needs to be returned
  * @param ResourceType $resourceType Resource type of the $entryObject
  * 
  * @return string|null ETag value for the given resource 
  * (with values encoded for use in a URI)
  * if there are etag properties, NULL if there is no etag property.
  */
 protected function getETagForEntry($entryObject, ResourceType $resourceType)
 {
     $eTag = null;
     $comma = null;
     foreach ($resourceType->getETagProperties() as $eTagProperty) {
         $type = $eTagProperty->getInstanceType();
         $this->assert(!is_null($type) && $type instanceof IType, '!is_null($type) && $type instanceof IType');
         $value = $this->getPropertyValue($entryObject, $resourceType, $eTagProperty);
         if (is_null($value)) {
             $eTag = $eTag . $comma . 'null';
         } else {
             $eTag = $eTag . $comma . $type->convertToOData($value);
         }
         $comma = ',';
     }
     if (!is_null($eTag)) {
         // If eTag is made up of datetime or string properties then the above
         // IType::converToOData will perform utf8 and url encode. But we don't
         // want this for eTag value.
         $eTag = urldecode(utf8_decode($eTag));
         return ODataConstants::HTTP_WEAK_ETAG_PREFIX . rtrim($eTag, ',') . '"';
     }
     return null;
 }