File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 :
Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff line change 22
33namespace PUGX \FilterBundle \Tests ;
44
5+ use Filter \FilterException ;
56use PHPUnit \Framework \TestCase ;
67use PUGX \FilterBundle \Filter ;
78use 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 );
You can’t perform that action at this time.
0 commit comments