Skip to content

Fix #1008 multiple filters on eager loading #1010

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 1 commit into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ public function thereIsDummyObjectsWithDummyDate($nb)
$this->manager->flush();
}

/**
* @Given there is :nb dummy objects with dummyDate and dummyBoolean :bool
*/
public function thereIsDummyObjectsWithDummyDateAndDummyBoolean($nb, $bool)
{
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];

if (in_array($bool, ['true', '1', 1], true)) {
$bool = true;
} elseif (in_array($bool, ['false', '0', 0], true)) {
$bool = false;
} else {
$expected = ['true', 'false', '1', '0'];
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
}

for ($i = 1; $i <= $nb; ++$i) {
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));

$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setDescription($descriptions[($i - 1) % 2]);
$dummy->setDummyBoolean($bool);

// Last Dummy has a null date
if ($nb !== $i) {
$dummy->setDummyDate($date);
}

$this->manager->persist($dummy);
}

$this->manager->flush();
}

/**
* @Given there is :nb dummy objects with dummyDate and relatedDummy
*/
Expand Down
48 changes: 48 additions & 0 deletions features/doctrine/multiple_filter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Feature: Multiple filters on collections
In order to retrieve large collections of filtered resources
As a client software developer
I need to retrieve collections filtered by multiple parameters

@createSchema
@dropSchema
Scenario: Get collection filtered by multiple parameters
Given there is "30" dummy objects with dummyDate and dummyBoolean true
And there is "20" dummy objects with dummyDate and dummyBoolean false
When I send a "GET" request to "/dummies?dummyDate[after]=2015-04-28&dummyBoolean=1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/Dummy$"},
"@id": {"pattern": "^/dummies$"},
"@type": {"pattern": "^hydra:Collection$"},
"hydra:member": {
"type": "array",
"items": {
"type": "object",
"properties": {
"@id": {
"oneOf": [
{"pattern": "^/dummies/28$"},
{"pattern": "^/dummies/29$"}
]
}
}
},
"maxItems": 2
},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/dummies\\?dummyDate%5Bafter%5D=2015-04-28&dummyBoolean=1$"},
"@type": {"pattern": "^hydra:PartialCollectionView$"}
}
}
}
}
"""

Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGenerator
$queryBuilderClone->andWhere($queryBuilderClone->expr()->in('o', $this->getQueryBuilderWithNewAliases($queryBuilder, $queryNameGenerator)->getDQL()));

$queryBuilder->resetDQLPart('where');
foreach ($queryBuilderClone->getDQLPart('where')->getParts() as $wherePart) {
$queryBuilder->add('where', $wherePart);
}
$queryBuilder->add('where', $queryBuilderClone->getDQLPart('where'));
}

/**
Expand Down Expand Up @@ -101,10 +99,7 @@ private function getQueryBuilderWithNewAliases(QueryBuilder $queryBuilder, Query
$queryBuilderClone->add('join', [$join], true);
}

//Change where aliases
foreach ($wherePart->getParts() as $where) {
$queryBuilderClone->add('where', str_replace($aliases, $replacements, $where));
}
$queryBuilderClone->add('where', str_replace($aliases, $replacements, (string) $wherePart));

return $queryBuilderClone;
}
Expand Down