Skip to content

Commit 79a30f7

Browse files
committed
Fix tests
1 parent 0f350cc commit 79a30f7

14 files changed

Lines changed: 82 additions & 77 deletions

src/Filters/GlobalFilters/GlobalFilters.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ final class GlobalFilters implements Arrayable
1111
{
1212
use HasFilters;
1313

14+
public static string $defaultKey = 'root';
15+
1416
public function getFilters(): array
1517
{
1618
return collect(sharp()->config()->get('global_filters'))

src/Http/Context/SharpBreadcrumb.php

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

33
namespace Code16\Sharp\Http\Context;
44

5-
use Code16\Sharp\Filters\GlobalRequiredFilter;
65
use Code16\Sharp\Http\Context\Util\BreadcrumbItem;
76
use Code16\Sharp\Utils\Entities\SharpEntityManager;
87
use Code16\Sharp\Utils\Entities\ValueObjects\EntityKey;
@@ -79,7 +78,7 @@ public function getCurrentSegmentUrl(): string
7978
sprintf(
8079
'%s/%s/%s',
8180
sharp()->config()->get('custom_url_segment'),
82-
$this->getGlobalFilterKey(),
81+
sharp()->context()->globalFilterUrlSegmentValue(),
8382
$this->getCurrentPath()
8483
)
8584
);
@@ -98,7 +97,7 @@ public function getPreviousSegmentUrl(): string
9897
sprintf(
9998
'%s/%s/%s',
10099
sharp()->config()->get('custom_url_segment'),
101-
$this->getGlobalFilterKey(),
100+
sharp()->context()->globalFilterUrlSegmentValue(),
102101
$this->breadcrumbItems()
103102
->slice(0, -1)
104103
->map(fn (BreadcrumbItem $item) => $item->toUri())
@@ -333,14 +332,4 @@ public function forceRequestSegments(array|Collection $segments): void
333332
$this->breadcrumbItems = null;
334333
$this->forcedSegments = collect($segments)->values();
335334
}
336-
337-
private function getGlobalFilterKey(): string
338-
{
339-
$globalFilterValues = collect(sharp()->config()->get('global_filters'))
340-
->map(fn ($globalFilterClassOrInstance) => is_string($globalFilterClassOrInstance) ? app($globalFilterClassOrInstance) : $globalFilterClassOrInstance)
341-
->map(fn (GlobalRequiredFilter $globalFilter) => $globalFilter->currentValue())
342-
->filter();
343-
344-
return $globalFilterValues->isEmpty() ? 'root' : $globalFilterValues->implode('-');
345-
}
346335
}

src/Http/Context/SharpContext.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public function globalFilterValue(string $handlerClassOrKey): array|string|null
2323
return $handler->currentValue();
2424
}
2525

