Skip to content

Commit 9e9aca0

Browse files
authored
Merge pull request #683 from code16/fix-global-filter-value-exception
Fix global filter value exception
2 parents bf3a68d + 61ddf2c commit 9e9aca0

4 files changed

Lines changed: 174 additions & 81 deletions

File tree

src/Filters/GlobalFilters/GlobalFilters.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ public function toArray(): array
5252
];
5353
}
5454

55+
public function isDeclared(string $key): bool
56+
{
57+
return collect(sharp()->config()->get('global_filters'))
58+
->contains(function (GlobalRequiredFilter $filter) use ($key) {
59+
$filter->buildFilterConfig();
60+
61+
if (class_exists($key)) {
62+
return $filter instanceof $key;
63+
}
64+
65+
return $filter->getKey() === $key;
66+
});
67+
}
68+
5569
public function findFilter(string $key): ?Filter
5670
{
5771
return $this->filterContainer()->findFilterHandler($key);

src/Http/Context/SharpContext.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ public function globalFilterValue(string $handlerClassOrKey): array|string|null
1919
{
2020
$handler = $this->globalFiltersHandler->findFilter($handlerClassOrKey);
2121

22-
throw_if(
23-
! $handler instanceof GlobalRequiredFilter,
24-
new SharpInvalidGlobalFilterKeyException('Filter ['.$handlerClassOrKey.'] is not a global required filter.')
25-
);
22+
if (! $handler instanceof GlobalRequiredFilter) {
23+
if ($this->globalFiltersHandler->isDeclared($handlerClassOrKey)) {
24+
return null;
25+
} else {
26+
throw new SharpInvalidGlobalFilterKeyException('Filter ['.$handlerClassOrKey.'] is not a global required filter.');
27+
}
28+
}
2629

2730
return $handler->currentValue();
2831
}

tests/Http/GlobalFilterControllerTest.php

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -93,80 +93,3 @@ public function defaultValue(): mixed
9393
);
9494
});
9595
});
96-
97-
it('does not sends filters of none configured', function () {
98-
sharp()->config()->declareEntity(PersonEntity::class);
99-
100-
$this
101-
->followingRedirects()
102-
->get('/sharp/s-list/person')
103-
->assertInertia(fn (Assert $page) => $page
104-
->where('globalFilters', null)
105-
);
106-
});
107-
108-
it('does not sends filters of there have no values', function () {
109-
sharp()->config()->declareEntity(PersonEntity::class);
110-
111-
sharp()->config()->addGlobalFilter(
112-
new class() extends GlobalRequiredFilter
113-
{
114-
public function buildFilterConfig(): void
115-
{
116-
$this->configureKey('test-no-values');
117-
}
118-
119-
public function values(): array
120-
{
121-
return [];
122-
}
123-
124-
public function defaultValue(): mixed
125-
{
126-
return null;
127-
}
128-
}
129-
);
130-
131-
$this
132-
->followingRedirects()
133-
->get('/sharp/s-list/person')
134-
->assertInertia(fn (Assert $page) => $page
135-
->where('globalFilters', null)
136-
);
137-
138-
sharp()->config()->addGlobalFilter(
139-
new class() extends GlobalRequiredFilter
140-
{
141-
public function buildFilterConfig(): void
142-
{
143-
$this->configureKey('test');
144-
}
145-
146-
public function values(): array
147-
{
148-
return [
149-
1 => 'One',
150-
2 => 'Two',
151-
3 => 'Three',
152-
];
153-
}
154-
155-
public function defaultValue(): mixed
156-
{
157-
return 2;
158-
}
159-
}
160-
);
161-
162-
$this
163-
->followingRedirects()
164-
->get('/sharp/3/s-list/person')
165-
->assertInertia(fn (Assert $page) => $page
166-
->has('globalFilters.config.filters._root', 1)
167-
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
168-
->where('key', 'test')
169-
->etc()
170-
)
171-
);
172-
});
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
use Code16\Sharp\Exceptions\SharpInvalidGlobalFilterKeyException;
4+
use Code16\Sharp\Filters\GlobalRequiredFilter;
5+
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
6+
use Inertia\Testing\AssertableInertia as Assert;
7+
8+
beforeEach(function () {
9+
login();
10+
sharp()->config()->declareEntity(PersonEntity::class);
11+
});
12+
13+
it('does not sends filters of none configured', function () {
14+
$this
15+
->followingRedirects()
16+
->get('/sharp/s-list/person')
17+
->assertInertia(fn (Assert $page) => $page
18+
->where('globalFilters', null)
19+
);
20+
});
21+
22+
it('does not sends filters of there have no values', function () {
23+
sharp()->config()->addGlobalFilter(
24+
new class() extends GlobalRequiredFilter
25+
{
26+
public function buildFilterConfig(): void
27+
{
28+
$this->configureKey('test-no-values');
29+
}
30+
31+
public function values(): array
32+
{
33+
return [];
34+
}
35+
36+
public function defaultValue(): mixed
37+
{
38+
return null;
39+
}
40+
}
41+
);
42+
43+
$this
44+
->followingRedirects()
45+
->get('/sharp/s-list/person')
46+
->assertInertia(fn (Assert $page) => $page
47+
->where('globalFilters', null)
48+
);
49+
50+
sharp()->config()->addGlobalFilter(
51+
new class() extends GlobalRequiredFilter
52+
{
53+
public function buildFilterConfig(): void
54+
{
55+
$this->configureKey('test');
56+
}
57+
58+
public function values(): array
59+
{
60+
return [
61+
1 => 'One',
62+
2 => 'Two',
63+
3 => 'Three',
64+
];
65+
}
66+
67+
public function defaultValue(): mixed
68+
{
69+
return 2;
70+
}
71+
}
72+
);
73+
74+
$this
75+
->followingRedirects()
76+
->get('/sharp/3/s-list/person')
77+
->assertInertia(fn (Assert $page) => $page
78+
->has('globalFilters.config.filters._root', 1)
79+
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
80+
->where('key', 'test')
81+
->etc()
82+
)
83+
);
84+
});
85+
86+
it('globalFilterValue() throws if global filter is not declared', function () {
87+
$this
88+
->followingRedirects()
89+
->get('/sharp/s-list/person');
90+
91+
expect(fn () => sharp()->context()->globalFilterValue('test'))->toThrow(SharpInvalidGlobalFilterKeyException::class);
92+
});
93+
94+
it('globalFilterValue() returns null if global filter is not authorized', function () {
95+
sharp()->config()->addGlobalFilter(
96+
new class() extends GlobalRequiredFilter
97+
{
98+
public function buildFilterConfig(): void
99+
{
100+
$this->configureKey('test');
101+
}
102+
103+
public function values(): array
104+
{
105+
return [
106+
1 => 'One',
107+
2 => 'Two',
108+
3 => 'Three',
109+
];
110+
}
111+
112+
public function defaultValue(): mixed
113+
{
114+
return 2;
115+
}
116+
117+
public function authorize(): bool
118+
{
119+
return false;
120+
}
121+
}
122+
);
123+
124+
$this->get(route('code16.sharp.list', ['globalFilter' => 'root', 'entityKey' => 'person']));
125+
126+
expect(sharp()->context()->globalFilterValue('test'))->toBeNull();
127+
});
128+
129+
it('globalFilterValue() returns null if global filter has no values', function () {
130+
sharp()->config()->addGlobalFilter(
131+
new class() extends GlobalRequiredFilter
132+
{
133+
public function buildFilterConfig(): void
134+
{
135+
$this->configureKey('test');
136+
}
137+
138+
public function values(): array
139+
{
140+
return [];
141+
}
142+
143+
public function defaultValue(): mixed
144+
{
145+
return 2;
146+
}
147+
}
148+
);
149+
150+
$this->get(route('code16.sharp.list', ['globalFilter' => 'root', 'entityKey' => 'person']));
151+
152+
expect(sharp()->context()->globalFilterValue('test'))->toBeNull();
153+
});

0 commit comments

Comments
 (0)