diff --git a/src/Datagrid/Datagrid.php b/src/Datagrid/Datagrid.php index 997132ced2..763321577f 100644 --- a/src/Datagrid/Datagrid.php +++ b/src/Datagrid/Datagrid.php @@ -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]); } } diff --git a/tests/Datagrid/DatagridTest.php b/tests/Datagrid/DatagridTest.php index 57895e2cd6..945049e572 100644 --- a/tests/Datagrid/DatagridTest.php +++ b/tests/Datagrid/DatagridTest.php @@ -56,6 +56,11 @@ class DatagridTest extends TestCase */ private $formBuilder; + /** + * @var mixed[] + */ + private $formData; + /** * @var array */ @@ -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) @@ -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 = []; @@ -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 + */ + 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);