Skip to content

Commit 0c6e8c0

Browse files
committed
🦺 check for form in filter
1 parent fafc424 commit 0c6e8c0

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

‎.github/workflows/build.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
run: composer config minimum-stability dev && composer config prefer-stable true
7373
if: contains(matrix.symfony, '@dev')
7474
- name: Install dependencies
75-
uses: ramsey/composer-install@v3
75+
uses: ramsey/composer-install@v4
7676
with:
7777
dependency-versions: ${{ matrix.dependencies }}
7878
env:

‎src/Filter.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@ final class Filter
1515
/** @var array<int|string, FormInterface> */
1616
private array $forms = [];
1717

18-
public function __construct(private FormFactoryInterface $formFactory, private RequestStack $requestStack)
19-
{
18+
public function __construct(
19+
private readonly FormFactoryInterface $formFactory,
20+
private readonly RequestStack $requestStack,
21+
) {
2022
}
2123

2224
/**
2325
* Perform actual filtering. You need to pass an identifying name.
2426
* You'll get an array with name of fields as keys and the filtered values
2527
* as values (except for "_sort" key, which holds info for sorting).
28+
* You need to call saveFilter() before calling this method.
2629
*
2730
* @return array<string, mixed>
2831
*/
2932
public function filter(string $name): array
3033
{
3134
$filter = [];
3235
$fname = $name.$this->getSession()->getId();
36+
if (!isset($this->forms[$fname])) {
37+
throw new \UnexpectedValueException(\sprintf('No filter found for "%s". Did you call saveFilter()?', $name));
38+
}
3339
/** @var array<string, mixed>|null $values */
3440
$values = $this->getSession()->get('filter.'.$name);
3541
if (null !== $values) {

‎tests/FilterTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PUGX\FilterBundle\Tests;
44

5+
use Filter\FilterException;
56
use PHPUnit\Framework\TestCase;
67
use PUGX\FilterBundle\Filter;
78
use Symfony\Component\Form\FormFactoryInterface;
@@ -29,6 +30,12 @@ protected function setUp(): void
2930
$this->filter = new Filter($this->factory, $stack);
3031
}
3132

33+
public function testFilterWithoutSaveFilterShouldThrowException(): void
34+
{
35+
$this->expectException(\UnexpectedValueException::class);
36+
$this->filter->filter('foo');
37+
}
38+
3239
public function testFilter(): void
3340
{
3441
$this->filter->saveFilter(StubFormType::class, 'foo');
@@ -72,6 +79,7 @@ public function testFormViewWithoutPreviousForm(): void
7279

7380
public function testSort(): void
7481
{
82+
$this->filter->saveFilter(StubFormType::class, 'foo');
7583
$this->filter->sort('foo', 'bar');
7684
$filter = $this->filter->filter('foo');
7785
self::assertArrayHasKey('_sort', $filter);

0 commit comments

Comments
 (0)