Skip to content

Commit

Permalink
Fix countFilter condition (#1287)
Browse files Browse the repository at this point in the history
* Fix countFilter condition

* Improve tests
  • Loading branch information
VincentLanglet authored Feb 5, 2021
1 parent 83a789f commit 0a32db3
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/Filter/CountFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function filter(BaseProxyQueryInterface $query, $alias, $field, $data)
$parameterName = $this->getNewParameterName($query);
$rootAlias = current($query->getQueryBuilder()->getRootAliases());
$query->getQueryBuilder()->addGroupBy($rootAlias);
$this->applyWhere($query, sprintf('COUNT(%s.%s) %s :%s', $alias, $field, $operator, $parameterName));
$this->applyHaving($query, sprintf('COUNT(%s.%s) %s :%s', $alias, $field, $operator, $parameterName));
$query->getQueryBuilder()->setParameter($parameterName, $data['value']);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Filter/BooleanFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testFilterNo(): void

$filter->filter($builder, 'alias', 'field', ['type' => null, 'value' => BooleanType::TYPE_NO]);

$this->assertSame(['alias.field = :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias.field = :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => 0], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -72,7 +72,7 @@ public function testFilterYes(): void

$filter->filter($builder, 'alias', 'field', ['type' => null, 'value' => BooleanType::TYPE_YES]);

$this->assertSame(['alias.field = :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias.field = :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => 1], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -86,7 +86,7 @@ public function testFilterArray(): void

$filter->filter($builder, 'alias', 'field', ['type' => null, 'value' => [BooleanType::TYPE_NO]]);

$this->assertSame(['alias.field IN ("0")'], $builder->query);
$this->assertSame(['WHERE alias.field IN ("0")'], $builder->query);
$this->assertTrue($filter->isActive());
}
}
8 changes: 4 additions & 4 deletions tests/Filter/CallbackFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testFilterClosure(): void

$filter->filter($builder, 'alias', 'field', 'myValue');

$this->assertSame(['CUSTOM QUERY alias.field'], $builder->query);
$this->assertSame(['WHERE CUSTOM QUERY alias.field'], $builder->query);
$this->assertSame(['value' => 'myValue'], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -64,7 +64,7 @@ public function testFilterMethod(): void

$filter->filter($builder, 'alias', 'field', 'myValue');

$this->assertSame(['CUSTOM QUERY alias.field'], $builder->query);
$this->assertSame(['WHERE CUSTOM QUERY alias.field'], $builder->query);
$this->assertSame(['value' => 'myValue'], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testApplyMethod(): void

$filter->apply($builder, ['value' => 'myValue']);

$this->assertSame(['CUSTOM QUERY o.field_name_test'], $builder->query);
$this->assertSame(['WHERE CUSTOM QUERY o.field_name_test'], $builder->query);
$this->assertSame(['value' => 'myValue'], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public function testWrongCallbackReturnType(): void
);
$filter->filter($builder, 'alias', 'field', 'myValue');

$this->assertSame(['CUSTOM QUERY alias.field'], $builder->query);
$this->assertSame(['WHERE CUSTOM QUERY alias.field'], $builder->query);
$this->assertSame(['value' => 'myValue'], $builder->queryParameters);
}
}
14 changes: 7 additions & 7 deletions tests/Filter/ChoiceFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testFilterArray(): void

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => ['1', '2']]);

$this->assertSame(['alias.field IN :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias.field IN :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => ['1', '2']], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -67,15 +67,15 @@ public function testFilterArrayWithNullValue(): void

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => ['1', null]]);

$this->assertSame(['alias.field IN :field_name_0 OR alias.field IS NULL'], $builder->query);
$this->assertSame(['WHERE alias.field IN :field_name_0 OR alias.field IS NULL'], $builder->query);
$this->assertSame(['field_name_0' => ['1']], $builder->queryParameters);
$this->assertTrue($filter->isActive());

$builder = new ProxyQuery($this->createQueryBuilderStub());

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_NOT_EQUAL, 'value' => ['1', null]]);

$this->assertSame(['alias.field IS NOT NULL AND alias.field NOT IN :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias.field IS NOT NULL AND alias.field NOT IN :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => ['1']], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -89,7 +89,7 @@ public function testFilterScalar(): void

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => '1']);

$this->assertSame(['alias.field = :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias.field = :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => '1'], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -103,15 +103,15 @@ public function testFilterNull(): void

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => null]);

$this->assertSame(['alias.field IS NULL'], $builder->query);
$this->assertSame(['WHERE alias.field IS NULL'], $builder->query);
$this->assertSame([], $builder->queryParameters);
$this->assertTrue($filter->isActive());

$builder = new ProxyQuery($this->createQueryBuilderStub());

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_NOT_EQUAL, 'value' => null]);