26+
public function globalFilterUrlSegmentValue(): string
27+
{
28+
return collect(sharp()->config()->get('global_filters'))
29+
->map(fn ($globalFilterClassOrInstance) => is_string($globalFilterClassOrInstance) ? app($globalFilterClassOrInstance) : $globalFilterClassOrInstance)
30+
->map(fn (GlobalRequiredFilter $globalFilter) => $globalFilter->currentValue())
31+
->filter()
32+
->implode('-') ?: GlobalFilters::$defaultKey;
33+
}
34+
2635
public function retainedFilterValue(string $handlerClassOrKey): array|string|null
2736
{
2837
$key = class_exists($handlerClassOrKey)

src/Http/Middleware/HandleGlobalFilters.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@
33
namespace Code16\Sharp\Http\Middleware;
44

55
use Closure;
6-
use Code16\Sharp\Filters\GlobalRequiredFilter;
76
use Illuminate\Support\Facades\URL;
87

98
class HandleGlobalFilters
109
{
1110
public function handle($request, Closure $next)
1211
{
13-
$globalFilterValues = collect(sharp()->config()->get('global_filters'))
14-
->map(fn ($globalFilterClassOrInstance) => is_string($globalFilterClassOrInstance) ? app($globalFilterClassOrInstance) : $globalFilterClassOrInstance)
15-
->map(fn (GlobalRequiredFilter $globalFilter) => $globalFilter->currentValue())
16-
->filter();
17-
18-
URL::defaults(['filterKey' => $globalFilterValues->isEmpty() ? 'root' : $globalFilterValues->implode('-')]);
12+
URL::defaults(['filterKey' => sharp()->context()->globalFilterUrlSegmentValue()]);
1913

2014
return $next($request);
2115
}

src/Utils/Links/LinkToShowPage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public function renderAsUrl(): string
4242
if (isset($this->breadcrumbBuilder)) {
4343
return url(
4444
sprintf(
45-
'%s/%s/%s',
45+
'%s/%s/%s/%s',
4646
sharp()->config()->get('custom_url_segment'),
47+
sharp()->context()->globalFilterUrlSegmentValue(),
4748
$this->breadcrumbBuilder->generateUri(),
4849
$this->generateUri()
4950
)

tests/Http/Api/Commands/ApiEntityListQuickCreationCommandControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ public function update($id, array $data)
287287
$this
288288
->get(
289289
route('code16.sharp.show.show', [
290+
'filterKey' => 'root',
290291
'parentUri' => 's-list/person/',
291292
'person',
292293
1,
@@ -303,6 +304,6 @@ public function update($id, array $data)
303304
->assertOk()
304305
->assertJson([
305306
'action' => 'link',
306-
'link' => url('/sharp/s-list/person/s-show/person/1/s-show/colleague/4'),
307+
'link' => url('/sharp/root/s-list/person/s-show/person/1/s-show/colleague/4'),
307308
]);
308309
});

tests/Http/BreadcrumbTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Inertia\Testing\AssertableInertia as Assert;
1111

1212
beforeEach(function () {
13-
sharp()->config()
14-
->displayBreadcrumb()
15-
->declareEntity(PersonEntity::class);
13+
sharp()->config()->displayBreadcrumb()->declareEntity(PersonEntity::class);
1614
login();
15+
16+
// We add a default here to avoid putting this everywhere in unit tests
17+
// it's handled by middleware in a real request, but we don't want to test that here.
18+
\Illuminate\Support\Facades\URL::defaults(['filterKey' => \Code16\Sharp\Filters\GlobalFilters\GlobalFilters::$defaultKey]);
1719
});
1820

1921
it('builds the breadcrumb for an entity list', function () {

tests/Http/FiltersInRequestTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function getListData(): array|Arrayable
5050
});
5151

5252
$this
53-
->get('/sharp/s-list/person?filter_job=physicist')
53+
->get('/sharp/root/s-list/person?filter_job=physicist')
5454
->assertOk()
5555
->assertInertia(fn (Assert $page) => $page
5656
->has('entityList.data.0', fn (Assert $json) => $json
@@ -103,7 +103,7 @@ public function getListData(): array|Arrayable
103103
});
104104

105105
$this
106-
->get('/sharp/s-list/person')
106+
->get('/sharp/root/s-list/person')
107107
->assertOk()
108108
->assertInertia(fn (Assert $page) => $page
109109
->has('entityList.data.0', fn (Assert $json) => $json
@@ -154,7 +154,7 @@ public function getListData(): array|Arrayable
154154
});
155155

156156
$this
157-
->get('/sharp/s-list/person?filter_job=physicist,physician')
157+
->get('/sharp/root/s-list/person?filter_job=physicist,physician')
158158
->assertOk()
159159
->assertInertia(fn (Assert $page) => $page
160160
->has('entityList.data.0', fn (Assert $json) => $json
@@ -171,7 +171,7 @@ public function getListData(): array|Arrayable
171171
);
172172

173173
$this
174-
->get('/sharp/s-list/person?filter_job=0')
174+
->get('/sharp/root/s-list/person?filter_job=0')
175175
->assertOk()
176176
->assertInertia(fn (Assert $page) => $page
177177
->has('entityList.data.0', fn (Assert $json) => $json
@@ -231,13 +231,13 @@ public function getListData(): array|Arrayable
231231
],
232232
])
233233
->assertSessionHasNoErrors()
234-
->assertRedirect('/sharp/s-list/person?filter_job=physicist');
234+
->assertRedirect('/sharp/root/s-list/person?filter_job=physicist');
235235

236236
expect(session('_sharp_retained_filter_job'))->toBe('physicist');
237237

238238
// Second call: filter should be valued
239239
$this
240-
->get('/sharp/s-list/person')
240+
->get('/sharp/root/s-list/person')
241241
->assertOk()
242242
->assertInertia(fn (Assert $page) => $page
243243
->has('entityList.data.0', fn (Assert $json) => $json
@@ -250,7 +250,7 @@ public function getListData(): array|Arrayable
250250

251251
// Third call: should use QS instead of session
252252
$this
253-
->get('/sharp/s-list/person?filter_job=physician')
253+
->get('/sharp/root/s-list/person?filter_job=physician')
254254
->assertOk()
255255
->assertInertia(fn (Assert $page) => $page
256256
->has('entityList.data.0', fn (Assert $json) => $json
@@ -269,10 +269,10 @@ public function getListData(): array|Arrayable
269269
],
270270
])
271271
->assertSessionHasNoErrors()
272-
->assertRedirect('/sharp/s-list/person');
272+
->assertRedirect('/sharp/root/s-list/person');
273273

274274
$this
275-
->get('/sharp/s-list/person')
275+
->get('/sharp/root/s-list/person')
276276
->assertOk()
277277
->assertInertia(fn (Assert $page) => $page
278278
->has('entityList.data.0', fn (Assert $json) => $json
@@ -324,11 +324,11 @@ public function values(): array
324324
],
325325
])
326326
->assertSessionHasNoErrors()
327-
->assertRedirect('/sharp/s-list/person?filter_job='.urlencode('physicist,physician'));
327+
->assertRedirect('/sharp/root/s-list/person?filter_job='.urlencode('physicist,physician'));
328328

329329
// Call to retain the filter on session
330330
$this
331-
->get('/sharp/s-list/person?filter_job=physicist,physician')
331+
->get('/sharp/root/s-list/person?filter_job=physicist,physician')
332332
->assertOk();
333333

334334
expect(session('_sharp_retained_filter_job'))->toBe('physicist,physician');
@@ -379,7 +379,7 @@ public function getListData(): array|Arrayable
379379

380380
// First call: use default value
381381
$this
382-
->get('/sharp/s-list/person')
382+
->get('/sharp/root/s-list/person')
383383
->assertOk()
384384
->assertInertia(fn (Assert $page) => $page
385385
->has('entityList.data.0', fn (Assert $json) => $json
@@ -397,11 +397,11 @@ public function getListData(): array|Arrayable
397397
],
398398
])
399399
->assertSessionHasNoErrors()
400-
->assertRedirect('/sharp/s-list/person?filter_job=physician');
400+
->assertRedirect('/sharp/root/s-list/person?filter_job=physician');
401401

402402
// Second call: use filter value
403403
$this
404-
->get('/sharp/s-list/person?filter_job=physician')
404+
->get('/sharp/root/s-list/person?filter_job=physician')
405405
->assertOk()
406406
->assertInertia(fn (Assert $page) => $page
407407
->has('entityList.data.0', fn (Assert $json) => $json
@@ -414,7 +414,7 @@ public function getListData(): array|Arrayable
414414

415415
// Third call: no filter, use retained value
416416
$this
417-
->get('/sharp/s-list/person')
417+
->get('/sharp/root/s-list/person')
418418
->assertOk()
419419
->assertInertia(fn (Assert $page) => $page
420420
->has('entityList.data.0', fn (Assert $json) => $json
@@ -432,11 +432,11 @@ public function getListData(): array|Arrayable
432432
],
433433
])
434434
->assertSessionHasNoErrors()
435-
->assertRedirect('/sharp/s-list/person');
435+
->assertRedirect('/sharp/root/s-list/person');
436436

437437
// Fourth call: reset retained value
438438
$this
439-
->get('/sharp/s-list/person')
439+
->get('/sharp/root/s-list/person')
440440
->assertOk()
441441
->assertInertia(fn (Assert $page) => $page
442442
->has('entityList.data.0', fn (Assert $json) => $json
@@ -482,5 +482,5 @@ public function values(): array
482482
],
483483
])
484484
->assertSessionHasNoErrors()
485-
->assertRedirect('/sharp/s-list/person?filter_job=physicist');
485+
->assertRedirect('/sharp/root/s-list/person?filter_job=physicist');
486486
});

tests/Http/Form/FormControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ public function buildFormLayout(FormLayout $formLayout): void
158158
->post('/sharp/s-list/person/s-form/person', [
159159
'name' => 'Stephen Hawking',
160160
])
161-
->assertRedirectContains('/sharp/s-list/person')
161+
->assertRedirectContains('/sharp/root/s-list/person')
162162
->assertRedirectContains('highlighted_entity_key=person')
163163
->assertRedirectContains('highlighted_instance_id=1');
164164

165165
$this
166166
->post('/sharp/s-list/person/s-form/person/1', [
167167
'name' => 'Stephen Hawking',
168168
])
169-
->assertRedirectContains('/sharp/s-list/person')
169+
->assertRedirectContains('/sharp/root/s-list/person')
170170
->assertRedirectContains('highlighted_entity_key=person')
171171
->assertRedirectContains('highlighted_instance_id=1');
172172
});

tests/Pest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
uses(TestCase::class)
1111
->in(__DIR__);
1212

13+
uses()
14+
->beforeEach(function () {
15+
// We add a default here to avoid putting this everywhere in unit tests
16+
// it's handled by middleware in a real request, but we don't want to test that here.
17+
\Illuminate\Support\Facades\URL::defaults(['filterKey' => \Code16\Sharp\Filters\GlobalFilters\GlobalFilters::$defaultKey]);
18+
})
19+
->in(__DIR__.'/Unit');
20+
1321
uses()
1422
->group('eloquent')
1523
->beforeEach(function () {

0 commit comments

Comments
 (0)