Skip to content

Commit 3432fda

Browse files
committed
wip
1 parent 29dff62 commit 3432fda

6 files changed

Lines changed: 255 additions & 16 deletions

File tree

src/Utils/Testing/DelegatesToResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function __call(string $name, array $arguments)
1919

2020
return $this;
2121
}
22+
23+
public function __get(string $name)
24+
{
25+
return $this->response->{$name};
26+
}
2227
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Code16\Sharp\Utils\Testing\Form;
4+
5+
use Code16\Sharp\Utils\Testing\DelegatesToResponse;
6+
use Illuminate\Testing\TestResponse;
7+
8+
class AssertableForm
9+
{
10+
use DelegatesToResponse;
11+
use FormatsDataForUpdate;
12+
13+
public function __construct(
14+
protected TestResponse $response,
15+
protected PendingForm $pendingForm,
16+
) {}
17+
18+
public function store(array $data = []): TestResponse
19+
{
20+
return $this->pendingForm->store(
21+
$this->formatDataForUpdate($this->pendingForm->form, $data, baseData: $this->formData())
22+
);
23+
}
24+
25+
public function update(array $data = []): TestResponse
26+
{
27+
return $this->pendingForm->update(
28+
$this->formatDataForUpdate($this->pendingForm->form, $data, baseData: $this->formData())
29+
);
30+
}
31+
32+
public function formData(): array
33+
{
34+
return $this->response->inertiaProps('form.data');
35+
}
36+
}
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\Form;
4+
5+
use Code16\Sharp\Form\SharpForm;
6+
7+
trait FormatsDataForUpdate
8+
{
9+
protected function formatDataForUpdate(SharpForm $form, array $data, ?array $baseData = null): array
10+
{
11+
return [
12+
...$baseData ?: [],
13+
...collect($form->applyFormatters($data))
14+
->when($baseData)->only(array_keys($data))
15+
->all(),
16+
];
17+
}
18+
}

src/Utils/Testing/Form/PendingForm.php

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Code16\Sharp\Utils\Testing\Form;
44

5-
use Code16\Sharp\Show\SharpShow;
5+
use Code16\Sharp\Form\SharpForm;
66
use Code16\Sharp\Utils\Entities\SharpEntityManager;
77
use Code16\Sharp\Utils\Testing\EntityList\PendingEntityList;
88
use Code16\Sharp\Utils\Testing\GeneratesGlobalFilterUrl;
@@ -13,11 +13,13 @@
1313

