Skip to content

Commit d235fe5

Browse files
authored
Merge pull request #3488 from epourail/feature-improve-between-range-filter
Improve the RangeFilter DQL query in case the "between" values are equals
2 parents da3a295 + ceaa59d commit d235fe5

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.5.x-dev
44

55
* GraphQL: Do not allow empty cursor values on `before` or `after`
6+
* Filter: Improve the RangeFilter query in case the values are equals using the between operator
67

78
## 2.5.4
89

features/doctrine/range_filter.feature

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,51 @@ Feature: Range filter on collections
6060
}
6161
"""
6262

63+
@createSchema
64+
Scenario: Get collection filtered by range (between the same values)
65+
Given there are 30 dummy objects with dummyPrice
66+
When I send a "GET" request to "/dummies?dummyPrice[between]=12.99..12.99"
67+
Then the response status code should be 200
68+
And the response should be in JSON
69+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
70+
And the JSON should be valid according to this schema:
71+
"""
72+
{
73+
"type": "object",
74+
"properties": {
75+
"@context": {"pattern": "^/contexts/Dummy$"},
76+
"@id": {"pattern": "^/dummies$"},
77+
"@type": {"pattern": "^hydra:Collection$"},
78+
"hydra:member": {
79+
"type": "array",
80+
"items": {
81+
"type": "object",
82+
"properties": {
83+
"@id": {
84+
"oneOf": [
85+
{"pattern": "^/dummies/2$"},
86+
{"pattern": "^/dummies/6$"},
87+
{"pattern": "^/dummies/10$"}
88+
]
89+
}
90+
}
91+
},
92+
"minItems": 3,
93+
"maxItems": 3,
94+
"uniqueItems": true
95+
},
96+
"hydra:totalItems": {"type": "number", "minimum": 8, "maximum": 8},
97+
"hydra:view": {
98+
"type": "object",
99+
"properties": {
100+
"@id": {"pattern": "^/dummies\\?dummyPrice%5Bbetween%5D=12.99..12.99"},
101+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
102+
}
103+
}
104+
}
105+
}
106+
"""
107+
63108
Scenario: Filter by range (between) with invalid format
64109
When I send a "GET" request to "/dummies?dummyPrice[between]=9.99..12.99..15.99"
65110
Then the response status code should be 200

src/Bridge/Doctrine/MongoDbOdm/Filter/RangeFilter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ protected function addMatch(Builder $aggregationBuilder, string $field, string $
7878
return;
7979
}
8080

81+
if ($rangeValue[0] === $rangeValue[1]) {
82+
$aggregationBuilder->match()->field($matchField)->equals($rangeValue[0]);
83+
84+
return;
85+
}
86+
8187
$aggregationBuilder->match()->field($matchField)->gte($rangeValue[0])->lte($rangeValue[1]);
8288

8389
break;

src/Bridge/Doctrine/Orm/Filter/RangeFilter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
8585
return;
8686
}
8787

88+
if ($rangeValue[0] === $rangeValue[1]) {
89+
$queryBuilder
90+
->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
91+
->setParameter($valueParameter, $rangeValue[0]);
92+
93+
return;
94+
}
95+
8896
$queryBuilder
8997
->andWhere(sprintf('%1$s.%2$s BETWEEN :%3$s_1 AND :%3$s_2', $alias, $field, $valueParameter))
9098
->setParameter(sprintf('%s_1', $valueParameter), $rangeValue[0])

tests/Bridge/Doctrine/Common/Filter/RangeFilterTestTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ private function provideApplyTestArguments(): array
2929
],
3030
],
3131
],
32+
'between (same values)' => [
33+
null,
34+
[
35+
'dummyPrice' => [
36+
'between' => '9.99..9.99',
37+
],
38+
],
39+
],
3240
'between (too many operands)' => [
3341
null,
3442
[

tests/Bridge/Doctrine/MongoDbOdm/Filter/RangeFilterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ public function provideApplyTestData(): array
453453
],
454454
],
455455
],
456+
'between (same values)' => [
457+
[
458+
[
459+
'$match' => [
460+
'dummyPrice' => 9.99,
461+
],
462+
],
463+
],
464+
],
456465
'between (too many operands)' => [
457466
[],
458467
],

tests/Bridge/Doctrine/Orm/Filter/RangeFilterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ public function provideApplyTestData(): array
343343
'between' => [
344344
sprintf('SELECT o FROM %s o WHERE o.dummyPrice BETWEEN :dummyPrice_p1_1 AND :dummyPrice_p1_2', Dummy::class),
345345
],
346+
'between (same values)' => [
347+
sprintf('SELECT o FROM %s o WHERE o.dummyPrice = :dummyPrice_p1', Dummy::class),
348+
],
346349
'between (too many operands)' => [
347350
sprintf('SELECT o FROM %s o', Dummy::class),
348351
],

0 commit comments

Comments
 (0)