Skip to content

Support for Aggregate Queries #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,65 @@ public function count($useMasterKey = false)
return $result['count'];
}

/**
* Execute a distinct query and return unique values.
*
* @param string $key field to find distinct values
*
* @return array
*/
public function distinct($key)
{
$sessionToken = null;
if (ParseUser::getCurrentUser()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do something like this to avoid calling ParseUser::getCurrentUser() twice, while still validating there is a current user.

if($user = ParseUser::getCurrentUser()) {
  ...
}

$user would be set in the conditional block then to use.

$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$opts = [];
if (!empty($this->where)) {
$opts['where'] = $this->where;
}
$opts['distinct'] = $key;
$queryString = $this->buildQueryString($opts);
$result = ParseClient::_request(
'GET',
'aggregate/'.$this->className.'?'.$queryString,
$sessionToken,
null,
true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked over the PR in detail yet over on the server, but does this require the master key to work? If not we should be adding a method parameter to optionally use the master key, same as how the other query methods work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server requires a masterKey for security purposes

);

return $result['results'];
}

/**
* Execute a aggregate query and returns aggregate results.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little textual typo on a, Execute an aggregrate...

*
* @param array $pipeline stages to process query
*
* @return array
*/
public function aggregate($pipeline)
{
$sessionToken = null;
if (ParseUser::getCurrentUser()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as above regarding calling this twice.

$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$stages = [];
foreach ($pipeline as $stage => $value) {
$stages[$stage] = json_encode($value);
}
$queryString = $this->buildQueryString($stages);
$result = ParseClient::_request(
'GET',
'aggregate/'.$this->className.'?'.$queryString,
$sessionToken,
null,
true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here regarding the use of the master key. If it's not required to work it should be a method parameter with a default of false.

);

return $result['results'];
}

/**
* Execute a find query and return the results.
*
Expand Down
213 changes: 213 additions & 0 deletions tests/Parse/ParseQueryAggregateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

namespace Parse\Test;

use Parse\ParseACL;
use Parse\ParseException;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;

class ParseQueryAggregateTest extends \PHPUnit_Framework_TestCase
{
public static function setUpBeforeClass()
{
Helper::setUp();
}

public function setUp()
{
Helper::clearClass('TestObject');
}

public function tearDown()
{
Helper::tearDown();
}

/**
* This function used as a helper function in test functions
*/
public function loadObjects()
{
$obj1 = new ParseObject('TestObject');
$obj2 = new ParseObject('TestObject');
$obj3 = new ParseObject('TestObject');
$obj4 = new ParseObject('TestObject');

$obj1->set('score', 10);
$obj2->set('score', 10);
$obj3->set('score', 10);
$obj4->set('score', 20);

$obj1->set('name', 'foo');
$obj2->set('name', 'foo');
$obj3->set('name', 'bar');
$obj4->set('name', 'dpl');

$objects = [$obj1, $obj2, $obj3, $obj4];
ParseObject::saveAll($objects);
}

public function testDistinctQuery()
{
$this->loadObjects();
$query = new ParseQuery('TestObject');
$results = $query->distinct('score');

$this->assertEquals(2, count($results));
$this->assertEquals($results[0], 10);
$this->assertEquals($results[1], 20);
}

public function testDistinctWhereQuery()
{
$this->loadObjects();
$query = new ParseQuery('TestObject');
$query->equalTo('name', 'foo');
$results = $query->distinct('score');

$this->assertEquals(1, count($results));
$this->assertEquals($results[0], 10);
}

public function testDistinctClassNotExistQuery()
{
$this->loadObjects();
$query = new ParseQuery('UnknownClass');
$results = $query->distinct('score');

$this->assertEquals(0, count($results));
}

public function testDistinctFieldNotExistQuery()
{
$this->loadObjects();
$query = new ParseQuery('TestObject');
$results = $query->distinct('unknown');

$this->assertEquals(0, count($results));
}

public function testAggregateGroupQuery()
{
$pipeline = [
'group' => [
'objectId' => '$name'
]
];
$this->loadObjects();
$query = new ParseQuery('TestObject');
$results = $query->aggregate($pipeline);

$this->assertEquals(3, count($results));
}

public function testAggregateGroupClassNotExistQuery()
{
$pipeline = [
'group' => [
'objectId' => '$name'
]
];
$this->loadObjects();
$query = new ParseQuery('UnknownClass');
$results = $query->aggregate($pipeline);

$this->assertEquals(0, count($results));
}

public function testAggregateGroupFieldNotExistQuery()
{
$pipeline = [
'group' => [
'objectId' => '$unknown'
]
];
$this->loadObjects();
$query = new ParseQuery('UnknownClass');
$results = $query->aggregate($pipeline);

$this->assertEquals(0, count($results));
}

public function testAggregateMatchQuery()
{
$pipeline = [
'match' => [
'score' => [ '$gt' => 15 ]
]
];
$this->loadObjects();
$query = new ParseQuery('TestObject');
$results = $query->aggregate($pipeline);

$this->assertEquals(1, count($results));
$this->assertEquals(20, $results[0]['score']);
}

public function testAggregateProjectQuery()
{
$pipeline = [
'project' => [
'name' => 1
]
];
$this->loadObjects();
$query = new ParseQuery('TestObject');
$results = $query->aggregate($pipeline);

foreach ($results as $result) {
$this->assertEquals(array_key_exists('name', $result), true);
$this->assertEquals(array_key_exists('objectId', $result), true);
$this->assertEquals(array_key_exists('score', $result), false);
}
}

public function testAggregatePipelineInvalid()
{
$pipeline = [
'unknown' => []
];
$this->loadObjects();
$query = new ParseQuery('TestObject');
$this->setExpectedException(
'Parse\ParseException',
'Invalid parameter for query: unknown',
102
);
$results = $query->aggregate($pipeline);
}

public function testAggregateGroupInvalid()
{
$pipeline = [
'group' => [
'_id' => '$name'
]
];
$this->loadObjects();
$query = new ParseQuery('TestObject');
$this->setExpectedException(
'Parse\ParseException',
'Invalid parameter for query: group. Please use objectId instead of _id',
102
);
$results = $query->aggregate($pipeline);
}

public function testAggregateGroupObjectIdRequired()
{
$pipeline = [
'group' => []
];
$this->loadObjects();
$query = new ParseQuery('TestObject');
$this->setExpectedException(
'Parse\ParseException',
'Invalid parameter for query: group. objectId is required',
102
);
$results = $query->aggregate($pipeline);
}
}