1414
class PendingForm
1515
{
16+
use FormatsDataForUpdate;
1617
use GeneratesGlobalFilterUrl;
1718
use IsPendingComponent;
1819

19-
protected SharpShow $show;
20+
public SharpForm $form;
2021
public string $entityKey;
22+
protected bool $isSubsequentRequest = false;
2123

2224
public function __construct(
2325
/** @var TestCase $test */
@@ -27,18 +29,72 @@ public function __construct(
2729
public PendingEntityList|PendingShow|null $parent = null,
2830
) {
2931
$this->entityKey = app(SharpEntityManager::class)->entityKeyFor($entityKey);
30-
$this->show = app(SharpEntityManager::class)->entityFor($this->entityKey)->getShowOrFail();
32+
$this->form = app(SharpEntityManager::class)->entityFor($this->entityKey)->getFormOrFail();
3133
}
3234

33-
public function get(): TestResponse
35+
public function create(): AssertableForm
36+
{
37+
$this->setGlobalFilterUrlDefault();
38+
39+
return new AssertableForm(
40+
$this->test
41+
->get(route('code16.sharp.form.create', [
42+
'parentUri' => $this->getParentUri(),
43+
'entityKey' => $this->entityKey,
44+
])),
45+
pendingForm: $this->forSubsequentRequest(),
46+
);
47+
}
48+
49+
public function edit(): AssertableForm
50+
{
51+
$this->setGlobalFilterUrlDefault();
52+
53+
return new AssertableForm(
54+
$this->test
55+
->get(route('code16.sharp.form.edit', [
56+
'parentUri' => $this->getParentUri(),
57+
'entityKey' => $this->entityKey,
58+
'instanceId' => $this->instanceId,
59+
])),
60+
pendingForm: $this->forSubsequentRequest(),
61+
);
62+
}
63+
64+
public function store(array $data): TestResponse
3465
{
3566
$this->setGlobalFilterUrlDefault();
3667

3768
return $this->test
38-
->get(route('code16.sharp.form.edit', [
39-
'parentUri' => $this->getParentUri(),
40-
'entityKey' => $this->entityKey,
41-
'instanceId' => $this->instanceId,
42-
]));
69+
->post(
70+
route('code16.sharp.form.store', [
71+
'parentUri' => $this->getParentUri(),
72+
'entityKey' => $this->entityKey,
73+
]),
74+
$this->isSubsequentRequest ? $data : $this->formatDataForUpdate($this->form, $data),
75+
);
76+
}
77+
78+
public function update(array $data): TestResponse
79+
{
80+
$this->setGlobalFilterUrlDefault();
81+
82+
return $this->test
83+
->post(
84+
route('code16.sharp.form.update', [
85+
'parentUri' => $this->getParentUri(),
86+
'entityKey' => $this->entityKey,
87+
'instanceId' => $this->instanceId,
88+
]),
89+
$this->isSubsequentRequest ? $data : $this->formatDataForUpdate($this->form, $data),
90+
);
91+
}
92+
93+
protected function forSubsequentRequest(): static
94+
{
95+
$pendingForm = clone $this;
96+
$pendingForm->isSubsequentRequest = true;
97+
98+
return $pendingForm;
4399
}
44100
}

src/Utils/Testing/IsPendingComponent.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Code16\Sharp\Utils\Testing;
44

5+
use Code16\Sharp\Form\SharpSingleForm;
56
use Code16\Sharp\Utils\Entities\SharpEntityManager;
67
use Code16\Sharp\Utils\Links\BreadcrumbBuilder;
78
use Code16\Sharp\Utils\Testing\EntityList\PendingEntityList;
@@ -32,13 +33,13 @@ protected function breadcrumbBuilder(array $components): BreadcrumbBuilder
3233
if ($first instanceof PendingShow && $first->instanceId) {
3334
$breadcrumb->appendEntityList($first->entityKey);
3435
} elseif ($first instanceof PendingForm) {
35-
if ($first->instanceId) {
36+
if ($first->form instanceof SharpSingleForm) {
37+
$breadcrumb->appendSingleShowPage($first->entityKey);
38+
} else {
3639
$breadcrumb->appendEntityList($first->entityKey);
37-
if (app(SharpEntityManager::class)->entityFor($first->entityKey)->hasShow()) {
40+
if (app(SharpEntityManager::class)->entityFor($first->entityKey)->hasShow() && $first->instanceId) {
3841
$breadcrumb->appendShowPage($first->entityKey, $first->instanceId);
3942
}
40-
} else {
41-
$breadcrumb->appendSingleShowPage($first->entityKey);
4243
}
4344
}
4445

tests/Http/SharpAssertionsHttpTest.php

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
use Code16\Sharp\EntityList\Commands\EntityCommand;
44
use Code16\Sharp\EntityList\Commands\Wizards\EntityWizardCommand;
5+
use Code16\Sharp\Form\Fields\SharpFormEditorField;
56
use Code16\Sharp\Form\Fields\SharpFormTextField;
67
use Code16\Sharp\Show\Fields\SharpShowEntityListField;
78
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
9+
use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm;
810
use Code16\Sharp\Tests\Fixtures\Sharp\PersonList;
911
use Code16\Sharp\Tests\Fixtures\Sharp\PersonShow;
1012
use Code16\Sharp\Utils\Fields\FieldsContainer;
@@ -202,8 +204,129 @@ public function getListData(): array
202204
->assertListContains(['name' => 'Marie Curie']);
203205
});
204206

205-
test('get & assert form', function () {
207+
test('nested form', function () {
208+
$this->sharpForm(PersonEntity::class)->create();
209+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-form/person');
210+
$this->sharpForm(PersonEntity::class, 1)->update([]);
211+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1/s-form/person/1');
212+
});
213+
214+
test('create & store form', function () {
215+
fakeFormFor('person', new class() extends PersonForm
216+
{
217+
public function buildFormFields(FieldsContainer $formFields): void
218+
{
219+
$formFields
220+
->addField(SharpFormEditorField::make('name'))
221+
->addField(SharpFormEditorField::make('job'));
222+
}
223+
224+
public function find($id): array
225+
{
226+
return ['name' => 'John Wayne', 'job' => 'actor'];
227+
}
228+
229+
public function update($id, array $data)
230+
{
231+
expect($data)->toEqual(['name' => 'John Doe', 'job' => null]);
232+
233+
return 1;
234+
}
235+
});
236+
237+
$this->sharpForm(PersonEntity::class)
238+
->create()
239+
->assertOk()
240+
->store(['name' => 'John Doe'])
241+
->assertValid()
242+
->assertRedirect();
243+
});
244+
245+
test('store form', function () {
246+
fakeFormFor('person', new class() extends PersonForm
247+
{
248+
public function buildFormFields(FieldsContainer $formFields): void
249+
{
250+
$formFields
251+
->addField(SharpFormEditorField::make('name'))
252+
->addField(SharpFormEditorField::make('job'));
253+
}
254+
255+
public function find($id): array
256+
{
257+
return ['name' => 'John Wayne', 'job' => 'actor'];
258+
}
259+
260+
public function update($id, array $data)
261+
{
262+
expect($data)->toEqual(['name' => 'John Doe', 'job' => null]);
263+
264+
return 1;
265+
}
266+
});
267+
268+
$this->sharpForm(PersonEntity::class)
269+
->store(['name' => 'John Doe'])
270+
->assertValid()
271+
->assertRedirect();
272+
});
273+
274+
test('edit & update form', function () {
275+
fakeFormFor('person', new class() extends PersonForm
276+
{
277+
public function buildFormFields(FieldsContainer $formFields): void
278+
{
279+
$formFields
280+
->addField(SharpFormEditorField::make('name'))
281+
->addField(SharpFormEditorField::make('job'));
282+
}
283+
284+
public function find($id): array
285+
{
286+
return ['name' => 'John Wayne', 'job' => 'actor'];
287+
}
288+
289+
public function update($id, array $data)
290+
{
291+
expect($data)->toEqual(['name' => 'John Doe', 'job' => 'actor']);
292+
293+
return 1;
294+
}
295+
});
296+
206297
$this->sharpForm(PersonEntity::class, 1)
207-
->get()
208-
->assertOk();
298+
->edit()
299+
->assertOk()
300+
->update(['name' => 'John Doe'])
301+
->assertValid()
302+
->assertRedirect();
303+
});
304+
305+
test('update form', function () {
306+
fakeFormFor('person', new class() extends PersonForm
307+
{
308+
public function buildFormFields(FieldsContainer $formFields): void
309+
{
310+
$formFields
311+
->addField(SharpFormEditorField::make('name'))
312+
->addField(SharpFormEditorField::make('job'));
313+
}
314+
315+
public function find($id): array
316+
{
317+
return ['name' => 'John Wayne', 'job' => 'actor'];
318+
}
319+
320+
public function update($id, array $data)
321+
{
322+
expect($data)->toEqual(['name' => 'John Doe', 'job' => null]);
323+
324+
return 1;
325+
}
326+
});
327+
328+
$this->sharpForm(PersonEntity::class, 1)
329+
->update(['name' => 'John Doe'])
330+
->assertValid()
331+
->assertRedirect();
209332
});

0 commit comments

Comments
 (0)