/**
  * @covers WindowsAzure\Table\TableRestProxy::queryEntities
  * @covers WindowsAzure\Table\TableRestProxy::_addOptionalQuery
  * @covers WindowsAzure\Table\TableRestProxy::_encodeODataUriValues
  * @covers WindowsAzure\Table\TableRestProxy::_encodeODataUriValue
  * @covers WindowsAzure\Table\Internal\AtomReaderWriter::_parseBody
  * @covers WindowsAzure\Table\Internal\AtomReaderWriter::parseEntities
  * @covers WindowsAzure\Table\Internal\AtomReaderWriter::_parseOneEntity
  * @covers WindowsAzure\Common\Internal\ServiceRestProxy::sendContext
  */
 public function testQueryEntitiesWithGetTop()
 {
     // Setup
     $name = 'queryentitieswithgettop';
     $pk1 = '123';
     $pk2 = '124';
     $pk3 = '125';
     $e1 = TestResources::getTestEntity($pk1, '1');
     $e2 = TestResources::getTestEntity($pk2, '2');
     $e3 = TestResources::getTestEntity($pk3, '3');
     $this->createTable($name);
     $this->restProxy->insertEntity($name, $e1);
     $this->restProxy->insertEntity($name, $e2);
     $this->restProxy->insertEntity($name, $e3);
     $query = new Query();
     $query->setTop(1);
     $options = new QueryEntitiesOptions();
     $options->setQuery($query);
     // Test
     $result = $this->restProxy->queryEntities($name, $options);
     // Assert
     $entities = $result->getEntities();
     $this->assertCount(1, $entities);
     $this->assertEquals($pk1, $entities[0]->getPartitionKey());
 }
예제 #2
0
 /**
  * @covers WindowsAzure\Table\Models\Query::addSelectField
  * @covers WindowsAzure\Table\Models\Query::getSelectFields
  */
 public function testAddSelectField()
 {
     // Setup
     $query = new Query();
     $field = 'customerId';
     $expected = array($field);
     // Test
     $query->addSelectField($field);
     // Assert
     $this->assertEquals($expected, $query->getSelectFields());
 }
 public function testCheckQuery()
 {
     $query = new Query();
     $this->assertNull($query->getFilter(), 'Default Query->getFilter');
     $this->assertNull($query->getSelectFields(), 'Default Query->getSelectFields');
     $this->assertNull($query->getTop(), 'Default Query->getTop');
     $query->addSelectField('bar');
     $query->addSelectField('baz');
     $this->assertNotNull($query->getSelectFields(), 'Add Query->getSelectFields');
     $this->assertEquals(2, count($query->getSelectFields()), 'Add Query->getSelectFields->size');
     $filter = Filter::applyConstant('foo', EdmType::STRING);
     $query->setFilter($filter);
     $query->setSelectFields(null);
     $query->setTop(TableServiceFunctionalTestData::IntegerMAX_VALUE);
     $this->assertEquals($filter, $query->getFilter(), 'Set Query->getFilter');
     $this->assertNull($query->getSelectFields(), 'Set Query->getSelectFields');
     $this->assertEquals(TableServiceFunctionalTestData::IntegerMAX_VALUE, $query->getTop(), 'Set Query->getTop');
 }
 private static function addBinaryFilter($name, $edmType, $values)
 {
     $counter = 0;
     $ret = array();
     foreach ($values as $o) {
         $f = self::getBinaryFilterFromIndex($counter, Filter::applyPropertyName($name), Filter::applyConstant($o, $edmType));
         $q = new Query();
         $q->setFilter($f);
         $qeo = new QueryEntitiesOptions();
         $qeo->setQuery($q);
         array_push($ret, $qeo);
         $counter = ($counter + 1) % 6;
     }
     return $ret;
 }
