/**
  * @dataProvider provider
  */
 public function testGetResponseContentType($id, TargetKind $target, Version $version, $acceptsHeader, $format, $expectedValue)
 {
     Phockito::when($this->mockRequest->getTargetKind())->return($target);
     Phockito::when($this->mockHost->getRequestAccept())->return($acceptsHeader);
     Phockito::when($this->mockHost->getQueryStringItem(ODataConstants::HTTPQUERY_STRING_FORMAT))->return($format);
     Phockito::when($this->mockRequest->getResponseVersion())->return($version);
     $actual = BaseService::getResponseContentType($this->mockRequest, $this->mockUriProcessor, $this->mockService);
     //accepts doesn't match any possibles actual for that format..so it should return null
     $this->assertEquals($expectedValue, $actual, $id);
 }
Ejemplo n.º 2
0
 public function __construct(array $hostInfo)
 {
     $this->_hostInfo = $hostInfo;
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $_SERVER[ODataConstants::HTTPREQUEST_PROTOCOL] = $this->_hostInfo['AbsoluteRequestUri']->getScheme();
     $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_HOST)] = $this->_hostInfo['AbsoluteRequestUri']->getHost() . ':' . $this->_hostInfo['AbsoluteRequestUri']->getPort();
     $_SERVER[ODataConstants::HTTPREQUEST_URI] = $this->_hostInfo['AbsoluteRequestUri']->getPath();
     if (array_key_exists('DataServiceVersion', $this->_hostInfo)) {
         $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_DATA_SERVICE_VERSION)] = $this->_hostInfo['DataServiceVersion']->toString();
     }
     if (array_key_exists('MaxDataServiceVersion', $this->_hostInfo)) {
         $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_MAX_DATA_SERVICE_VERSION)] = $this->_hostInfo['MaxDataServiceVersion']->toString();
     }
     if (array_key_exists('RequestIfMatch', $this->_hostInfo)) {
         $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_IF_MATCH)] = $this->_hostInfo['RequestIfMatch'];
     }
     if (array_key_exists('RequestIfNoneMatch', $this->_hostInfo)) {
         $_SERVER[HttpProcessUtility::headerToServerKey(ODataConstants::HTTPREQUEST_HEADER_IF_NONE)] = $this->_hostInfo['RequestIfNoneMatch'];
     }
     if (array_key_exists('QueryString', $this->_hostInfo)) {
         $_SERVER[ODataConstants::HTTPREQUEST_QUERY_STRING] = $this->_hostInfo['QueryString'];
     }
     //print_r($_SERVER);
     parent::__construct();
     if (array_key_exists('AbsoluteServiceUri', $this->_hostInfo)) {
         $this->setServiceUri($this->_hostInfo['AbsoluteServiceUri']->getUrlAsString());
     }
 }
Ejemplo n.º 3
0
 public function testRegisterWritersV3()
 {
     /** @var BaseService $service */
     $service = Phockito::spy('\\POData\\BaseService');
     $service->setHost($this->mockHost);
     //TODO: have to do this since the registry & config is actually only instantiated during a handleRequest
     //will change this once that request pipeline is cleaned up
     Phockito::when($service->getODataWriterRegistry())->return($this->mockRegistry);
     $fakeConfig = new ServiceConfiguration($this->mockMetaProvider);
     $fakeConfig->setMaxDataServiceVersion(ProtocolVersion::V3());
     Phockito::when($service->getConfiguration())->return($fakeConfig);
     //fake the service url
     $fakeUrl = "http://host/service.svc/Collection";
     Phockito::when($this->mockHost->getAbsoluteServiceUri())->return(new Url($fakeUrl));
     Phockito::verify($this->mockRegistry, 0)->register(anything());
     //nothing should be registered at first
     $service->registerWriters();
     //only 2 writers for v1
     Phockito::verify($this->mockRegistry, 6)->register(anything());
     Phockito::verify($this->mockRegistry, 1)->register(anInstanceOf('\\POData\\Writers\\Atom\\AtomODataWriter'));
     Phockito::verify($this->mockRegistry, 5)->register(anInstanceOf('\\POData\\Writers\\Json\\JsonODataV1Writer'));
     //since v2 & light derives from this,,it's 1+1+3 times
     Phockito::verify($this->mockRegistry, 4)->register(anInstanceOf('\\POData\\Writers\\Json\\JsonODataV2Writer'));
     //since light derives from this it's 1+3 times
     Phockito::verify($this->mockRegistry, 3)->register(anInstanceOf('\\POData\\Writers\\Json\\JsonLightODataWriter'));
 }
