Skip to content

Commit 472cff9

Browse files
committed
Add tests
1 parent d74e804 commit 472cff9

5 files changed

Lines changed: 350 additions & 25 deletions

File tree

src/Http/Controllers/Api/ApiFormRefreshController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function update(EntityKey $entityKey)
1616
'form' => [
1717
'data' => $fieldContainer->formatHtmlFields(
1818
request()->all(),
19-
keepOnlyHtmlFields: true,
19+
keepOnlyRefreshableFields: true,
2020
),
2121
],
2222
]);

src/Utils/Fields/HandleFormHtmlFields.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77

88
trait HandleFormHtmlFields
99
{
10-
final public function hasHtmlFields(bool $withLiveRefresh = false): bool
10+
final public function hasHtmlFields(): bool
1111
{
12-
$htmlFields = collect($this->getBuiltFields())
12+
return collect($this->getBuiltFields())
1313
->whereInstanceOf(SharpFormHtmlField::class)
1414
->merge(
1515
collect($this->getBuiltFields())
1616
->whereInstanceOf(SharpFormListField::class)
1717
->map(fn ($field) => $field->itemFields()->whereInstanceOf(SharpFormHtmlField::class))
1818
)
19-
->flatten();
20-
21-
return $withLiveRefresh ? $htmlFields->some->hasLiveRefresh() : $htmlFields->isNotEmpty();
19+
->flatten()
20+
->isNotEmpty();
2221
}
2322

24-
final public function formatHtmlFields(array $frontData, bool $keepOnlyHtmlFields = false): array
23+
final public function formatHtmlFields(array $frontData, bool $keepOnlyRefreshableFields = false): array
2524
{
2625
if (! $this->hasHtmlFields()) {
2726
return $frontData;
@@ -30,27 +29,29 @@ final public function formatHtmlFields(array $frontData, bool $keepOnlyHtmlField
3029
$formattedData = $this->formatRequestData($frontData);
3130

3231
return collect($frontData)
33-
->mapWithKeys(function ($value, $key) use ($formattedData, $keepOnlyHtmlFields) {
32+
->mapWithKeys(function ($value, $key) use ($formattedData, $keepOnlyRefreshableFields) {
3433
$field = $this->findFieldByKey($key);
3534

3635
if ($field instanceof SharpFormHtmlField) {
37-
return [
38-
$key => $field->render([
39-
...$formattedData,
40-
...(is_array($value) ? $value : []),
41-
]),
42-
];
36+
return $keepOnlyRefreshableFields && ! $field->hasLiveRefresh()
37+
? []
38+
: [
39+
$key => $field->render([
40+
...$formattedData,
41+
...(is_array($value) ? $value : []),
42+
]),
43+
];
4344
}
4445

4546
if ($field instanceof SharpFormListField
4647
&& $field->itemFields()->whereInstanceOf(SharpFormHtmlField::class)->isNotEmpty()
4748
) {
4849
return [
49-
$key => $this->formatListHtmlFields($field, $value, $formattedData, $keepOnlyHtmlFields),
50+
$key => $this->formatListHtmlFields($field, $value, $formattedData, $keepOnlyRefreshableFields),
5051
];
5152
}
5253

53-
return $keepOnlyHtmlFields ? [] : [$key => $value];
54+
return $keepOnlyRefreshableFields ? [] : [$key => $value];
5455
})
5556
->all();
5657
}
@@ -59,22 +60,24 @@ private function formatListHtmlFields(
5960
SharpFormListField $field,
6061
array $listValue,
6162
array $formattedData,
62-
bool $keepOnlyHtmlFields
63+
bool $keepOnlyRefreshableFields
6364
): array {
6465
return collect($listValue)->map(fn ($item, $index) => collect($item)->mapWithKeys(
65-
function ($itemFieldValue, $itemFieldKey) use ($field, $index, $formattedData, $keepOnlyHtmlFields) {
66+
function ($itemFieldValue, $itemFieldKey) use ($field, $index, $formattedData, $keepOnlyRefreshableFields) {
6667
$itemField = $field->findItemFormFieldByKey($itemFieldKey);
6768

6869
if ($itemField instanceof SharpFormHtmlField) {
69-
return [
70-
$itemFieldKey => $itemField->render([
71-
...($formattedData[$field->key][$index] ?? []),
72-
...(is_array($itemFieldValue) ? $itemFieldValue : []),
73-
]),
74-
];
70+
return $keepOnlyRefreshableFields && ! $itemField->hasLiveRefresh()
71+
? []
72+
: [
73+
$itemFieldKey => $itemField->render([
74+
...($formattedData[$field->key][$index] ?? []),
75+
...(is_array($itemFieldValue) ? $itemFieldValue : []),
76+
]),
77+
];
7578
}
7679

77-
return $keepOnlyHtmlFields ? [] : [$itemFieldKey => $itemFieldValue];
80+
return $keepOnlyRefreshableFields ? [] : [$itemFieldKey => $itemFieldValue];
7881
})->all()
7982
)->all();
8083
}
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
<?php
2+
3+
use Code16\Sharp\EntityList\Commands\EntityCommand;
4+
use Code16\Sharp\EntityList\Commands\InstanceCommand;
5+
use Code16\Sharp\Form\Layout\FormLayout;
6+
use Code16\Sharp\Form\Layout\FormLayoutColumn;
7+
use Code16\Sharp\Form\SharpForm;
8+
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
9+
use Code16\Sharp\Tests\Fixtures\Sharp\PersonList;
10+
use Code16\Sharp\Tests\Fixtures\Sharp\PersonShow;
11+
use Code16\Sharp\Tests\Http\Api\Fixtures\ApiFormRefreshControllerEmbed;
12+
use Code16\Sharp\Tests\Http\Api\Fixtures\RefreshFormFields;
13+
use Illuminate\Testing\Fluent\AssertableJson;
14+
15+
beforeEach(function () {
16+
sharp()->config()->declareEntity(PersonEntity::class);
17+
login();
18+
});
19+
20+
it('gets updated HTML fields with live refresh of a form', function () {
21+
fakeFormFor('person', new class() extends SharpForm
22+
{
23+
use RefreshFormFields;
24+
25+
public function buildFormLayout(FormLayout $formLayout): void
26+
{
27+
$formLayout->addColumn(12, function (FormLayoutColumn $column) {
28+
$column->withField('html_string')
29+
->withField('html_non_refreshable')
30+
->withField('html_view')
31+
->withField('html_closure')
32+
->withListField('list', function (FormLayoutColumn $column) {
33+
$column->withField('list_name')
34+
->withField('list_html');
35+
});
36+
});
37+
}
38+
39+
public function find($id): array
40+
{
41+
return $this->transform([]);
42+
}
43+
44+
public function update(mixed $id, array $data) {}
45+
});
46+
47+
$this->post(
48+
route('code16.sharp.api.form.refresh.update', [
49+
'entityKey' => 'person',
50+
'instance_id' => 1,
51+
]),
52+
[
53+
'name' => 'Albert Einstein',
54+
'html_non_refreshable' => ['text' => 'example'],
55+
'html_string' => ['text' => 'example'],
56+
'html_view' => ['text' => 'example'],
57+
'html_closure' => ['text' => 'example'],
58+
'list' => [
59+
['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']],
60+
],
61+
]
62+
)
63+
->assertOk()
64+
->assertJson(fn (AssertableJson $json) => $json
65+
->missing('form.data.html_non_refreshable')
66+
->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>')
67+
->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n")
68+
->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>')
69+
->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>')
70+
);
71+
});
72+
73+
it('gets updated HTML fields with live refresh of an entity list entity command form', function () {
74+
fakeListFor('person', new class() extends PersonList
75+
{
76+
protected function getEntityCommands(): ?array
77+
{
78+
return [
79+
'cmd' => new class() extends EntityCommand
80+
{
81+
use RefreshFormFields;
82+
83+
public function label(): ?string
84+
{
85+
return 'entity';
86+
}
87+
88+
public function execute(array $data = []): array
89+
{
90+
return [];
91+
}
92+
},
93+
];
94+
}
95+
});
96+
97+
$this->post(
98+
route('code16.sharp.api.form.refresh.update', [
99+
'entityKey' => 'person',
100+
'entity_list_command_key' => 'cmd',
101+
]),
102+
[
103+
'name' => 'Albert Einstein',
104+
'html_non_refreshable' => ['text' => 'example'],
105+
'html_string' => ['text' => 'example'],
106+
'html_view' => ['text' => 'example'],
107+
'html_closure' => ['text' => 'example'],
108+
'list' => [
109+
['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']],
110+
],
111+
]
112+
)
113+
->assertOk()
114+
->assertJson(fn (AssertableJson $json) => $json
115+
->missing('form.data.html_non_refreshable')
116+
->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>')
117+
->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n")
118+
->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>')
119+
->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>')
120+
);
121+
});
122+
123+
it('gets updated HTML fields with live refresh of an entity list instance command form', function () {
124+
fakeListFor('person', new class() extends PersonList
125+
{
126+
protected function getInstanceCommands(): ?array
127+
{
128+
return [
129+
'cmd' => new class() extends InstanceCommand
130+
{
131+
use RefreshFormFields;
132+
133+
public function label(): ?string
134+
{
135+
return 'entity';
136+
}
137+
138+
public function execute(mixed $instanceId, array $data = []): array
139+
{
140+
return [];
141+
}
142+
},
143+
];
144+
}
145+
});
146+
147+
$this->post(
148+
route('code16.sharp.api.form.refresh.update', [
149+
'entityKey' => 'person',
150+
'entity_list_command_key' => 'cmd',
151+
'instance_id' => 1,
152+
]),
153+
[
154+
'name' => 'Albert Einstein',
155+
'html_non_refreshable' => ['text' => 'example'],
156+
'html_string' => ['text' => 'example'],
157+
'html_view' => ['text' => 'example'],
158+
'html_closure' => ['text' => 'example'],
159+
'list' => [
160+
['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']],
161+
],
162+
]
163+
)
164+
->assertOk()
165+
->assertJson(fn (AssertableJson $json) => $json
166+
->missing('form.data.html_non_refreshable')
167+
->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>')
168+
->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n")
169+
->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>')
170+
->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>')
171+
);
172+
});
173+
174+
it('gets updated HTML fields with live refresh of a show instance command form', function () {
175+
fakeShowFor('person', new class() extends PersonShow
176+
{
177+
public function getInstanceCommands(): ?array
178+
{
179+
return [
180+
'cmd' => new class() extends InstanceCommand
181+
{
182+
use RefreshFormFields;
183+
184+
public function label(): ?string
185+
{
186+
return 'entity';
187+
}
188+
189+
public function execute(mixed $instanceId, array $data = []): array
190+
{
191+
return [];
192+
}
193+
},
194+
];
195+
}
196+
});
197+
198+
$this->post(
199+
route('code16.sharp.api.form.refresh.update', [
200+
'entityKey' => 'person',
201+
'show_command_key' => 'cmd',
202+
'instance_id' => 1,
203+
]),
204+
[
205+
'name' => 'Albert Einstein',
206+
'html_non_refreshable' => ['text' => 'example'],
207+
'html_string' => ['text' => 'example'],
208+
'html_view' => ['text' => 'example'],
209+
'html_closure' => ['text' => 'example'],
210+
'list' => [
211+
['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']],
212+
],
213+
]
214+
)
215+
->assertOk()
216+
->assertJson(fn (AssertableJson $json) => $json
217+
->missing('form.data.html_non_refreshable')
218+
->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>')
219+
->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n")
220+
->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>')
221+
->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>')
222+
);
223+
});
224+
225+
it('gets updated HTML fields with live refresh of an embed form', function () {
226+
$this
227+
->postJson(
228+
route('code16.sharp.api.form.refresh.update', [
229+
'entityKey' => 'person',
230+
'embed_key' => (new ApiFormRefreshControllerEmbed())->key(),
231+
]),
232+
[
233+
'name' => 'Albert Einstein',
234+
'html_non_refreshable' => ['text' => 'example'],
235+
'html_string' => ['text' => 'example'],
236+
'html_view' => ['text' => 'example'],
237+
'html_closure' => ['text' => 'example'],
238+
'list' => [
239+
['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']],
240+
],
241+
]
242+
)
243+
->assertOk()
244+
->assertJson(fn (AssertableJson $json) => $json
245+
->missing('form.data.html_non_refreshable')
246+
->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>')
247+
->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n")
248+
->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>')
249+
->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>')
250+
);
251+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Tests\Http\Api\Fixtures;
4+
5+
use Code16\Sharp\Form\Fields\Embeds\SharpFormEditorEmbed;
6+
7+
class ApiFormRefreshControllerEmbed extends SharpFormEditorEmbed
8+
{
9+
use RefreshFormFields;
10+
11+
public function buildEmbedConfig(): void
12+
{
13+
$this->configureTagName('x-embed');
14+
}
15+
16+
public function updateContent(array $data = []): array
17+
{
18+
return $data;
19+
}
20+
}

0 commit comments

Comments
 (0)