Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Filters/GlobalFilters/GlobalFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public function toArray(): array
];
}

public function isDeclared(string $key): bool
{
return collect(sharp()->config()->get('global_filters'))
->contains(function (GlobalRequiredFilter $filter) use ($key) {
$filter->buildFilterConfig();

if (class_exists($key)) {
return $filter instanceof $key;
}

return $filter->getKey() === $key;
});
}

public function findFilter(string $key): ?Filter
{
return $this->filterContainer()->findFilterHandler($key);
Expand Down
11 changes: 7 additions & 4 deletions src/Http/Context/SharpContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public function globalFilterValue(string $handlerClassOrKey): array|string|null
{
$handler = $this->globalFiltersHandler->findFilter($handlerClassOrKey);

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

return $handler->currentValue();
}
Expand Down
77 changes: 0 additions & 77 deletions tests/Http/GlobalFilterControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,80 +93,3 @@ public function defaultValue(): mixed
);
});
});

it('does not sends filters of none configured', function () {
sharp()->config()->declareEntity(PersonEntity::class);

$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->where('globalFilters', null)
);
});

it('does not sends filters of there have no values', function () {
sharp()->config()->declareEntity(PersonEntity::class);

sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test-no-values');
}

public function values(): array
{
return [];
}

public function defaultValue(): mixed
{
return null;
}
}
);

$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->where('globalFilters', null)
);

sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test');
}

public function values(): array
{
return [
1 => 'One',
2 => 'Two',
3 => 'Three',
];
}

public function defaultValue(): mixed
{
return 2;
}
}
);

$this
->followingRedirects()
->get('/sharp/3/s-list/person')
->assertInertia(fn (Assert $page) => $page
->has('globalFilters.config.filters._root', 1)
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
->where('key', 'test')
->etc()
)
);
});
153 changes: 153 additions & 0 deletions tests/Http/GlobalFilterDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

use Code16\Sharp\Exceptions\SharpInvalidGlobalFilterKeyException;
use Code16\Sharp\Filters\GlobalRequiredFilter;
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
use Inertia\Testing\AssertableInertia as Assert;

beforeEach(function () {
login();
sharp()->config()->declareEntity(PersonEntity::class);
});

it('does not sends filters of none configured', function () {
$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->where('globalFilters', null)
);
});

it('does not sends filters of there have no values', function () {
sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test-no-values');
}

public function values(): array
{
return [];
}

public function defaultValue(): mixed
{
return null;
}
}
);

$this
->followingRedirects()
->get('/sharp/s-list/person')
->assertInertia(fn (Assert $page) => $page
->where('globalFilters', null)
);

sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test');
}

public function values(): array
{
return [
1 => 'One',
2 => 'Two',
3 => 'Three',
];
}

public function defaultValue(): mixed
{
return 2;
}
}
);

$this
->followingRedirects()
->get('/sharp/3/s-list/person')
->assertInertia(fn (Assert $page) => $page
->has('globalFilters.config.filters._root', 1)
->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter
->where('key', 'test')
->etc()
)
);
});

it('globalFilterValue() throws if global filter is not declared', function () {
$this
->followingRedirects()
->get('/sharp/s-list/person');

expect(fn () => sharp()->context()->globalFilterValue('test'))->toThrow(SharpInvalidGlobalFilterKeyException::class);
});

it('globalFilterValue() returns null if global filter is not authorized', function () {
sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test');
}

public function values(): array
{
return [
1 => 'One',
2 => 'Two',
3 => 'Three',
];
}

public function defaultValue(): mixed
{
return 2;
}

public function authorize(): bool
{
return false;
}
}
);

$this->get(route('code16.sharp.list', ['globalFilter' => 'root', 'entityKey' => 'person']));

expect(sharp()->context()->globalFilterValue('test'))->toBeNull();
});

it('globalFilterValue() returns null if global filter has no values', function () {
sharp()->config()->addGlobalFilter(
new class() extends GlobalRequiredFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('test');
}

public function values(): array
{
return [];
}

public function defaultValue(): mixed
{
return 2;
}
}
);

$this->get(route('code16.sharp.list', ['globalFilter' => 'root', 'entityKey' => 'person']));

expect(sharp()->context()->globalFilterValue('test'))->toBeNull();
});
Loading