public function testGetResourceSetsSecondOneIsNotVisible() { $fakeSets = array($this->mockResourceSet, $this->mockResourceSet2); Phockito::when($this->mockMetadataProvider->getResourceSets())->return($fakeSets); Phockito::when($this->mockResourceSet->getName())->return("fake name 1"); Phockito::when($this->mockResourceSet2->getName())->return("fake name 2"); Phockito::when($this->mockResourceSet->getResourceType())->return($this->mockResourceType); Phockito::when($this->mockResourceSet2->getResourceType())->return($this->mockResourceType); Phockito::when($this->mockServiceConfig->getEntitySetAccessRule($this->mockResourceSet))->return(EntitySetRights::NONE); Phockito::when($this->mockServiceConfig->getEntitySetAccessRule($this->mockResourceSet2))->return(EntitySetRights::READ_SINGLE); $wrapper = $this->getMockedWrapper(); $actual = $wrapper->getResourceSets(); $expected = array(new ResourceSetWrapper($this->mockResourceSet2, $this->mockServiceConfig)); $this->assertEquals($expected, $actual); }
/** * Check wrapped resource set's resource type or any of the resource type derived * from the this resource type has bag property associated with it. * * @param ProvidersWrapper $provider Metadata query provider wrapper * * @return boolean */ public function hasBagProperty(ProvidersWrapper $provider) { $arrayToDetectLoop = array(); $hasBagProperty = $this->_resourceSet->getResourceType()->hasBagProperty($arrayToDetectLoop); unset($arrayToDetectLoop); // This will check only the resource type associated with // the resource set, we need to check presence of bag property // in resource type which is derived form this resource type also. if (!$hasBagProperty) { $derivedTypes = $provider->getDerivedTypes($this->_resourceSet->getResourceType()); foreach ($derivedTypes as $derivedType) { $arrayToDetectLoop = array(); if ($derivedType->hasBagProperty($arrayToDetectLoop)) { return true; } } } return false; }
/** * Gets an entity instance from an entity set identifed by a key * * @param ResourceSet $resourceSet The entity set from which * an entity needs to be fetched * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched * * @return object|null Returns entity instance if found else null */ public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor) { $resourceSetName = $resourceSet->getName(); if ($resourceSetName !== 'Customers' && $resourceSetName !== 'Orders' && $resourceSetName !== 'Order_Details' && $resourceSetName !== 'Products' && $resourceSetName !== 'Employees') { die('(NorthWindQueryProvider) Unknown resource set ' . $resourceSetName); } if ($resourceSetName === 'Order_Details') { $resourceSetName = 'Order Details'; } $namedKeyValues = $keyDescriptor->getValidatedNamedValues(); $condition = null; foreach ($namedKeyValues as $key => $value) { $condition .= $key . ' = ' . $value[0] . ' and '; } $len = strlen($condition); $condition = substr($condition, 0, $len - 5); $query = "SELECT * FROM [{$resourceSetName}] WHERE {$condition}"; $stmt = sqlsrv_query($this->_connectionHandle, $query); if ($stmt === false) { $errorAsString = self::_getSQLSRVError(); throw ODataException::createInternalServerError($errorAsString); } //If resource not found return null to the library if (!sqlsrv_has_rows($stmt)) { return null; } $result = null; while ($record = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { switch ($resourceSetName) { case 'Customers': $result = $this->_serializeCustomer($record); break; case 'Orders': $result = $this->_serializeOrder($record); break; case 'Order Details': $result = $this->_serializeOrderDetail($record); break; case 'Employees': $result = $this->_serializeEmployee($record); break; } } sqlsrv_free_stmt($stmt); return $result; }
/** * For queries like http://localhost/NorthWind.svc/Orders(10643)/Customer */ public function getRelatedResourceReference(ResourceSet $sourceResourceSet, $sourceEntityInstance, ResourceSet $targetResourceSet, ResourceProperty $targetProperty) { $entityClassName = $targetResourceSet->getResourceType()->getInstanceType()->name; $entityName = $this->getEntityName($entityClassName); $fieldName = $this->getTableName($entityName) . '_id'; return $this->getResource($targetResourceSet, null, ['id' => $sourceEntityInstance->{$fieldName}]); }
/** * To add a navigation property (resource set or resource reference) * to a resource type * * @param ResourceType $resourceType The resource type to add * the resource reference * or resource * reference set property to * @param string $name The name of the * property to add * @param ResourceSet $targetResourceSet The resource set the * resource reference * or reference * set property * ponits to * @param ResourcePropertyKind $resourcePropertyKind The property kind * * @return void */ private function _addReferencePropertyInternal(ResourceType $resourceType, $name, ResourceSet $targetResourceSet, $resourcePropertyKind) { try { $resourceType->getInstanceType()->getProperty($name); } catch (\ReflectionException $exception) { throw new InvalidOperationException('Can\'t add a property which does not exist on the instance type.'); } if (!($resourcePropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE || $resourcePropertyKind == ResourcePropertyKind::RESOURCE_REFERENCE)) { throw new InvalidOperationException('Property kind should be ResourceSetReference or ResourceReference'); } $targetResourceType = $targetResourceSet->getResourceType(); $resourceProperty = new ResourceProperty($name, null, $resourcePropertyKind, $targetResourceType); $resourceType->addProperty($resourceProperty); //Create instance of AssociationSet for this relationship $sourceResourceSet = $resourceType->getCustomState(); if (is_null($sourceResourceSet)) { throw new InvalidOperationException('Failed to retrieve the custom state from ' . $resourceType->getName()); } //Customer_Orders_Orders, Order_Customer_Customers //(source type::name _ source property::name _ target set::name) $setKey = $resourceType->getName() . '_' . $name . '_' . $targetResourceSet->getName(); $set = new ResourceAssociationSet($setKey, new ResourceAssociationSetEnd($sourceResourceSet, $resourceType, $resourceProperty), new ResourceAssociationSetEnd($targetResourceSet, $targetResourceSet->getResourceType(), null)); $this->associationSets[$setKey] = $set; }
/** * test ResourceSet class */ public function testResourceSet() { $int64 = ResourceType::getPrimitiveResourceType(EdmPrimitiveType::INT64); try { $customerResourceSet = new ResourceSet('Customers', $int64); $this->fail('An expected InvalidArgumentException for \'non-entity type resource type\' has not been raised'); } catch (\InvalidArgumentException $exception) { $this->assertStringStartsWith('The ResourceTypeKind property of a ResourceType instance associated with a ResourceSet', $exception->getMessage()); } $customerResType = $this->_getCustomerResourceType(); $customerResourceSet = new ResourceSet('Customers', $customerResType); $this->assertEquals($customerResourceSet->getName(), 'Customers'); $this->assertEquals($customerResourceSet->getResourceType()->getName(), 'Customer'); }
/** * To check this relationship belongs to a specific resource set, type * and property * * @param ResourceSet $resourceSet Resource set for the association * end * @param ResourceType $resourceType Resource type for the association * end * @param ResourceProperty $resourceProperty Resource property for the * association end * * @return boolean */ public function isBelongsTo(ResourceSet $resourceSet, ResourceType $resourceType, ResourceProperty $resourceProperty) { return strcmp($resourceSet->getName(), $this->_resourceSet->getName()) == 0 && $this->_resourceType->isAssignableFrom($resourceType) && (is_null($resourceProperty) && is_null($this->_resourceProperty) || !is_null($resourceProperty) && !is_null($this->_resourceProperty) && strcmp($resourceProperty->getName(), $this->_resourceProperty->getName()) == 0); }
/** * Gets an entity instance from an entity set identifed by a key * * @param ResourceSet $resourceSet The entity set from which an entity * needs to be fetched * @param KeyDescriptor $keyDescriptor The key to identify the entity * to be fetched * * @return object|null Returns entity instance if found else null */ public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor) { $resourceSetName = $resourceSet->getName(); if ($resourceSetName !== 'Posts' && $resourceSetName !== 'Tags' && $resourceSetName !== 'Categories' && $resourceSetName !== 'Comments' && $resourceSetName !== 'Users') { die('(WordPressQueryProvider) Unknown resource set ' . $resourceSetName); } $namedKeyValues = $keyDescriptor->getValidatedNamedValues(); $keys = array(); foreach ($namedKeyValues as $key => $value) { $keys[] = "{$key} = '{$value['0']}' "; } $conditionStr = implode(' AND ', $keys); switch ($resourceSetName) { case 'Posts': $query = "SELECT * FROM `wp_posts` WHERE" . " wp_posts.post_type = 'post'" . " AND wp_posts.post_status = 'publish'" . " AND wp_posts.ID = " . $namedKeyValues['PostID'][0]; $stmt = mysql_query($query); //If resource not found return null to the library if (!mysql_num_rows($stmt)) { return null; } $data = mysql_fetch_assoc($stmt); $result = $this->_serializePost($data); break; case 'Tags': $query = "SELECT t.*, tt.description" . " FROM `wp_terms` AS t INNER JOIN `wp_term_taxonomy` as tt" . " ON tt.term_id = t.term_id" . " WHERE tt.taxonomy = 'post_tag'" . " AND t.term_id = " . $namedKeyValues['TagID'][0]; $stmt = mysql_query($query); //If resource not found return null to the library if (!mysql_num_rows($stmt)) { return null; } $data = mysql_fetch_assoc($stmt); $result = $this->_serializeTag($data); break; case 'Categories': $query = "SELECT t.*, tt.description" . " FROM `wp_terms` AS t INNER JOIN `wp_term_taxonomy` as tt" . " ON tt.term_id = t.term_id" . " WHERE tt.taxonomy = 'category'" . " AND t.term_id = " . $namedKeyValues['CategoryID'][0]; $stmt = mysql_query($query); //If resource not found return null to the library if (!mysql_num_rows($stmt)) { return null; } $data = mysql_fetch_assoc($stmt); $result = $this->_serializeCategory($data); break; case 'Comments': $query = "SELECT * FROM `wp_comments`" . " WHERE comment_approved = 1" . " AND comment_ID = " . $namedKeyValues['CommentID'][0]; $stmt = mysql_query($query); //If resource not found return null to the library if (!mysql_num_rows($stmt)) { return null; } $data = mysql_fetch_assoc($stmt); $result = $this->_serializeComment($data); break; case 'Users': $query = "SELECT * FROM `wp_users` WHERE ID = " . $namedKeyValues['UserID'][0]; $stmt = mysql_query($query); //If resource not found return null to the library if (!mysql_num_rows($stmt)) { return null; } $data = mysql_fetch_assoc($stmt); $result = $this->_serializeUser($data); break; } mysql_free_result($stmt); return $result; }
/** * Validate the given entity instance. * * @param object $entityInstance Entity instance to validate * @param ResourceSet &$resourceSet Resource set to which the entity * instance belongs to. * @param KeyDescriptor &$keyDescriptor The key descriptor. * @param string $methodName Method from which this function * invoked. * * @return void * * @throws ODataException */ private function _validateEntityInstance($entityInstance, ResourceSet &$resourceSet, KeyDescriptor &$keyDescriptor, $methodName) { if (is_null($entityInstance)) { throw ODataException::createResourceNotFoundError($resourceSet->getName()); } // lion: if ($_SERVER['REQUEST_METHOD'] == HTTPRequestMethod::DELETE()) { return; } $entityName = $resourceSet->getResourceType()->getInstanceType()->getName(); if (!is_object($entityInstance) || !$entityInstance instanceof $entityName) { throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsUnExpectedType($entityName, $methodName)); } foreach ($keyDescriptor->getValidatedNamedValues() as $keyName => $valueDescription) { try { $keyProperty = new \ReflectionProperty($entityInstance, $keyName); $keyValue = $keyProperty->getValue($entityInstance); if (is_null($keyValue)) { throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsInstanceWithNullKeyProperties($methodName)); } $convertedValue = $valueDescription[1]->convert($valueDescription[0]); if ($keyValue != $convertedValue) { throw ODataException::createInternalServerError(Messages::providersWrapperIDSQPMethodReturnsInstanceWithNonMatchingKeys($methodName)); } } catch (\ReflectionException $reflectionException) { //throw ODataException::createInternalServerError( // Messages::orderByParserFailedToAccessOrInitializeProperty( // $resourceProperty->getName(), $resourceType->getName() // ) //); } } }
/** * Gets the maximum page size for an entity set resource * * @param ResourceSet $resourceSet Entity set for which to get the page size * * @return int */ public function getEntitySetPageSize(ResourceSet $resourceSet) { if (!array_key_exists($resourceSet->getName(), $this->_pageSizes)) { return $this->_defaultPageSize; } return $this->_pageSizes[$resourceSet->getName()]; }