Skip to content

Commit 26db002

Browse files
authored
fix: Prevent query->count from changing query internals (#511)
1 parent 8b17fa9 commit 26db002

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/Parse/ParseQuery.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,10 @@ public function count($useMasterKey = false)
586586
if (ParseUser::getCurrentUser()) {
587587
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
588588
}
589-
$this->limit = 0;
590-
$this->count = 1;
591-
$queryString = $this->buildQueryString($this->_getOptions());
589+
$queryParams = $this->_getOptions();
590+
$queryParams['limit'] = 0;
591+
$queryParams['count'] = 1;
592+
$queryString = $this->buildQueryString($queryParams);
592593
$result = ParseClient::_request(
593594
'GET',
594595
'classes/'.$this->className.'?'.$queryString,

tests/Parse/ParseQueryTest.php

+41-4
Original file line numberDiff line numberDiff line change
@@ -2654,8 +2654,8 @@ public function testGetAndSetConditions()
26542654
$query->select(['select1','select2']);
26552655
$query->skip(24);
26562656

2657-
// sets count = 1 and limit = 0
2658-
$query->count();
2657+
// sets count = 1
2658+
$query->withCount();
26592659
// reset limit up to 42
26602660
$query->limit(42);
26612661

@@ -2695,6 +2695,45 @@ public function testGetAndSetConditions()
26952695
);
26962696
}
26972697

2698+
/**
2699+
* @group query-count-conditions
2700+
*/
2701+
public function testCountDoesNotOverrideConditions()
2702+
{
2703+
$obj = new ParseObject('TestObject');
2704+
$obj->set('name', 'John');
2705+
$obj->set('country', 'US');
2706+
$obj->save();
2707+
2708+
$obj = new ParseObject('TestObject');
2709+
$obj->set('name', 'Bob');
2710+
$obj->set('country', 'US');
2711+
$obj->save();
2712+
2713+
$obj = new ParseObject('TestObject');
2714+
$obj->set('name', 'Mike');
2715+
$obj->set('country', 'CA');
2716+
$obj->save();
2717+
2718+
$query = new ParseQuery('TestObject');
2719+
$query->equalTo('country', 'US');
2720+
$query->limit(1);
2721+
$count = $query->count();
2722+
$results = $query->find();
2723+
2724+
$this->assertEquals(1, count($results));
2725+
$this->assertEquals(2, $count);
2726+
2727+
$this->assertSame([
2728+
'where' => [
2729+
'country' => [
2730+
'$eq' => 'US'
2731+
]
2732+
],
2733+
'limit' => 1,
2734+
], $query->_getOptions());
2735+
}
2736+
26982737
public function testNotArrayConditions()
26992738
{
27002739
$this->expectException(
@@ -2770,8 +2809,6 @@ public function testEqualToWithSameKeyDoesNotOverrideOtherConditions()
27702809
'$eq' => 'bar',
27712810
]
27722811
],
2773-
'limit' => 0,
2774-
'count' => 1,
27752812
], $query->_getOptions());
27762813
}
27772814
}

0 commit comments

Comments
 (0)