예제 #5
0
 /**
  * Quries tables in the given storage account.
  * 
  * @param Models\QueryTablesOptions|string|Models\Filter $options Could be
  * optional parameters, table prefix or filter to apply.
  * 
  * @return Models\QueryTablesResult
  * 
  * @see http://msdn.microsoft.com/en-us/library/windowsazure/dd179405.aspx
  */
 public function queryTables($options = null)
 {
     $method = Resources::HTTP_GET;
     $headers = array();
     $postParams = array();
     $queryParams = array();
     $statusCode = Resources::STATUS_OK;
     $path = 'Tables';
     if (is_null($options)) {
         $options = new QueryTablesOptions();
     } else {
         if (is_string($options)) {
             $prefix = $options;
             $options = new QueryTablesOptions();
             $options->setPrefix($prefix);
         } else {
             if ($options instanceof Filter) {
                 $filter = $options;
                 $options = new QueryTablesOptions();
                 $options->setFilter($filter);
             }
         }
     }
     $query = $options->getQuery();
     $next = $options->getNextTableName();
     $prefix = $options->getPrefix();
     $timeout = $options->getTimeout();
     if (!empty($prefix)) {
         // Append Max char to end '{' is 1 + 'z' in AsciiTable ==> upperBound
         // is prefix + '{'
         $prefixFilter = Filter::applyAnd(Filter::applyGe(Filter::applyPropertyName('TableName'), Filter::applyConstant($prefix, EdmType::STRING)), Filter::applyLe(Filter::applyPropertyName('TableName'), Filter::applyConstant($prefix . '{', EdmType::STRING)));
         if (is_null($query)) {
             $query = new Models\Query();
         }
         if (is_null($query->getFilter())) {
             // use the prefix filter if the query filter is null
             $query->setFilter($prefixFilter);
         } else {
             // combine and use the prefix filter if the query filter exists
             $combinedFilter = Filter::applyAnd($query->getFilter(), $prefixFilter);
             $query->setFilter($combinedFilter);
         }
     }
     $queryParams = $this->_addOptionalQuery($queryParams, $query);
     $this->addOptionalQueryParam($queryParams, Resources::QP_NEXT_TABLE_NAME, $next);
     $this->addOptionalQueryParam($queryParams, Resources::QP_TIMEOUT, $timeout);
     // One can specify the NextTableName option to get table entities starting
     // from the specified name. However, there appears to be an issue in the
     // Azure Table service where this does not engage on the server unless
     // $filter appears in the URL. The current behavior is to just ignore the
     // NextTableName options, which is not expected or easily detectable.
     if (array_key_exists(Resources::QP_NEXT_TABLE_NAME, $queryParams) && !array_key_exists(Resources::QP_FILTER, $queryParams)) {
         $queryParams[Resources::QP_FILTER] = Resources::EMPTY_STRING;
     }
     $response = $this->send($method, $headers, $queryParams, $postParams, $path, $statusCode);
     $tables = $this->_atomSerializer->parseTableEntries($response->getBody());
     return QueryTablesResult::create($response->getHeader(), $tables);
 }
 /**
  * @covers WindowsAzure\Table\TableRestProxy::insertEntity
  * @covers WindowsAzure\Table\TableRestProxy::queryEntities
  */
 public function testQueryEntitiesWithFilterWorks()
 {
     // Arrange
     $table = self::$testTable5;
     $numberOfEntries = 5;
     $entities = array();
     for ($i = 0; $i < $numberOfEntries; $i++) {
         $entity = new Entity();
         $entity->setPartitionKey('001');
         $entity->setRowKey('queryEntitiesWithFilterWorks-' . $i);
         $entity->addProperty('test', EdmType::BOOLEAN, $i % 2 == 0);
         $entity->addProperty('test2', EdmType::STRING, '\'value" ' . $i);
         $entity->addProperty('test3', EdmType::INT32, $i);
         $entity->addProperty('test4', EdmType::INT64, strval('12345678901' + $i));
         $entity->addProperty('test5', EdmType::DATETIME, new \DateTime('2012-01-0' . $i));
         $entity->addProperty('test6', EdmType::BINARY, chr($i));
         $entity->addProperty('test7', EdmType::GUID, Utilities::getGuid());
         $entities[$i] = $entity;
         $this->restProxy->insertEntity($table, $entity);
     }
     // Act
     $f = Filter::applyEq(Filter::applyPropertyName('RowKey'), Filter::applyConstant('queryEntitiesWithFilterWorks-3', EdmType::STRING));
     $q = new Query();
     $q->setFilter($f);
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(1, count($result->getEntities()), 'count($result->getEntities())');
     $resEnts = $result->getEntities();
     $this->assertEquals('queryEntitiesWithFilterWorks-3', $resEnts[0]->getRowKey(), '$resEnts[0]->getRowKey()');
     // Act
     $q = new Query();
     $q->setFilter(Filter::applyQueryString('RowKey eq \'queryEntitiesWithFilterWorks-3\''));
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(1, count($result->getEntities()), 'count($result->getEntities())');
     $resEnts = $result->getEntities();
     $this->assertEquals('queryEntitiesWithFilterWorks-3', $resEnts[0]->getRowKey(), '$resEnts[0]->getRowKey()');
     // Act
     $q = new Query();
     $q->setFilter(Filter::applyEq(Filter::applyPropertyName('test'), Filter::applyConstant(true, EdmType::BOOLEAN)));
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(3, count($result->getEntities()), 'count($result->getEntities())');
     // Act
     $q = new Query();
     $q->setFilter(Filter::applyEq(Filter::applyPropertyName('test2'), Filter::applyConstant('\'value" 3', EdmType::STRING)));
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(1, count($result->getEntities()), 'count($result->getEntities())');
     $resEnts = $result->getEntities();
     $this->assertEquals('queryEntitiesWithFilterWorks-3', $resEnts[0]->getRowKey(), '$resEnts[0]->getRowKey()');
     // Act
     $q = new Query();
     $q->setFilter(Filter::applyEq(Filter::applyPropertyName('test4'), Filter::applyConstant(12345678903, EdmType::INT64)));
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(1, count($result->getEntities()), 'count($result->getEntities())');
     $resEnts = $result->getEntities();
     $this->assertEquals('queryEntitiesWithFilterWorks-2', $resEnts[0]->getRowKey(), '$resEnts[0]->getRowKey()');
     // Act
     $q = new Query();
     $q->setFilter(Filter::applyEq(Filter::applyPropertyName('test5'), Filter::applyConstant(new \DateTime('2012-01-03'), EdmType::DATETIME)));
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(1, count($result->getEntities()), 'count($result->getEntities())');
     $resEnts = $result->getEntities();
     $this->assertEquals('queryEntitiesWithFilterWorks-3', $resEnts[0]->getRowKey(), '$resEnts[0]->getRowKey()');
     // Act
     $q = new Query();
     $ent3 = $entities[3];
     $q->setFilter(Filter::applyEq(Filter::applyPropertyName('test6'), Filter::applyConstant(chr(3), EdmType::BINARY)));
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(1, count($result->getEntities()), 'count($result->getEntities())');
     $resEnts = $result->getEntities();
     $this->assertEquals('queryEntitiesWithFilterWorks-3', $resEnts[0]->getRowKey(), '$resEnts[0]->getRowKey()');
     // Act
     $q = new Query();
     $ent3 = $entities[3];
     $q->setFilter(Filter::applyEq(Filter::applyPropertyName('test7'), Filter::applyConstant($ent3->getPropertyValue('test7'), EdmType::GUID)));
     $qeo = new QueryEntitiesOptions();
     $qeo->setQuery($q);
     $result = $this->restProxy->queryEntities($table, $qeo);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(1, count($result->getEntities()), 'count($result->getEntities())');
     $resEnts = $result->getEntities();
     $this->assertEquals('queryEntitiesWithFilterWorks-3', $resEnts[0]->getRowKey(), '$resEnts[0]->getRowKey()');
 }