Skip to content

Commit 1e40549

Browse files
committed
Improve new testing global filter handling
1 parent d8d109b commit 1e40549

10 files changed

Lines changed: 43 additions & 83 deletions

File tree

src/Utils/Testing/Commands/PendingCommand.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public function __construct(
2121

2222
public function getForm(): AssertableCommandForm
2323
{
24-
$this->setGlobalFilterUrlDefault();
25-
2624
return new AssertableCommandForm(
2725
post: $this->post,
2826
getForm: $this->getForm,
@@ -33,8 +31,6 @@ public function getForm(): AssertableCommandForm
3331

3432
public function post(): AssertableCommand
3533
{
36-
$this->setGlobalFilterUrlDefault();
37-
3834
return new AssertableCommand(
3935
postCommand: $this->post,
4036
getForm: $this->getForm,

src/Utils/Testing/Dashboard/PendingDashboard.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Code16\Sharp\Utils\Testing\Commands\FormatsDataForCommand;
1111
use Code16\Sharp\Utils\Testing\Commands\PendingCommand;
1212
use Code16\Sharp\Utils\Testing\IsPendingComponent;
13+
use Code16\Sharp\Utils\Testing\SharpAssertions;
1314
use Code16\Sharp\Utils\Testing\Show\PendingShow;
1415
use Illuminate\Foundation\Testing\TestCase;
1516

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

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

4344
public function get(): AssertableDashboard
4445
{
45-
$this->setGlobalFilterUrlDefault();
46-
4746
return new AssertableDashboard(
4847
$this->parent instanceof PendingShow
4948
? $this->test
@@ -69,8 +68,6 @@ public function get(): AssertableDashboard
6968

7069
public function dashboardCommand(string $commandKeyOrClassName): PendingCommand
7170
{
72-
$this->setGlobalFilterUrlDefault();
73-
7471
$commandKey = class_exists($commandKeyOrClassName)
7572
? class_basename($commandKeyOrClassName)
7673
: $commandKeyOrClassName;

src/Utils/Testing/EntityList/PendingEntityList.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Code16\Sharp\Utils\Testing\Commands\PendingCommand;
1212
use Code16\Sharp\Utils\Testing\Form\PendingForm;
1313
use Code16\Sharp\Utils\Testing\IsPendingComponent;
14+
use Code16\Sharp\Utils\Testing\SharpAssertions;
1415
use Code16\Sharp\Utils\Testing\Show\PendingShow;
1516
use Illuminate\Foundation\Testing\TestCase;
1617
use PHPUnit\Framework\Assert as PHPUnit;
@@ -25,7 +26,7 @@ class PendingEntityList
2526
protected array $filterValues = [];
2627

2728
public function __construct(
28-
/** @var TestCase $test */
29+
/** @var TestCase&SharpAssertions $test */
2930
protected object $test,
3031
string $entityKey,
3132
public ?PendingShow $parent = null,
@@ -58,8 +59,6 @@ public function withFilter(string $filterKey, mixed $value): static
5859

5960
public function get(): AssertableEntityList
6061
{
61-
$this->setGlobalFilterUrlDefault();
62-
6362
return new AssertableEntityList(
6463
$this->parent instanceof PendingShow
6564
? $this->test
@@ -85,8 +84,6 @@ public function get(): AssertableEntityList
8584

8685
public function entityCommand(string $commandKeyOrClassName): PendingCommand
8786
{
88-
$this->setGlobalFilterUrlDefault();
89-
9087
$commandKey = class_exists($commandKeyOrClassName)
9188
? class_basename($commandKeyOrClassName)
9289
: $commandKeyOrClassName;
@@ -134,8 +131,6 @@ public function entityCommand(string $commandKeyOrClassName): PendingCommand
134131

135132
public function instanceCommand(string $commandKeyOrClassName, int|string $instanceId): PendingCommand
136133
{
137-
$this->setGlobalFilterUrlDefault();
138-
139134
$commandKey = class_exists($commandKeyOrClassName)
140135
? class_basename($commandKeyOrClassName)
141136
: $commandKeyOrClassName;

src/Utils/Testing/Form/PendingForm.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Code16\Sharp\Form\SharpSingleForm;
77
use Code16\Sharp\Utils\Entities\SharpEntityManager;
88
use Code16\Sharp\Utils\Testing\EntityList\PendingEntityList;
9-
use Code16\Sharp\Utils\Testing\GeneratesGlobalFilterUrl;
109
use Code16\Sharp\Utils\Testing\IsPendingComponent;
10+
use Code16\Sharp\Utils\Testing\SharpAssertions;
1111
use Code16\Sharp\Utils\Testing\Show\PendingShow;
1212
use Illuminate\Foundation\Testing\TestCase;
1313
use Illuminate\Testing\TestResponse;
@@ -16,15 +16,14 @@
1616
class PendingForm
1717
{
1818
use FormatsDataForUpdate;
19-
use GeneratesGlobalFilterUrl;
2019
use IsPendingComponent;
2120

2221
public SharpForm $form;
2322
public string $entityKey;
2423
protected bool $isSubsequentRequest = false;
2524

2625
public function __construct(
27-
/** @var TestCase $test */
26+
/** @var TestCase&SharpAssertions $test */
2827
protected object $test,
2928
string $entityKey,
3029
public string|int|null $instanceId = null,
@@ -36,8 +35,6 @@ public function __construct(
3635

3736
public function create(): AssertableForm
3837
{
39-
$this->setGlobalFilterUrlDefault();
40-
4138
PHPUnit::assertNotInstanceOf(SharpSingleForm::class, $this->form);
4239

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

5350
public function edit(): AssertableForm
5451
{
55-
$this->setGlobalFilterUrlDefault();
56-
5752
if (! $this->form instanceof SharpSingleForm) {
5853
PHPUnit::assertNotNull($this->instanceId, 'You can’t edit a form without an instance ID.');
5954
}
@@ -71,8 +66,6 @@ public function edit(): AssertableForm
7166

7267
public function store(array $data): TestResponse
7368
{
74-
$this->setGlobalFilterUrlDefault();
75-
7669
PHPUnit::assertNotInstanceOf(SharpSingleForm::class, $this->form);
7770

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

8881
public function update(array $data): TestResponse
8982
{
90-
$this->setGlobalFilterUrlDefault();
91-
9283
if (! $this->form instanceof SharpSingleForm) {
9384
PHPUnit::assertNotNull($this->instanceId, 'You can’t update a form without an instance ID.');
9485
}

src/Utils/Testing/GeneratesGlobalFilterUrl.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Utils/Testing/IsPendingComponent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
trait IsPendingComponent
1313
{
1414
use GeneratesCurrentPageUrl;
15-
use GeneratesGlobalFilterUrl;
1615

1716
protected function getParentUri(): string
1817
{

src/Utils/Testing/SharpAssertions.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@
33
namespace Code16\Sharp\Utils\Testing;
44

55
use Closure;
6+
use Code16\Sharp\Filters\GlobalFilters\GlobalFilters;
67
use Code16\Sharp\Http\Context\SharpBreadcrumb;
78
use Code16\Sharp\Utils\Entities\SharpEntityManager;
89
use Code16\Sharp\Utils\Links\BreadcrumbBuilder;
910
use Code16\Sharp\Utils\Testing\Dashboard\PendingDashboard;
1011
use Code16\Sharp\Utils\Testing\EntityList\PendingEntityList;
1112
use Code16\Sharp\Utils\Testing\Form\PendingForm;
1213
use Code16\Sharp\Utils\Testing\Show\PendingShow;
14+
use Illuminate\Support\Facades\URL;
1315

1416
trait SharpAssertions
1517
{
1618
use GeneratesCurrentPageUrl;
17-
use GeneratesGlobalFilterUrl;
1819

1920
private BreadcrumbBuilder $breadcrumbBuilder;
21+
private ?string $globalFilter = null;
22+
23+
public function setUpSharpAssertions(): void
24+
{
25+
URL::defaults(['globalFilter' => $this->globalFilter ?: GlobalFilters::$defaultKey]);
26+
}
2027

2128
public function sharpList(string $entityClassNameOrKey): PendingEntityList
2229
{
@@ -38,6 +45,16 @@ public function sharpDashboard(string $entityClassNameOrKey): PendingDashboard
3845
return new PendingDashboard($this, $entityClassNameOrKey);
3946
}
4047

48+
public function withSharpGlobalFilterValues(array|string $globalFilterValues): self
49+
{
50+
$this->globalFilter = collect((array) $globalFilterValues)
51+
->implode(GlobalFilters::$valuesUrlSeparator);
52+
53+
URL::defaults(['globalFilter' => $this->globalFilter ?: GlobalFilters::$defaultKey]);
54+
55+
return $this;
56+
}
57+
4158
/**
4259
* @deprecated use withSharpBreadcrumb() instead
4360
*/
@@ -69,8 +86,6 @@ public function withSharpBreadcrumb(Closure $callback): self
6986

7087
public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanceId)
7188
{
72-
$this->setGlobalFilterUrlDefault();
73-
7489
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
7590

7691
return $this
@@ -88,8 +103,6 @@ public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanc
88103

89104
public function deleteFromSharpList(string $entityClassNameOrKey, mixed $instanceId)
90105
{
91-
$this->setGlobalFilterUrlDefault();
92-
93106
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
94107

95108
return $this
@@ -110,8 +123,6 @@ public function deleteFromSharpList(string $entityClassNameOrKey, mixed $instanc
110123

111124
public function getSharpForm(string $entityClassNameOrKey, mixed $instanceId = null)
112125
{
113-
$this->setGlobalFilterUrlDefault();
114-
115126
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
116127

117128
return $this
@@ -139,8 +150,6 @@ public function getSharpForm(string $entityClassNameOrKey, mixed $instanceId = n
139150

140151
public function getSharpSingleForm(string $entityClassNameOrKey)
141152
{
142-
$this->setGlobalFilterUrlDefault();
143-
144153
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
145154

146155
return $this
@@ -158,8 +167,6 @@ public function getSharpSingleForm(string $entityClassNameOrKey)
158167

159168
public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array $data)
160169
{
161-
$this->setGlobalFilterUrlDefault();
162-
163170
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
164171

165172
return $this
@@ -178,8 +185,6 @@ public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array
178185

179186
public function updateSharpSingleForm(string $entityClassNameOrKey, array $data)
180187
{
181-
$this->setGlobalFilterUrlDefault();
182-
183188
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
184189

185190
return $this
@@ -197,8 +202,6 @@ public function updateSharpSingleForm(string $entityClassNameOrKey, array $data)
197202

198203
public function getSharpShow(string $entityClassNameOrKey, $instanceId)
199204
{
200-
$this->setGlobalFilterUrlDefault();
201-
202205
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
203206

204207
return $this
@@ -217,8 +220,6 @@ public function getSharpShow(string $entityClassNameOrKey, $instanceId)
217220

218221
public function storeSharpForm(string $entityClassNameOrKey, array $data)
219222
{
220-
$this->setGlobalFilterUrlDefault();
221-
222223
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
223224

224225
return $this
@@ -242,8 +243,6 @@ public function callSharpInstanceCommandFromList(
242243
array $data = [],
243244
?string $commandStep = null
244245
) {
245-
$this->setGlobalFilterUrlDefault();
246-
247246
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
248247

249248
$commandKey = class_exists($commandKeyOrClassName)
@@ -273,8 +272,6 @@ public function callSharpInstanceCommandFromShow(
273272
array $data = [],
274273
?string $commandStep = null
275274
) {
276-
$this->setGlobalFilterUrlDefault();
277-
278275
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
279276

280277
$commandKey = class_exists($commandKeyOrClassName)
@@ -303,8 +300,6 @@ public function callSharpEntityCommandFromList(
303300
array $data = [],
304301
?string $commandStep = null
305302
) {
306-
$this->setGlobalFilterUrlDefault();
307-
308303
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
309304

310305
$commandKey = class_exists($commandKeyOrClassName)

src/Utils/Testing/Show/PendingShow.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111
use Code16\Sharp\Utils\Testing\Dashboard\PendingDashboard;
1212
use Code16\Sharp\Utils\Testing\EntityList\PendingEntityList;
1313
use Code16\Sharp\Utils\Testing\Form\PendingForm;
14-
use Code16\Sharp\Utils\Testing\GeneratesGlobalFilterUrl;
1514
use Code16\Sharp\Utils\Testing\IsPendingComponent;
15+
use Code16\Sharp\Utils\Testing\SharpAssertions;
1616
use Illuminate\Foundation\Testing\TestCase;
1717

1818
class PendingShow
1919
{
2020
use FormatsDataForCommand;
21-
use GeneratesGlobalFilterUrl;
2221
use IsPendingComponent;
2322

2423
public SharpShow $show;
2524
public string $entityKey;
2625

2726
public function __construct(
28-
/** @var TestCase $test */
27+
/** @var TestCase&SharpAssertions $test */
2928
protected object $test,
3029
string $entityKey,
3130
public string|int|null $instanceId = null,
@@ -52,8 +51,6 @@ public function sharpDashboardField(string $entityClassNameOrKey): PendingDashbo
5251

5352
public function get(): AssertableShow
5453
{
55-
$this->setGlobalFilterUrlDefault();
56-
5754
return new AssertableShow(
5855
$this->test
5956
->get($this->show instanceof SharpSingleShow
@@ -71,8 +68,6 @@ public function get(): AssertableShow
7168

7269
public function instanceCommand(string $commandKeyOrClassName): PendingCommand
7370
{
74-
$this->setGlobalFilterUrlDefault();
75-
7671
$commandKey = class_exists($commandKeyOrClassName)
7772
? class_basename($commandKeyOrClassName)
7873
: $commandKeyOrClassName;

tests/Http/SharpAssertionsHttpTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
use Code16\Sharp\Tests\Fixtures\Sharp\PersonShow;
2020
use Code16\Sharp\Tests\Fixtures\Sharp\SinglePersonShow;
2121
use Code16\Sharp\Tests\Fixtures\Sharp\TestDashboard;
22+
use Code16\Sharp\Tests\ResetUrlDefaults;
2223
use Code16\Sharp\Utils\Fields\FieldsContainer;
2324
use Code16\Sharp\Utils\Testing\SharpAssertions;
2425
use Illuminate\Http\UploadedFile;
2526
use Illuminate\Testing\Fluent\AssertableJson;
2627

27-
pest()->use(SharpAssertions::class);
28+
pest()
29+
->use(ResetUrlDefaults::class)
30+
->use(SharpAssertions::class);
2831

2932
beforeEach(function () {
3033
login();

0 commit comments

Comments
 (0)