Skip to content

Commit

Permalink
Support query with count (#448)
Browse files Browse the repository at this point in the history
* Support query with count

* lint!!!
  • Loading branch information
dplewis authored Jul 23, 2019
1 parent c6a158f commit 942dcc2
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public function _getOptions()
* Execute a query to get only the first result.
*
* @param bool $useMasterKey If the query should use the master key
* @param bool $decodeObjects If set to false, will not return raw data instead of ParseObject instances
* @param bool $decodeObjects If set to false, will return raw data instead of ParseObject instances
*
* @return array|ParseObject Returns the first object or an empty array
*/
Expand Down Expand Up @@ -600,6 +600,23 @@ public function count($useMasterKey = false)
return $result['count'];
}

/**
* The response will include the total number of objects satisfying this query,
* dispite limit / skip. Might be useful for pagination.
*
* Note: the results will be an object
* `results`: holding {ParseObject} array and `count`: integer holding total number
*
* @param bool $includeCount If response should include count, true by default.
*
* @return ParseQuery Returns this query, so you can chain this call.
*/
public function withCount($includeCount = true)
{
$this->count = (int)$includeCount;
return $this;
}

/**
* Execute a distinct query and return unique values.
*
Expand Down Expand Up @@ -663,7 +680,7 @@ public function aggregate($pipeline)
* Execute a find query and return the results.
*
* @param bool $useMasterKey
* @param bool $decodeObjects If set to false, will not return raw data instead of ParseObject instances
* @param bool $decodeObjects If set to false, will return raw data instead of ParseObject instances
*
* @return ParseObject[]
*/
Expand All @@ -681,6 +698,27 @@ public function find($useMasterKey = false, $decodeObjects = true)
null,
$useMasterKey
);

$response = [];
if (isset($result['count'])) {
$response['count'] = $result['count'];
$response['results'] = $this->handleQueryResult($result, $decodeObjects);
return $response;
}

return $this->handleQueryResult($result, $decodeObjects);
}

/**
* Handles result from ParseClient::_request
*
* @param array $result Array of ParseObject raw data.
* @param bool $decodeObjects If set to false, will return raw data instead of ParseObject instances
*
* @return Array Array of ParseObjects or raw data.
*/
public function handleQueryResult($result, $decodeObjects)
{
if (!isset($result['results'])) {
return [];
}
Expand Down
125 changes: 125 additions & 0 deletions tests/Parse/ParseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,131 @@ function ($i) {
);
}

/**
* @group withCount
*/
public function testWithCount()
{
Helper::clearClass('BoxedNumber');
$this->saveObjects(
3,
function ($i) {
$boxedNumber = ParseObject::create('BoxedNumber');
$boxedNumber->set('x', $i + 1);

return $boxedNumber;
}
);
$query = new ParseQuery('BoxedNumber');
$query->withCount();
$response = $query->find();
$this->assertEquals($response['count'], 3);
$this->assertEquals(count($response['results']), 3);
}

/**
* @group withCount
*/
public function testWithCountDestructure()
{
Helper::clearClass('BoxedNumber');
$this->saveObjects(
3,
function ($i) {
$boxedNumber = ParseObject::create('BoxedNumber');
$boxedNumber->set('x', $i + 1);

return $boxedNumber;
}
);
$query = new ParseQuery('BoxedNumber');
$query->withCount();
['count' => $count, 'results' => $results] = $query->find();
$this->assertEquals($count, 3);
$this->assertEquals(count($results), 3);
}

/**
* @group withCount
*/
public function testWithCountFalse()
{
Helper::clearClass('BoxedNumber');
$this->saveObjects(
3,
function ($i) {
$boxedNumber = ParseObject::create('BoxedNumber');
$boxedNumber->set('x', $i + 1);

return $boxedNumber;
}
);
$query = new ParseQuery('BoxedNumber');
$query->withCount(false);
$response = $query->find();
$this->assertEquals(isset($response['count']), false);
$this->assertEquals(count($response), 3);
}

/**
* @group withCount
*/
public function testWithCountEmptyClass()
{
Helper::clearClass('BoxedNumber');
$query = new ParseQuery('BoxedNumber');
$query->withCount();
$response = $query->find();
$this->assertEquals($response['count'], 0);
$this->assertEquals(count($response['results']), 0);
}

/**
* @group withCount
*/
public function testWithCountAndLimit()
{
Helper::clearClass('BoxedNumber');
$this->saveObjects(
4,
function ($i) {
$boxedNumber = ParseObject::create('BoxedNumber');
$boxedNumber->set('x', $i + 1);

return $boxedNumber;
}
);
$query = new ParseQuery('BoxedNumber');
$query->withCount();
$query->limit(2);
$response = $query->find();
$this->assertEquals($response['count'], 4);
$this->assertEquals(count($response['results']), 2);
}

/**
* @group withCount
*/
public function testWithCountAndSkip()
{
Helper::clearClass('BoxedNumber');
$this->saveObjects(
4,
function ($i) {
$boxedNumber = ParseObject::create('BoxedNumber');
$boxedNumber->set('x', $i + 1);

return $boxedNumber;
}
);
$query = new ParseQuery('BoxedNumber');
$query->withCount();
$query->skip(3);
$response = $query->find();
$this->assertEquals($response['count'], 4);
$this->assertEquals(count($response['results']), 1);
}

public function testCountError()
{
$query = new ParseQuery('Test');
Expand Down

0 comments on commit 942dcc2

Please sign in to comment.