From e98ebc410d1d5e2608e1e92bbb1c951cb062f7dc Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Mon, 3 Dec 2018 16:09:34 -0600 Subject: [PATCH 1/3] Add encode option to ParseQuery:find the ability to get the result from a ParseQuery->find() without the results being decoded into ParseObject instances. Similar to https://github.com/parse-community/parse-php-sdk/pull/26 --- src/Parse/ParseQuery.php | 10 +++++++++- tests/Parse/ParseQueryTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Parse/ParseQuery.php b/src/Parse/ParseQuery.php index b0b35e09..bd90b3e6 100755 --- a/src/Parse/ParseQuery.php +++ b/src/Parse/ParseQuery.php @@ -610,10 +610,11 @@ 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 * * @return ParseObject[] */ - public function find($useMasterKey = false) + public function find($useMasterKey = false, $decodeObjects = true) { $sessionToken = null; if (ParseUser::getCurrentUser()) { @@ -627,6 +628,13 @@ public function find($useMasterKey = false) null, $useMasterKey ); + if (!$decodeObjects) { + if (array_key_exists('results', $result)) { + return $result['results']; + } else { + return []; + } + } $output = []; foreach ($result['results'] as $row) { $obj = ParseObject::create($this->className, $row['objectId']); diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index 40f8bbba..1cb6c683 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -2365,6 +2365,34 @@ public function testAndQueriesVaryingClasses() ]); } + public function testQueryFindEncoded() + { + $obj = new ParseObject('TestObject'); + $obj->set('name', 'John'); + $obj->set('country', 'US'); + $obj->save(); + + $obj = new ParseObject('TestObject'); + $obj->set('name', 'Bob'); + $obj->set('country', 'US'); + $obj->save(); + + $obj = new ParseObject('TestObject'); + $obj->set('name', 'Joel'); + $obj->set('country', 'CA'); + $obj->save(); + + $query = new ParseQuery('TestObject'); + $query->ascending(['country','name']); + $results = $query->find(false, false); + + $this->assertEquals(3, count($results)); + + $this->assertEquals('Joel', $results[0]['name']); + $this->assertEquals('Bob', $results[1]['name']); + $this->assertEquals('John', $results[2]['name']); + } + /** * @group query-set-conditions */ From 5bb101ac57bb555c40e9a211717ee6b644e669b4 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Mon, 3 Dec 2018 17:56:36 -0600 Subject: [PATCH 2/3] improve coverage --- tests/Parse/Helper.php | 5 ++++- tests/Parse/HttpClientMock.php | 23 +++++++++++++++++++++++ tests/Parse/ParseQueryTest.php | 19 ++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/Parse/HttpClientMock.php diff --git a/tests/Parse/Helper.php b/tests/Parse/Helper.php index 56333238..e85cac23 100644 --- a/tests/Parse/Helper.php +++ b/tests/Parse/Helper.php @@ -52,7 +52,7 @@ public static function setUp() self::setHttpClient(); } - public static function setHttpClient() + public static function setHttpClient($httpClient = null) { // // Set a curl http client to run primary tests under @@ -61,6 +61,9 @@ public static function setHttpClient() // ParseCurlHttpClient // ParseStreamHttpClient // + if ($httpClient) { + return ParseClient::setHttpClient($httpClient); + } global $USE_CLIENT_STREAM; diff --git a/tests/Parse/HttpClientMock.php b/tests/Parse/HttpClientMock.php new file mode 100644 index 00000000..dc24e6ce --- /dev/null +++ b/tests/Parse/HttpClientMock.php @@ -0,0 +1,23 @@ +response; + } + + public function setResponse($resp) + { + $this->response = $resp; + } +} diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index 1cb6c683..4c141053 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -2383,7 +2383,7 @@ public function testQueryFindEncoded() $obj->save(); $query = new ParseQuery('TestObject'); - $query->ascending(['country','name']); + $query->ascending(['country', 'name']); $results = $query->find(false, false); $this->assertEquals(3, count($results)); @@ -2393,6 +2393,23 @@ public function testQueryFindEncoded() $this->assertEquals('John', $results[2]['name']); } + public function testQueryFindEncodedInvalidResponse() + { + $obj = new ParseObject('TestObject'); + $obj->set('name', 'John'); + $obj->set('country', 'US'); + $obj->save(); + + $httpClient = new HttpClientMock(); + $httpClient->setResponse('{}'); + Helper::setHttpClient($httpClient); + + $query = new ParseQuery('TestObject'); + $results = $query->find(false, false); + + $this->assertEquals(0, count($results)); + } + /** * @group query-set-conditions */ From 90ff1c9cb39452b89bc92df3683a74c0ef7a719d Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Mon, 3 Dec 2018 18:15:02 -0600 Subject: [PATCH 3/3] fix tests --- tests/Parse/ParseQueryTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Parse/ParseQueryTest.php b/tests/Parse/ParseQueryTest.php index 4c141053..8f42b6a0 100644 --- a/tests/Parse/ParseQueryTest.php +++ b/tests/Parse/ParseQueryTest.php @@ -2408,6 +2408,9 @@ public function testQueryFindEncodedInvalidResponse() $results = $query->find(false, false); $this->assertEquals(0, count($results)); + + // Reset HttpClient + Helper::setUp(); } /**