$this->assertSame(['alias.field IS NOT NULL'], $builder->query);
$this->assertSame(['WHERE alias.field IS NOT NULL'], $builder->query);
$this->assertSame([], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -125,7 +125,7 @@ public function testFilterZero(): void

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => 0]);

$this->assertSame(['alias.field = :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias.field = :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => 0], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Filter/ClassFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public function testFilter(): void
$filter->filter($builder, 'alias', 'field', ['value' => 'type']);

$expected = [
'alias INSTANCE OF type',
'alias NOT INSTANCE OF type',
'alias INSTANCE OF type',
'WHERE alias INSTANCE OF type',
'WHERE alias NOT INSTANCE OF type',
'WHERE alias INSTANCE OF type',
];

$this->assertSame($expected, $builder->query);
Expand Down
12 changes: 6 additions & 6 deletions tests/Filter/CountFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public function testFilter(string $expected, ?int $type): void
public function filterDataProvider(): array
{
return [
['COUNT(alias.field) = :field_name_0', NumberOperatorType::TYPE_EQUAL],
['COUNT(alias.field) >= :field_name_0', NumberOperatorType::TYPE_GREATER_EQUAL],
['COUNT(alias.field) > :field_name_0', NumberOperatorType::TYPE_GREATER_THAN],
['COUNT(alias.field) <= :field_name_0', NumberOperatorType::TYPE_LESS_EQUAL],
['COUNT(alias.field) < :field_name_0', NumberOperatorType::TYPE_LESS_THAN],
['COUNT(alias.field) = :field_name_0', null],
['HAVING COUNT(alias.field) = :field_name_0', NumberOperatorType::TYPE_EQUAL],
['HAVING COUNT(alias.field) >= :field_name_0', NumberOperatorType::TYPE_GREATER_EQUAL],
['HAVING COUNT(alias.field) > :field_name_0', NumberOperatorType::TYPE_GREATER_THAN],
['HAVING COUNT(alias.field) <= :field_name_0', NumberOperatorType::TYPE_LESS_EQUAL],
['HAVING COUNT(alias.field) < :field_name_0', NumberOperatorType::TYPE_LESS_THAN],
['HAVING COUNT(alias.field) = :field_name_0', null],
];
}
}
10 changes: 5 additions & 5 deletions tests/Filter/DateRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testFilterStartDateAndEndDate(): void
],
]);

$this->assertSame(['alias.field >= :field_name_0', 'alias.field <= :field_name_1'], $builder->query);
$this->assertSame(['WHERE alias.field >= :field_name_0', 'WHERE alias.field <= :field_name_1'], $builder->query);
$this->assertSame([
'field_name_0' => $startDateTime,
'field_name_1' => $endDateTime,
Expand All @@ -92,7 +92,7 @@ public function testFilterStartDate(): void
],
]);

$this->assertSame(['alias.field >= :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias.field >= :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => $startDateTime], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -114,7 +114,7 @@ public function testFilterEndDate(): void
],
]);

$this->assertSame(['alias.field <= :field_name_1'], $builder->query);
$this->assertSame(['WHERE alias.field <= :field_name_1'], $builder->query);
$this->assertSame(['field_name_1' => $endDateTime], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public function testFilterEndDateCoversWholeDay(
]);

$this->assertTrue($filter->isActive());
$this->assertSame(['alias.field <= :field_name_1'], $builder->query);
$this->assertSame(['WHERE alias.field <= :field_name_1'], $builder->query);
$this->assertSame(['field_name_1' => $modelEndDateTime], $builder->queryParameters);
$this->assertSame($expectedEndDateTime->getTimestamp(), $modelEndDateTime->getTimestamp());
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public function testFilterEndDateImmutable(): void
],
]);

$this->assertSame(['alias.field <= :field_name_1'], $builder->query);
$this->assertSame(['WHERE alias.field <= :field_name_1'], $builder->query);
$this->assertCount(1, $builder->queryParameters);
$this->assertSame(
$endDateTime->modify('+23 hours 59 minutes 59 seconds')->getTimestamp(),
Expand Down
8 changes: 4 additions & 4 deletions tests/Filter/EmptyFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public function testRenderSettings(): void
public function valueDataProvider(): array
{
return [
[false, BooleanType::TYPE_YES, 'alias.field IS NULL'],
[false, BooleanType::TYPE_NO, 'alias.field IS NOT NULL'],
[true, BooleanType::TYPE_YES, 'alias.field IS NOT NULL'],
[true, BooleanType::TYPE_NO, 'alias.field IS NULL'],
[false, BooleanType::TYPE_YES, 'WHERE alias.field IS NULL'],
[false, BooleanType::TYPE_NO, 'WHERE alias.field IS NOT NULL'],
[true, BooleanType::TYPE_YES, 'WHERE alias.field IS NOT NULL'],
[true, BooleanType::TYPE_NO, 'WHERE alias.field IS NULL'],
];
}
}
4 changes: 2 additions & 2 deletions tests/Filter/FilterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ static function (string $name, $value) use ($queryBuilder): void {

$queryBuilder->method('andWhere')->willReturnCallback(
static function ($query) use ($queryBuilder): void {
$queryBuilder->query[] = (string) $query;
$queryBuilder->query[] = sprintf('WHERE %s', $query);
}
);

$queryBuilder->method('andHaving')->willReturnCallback(
static function ($query) use ($queryBuilder): void {
$queryBuilder->query[] = (string) $query;
$queryBuilder->query[] = sprintf('HAVING %s', $query);
}
);

Expand Down
12 changes: 6 additions & 6 deletions tests/Filter/ModelFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testFilterArray(): void
]);

