/**
  * Get related resource for a resource
  * 
  * @param ResourceSet      $sourceResourceSet The source resource set
  * @param object           $sourceEntity      The source resource
  * @param ResourceSet      $targetResourceSet The resource set of the navigation
  *                                            property
  * @param ResourceProperty $targetProperty    The navigation property to be 
  *                                            retrieved
  * 
  * @return object|null The related resource if exists else null
  */
 public function getRelatedResourceReference(ResourceSet $sourceResourceSet, $sourceEntity, ResourceSet $targetResourceSet, ResourceProperty $targetProperty)
 {
     $entityInstance = $this->queryProvider->getRelatedResourceReference($sourceResourceSet, $sourceEntity, $targetResourceSet, $targetProperty);
     // we will not throw error if the resource reference is null
     // e.g. Orders(1234)/Customer => Customer can be null, this is
     // allowed if Customer is last segment. consider the following:
     // Orders(1234)/Customer/Orders => here if Customer is null then
     // the UriProcessor will throw error.
     if (!is_null($entityInstance)) {
         $entityName = $targetResourceSet->getResourceType()->getInstanceType()->getName();
         if (!is_object($entityInstance) || !$entityInstance instanceof $entityName) {
             throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsUnExpectedType($entityName, 'IQueryProvider::getRelatedResourceReference'));
         }
         foreach ($targetProperty->getResourceType()->getKeyProperties() as $keyName => $resourceProperty) {
             try {
                 $keyProperty = new \ReflectionProperty($entityInstance, $keyName);
                 $keyValue = $keyProperty->getValue($entityInstance);
                 if (is_null($keyValue)) {
                     throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsInstanceWithNullKeyProperties('IDSQP::getRelatedResourceReference'));
                 }
             } catch (\ReflectionException $reflectionException) {
                 //throw ODataException::createInternalServerError(
                 //    Messages::orderByParserFailedToAccessOrInitializeProperty(
                 //        $resourceProperty->getName(), $resourceType->getName()
                 //    )
                 //);
             }
         }
     }
     return $entityInstance;
 }
 public function testGetRelatedResourceSetReturnsArrayWhenQueryTypeIsEntitiesWithCount()
 {
     $orderBy = null;
     $top = 10;
     $skip = 10;
     $fakeQueryResult = new QueryResult();
     $fakeQueryResult->count = 4;
     $fakeQueryResult->results = null;
     //null is not an array
     $fakeSourceEntity = new \stdClass();
     Phockito::when($this->mockQueryProvider->getRelatedResourceSet(QueryType::ENTITIES_WITH_COUNT(), $this->mockResourceSet, $fakeSourceEntity, $this->mockResourceSet2, $this->mockResourceProperty, $this->mockFilterInfo, $orderBy, $top, $skip))->return($fakeQueryResult);
     $wrapper = $this->getMockedWrapper();
     try {
         $wrapper->getRelatedResourceSet(QueryType::ENTITIES_WITH_COUNT(), $this->mockResourceSet, $fakeSourceEntity, $this->mockResourceSet2, $this->mockResourceProperty, $this->mockFilterInfo, $orderBy, $top, $skip);
         $this->fail("expected exception not thrown");
     } catch (ODataException $ex) {
         $this->assertEquals(Messages::queryProviderResultsMissing("IQueryProvider::getRelatedResourceSet", QueryType::ENTITIES_WITH_COUNT()), $ex->getMessage());
         $this->assertEquals(500, $ex->getStatusCode());
     }
 }