Skip to content

Commit 8d999f5

Browse files
committed
wip
1 parent db1c3f5 commit 8d999f5

11 files changed

Lines changed: 591 additions & 109 deletions

src/EntityList/Traits/HandleEntityCommands.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ protected function appendEntityCommandsToConfig(array &$config): void
4545

4646
final public function getEntityCommandsHandlers(): Collection
4747
{
48+
ray($this->entityCommandHandlers);
49+
ray()->trace();
4850
if ($this->entityCommandHandlers === null) {
4951
$groupIndex = 0;
5052
$this->entityCommandHandlers = collect($this->getEntityCommands())
@@ -73,6 +75,7 @@ final public function getEntityCommandsHandlers(): Collection
7375
$commandHandler->setCommandKey($commandKey);
7476
}
7577

78+
// ray($this->queryParams ?? null, $commandHandler)->trace();
7679
if (isset($this->queryParams)) {
7780
// We have to init query params of the command
7881
$commandHandler->initQueryParams($this->queryParams);

src/Utils/Testing/Commands/AssertableCommand.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class AssertableCommand
2323
public function __construct(
2424
/** @var Closure(array,string): TestResponse */
2525
protected Closure $postCommand,
26+
/** @var Closure(?string): TestResponse */
27+
protected Closure $getForm,
2628
protected SharpEntityList|SharpShow|SharpDashboard $commandContainer,
2729
protected array $data = [],
2830
protected ?string $step = null,
@@ -56,7 +58,7 @@ public function assertViewIs($value)
5658

5759
public function assertReturnsView(?string $view = null, ?array $data = null): static
5860
{
59-
$this->response->assertJson(fn (AssertableJson $json) => $json
61+
$this->response->assertOk()->assertJson(fn (AssertableJson $json) => $json
6062
->where('action', 'view')
6163
->etc()
6264
);
@@ -74,7 +76,7 @@ public function assertReturnsView(?string $view = null, ?array $data = null): st
7476

7577
public function assertReturnsInfo(string $message = ''): static
7678
{
77-
$this->response->assertJson(fn (AssertableJson $json) => $json
79+
$this->response->assertOk()->assertJson(fn (AssertableJson $json) => $json
7880
->where('action', 'info')
7981
->when($message)->where('message', $message)
8082
->etc()
@@ -85,7 +87,7 @@ public function assertReturnsInfo(string $message = ''): static
8587

8688
public function assertReturnsLink(string $url = ''): static
8789
{
88-
$this->response->assertJson(fn (AssertableJson $json) => $json
90+
$this->response->assertOk()->assertJson(fn (AssertableJson $json) => $json
8991
->where('action', 'link')
9092
->when($url)->where('link', $url)
9193
->etc()
@@ -96,7 +98,7 @@ public function assertReturnsLink(string $url = ''): static
9698

9799
public function assertReturnsReload(): static
98100
{
99-
$this->response->assertJson(fn (AssertableJson $json) => $json
101+
$this->response->assertOk()->assertJson(fn (AssertableJson $json) => $json
100102
->where('action', 'reload')
101103
->etc()
102104
);
@@ -106,7 +108,7 @@ public function assertReturnsReload(): static
106108

107109
public function assertReturnsRefresh(array $ids): static
108110
{
109-
$this->response->assertJson(fn (AssertableJson $json) => $json
111+
$this->response->assertOk()->assertJson(fn (AssertableJson $json) => $json
110112
->where('action', 'refresh')
111113
->etc()
112114
);
@@ -121,7 +123,7 @@ public function assertReturnsRefresh(array $ids): static
121123

122124
public function assertReturnsStep(?string $step = null): static
123125
{
124-
$this->response->assertJson(fn (AssertableJson $json) => $json
126+
$this->response->assertOk()->assertJson(fn (AssertableJson $json) => $json
125127
->where('action', 'step')
126128
->etc()
127129
);
@@ -135,7 +137,7 @@ public function assertReturnsStep(?string $step = null): static
135137

136138
public function assertReturnsDownload(?string $filename = null): static
137139
{
138-
$this->response->assertStreamed();
140+
$this->response->assertOk()->assertStreamed();
139141

140142
if ($filename) {
141143
preg_match('/filename="?([^";]+)"?/', $this->response->headers->get('Content-Disposition'), $matches);
@@ -145,14 +147,14 @@ public function assertReturnsDownload(?string $filename = null): static
145147
return $this;
146148
}
147149

148-
public function callNextStep(array $data = []): static
150+
public function getNextStepForm(): AssertableCommandForm
149151
{
150152
$this->assertReturnsStep();
151153

152-
return new AssertableCommand(
153-
$this->postCommand,
154+
return new AssertableCommandForm(
155+
post: $this->postCommand,
156+
getForm: $this->getForm,
154157
commandContainer: $this->commandContainer,
155-
data: $data,
156158
step: $this->response->json('step'),
157159
);
158160
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Utils\Testing\Commands;
4+
5+
use Closure;
6+
use Code16\Sharp\Dashboard\SharpDashboard;
7+
use Code16\Sharp\EntityList\SharpEntityList;
8+
use Code16\Sharp\Show\SharpShow;
9+
use Code16\Sharp\Utils\Testing\DelegatesToResponse;
10+
use Illuminate\Testing\TestResponse;
11+
12+
class AssertableCommandForm
13+
{
14+
use DelegatesToResponse;
15+
16+
public function __construct(
17+
/** @var Closure(array, ?string, ?array): TestResponse */
18+
protected Closure $post,
19+
/** @var Closure(?string): TestResponse */
20+
protected Closure $getForm,
21+
protected SharpEntityList|SharpShow|SharpDashboard $commandContainer,
22+
protected ?string $step = null,
23+
) {
24+
$this->response = ($this->getForm)($this->step);
25+
}
26+
27+
public function post(array $data = []): AssertableCommand
28+
{
29+
ray($this->formData());
30+
31+
return new AssertableCommand(
32+
postCommand: fn ($data, $step) => ($this->post)($data, $step, $this->formData()),
33+
getForm: $this->getForm,
34+
commandContainer: $this->commandContainer,
35+
data: $data,
36+
step: $this->step,
37+
);
38+
}
39+
40+
public function formData(): ?array
41+
{
42+
return $this->response->json('data');
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Utils\Testing\Commands;
4+
5+
use Code16\Sharp\EntityList\Commands\Command;
6+
7+
trait FormatsDataForCommand
8+
{
9+
protected function formatDataForCommand(Command $commandHandler, array $data, ?array $baseData = null): array
10+
{
11+
return [
12+
...$baseData ?: [],
13+
...collect($commandHandler->applyFormatters($data))
14+
->when($baseData)->only(array_keys($data))
15+
->all(),
16+
];
17+
}
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Utils\Testing\Commands;
4+
5+
use Closure;
6+
use Code16\Sharp\Dashboard\SharpDashboard;
7+
use Code16\Sharp\EntityList\SharpEntityList;
8+
use Code16\Sharp\Show\SharpShow;
9+
use Code16\Sharp\Utils\Testing\IsPendingComponent;
10+
11+
class PendingCommand
12+
{
13+
use IsPendingComponent;
14+
15+
public function __construct(
16+
protected Closure $getForm,
17+
protected Closure $post,
18+
protected SharpShow|SharpEntityList|SharpDashboard $commandContainer,
19+
protected ?string $step = null,
20+
) {}
21+
22+
public function getForm(): AssertableCommandForm
23+
{
24+
$this->setGlobalFilterUrlDefault();
25+
26+
return new AssertableCommandForm(
27+
post: $this->post,
28+
getForm: $this->getForm,
29+
commandContainer: $this->commandContainer,
30+
step: $this->step
31+
);
32+
}
33+
34+
public function post(): AssertableCommand
35+
{
36+
$this->setGlobalFilterUrlDefault();
37+
38+
return new AssertableCommand(
39+
postCommand: $this->post,
40+
getForm: $this->getForm,
41+
commandContainer: $this->commandContainer,
42+
step: $this->step
43+
);
44+
}
45+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Utils\Testing\Dashboard;
4+
5+
use Code16\Sharp\Utils\Testing\DelegatesToResponse;
6+
use Code16\Sharp\Utils\Testing\Show\PendingShow;
7+
use Illuminate\Testing\TestResponse;
8+
9+
class AssertableDashboard
10+
{
11+
use DelegatesToResponse;
12+
13+
public function __construct(
14+
protected TestResponse $response,
15+
protected PendingDashboard $pendingDashboard,
16+
) {}
17+
18+
public function dashboardData(): array
19+
{
20+
return $this->pendingDashboard->parent instanceof PendingShow
21+
? $this->response->json('data')
22+
: $this->response->inertiaProps('dashboard.data');
23+
}
24+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Utils\Testing\Dashboard;
4+
5+
use Code16\Sharp\Dashboard\SharpDashboard;
6+
use Code16\Sharp\Http\Context\SharpBreadcrumb;
7+
use Code16\Sharp\Show\Fields\SharpShowDashboardField;
8+
use Code16\Sharp\Show\Fields\SharpShowField;
9+
use Code16\Sharp\Utils\Entities\SharpEntityManager;
10+
use Code16\Sharp\Utils\Testing\Commands\FormatsDataForCommand;
11+
use Code16\Sharp\Utils\Testing\Commands\PendingCommand;
12+
use Code16\Sharp\Utils\Testing\IsPendingComponent;
13+
use Code16\Sharp\Utils\Testing\Show\PendingShow;
14+
use Illuminate\Foundation\Testing\TestCase;
15+
16+
class PendingDashboard
17+
{
18+
use FormatsDataForCommand;
19+
use IsPendingComponent;
20+
21+
protected SharpDashboard $dashboard;
22+
public string $entityKey;
23+
protected array $filterValues = [];
24+
25+
public function __construct(
26+
/** @var TestCase $test */
27+
protected object $test,
28+
string $entityKey,
29+
public ?PendingShow $parent = null,
30+
) {
31+
$this->entityKey = app(SharpEntityManager::class)->entityKeyFor($entityKey);
32+
$this->dashboard = app(SharpEntityManager::class)->entityFor($this->entityKey)->getViewOrFail();
33+
}
34+
35+
public function withFilter(string $filterKey, mixed $value): static
36+
{
37+
$key = $this->dashboard->filterContainer()->findFilterHandler($filterKey)->getKey();
38+
$this->filterValues[$key] = $value;
39+
40+
return $this;
41+
}
42+
43+
public function get(): AssertableDashboard
44+
{
45+
$this->setGlobalFilterUrlDefault();
46+
47+
return new AssertableDashboard(
48+
$this->parent instanceof PendingShow
49+
? $this->test
50+
->getJson(
51+
route('code16.sharp.api.dashboard', [
52+
'dashboardKey' => $this->entityKey,
53+
...$this->dashboard
54+
->filterContainer()
55+
->getQueryParamsFromFilterValues($this->dashboardShowField()->toArray()['hiddenFilters'] ?? []),
56+
...$this->dashboardQueryParams(),
57+
]),
58+
headers: [
59+
SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(),
60+
]
61+
)
62+
: $this->test->get(route('code16.sharp.dashboard', [
63+
'dashboardKey' => $this->entityKey,
64+
...$this->dashboardQueryParams(),
65+
])),
66+
$this,
67+
);
68+
}
69+
70+
public function dashboardCommand(string $commandKeyOrClassName): PendingCommand
71+
{
72+
$this->setGlobalFilterUrlDefault();
73+
74+
$commandKey = class_exists($commandKeyOrClassName)
75+
? class_basename($commandKeyOrClassName)
76+
: $commandKeyOrClassName;
77+
78+
return new PendingCommand(
79+
getForm: fn (?string $step = null) => $this
80+
->test
81+
->getJson(
82+
route(
83+
'code16.sharp.api.dashboard.command.form',
84+
[
85+
'entityKey' => $this->entityKey,
86+
'commandKey' => $commandKey,
87+
'command_step' => $step,
88+
...$this->dashboardQueryParams(),
89+
]
90+
),
91+
headers: [
92+
SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(),
93+
]
94+
),
95+
post: fn (array $data, ?string $step = null, ?array $baseData = null) => $this
96+
->test
97+
->postJson(
98+
route(
99+
'code16.sharp.api.dashboard.command',
100+
['entityKey' => $this->entityKey, 'commandKey' => $commandKey]
101+
),
102+
[
103+
'data' => $this->formatDataForCommand(
104+
$this->dashboard->findDashboardCommandHandler($commandKey),
105+
$data,
106+
$baseData
107+
),
108+
'query' => $this->dashboardQueryParams(),
109+
'command_step' => $step,
110+
],
111+
headers: [
112+
SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(),
113+
]
114+
),
115+
commandContainer: $this->dashboard,
116+
);
117+
}
118+
119+
protected function dashboardShowField(): ?SharpShowDashboardField
120+
{
121+
/** @noinspection PhpIncompatibleReturnTypeInspection */
122+
return $this->parent instanceof PendingShow
123+
? $this->parent->show->getBuiltFields()
124+
->first(fn (SharpShowField $field) => $field instanceof SharpShowDashboardField
125+
&& $field->toArray()['dashboardKey'] === $this->entityKey
126+
)
127+
: null;
128+
}
129+
130+
protected function dashboardQueryParams(): array
131+
{
132+
return $this->dashboard
133+
->filterContainer()
134+
->getQueryParamsFromFilterValues($this->filterValues)
135+
->all();
136+
}
137+
}

0 commit comments

Comments
 (0)