/**
  * @covers MicrosoftAzure\Storage\Table\Models\QueryEntitiesOptions::setQuery
  * @covers MicrosoftAzure\Storage\Table\Models\QueryEntitiesOptions::getQuery
  * @covers MicrosoftAzure\Storage\Table\Models\QueryEntitiesOptions::__construct
  */
 public function testSetQuery()
 {
     // Setup
     $options = new QueryEntitiesOptions();
     $expected = new Query();
     // Test
     $options->setQuery($expected);
     // Assert
     $this->assertEquals($expected, $options->getQuery());
 }
 /**
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::insertEntity
  * @covers MicrosoftAzure\Storage\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()');
 }
 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;
 }
 public function testCheckQueryEntitiesOptions()
 {
     $options = new QueryEntitiesOptions();
     $query = new Query();
     $nextPartitionKey = 'aaa';
     $nextRowKey = 'bbb';
     $this->assertNull($options->getNextPartitionKey(), 'Default QueryEntitiesOptions->getNextPartitionKey');
     $this->assertNull($options->getNextRowKey(), 'Default QueryEntitiesOptions->getNextRowKey');
     $this->assertNotNull($options->getQuery(), 'Default QueryEntitiesOptions->getQuery');
     $options->setNextPartitionKey($nextPartitionKey);
     $options->setNextRowKey($nextRowKey);
     $options->setQuery($query);
     $this->assertEquals($nextPartitionKey, $options->getNextPartitionKey(), 'Set QueryEntitiesOptions->getNextPartitionKey');
     $this->assertEquals($nextRowKey, $options->getNextRowKey(), 'Set QueryEntitiesOptions->getNextRowKey');
     $this->assertEquals($query, $options->getQuery(), 'Set QueryEntitiesOptions->getQuery');
     $options->addSelectField('bar');
     $options->addSelectField('baz');
     $this->assertNotNull($options->getSelectFields(), 'Add $options->getSelectFields');
     $this->assertNotNull($options->getQuery()->getSelectFields(), 'Add $options->getQuery->getSelectFields');
     $this->assertEquals(2, count($options->getSelectFields()), 'Add $options->getSelectFields->size');
     $this->assertEquals(2, count($options->getQuery()->getSelectFields()), 'Add $options->getQuery->getSelectFields->size');
     $filter = Filter::applyConstant('foo', EdmType::STRING);
     $options->setFilter($filter);
     $options->setSelectFields(null);
     $options->setTop(TableServiceFunctionalTestData::INT_MAX_VALUE);
     $this->assertEquals($filter, $options->getFilter(), 'Set $options->getFilter');
     $this->assertEquals($filter, $options->getQuery()->getFilter(), 'Set $options->getQuery->getFilter');
     $this->assertNull($options->getSelectFields(), 'Set $options->getSelectFields');
     $this->assertNull($options->getQuery()->getSelectFields(), 'Set $options->getQuery->getSelectFields');
     $this->assertEquals(TableServiceFunctionalTestData::INT_MAX_VALUE, $options->getTop(), 'Set $options->getTop');
     $this->assertEquals(TableServiceFunctionalTestData::INT_MAX_VALUE, $options->getQuery()->getTop(), 'Set $options->getQuery->getTop');
 }
 /**
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::queryEntities
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::_addOptionalQuery
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::_encodeODataUriValues
  * @covers MicrosoftAzure\Storage\Table\TableRestProxy::_encodeODataUriValue
  * @covers MicrosoftAzure\Storage\Table\Internal\AtomReaderWriter::_parseBody
  * @covers MicrosoftAzure\Storage\Table\Internal\AtomReaderWriter::parseEntities
  * @covers MicrosoftAzure\Storage\Table\Internal\AtomReaderWriter::_parseOneEntity
  * @covers MicrosoftAzure\Storage\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());
 }