// the alias is now computer by the entityJoin method
$this->assertSame(['alias IN :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias IN :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => ['1', '2']], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -79,7 +79,7 @@ public function testFilterArrayTypeIsNotEqual(): void

// the alias is now computer by the entityJoin method
$this->assertSame(
'alias NOT IN :field_name_0 OR IDENTITY('.current(($builder->getRootAliases())).'.field_name) IS NULL',
'WHERE alias NOT IN :field_name_0 OR IDENTITY('.current(($builder->getRootAliases())).'.field_name) IS NULL',
$builder->query[0]
);
$this->assertSame(['field_name_0' => ['1', '2']], $builder->queryParameters);
Expand All @@ -95,7 +95,7 @@ public function testFilterScalar(): void

$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => 2]);

$this->assertSame(['alias IN :field_name_0'], $builder->query);
$this->assertSame(['WHERE alias IN :field_name_0'], $builder->query);
$this->assertSame(['field_name_0' => [2]], $builder->queryParameters);
$this->assertTrue($filter->isActive());
}
Expand All @@ -110,7 +110,7 @@ public function testFilterScalarTypeIsNotEqual(): void
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_NOT_EQUAL, 'value' => 2]);

$this->assertSame(
'alias NOT IN :field_name_0 OR IDENTITY('.current(($builder->getRootAliases())).'.field_name) IS NULL',
'WHERE alias NOT IN :field_name_0 OR IDENTITY('.current(($builder->getRootAliases())).'.field_name) IS NULL',
$builder->query[0]
);

Expand Down Expand Up @@ -160,7 +160,7 @@ public function testAssociationWithValidMapping(): void

$this->assertSame([
'LEFT JOIN o.association_mapping AS s_association_mapping',
's_association_mapping IN :field_name_0',
'WHERE s_association_mapping IN :field_name_0',
], $builder->query);
$this->assertTrue($filter->isActive());
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public function testAssociationWithValidParentAssociationMappings(): void
'LEFT JOIN o.association_mapping AS s_association_mapping',
'LEFT JOIN s_association_mapping.sub_association_mapping AS s_association_mapping_sub_association_mapping',
'LEFT JOIN s_association_mapping_sub_association_mapping.sub_sub_association_mapping AS s_association_mapping_sub_association_mapping_sub_sub_association_mapping',
's_association_mapping_sub_association_mapping_sub_sub_association_mapping IN :field_name_0',
'WHERE s_association_mapping_sub_association_mapping_sub_sub_association_mapping IN :field_name_0',
], $builder->query);
$this->assertTrue($filter->isActive());
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Filter/NullFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function testRenderSettings(): void
public function valueDataProvider(): array
{
return [
[false, BooleanType::TYPE_YES, 'alias.field IS NULL'],
[false, BooleanType::TYPE_NO, 'alias.field IS NOT NULL'],
[true, BooleanType::TYPE_YES, 'alias.field IS NOT NULL'],
[true, BooleanType::TYPE_NO, 'alias.field IS NULL'],
[false, BooleanType::TYPE_YES, 'WHERE alias.field IS NULL'],
[false, BooleanType::TYPE_NO, 'WHERE alias.field IS NOT NULL'],
[true, BooleanType::TYPE_YES, 'WHERE alias.field IS NOT NULL'],
[true, BooleanType::TYPE_NO, 'WHERE alias.field IS NULL'],
];
}
}
12 changes: 6 additions & 6 deletions tests/Filter/NumberFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public function testFilter(): void
$filter->filter($builder, 'alias', 'field', ['value' => 42]);

$expected = [
'alias.field = :field_name_0',
'alias.field >= :field_name_1',
'alias.field > :field_name_2',
'alias.field <= :field_name_3',
'alias.field < :field_name_4',
'alias.field = :field_name_5',
'WHERE alias.field = :field_name_0',
'WHERE alias.field >= :field_name_1',
'WHERE alias.field > :field_name_2',
'WHERE alias.field <= :field_name_3',
'WHERE alias.field < :field_name_4',
'WHERE alias.field = :field_name_5',
];

$this->assertSame($expected, $builder->query);
Expand Down
Loading

0 comments on commit 0a32db3

Please sign in to comment.