Skip to content
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

Apply filter even if the operator is not provided #6591

Merged
merged 1 commit into from
Nov 15, 2020
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
8 changes: 5 additions & 3 deletions src/Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,11 @@ private function applyFilters(array $data): void
foreach ($this->getFilters() as $name => $filter) {
$this->values[$name] = $this->values[$name] ?? null;
$filterFormName = $filter->getFormName();
if (isset($this->values[$filterFormName]['type'], $this->values[$filterFormName]['value']) &&
('' !== $this->values[$filterFormName]['type'] || '' !== $this->values[$filterFormName]['value'])
) {

$value = $this->values[$filterFormName]['value'] ?? '';
$type = $this->values[$filterFormName]['type'] ?? '';

if ('' !== $value || '' !== $type) {
$filter->apply($this->query, $data[$filterFormName]);
}
}
Expand Down
58 changes: 51 additions & 7 deletions tests/Datagrid/DatagridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class DatagridTest extends TestCase
*/
private $formBuilder;

/**
* @var mixed[]
*/
private $formData;

/**
* @var array
*/
Expand All @@ -67,6 +72,7 @@ protected function setUp(): void
$this->columns = new FieldDescriptionCollection();
$this->pager = $this->createMock(PagerInterface::class);

$this->formData = [];
$this->formTypes = [];

$this->formBuilder = $this->getMockBuilder(FormBuilder::class)
Expand All @@ -93,13 +99,16 @@ protected function setUp(): void
);
});

$this->formBuilder
->method('getForm')
->willReturnCallback(function () {
return $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
});
$form = $this->createStub(Form::class);

$form->method('submit')->willReturnCallback(function (array $values): void {
$this->formData = $values;
});
$form->method('getData')->willReturnCallback(function (): array {
return $this->formData;
});

$this->formBuilder->method('getForm')->willReturn($form);

$values = [];

Expand Down Expand Up @@ -385,6 +394,41 @@ public function testBuildPager(): void
$this->assertInstanceOf(FormBuilder::class, $this->formBuilder->get('_per_page'));
}

/**
* @dataProvider applyFilterDataProvider
*/
public function testApplyFilter(?string $type, ?string $value, int $applyCallNumber): void
{
$this->datagrid->setValue('fooFormName', $type, $value);

$filter = $this->createMock(FilterInterface::class);
$filter->expects($this->once())->method('getName')->willReturn('foo');
$filter->method('getFormName')->willReturn('fooFormName');
$filter->method('isActive')->willReturn(false);
$filter->method('getRenderSettings')->willReturn(['foo1', ['bar1' => 'baz1']]);
$filter->expects($this->exactly($applyCallNumber))->method('apply');

$this->datagrid->addFilter($filter);

$this->datagrid->buildPager();
}

/**
* @return iterable<array{?string, ?string, int}>
*/
public function applyFilterDataProvider(): iterable
VincentLanglet marked this conversation as resolved.
Show resolved Hide resolved
{
yield ['fakeType', 'fakeValue', 1];
yield ['', 'fakeValue', 1];
yield [null, 'fakeValue', 1];
yield ['fakeType', '', 1];
yield ['fakeType', null, 1];
yield ['', '', 0];
yield ['', null, 0];
yield [null, '', 0];
yield [null, null, 0];
}

public function testBuildPagerWithException(): void
{
$this->expectException(\Symfony\Component\Form\Exception\UnexpectedTypeException::class);
Expand Down