From 942dcc2814f266353b3a36274984dfb68de15318 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Tue, 23 Jul 2019 00:03:49 -0500 Subject: [PATCH] Support query with count (#448) * Support query with count * lint!!! --- src/Parse/ParseQuery.php | 42 ++++++++++- tests/Parse/ParseQueryTest.php | 125 +++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 2 deletions(-) diff --git a/src/Parse/ParseQuery.php b/src/Parse/ParseQuery.php index ad78d55c..5011a591 100644 --- a/src/Parse/ParseQuery.php +++ b/src/Parse/ParseQuery.php @@ -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 */ @@ -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. * @@ -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[] */ @@ -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 []; } diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index 3903d8e0..00758dae 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -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');