Ejemplo n.º 4
0
 public function actionIndex($metaProviderClassName = 'qeti\\Yii2PODataAdapter\\implementation\\MetadataProvider', $queryProviderMap = '@vendor/qeti/Yii2PODataAdapter/implementation/QueryProvider.php')
 {
     yii::$classMap['qeti\\SimplePOData\\QueryProvider'] = $queryProviderMap;
     $op = new OperationContextAdapter(yii::$app->request);
     $host = new ServiceHost($op);
     $host->setServiceUri("/odata.svc/");
     $service = new DataService(yii::$app->db, $metaProviderClassName::create());
     $service->setHost($host);
     $service->handleRequest();
     $odataResponse = $op->outgoingResponse();
     $response = yii::$app->response;
     foreach ($odataResponse->getHeaders() as $headerName => $headerValue) {
         if (!is_null($headerValue)) {
             $response->headers->set($headerName, $headerValue);
         }
     }
     return $odataResponse->getStream();
 }
Ejemplo n.º 5
0
 public function testTranslateFormatToMimeVersion30FormatRandom()
 {
     $format = uniqid("xxx");
     $actual = ServiceHost::translateFormatToMime(Version::v3(), $format);
     $expected = "{$format};q=1.0";
     $this->assertEquals($expected, $actual);
 }
