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
10 changes: 10 additions & 0 deletions docs/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,13 @@ $this->sharpDashboard(MyDashboard::class)
->post()
->assertOk();
```

## Global filters

If your app contains global filters, you should be able to test normally, but it will be set to its default value. If you need, you can set a specific value using `withGlobalFilter()`:

```php
$this->withSharpGlobalFilter(CompanyFilter::class, 'apple')
->sharpList(Post::class)
//...
```
7 changes: 6 additions & 1 deletion src/Filters/GlobalFilters/GlobalFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ public function toArray(): array
}

public function isDeclared(string $key): bool
{
return $this->getDeclaredFilter($key) !== null;
}

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

if (class_exists($key)) {
Expand Down
4 changes: 0 additions & 4 deletions src/Utils/Testing/Commands/PendingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public function __construct(

public function getForm(): AssertableCommandForm
{
$this->setGlobalFilterUrlDefault();

return new AssertableCommandForm(
post: $this->post,
getForm: $this->getForm,
Expand All @@ -33,8 +31,6 @@ public function getForm(): AssertableCommandForm

public function post(): AssertableCommand
{
$this->setGlobalFilterUrlDefault();

return new AssertableCommand(
postCommand: $this->post,
getForm: $this->getForm,
Expand Down
7 changes: 2 additions & 5 deletions src/Utils/Testing/Dashboard/PendingDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Code16\Sharp\Utils\Testing\Commands\FormatsDataForCommand;
use Code16\Sharp\Utils\Testing\Commands\PendingCommand;
use Code16\Sharp\Utils\Testing\IsPendingComponent;
use Code16\Sharp\Utils\Testing\SharpAssertions;
use Code16\Sharp\Utils\Testing\Show\PendingShow;
use Illuminate\Foundation\Testing\TestCase;

Expand All @@ -23,7 +24,7 @@ class PendingDashboard
protected array $filterValues = [];

public function __construct(
/** @var TestCase $test */
/** @var TestCase&SharpAssertions $test */
protected object $test,
string $entityKey,
public ?PendingShow $parent = null,
Expand All @@ -42,8 +43,6 @@ public function withFilter(string $filterKey, mixed $value): static

public function get(): AssertableDashboard
{
$this->setGlobalFilterUrlDefault();

return new AssertableDashboard(
$this->parent instanceof PendingShow
? $this->test
Expand All @@ -69,8 +68,6 @@ public function get(): AssertableDashboard

public function dashboardCommand(string $commandKeyOrClassName): PendingCommand
{
$this->setGlobalFilterUrlDefault();

$commandKey = class_exists($commandKeyOrClassName)
? class_basename($commandKeyOrClassName)
: $commandKeyOrClassName;
Expand Down
16 changes: 7 additions & 9 deletions src/Utils/Testing/EntityList/PendingEntityList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Code16\Sharp\Utils\Testing\Commands\PendingCommand;
use Code16\Sharp\Utils\Testing\Form\PendingForm;
use Code16\Sharp\Utils\Testing\IsPendingComponent;
use Code16\Sharp\Utils\Testing\SharpAssertions;
use Code16\Sharp\Utils\Testing\Show\PendingShow;
use Illuminate\Foundation\Testing\TestCase;
use PHPUnit\Framework\Assert as PHPUnit;
Expand All @@ -25,7 +26,7 @@ class PendingEntityList
protected array $filterValues = [];

public function __construct(
/** @var TestCase $test */
/** @var TestCase&SharpAssertions $test */
protected object $test,
string $entityKey,
public ?PendingShow $parent = null,
Expand All @@ -50,16 +51,17 @@ public function sharpForm(string $entityClassNameOrKey, string|int $instanceId):

public function withFilter(string $filterKey, mixed $value): static
{
$key = $this->entityList->filterContainer()->findFilterHandler($filterKey)->getKey();
$this->filterValues[$key] = $value;
$filter = $this->entityList->filterContainer()->findFilterHandler($filterKey);

PHPUnit::assertNotNull($filter, sprintf('Unknown entity list filter [%s]', $filterKey));

$this->filterValues[$filter->getKey()] = $value;

return $this;
}

public function get(): AssertableEntityList
{
$this->setGlobalFilterUrlDefault();

return new AssertableEntityList(
$this->parent instanceof PendingShow
? $this->test
Expand All @@ -85,8 +87,6 @@ public function get(): AssertableEntityList

public function entityCommand(string $commandKeyOrClassName): PendingCommand
{
$this->setGlobalFilterUrlDefault();

$commandKey = class_exists($commandKeyOrClassName)
? class_basename($commandKeyOrClassName)
: $commandKeyOrClassName;
Expand Down Expand Up @@ -134,8 +134,6 @@ public function entityCommand(string $commandKeyOrClassName): PendingCommand

public function instanceCommand(string $commandKeyOrClassName, int|string $instanceId): PendingCommand
{
$this->setGlobalFilterUrlDefault();

$commandKey = class_exists($commandKeyOrClassName)
? class_basename($commandKeyOrClassName)
: $commandKeyOrClassName;
Expand Down
13 changes: 2 additions & 11 deletions src/Utils/Testing/Form/PendingForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Code16\Sharp\Form\SharpSingleForm;
use Code16\Sharp\Utils\Entities\SharpEntityManager;
use Code16\Sharp\Utils\Testing\EntityList\PendingEntityList;
use Code16\Sharp\Utils\Testing\GeneratesGlobalFilterUrl;
use Code16\Sharp\Utils\Testing\IsPendingComponent;
use Code16\Sharp\Utils\Testing\SharpAssertions;
use Code16\Sharp\Utils\Testing\Show\PendingShow;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Testing\TestResponse;
Expand All @@ -16,15 +16,14 @@
class PendingForm
{
use FormatsDataForUpdate;
use GeneratesGlobalFilterUrl;
use IsPendingComponent;

public SharpForm $form;
public string $entityKey;
protected bool $isSubsequentRequest = false;

public function __construct(
/** @var TestCase $test */
/** @var TestCase&SharpAssertions $test */
protected object $test,
string $entityKey,
public string|int|null $instanceId = null,
Expand All @@ -36,8 +35,6 @@ public function __construct(

public function create(): AssertableForm
{
$this->setGlobalFilterUrlDefault();

PHPUnit::assertNotInstanceOf(SharpSingleForm::class, $this->form);

return new AssertableForm(
Expand All @@ -52,8 +49,6 @@ public function create(): AssertableForm

public function edit(): AssertableForm
{
$this->setGlobalFilterUrlDefault();

if (! $this->form instanceof SharpSingleForm) {
PHPUnit::assertNotNull($this->instanceId, 'You can’t edit a form without an instance ID.');
}
Expand All @@ -71,8 +66,6 @@ public function edit(): AssertableForm

public function store(array $data): TestResponse
{
$this->setGlobalFilterUrlDefault();

PHPUnit::assertNotInstanceOf(SharpSingleForm::class, $this->form);

return $this->test
Expand All @@ -87,8 +80,6 @@ public function store(array $data): TestResponse

public function update(array $data): TestResponse
{
$this->setGlobalFilterUrlDefault();

if (! $this->form instanceof SharpSingleForm) {
PHPUnit::assertNotNull($this->instanceId, 'You can’t update a form without an instance ID.');
}
Expand Down
24 changes: 0 additions & 24 deletions src/Utils/Testing/GeneratesGlobalFilterUrl.php

This file was deleted.

67 changes: 67 additions & 0 deletions src/Utils/Testing/HasGlobalFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Code16\Sharp\Utils\Testing;

use Code16\Sharp\Filters\GlobalFilters\GlobalFilters;
use Code16\Sharp\Filters\GlobalRequiredFilter;
use Illuminate\Support\Facades\URL;
use PHPUnit\Framework\Assert as PHPUnit;

/**
* @internal
*/
trait HasGlobalFilters
{
private array $globalFilterValues = [];
private ?string $globalFilterSegment = null;

public function withSharpGlobalFilter(string $globalFilterKeyOrClassName, mixed $value): static
{
$filter = app(GlobalFilters::class)->filterContainer()->findFilterHandler($globalFilterKeyOrClassName);

if (! $filter) {
$declaredFilter = app(GlobalFilters::class)->getDeclaredFilter($globalFilterKeyOrClassName);
PHPUnit::assertNotNull($declaredFilter, "Global filter [$globalFilterKeyOrClassName] is not declared.");
PHPUnit::assertTrue($declaredFilter->authorize(), "Global filter [$globalFilterKeyOrClassName] is not authorized.");
PHPUnit::assertNotEmpty($declaredFilter->values(), "Global filter [$globalFilterKeyOrClassName] has no values.");
}

$this->globalFilterValues[$filter->getKey()] = $value;

$this->setGlobalFilterUrlDefault();

return $this;
}

/**
* @deprecated use withSharpGlobalFilter() instead
*/
public function withSharpGlobalFilterValues(array|string $globalFilterValues): static
{
$this->globalFilterSegment = collect((array) $globalFilterValues)
->implode(GlobalFilters::$valuesUrlSeparator);

$this->setGlobalFilterUrlDefault();

return $this;
}

/**
* @internal
*/
public function setGlobalFilterUrlDefault(): static
{
if ($this->globalFilterSegment) {
URL::defaults(['globalFilter' => $this->globalFilterSegment]);
} else {
$values = collect(app(GlobalFilters::class)->getFilters())
->map(fn (GlobalRequiredFilter $globalFilter) => $this->globalFilterValues[$globalFilter->getKey()] ?? $globalFilter->defaultValue()
)
->implode(GlobalFilters::$valuesUrlSeparator);

URL::defaults(['globalFilter' => $values ?: GlobalFilters::$defaultKey]);
}

return $this;
}
}
1 change: 0 additions & 1 deletion src/Utils/Testing/IsPendingComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
trait IsPendingComponent
{
use GeneratesCurrentPageUrl;
use GeneratesGlobalFilterUrl;

protected function getParentUri(): string
{
Expand Down
Loading
Loading