Skip to content

Commit 9db1a68

Browse files
committed
Fix testing with global filters
1 parent c946f77 commit 9db1a68

File tree

6 files changed

+140
-78
lines changed

6 files changed

+140
-78
lines changed

docs/guide/testing-with-sharp.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,20 @@ it('allows the user to display a leaf form', function () {
117117
->assertOk();
118118
});
119119
```
120+
121+
#### `withSharpGlobalFilterKeys(array|string $filterKeys): self`
122+
123+
You can specify the global filter keys to use in the Sharp context.
124+
125+
```php
126+
it('allows the user to display a leaf form', function () {
127+
$tenant = Tenant::factory()->create();
128+
$user = User::factory()->create(['tenant_id' => $tenant->id]);
129+
130+
$this
131+
->loginAsSharpUser($user)
132+
->withSharpGlobalFilterKeys($tenant->key)
133+
->getSharpForm(LeafEntity::class, 16)
134+
->assertOk();
135+
});
136+
```

src/Http/Middleware/HandleGlobalFilters.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public function handle(Request $request, Closure $next)
3030
->setCurrentValue($filterKeys[$index])
3131
);
3232

33-
if (sharp()->context()->globalFilterUrlSegmentValue() !== $filterKey) {
33+
if (! $request->wantsJson()
34+
&& $request->isMethod('GET')
35+
&& sharp()->context()->globalFilterUrlSegmentValue() !== $filterKey
36+
) {
3437
return redirect()->route('code16.sharp.home', [
3538
'filterKey' => sharp()->context()->globalFilterUrlSegmentValue(),
3639
]);

src/Utils/Testing/SharpAssertions.php

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
trait SharpAssertions
1313
{
1414
private BreadcrumbBuilder $breadcrumbBuilder;
15+
private ?string $filterKey = null;
1516

1617
/**
1718
* @deprecated use withSharpBreadcrumb() instead
@@ -42,28 +43,36 @@ public function withSharpBreadcrumb(Closure $callback): self
4243
return $this;
4344
}
4445

45-
public function deleteFromSharpShow(string $entityClassNameOrKey, $instanceId)
46+
public function withSharpGlobalFilterKeys(array|string $filterKeys): self
4647
{
47-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
48+
$this->filterKey = collect((array) $filterKeys)
49+
->implode(GlobalFilters::$valuesUrlSeparator);
50+
51+
return $this;
52+
}
53+
54+
public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanceId)
55+
{
56+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
4857

4958
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
5059

5160
return $this
5261
->delete(
5362
route(
5463
'code16.sharp.show.delete', [
55-
$this->breadcrumbBuilder($entityKey)
56-
->generateUri(),
57-
$entityKey,
58-
$instanceId,
64+
'filterKey' => $this->filterKey,
65+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
66+
'entityKey' => $entityKey,
67+
'instanceId' => $instanceId,
5968
]
6069
),
6170
);
6271
}
6372

64-
public function deleteFromSharpList(string $entityClassNameOrKey, $instanceId)
73+
public function deleteFromSharpList(string $entityClassNameOrKey, mixed $instanceId)
6574
{
66-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
75+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
6776

6877
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
6978

@@ -74,12 +83,18 @@ public function deleteFromSharpList(string $entityClassNameOrKey, $instanceId)
7483
$this->breadcrumbBuilder($entityKey)
7584
),
7685
)
77-
->delete(route('code16.sharp.api.list.delete', [$entityKey, $instanceId]));
86+
->delete(
87+
route('code16.sharp.api.list.delete', [
88+
'filterKey' => $this->filterKey,
89+
'entityKey' => $entityKey,
90+
'instanceId' => $instanceId,
91+
])
92+
);
7893
}
7994

80-
public function getSharpForm(string $entityClassNameOrKey, $instanceId = null)
95+
public function getSharpForm(string $entityClassNameOrKey, mixed $instanceId = null)
8196
{
82-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
97+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
8398

8499
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
85100

@@ -89,26 +104,26 @@ public function getSharpForm(string $entityClassNameOrKey, $instanceId = null)
89104
? route(
90105
'code16.sharp.form.edit',
91106
[
92-
$this->breadcrumbBuilder($entityKey)
93-
->generateUri(),
94-
$entityKey,
95-
$instanceId,
107+
'filterKey' => $this->filterKey,
108+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
109+
'entityKey' => $entityKey,
110+
'instanceId' => $instanceId,
96111
]
97112
)
98113
: route(
99114
'code16.sharp.form.create',
100115
[
101-
$this->breadcrumbBuilder($entityKey)
102-
->generateUri(),
103-
$entityKey,
116+
'filterKey' => $this->filterKey,
117+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
118+
'entityKey' => $entityKey,
104119
]
105120
),
106121
);
107122
}
108123

109124
public function getSharpSingleForm(string $entityClassNameOrKey)
110125
{
111-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
126+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
112127

113128
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
114129

@@ -117,28 +132,28 @@ public function getSharpSingleForm(string $entityClassNameOrKey)
117132
route(
118133
'code16.sharp.form.edit',
119134
[
120-
$this->breadcrumbBuilder($entityKey)
121-
->generateUri(),
122-
$entityKey,
135+
'filterKey' => $this->filterKey,
136+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
137+
'entityKey' => $entityKey,
123138
]
124139
)
125140
);
126141
}
127142

128143
public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array $data)
129144
{
130-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
145+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
131146

132147
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
133148

134149
return $this
135150
->post(
136151
route(
137152
'code16.sharp.form.update', [
138-
$this->breadcrumbBuilder($entityKey)
139-
->generateUri(),
140-
$entityKey,
141-
$instanceId,
153+
'filterKey' => $this->filterKey,
154+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
155+
'entityKey' => $entityKey,
156+
'instanceId' => $instanceId,
142157
]
143158
),
144159
$data,
@@ -147,17 +162,17 @@ public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array
147162

148163
public function updateSharpSingleForm(string $entityClassNameOrKey, array $data)
149164
{
150-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
165+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
151166

152167
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
153168

154169
return $this
155170
->post(
156171
route(
157172
'code16.sharp.form.update', [
158-
$this->breadcrumbBuilder($entityKey)
159-
->generateUri(),
160-
$entityKey,
173+
'filterKey' => $this->filterKey,
174+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
175+
'entityKey' => $entityKey,
161176
]
162177
),
163178
$data,
@@ -166,7 +181,7 @@ public function updateSharpSingleForm(string $entityClassNameOrKey, array $data)
166181

167182
public function getSharpShow(string $entityClassNameOrKey, $instanceId)
168183
{
169-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
184+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
170185

171186
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
172187

@@ -175,18 +190,18 @@ public function getSharpShow(string $entityClassNameOrKey, $instanceId)
175190
route(
176191
'code16.sharp.show.show',
177192
[
178-
$this->breadcrumbBuilder($entityKey)
179-
->generateUri(),
180-
$entityKey,
181-
$instanceId,
193+
'filterKey' => $this->filterKey,
194+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
195+
'entityKey' => $entityKey,
196+
'instanceId' => $instanceId,
182197
]
183198
),
184199
);
185200
}
186201

187202
public function storeSharpForm(string $entityClassNameOrKey, array $data)
188203
{
189-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
204+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
190205

191206
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
192207

@@ -195,9 +210,9 @@ public function storeSharpForm(string $entityClassNameOrKey, array $data)
195210
route(
196211
'code16.sharp.form.store',
197212
[
198-
$this->breadcrumbBuilder($entityKey)
199-
->generateUri(),
200-
$entityKey,
213+
'filterKey' => $this->filterKey,
214+
'parentUri' => $this->breadcrumbBuilder($entityKey)->generateUri(),
215+
'entityKey' => $entityKey,
201216
]
202217
),
203218
$data,
@@ -211,7 +226,7 @@ public function callSharpInstanceCommandFromList(
211226
array $data = [],
212227
?string $commandStep = null
213228
) {
214-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
229+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
215230

216231
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
217232

@@ -242,7 +257,7 @@ public function callSharpInstanceCommandFromShow(
242257
array $data = [],
243258
?string $commandStep = null
244259
) {
245-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
260+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
246261

247262
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
248263

@@ -272,7 +287,7 @@ public function callSharpEntityCommandFromList(
272287
array $data = [],
273288
?string $commandStep = null
274289
) {
275-
URL::defaults(['filterKey' => GlobalFilters::$defaultKey]);
290+
URL::defaults(['filterKey' => $this->filterKey ?: GlobalFilters::$defaultKey]);
276291

277292
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
278293

@@ -283,9 +298,7 @@ public function callSharpEntityCommandFromList(
283298
return $this
284299
->withHeader(
285300
SharpBreadcrumb::CURRENT_PAGE_URL_HEADER,
286-
$this->buildCurrentPageUrl(
287-
$this->breadcrumbBuilder($entityKey)
288-
),
301+
$this->buildCurrentPageUrl($this->breadcrumbBuilder($entityKey))
289302
)
290303
->postJson(
291304
route('code16.sharp.api.list.command.entity', compact('entityKey', 'commandKey')),
@@ -313,8 +326,9 @@ private function buildCurrentPageUrl(BreadcrumbBuilder $builder): string
313326
{
314327
return url(
315328
sprintf(
316-
'/%s/%s',
329+
'/%s/%s/%s',
317330
sharp()->config()->get('custom_url_segment'),
331+
sharp()->context()->globalFilterUrlSegmentValue(),
318332
$builder->generateUri()
319333
)
320334
);

tests/Http/GlobalFilterRoutesTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use Code16\Sharp\Filters\GlobalRequiredFilter;
43
use Code16\Sharp\Tests\Fixtures\Entities\DashboardEntity;
54
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
65
use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity;
@@ -77,32 +76,3 @@
7776
expect(sharp()->context()->globalFilterValue('test1'))->toEqual('one');
7877
expect(sharp()->context()->globalFilterValue('test2'))->toEqual('two');
7978
});
80-
81-
function fakeGlobalFilter(string $key = 'test'): void
82-
{
83-
sharp()->config()->addGlobalFilter(
84-
new class($key) extends GlobalRequiredFilter
85-
{
86-
public function __construct(private string $key) {}
87-
88-
public function buildFilterConfig(): void
89-
{
90-
$this->configureKey($this->key);
91-
}
92-
93-
public function values(): array
94-
{
95-
return [
96-
'one' => 'Company One',
97-
'two' => 'Company Two',
98-
'three' => 'Company Three',
99-
];
100-
}
101-
102-
public function defaultValue(): mixed
103-
{
104-
return 'two';
105-
}
106-
}
107-
);
108-
}

tests/Pest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Code16\Sharp\Filters\GlobalRequiredFilter;
34
use Code16\Sharp\Tests\Fixtures\User;
45
use Code16\Sharp\Tests\TestCase;
56
use Code16\Sharp\Utils\Entities\SharpEntityManager;
@@ -147,3 +148,31 @@ function createImage(string $disk = 'local', string $name = 'test.png'): string
147148

148149
return $file->storeAs('data', $name, ['disk' => $disk]);
149150
}
151+
152+
function fakeGlobalFilter(string $key = 'test'): void
153+
{
154+
sharp()->config()->addGlobalFilter(
155+
new class($key) extends GlobalRequiredFilter
156+
{
157+
public function __construct(private string $key) {}
158+
159+
public function buildFilterConfig(): void
160+
{
161+
$this->configureKey($this->key);
162+
}
163+
164+
public function values(): array
165+
{
166+
return [
167+
'one' => 'One',
168+
'two' => 'Two',
169+
];
170+
}
171+
172+
public function defaultValue(): mixed
173+
{
174+
return 'two';
175+
}
176+
}
177+
);
178+
}

0 commit comments

Comments
 (0)