Skip to content

Commit

Permalink
Apply filter even if the operator is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 15, 2020
1 parent f87ea9f commit 6dc0b93
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
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
{
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

0 comments on commit 6dc0b93

Please sign in to comment.