Skip to content

Commit d7b7c1a

Browse files
authored
Add encode option to ParseQuery:find (#423)
* 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 #26 * improve coverage * fix tests
1 parent 9fb3ce5 commit d7b7c1a

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

src/Parse/ParseQuery.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,11 @@ public function aggregate($pipeline)
610610
* Execute a find query and return the results.
611611
*
612612
* @param bool $useMasterKey
613+
* @param bool $decodeObjects If set to false, will not return raw data instead of ParseObject instances
613614
*
614615
* @return ParseObject[]
615616
*/
616-
public function find($useMasterKey = false)
617+
public function find($useMasterKey = false, $decodeObjects = true)
617618
{
618619
$sessionToken = null;
619620
if (ParseUser::getCurrentUser()) {
@@ -627,6 +628,13 @@ public function find($useMasterKey = false)
627628
null,
628629
$useMasterKey
629630
);
631+
if (!$decodeObjects) {
632+
if (array_key_exists('results', $result)) {
633+
return $result['results'];
634+
} else {
635+
return [];
636+
}
637+
}
630638
$output = [];
631639
foreach ($result['results'] as $row) {
632640
$obj = ParseObject::create($this->className, $row['objectId']);

tests/Parse/Helper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function setUp()
5252
self::setHttpClient();
5353
}
5454

55-
public static function setHttpClient()
55+
public static function setHttpClient($httpClient = null)
5656
{
5757
//
5858
// Set a curl http client to run primary tests under
@@ -61,6 +61,9 @@ public static function setHttpClient()
6161
// ParseCurlHttpClient
6262
// ParseStreamHttpClient
6363
//
64+
if ($httpClient) {
65+
return ParseClient::setHttpClient($httpClient);
66+
}
6467

6568
global $USE_CLIENT_STREAM;
6669

tests/Parse/HttpClientMock.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Class HttpClientMock | Parse/Test/HttpClientMock.php
4+
*/
5+
6+
namespace Parse\Test;
7+
8+
use Parse\HttpClients\ParseCurlHttpClient;
9+
10+
class HttpClientMock extends ParseCurlHttpClient
11+
{
12+
private $response = '';
13+
14+
public function send($url, $method = 'GET', $data = array())
15+
{
16+
return $this->response;
17+
}
18+
19+
public function setResponse($resp)
20+
{
21+
$this->response = $resp;
22+
}
23+
}

tests/Parse/ParseQueryTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,54 @@ public function testAndQueriesVaryingClasses()
23652365
]);
23662366
}
23672367

2368+
public function testQueryFindEncoded()
2369+
{
2370+
$obj = new ParseObject('TestObject');
2371+
$obj->set('name', 'John');
2372+
$obj->set('country', 'US');
2373+
$obj->save();
2374+
2375+
$obj = new ParseObject('TestObject');
2376+
$obj->set('name', 'Bob');
2377+
$obj->set('country', 'US');
2378+
$obj->save();
2379+
2380+
$obj = new ParseObject('TestObject');
2381+
$obj->set('name', 'Joel');
2382+
$obj->set('country', 'CA');
2383+
$obj->save();
2384+
2385+
$query = new ParseQuery('TestObject');
2386+
$query->ascending(['country', 'name']);
2387+
$results = $query->find(false, false);
2388+
2389+
$this->assertEquals(3, count($results));
2390+
2391+
$this->assertEquals('Joel', $results[0]['name']);
2392+
$this->assertEquals('Bob', $results[1]['name']);
2393+
$this->assertEquals('John', $results[2]['name']);
2394+
}
2395+
2396+
public function testQueryFindEncodedInvalidResponse()
2397+
{
2398+
$obj = new ParseObject('TestObject');
2399+
$obj->set('name', 'John');
2400+
$obj->set('country', 'US');
2401+
$obj->save();
2402+
2403+
$httpClient = new HttpClientMock();
2404+
$httpClient->setResponse('{}');
2405+
Helper::setHttpClient($httpClient);
2406+
2407+
$query = new ParseQuery('TestObject');
2408+
$results = $query->find(false, false);
2409+
2410+
$this->assertEquals(0, count($results));
2411+
2412+
// Reset HttpClient
2413+
Helper::setUp();
2414+
}
2415+
23682416
/**
23692417
* @group query-set-conditions
23702418
*/

0 commit comments

Comments
 (0)