Ejemplo n.º 6
0
use POData\OperationContext\ServiceHost;
use qeti\SimplePOData\DataService;
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/OperationContextAdapter.php';
require __DIR__ . '/RequestAdapter.php';
require __DIR__ . '/QueryProvider.php';
require __DIR__ . '/models/MetadataProvider.php';
require __DIR__ . '/models/Product.php';
// DB Connection
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = '******';
$password = '******';
$db = new \PDO($dsn, $user, $password);
// Realisation of QueryProvider
$db->queryProviderClassName = '\\QueryProvider';
// Controller
$op = new OperationContextAdapter($_GET);
$host = new ServiceHost($op);
$host->setServiceUri("/odata.svc/");
$service = new DataService($db, \models\MetadataProvider::create());
$service->setHost($host);
$service->handleRequest();
$odataResponse = $op->outgoingResponse();
// Headers for response
foreach ($odataResponse->getHeaders() as $headerName => $headerValue) {
    if (!is_null($headerValue)) {
        header($headerName . ': ' . $headerValue);
    }
}
// Body of response
echo $odataResponse->getStream();
Ejemplo n.º 7
0
 /**
  * For the given entry object compare it's eTag (if it has eTag properties)
  * with current eTag request headers (if it present).
  * 
  * @param mixed        &$entryObject             entity resource for which etag 
  *                                               needs to be checked.
  * @param ResourceType &$resourceType            Resource type of the entry 
  *                                               object.
  * @param boolean      &$needToSerializeResponse On return, this will contain 
  *                                               True if response needs to be
  *                                               serialized, False otherwise.
  *                                              
  * @return string|null The ETag for the entry object if it has eTag properties 
  *                     NULL otherwise.
  */
 protected function compareETag(&$entryObject, ResourceType &$resourceType, &$needToSerializeResponse)
 {
     $needToSerializeResponse = true;
     $eTag = null;
     $ifMatch = $this->_serviceHost->getRequestIfMatch();
     $ifNoneMatch = $this->_serviceHost->getRequestIfNoneMatch();
     if (is_null($entryObject)) {
         if (!is_null($ifMatch)) {
             throw ODataException::createPreConditionFailedError(Messages::eTagNotAllowedForNonExistingResource());
         }
         return null;
     }
     if ($this->config->getValidateETagHeader() && !$resourceType->hasETagProperties()) {
         if (!is_null($ifMatch) || !is_null($ifNoneMatch)) {
             // No eTag properties but request has eTag headers, bad request
             throw ODataException::createBadRequestError(Messages::noETagPropertiesForType());
         }
         // We need write the response but no eTag header
         return null;
     }
     if (!$this->config->getValidateETagHeader()) {
         // Configuration says do not validate ETag so we will not write ETag header in the
         // response even though the requested resource support it
         return null;
     }
     if (is_null($ifMatch) && is_null($ifNoneMatch)) {
         // No request eTag header, we need to write the response
         // and eTag header
     } else {
         if (strcmp($ifMatch, '*') == 0) {
             // If-Match:* => we need to write the response and eTag header
         } else {
             if (strcmp($ifNoneMatch, '*') == 0) {
                 // if-None-Match:* => Do not write the response (304 not modified),
                 // but write eTag header
                 $needToSerializeResponse = false;
             } else {
                 $eTag = $this->getETagForEntry($entryObject, $resourceType);
                 // Note: The following code for attaching the prefix W\"
                 // and the suffix " can be done in getETagForEntry function
                 // but that is causing an issue in Linux env where the
                 // firefix browser is unable to parse the ETag in this case.
                 // Need to follow up PHP core devs for this.
                 $eTag = ODataConstants::HTTP_WEAK_ETAG_PREFIX . $eTag . '"';
                 if (!is_null($ifMatch)) {
                     if (strcmp($eTag, $ifMatch) != 0) {
                         // Requested If-Match value does not match with current
                         // eTag Value then pre-condition error
                         // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
                         throw ODataException::createPreConditionFailedError(Messages::eTagValueDoesNotMatch());
                     }
                 } else {
                     if (strcmp($eTag, $ifNoneMatch) == 0) {
                         //304 not modified, but in write eTag header
                         $needToSerializeResponse = false;
                     }
                 }
             }
         }
     }
     if (is_null($eTag)) {
         $eTag = $this->getETagForEntry($entryObject, $resourceType);
         // Note: The following code for attaching the prefix W\"
         // and the suffix " can be done in getETagForEntry function
         // but that is causing an issue in Linux env where the
         // firefix browser is unable to parse the ETag in this case.
         // Need to follow up PHP core devs for this.
         $eTag = ODataConstants::HTTP_WEAK_ETAG_PREFIX . $eTag . '"';
     }
     return $eTag;
 }
Ejemplo n.º 8
0
 public function testProcessRequestForCollectionWithInlineCountProviderHandlesPaging()
 {
     $requestURI = new Url('http://host.com/data.svc/Collection/?$inlinecount=allpages');
     Phockito::when($this->mockServiceHost->getAbsoluteRequestUri())->return($requestURI);
     //mock inline count as all pages
     Phockito::when($this->mockServiceHost->getQueryStringItem(ODataConstants::HTTPQUERY_STRING_INLINECOUNT))->return("allpages");
     $this->fakeServiceConfig->setAcceptCountRequests(true);
     $this->fakeServiceConfig->setMaxDataServiceVersion(ProtocolVersion::V2());
     $uriProcessor = UriProcessor::process($this->mockService);
     $fakeQueryResult = new QueryResult();
     $fakeQueryResult->results = array(1, 2, 3);
     $fakeQueryResult->count = 10;
     Phockito::when($this->mockProvidersWrapper->getResourceSet(QueryType::ENTITIES_WITH_COUNT(), $this->mockCollectionResourceSetWrapper, null, null, null, null))->return($fakeQueryResult);
     //indicate that the Provider performs the paging (thus it will use the count in the QueryResult)
     Phockito::when($this->mockProvidersWrapper->handlesOrderedPaging())->return(true);
     $uriProcessor->execute();
     $request = $uriProcessor->getRequest();
     $actual = $request->getTargetResult();
     $this->assertEquals(array(1, 2, 3), $actual);
     $this->assertEquals(10, $request->getCountValue());
 }