Skip to content

Commit f67dd34

Browse files
committed
Add tests
1 parent a425bd2 commit f67dd34

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/Filters/GlobalFilters/GlobalFilters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class GlobalFilters implements Arrayable
1212
use HasFilters;
1313

1414
public static string $defaultKey = 'root';
15+
public static string $valuesUrlSeparator = '~';
1516

1617
public function getFilters(): array
1718
{

src/Http/Context/SharpContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function globalFilterUrlSegmentValue(): string
3232
)
3333
->map(fn (GlobalRequiredFilter $globalFilter) => $globalFilter->currentValue())
3434
->filter()
35-
->implode(':') ?: GlobalFilters::$defaultKey;
35+
->implode(GlobalFilters::$valuesUrlSeparator) ?: GlobalFilters::$defaultKey;
3636
}
3737

3838
public function retainedFilterValue(string $handlerClassOrKey): array|string|null

src/Http/Middleware/HandleGlobalFilters.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Code16\Sharp\Http\Middleware;
44

55
use Closure;
6+
use Code16\Sharp\Filters\GlobalFilters\GlobalFilters;
67
use Code16\Sharp\Filters\GlobalRequiredFilter;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\URL;
@@ -14,7 +15,7 @@ public function handle(Request $request, Closure $next)
1415
URL::defaults(['filterKey' => sharp()->context()->globalFilterUrlSegmentValue()]);
1516

1617
if ($request->isMethod('GET') && ($filterKey = $request->route('filterKey'))) {
17-
$filterKeys = explode(':', $filterKey);
18+
$filterKeys = explode(GlobalFilters::$valuesUrlSeparator, $filterKey);
1819
$configuredGlobalFilters = collect(sharp()->config()->get('global_filters'))
1920
->map(fn ($globalFilterClassOrInstance) => is_string($globalFilterClassOrInstance)
2021
? app($globalFilterClassOrInstance)

tests/Http/GlobalFilterRoutesTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,43 @@
6565
expect(sharp()->context()->globalFilterValue('test'))->toEqual('two');
6666
});
6767

68-
// TODO test multiple filters
68+
it('redirects to route with correct filterKeys when missing and multiple global filters are defined', function () {
69+
fakeGlobalFilter('test1');
70+
fakeGlobalFilter('test2');
6971

70-
function fakeGlobalFilter(): void
72+
$this->get('/sharp/s-list/person')
73+
->assertRedirect('/sharp/two~two/s-list/person');
74+
75+
$this->post(route('code16.sharp.filters.update', 'test1'), ['value' => 'one']);
76+
$this->post(route('code16.sharp.filters.update', 'test2'), ['value' => 'three']);
77+
78+
$this->get('/sharp/s-list/person/s-show/person/1')
79+
->assertRedirect('/sharp/one~three/s-list/person/s-show/person/1');
80+
});
81+
82+
it('sets the current multiple filterKeys according to the URL', function () {
83+
fakeGlobalFilter('test1');
84+
fakeGlobalFilter('test2');
85+
86+
$this->post(route('code16.sharp.filters.update', 'test1'), ['value' => 'three']);
87+
$this->post(route('code16.sharp.filters.update', 'test2'), ['value' => 'three']);
88+
$this->get('/sharp/one~two/s-list/person/s-show/person/1')
89+
->assertOk();
90+
91+
expect(sharp()->context()->globalFilterValue('test1'))->toEqual('one');
92+
expect(sharp()->context()->globalFilterValue('test2'))->toEqual('two');
93+
});
94+
95+
function fakeGlobalFilter(string $key = 'test'): void
7196
{
7297
sharp()->config()->addGlobalFilter(
73-
new class() extends GlobalRequiredFilter
98+
new class($key) extends GlobalRequiredFilter
7499
{
100+
public function __construct(private string $key) {}
101+
75102
public function buildFilterConfig(): void
76103
{
77-
$this->configureKey('test');
104+
$this->configureKey($this->key);
78105
}
79106

80107
public function values(): array

0 commit comments

Comments
 (0)