Skip to content

Commit 205ce3e

Browse files
committed
Log an exception if SharpForm::update() does not return the instance id on creation
1 parent 4ef5dc2 commit 205ce3e

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/EntityList/Commands/QuickCreate/QuickCreationCommand.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Code16\Sharp\EntityList\Commands\QuickCreate;
44

55
use Code16\Sharp\EntityList\Commands\EntityCommand;
6+
use Code16\Sharp\Exceptions\Form\SharpFormUpdateException;
67
use Code16\Sharp\Form\Fields\SharpFormField;
78
use Code16\Sharp\Form\SharpForm;
89
use Code16\Sharp\Utils\Fields\FieldsContainer;
@@ -73,6 +74,11 @@ public function getInstanceId(): mixed
7374
public function execute(array $data = []): array
7475
{
7576
$this->instanceId = $this->sharpForm->update(null, $data);
77+
78+
if ($this->instanceId === null) {
79+
report(new SharpFormUpdateException('The update() method in '.get_class($this->sharpForm).' must return the newly created instance id'));
80+
}
81+
7682
$currentUrl = sharp()->context()->breadcrumb()->getCurrentSegmentUrl();
7783

7884
return $this->sharpForm->isDisplayShowPageAfterCreation()

src/Http/Controllers/FormController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Code16\Sharp\Auth\SharpAuthorizationManager;
66
use Code16\Sharp\Data\BreadcrumbData;
77
use Code16\Sharp\Data\Form\FormData;
8+
use Code16\Sharp\Exceptions\Form\SharpFormUpdateException;
89
use Code16\Sharp\Form\SharpForm;
910
use Code16\Sharp\Form\SharpSingleForm;
1011
use Code16\Sharp\Utils\Entities\SharpEntityManager;
@@ -145,6 +146,11 @@ public function store(string $parentUri, EntityKey $entityKey)
145146

146147
$formattedData = $form->formatAndValidateRequestData(request()->all());
147148
$instanceId = $form->update(null, $formattedData);
149+
150+
if ($instanceId === null) {
151+
report(new SharpFormUpdateException('The update() method in '.get_class($form).' must return the newly created instance id'));
152+
}
153+
148154
$this->uploadManager->dispatchJobs($instanceId);
149155

150156
$previousUrl = sharp()->context()->breadcrumb()->getPreviousSegmentUrl();

tests/Http/Api/Commands/ApiEntityListQuickCreationCommandControllerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3+
use Code16\Sharp\Exceptions\Form\SharpFormUpdateException;
34
use Code16\Sharp\Form\Fields\SharpFormTextField;
45
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
56
use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm;
67
use Code16\Sharp\Tests\Fixtures\Sharp\PersonList;
78
use Code16\Sharp\Utils\Fields\FieldsContainer;
9+
use Illuminate\Support\Facades\Exceptions;
810

911
beforeEach(function () {
1012
sharp()->config()->addEntity('person', PersonEntity::class);
@@ -122,6 +124,33 @@ public function update($id, array $data)
122124
->assertJson(['action' => 'reload']);
123125
});
124126

127+
it('logs an error if the form’s update() method does not return the instance id', function () {
128+
Exceptions::fake();
129+
130+
fakeListFor('person', new class() extends PersonList
131+
{
132+
public function buildListConfig(): void
133+
{
134+
$this->configureQuickCreationForm();
135+
}
136+
});
137+
138+
fakeFormFor('person', new class() extends PersonForm
139+
{
140+
public function update($id, array $data) {}
141+
});
142+
143+
$this
144+
->postJson(
145+
route('code16.sharp.api.list.command.quick-creation-form.create', ['person']),
146+
['data' => []],
147+
)
148+
->assertOk()
149+
->assertJson(['action' => 'reload']);
150+
151+
Exceptions::assertReported(SharpFormUpdateException::class);
152+
});
153+
125154
it('validates posted data of a quick creation command', function () {
126155
fakeListFor('person', new class() extends PersonList
127156
{

tests/Http/Form/FormControllerTest.php

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

33
use Code16\Sharp\Enums\PageAlertLevel;
44
use Code16\Sharp\Exceptions\Form\SharpApplicativeException;
5+
use Code16\Sharp\Exceptions\Form\SharpFormUpdateException;
56
use Code16\Sharp\Form\Fields\SharpFormCheckField;
67
use Code16\Sharp\Form\Fields\SharpFormEditorField;
78
use Code16\Sharp\Form\Fields\SharpFormTextField;
@@ -14,6 +15,7 @@
1415
use Code16\Sharp\Utils\Entities\SharpEntityManager;
1516
use Code16\Sharp\Utils\Fields\FieldsContainer;
1617
use Code16\Sharp\Utils\PageAlerts\PageAlert;
18+
use Illuminate\Support\Facades\Exceptions;
1719
use Inertia\Testing\AssertableInertia as Assert;
1820

1921
beforeEach(function () {
@@ -189,6 +191,22 @@ public function buildFormConfig(): void
189191
->assertRedirect('/sharp/s-list/person/s-show/person/1');
190192
});
191193

194+
it('logs an error if the update() method does not return the instance id', function () {
195+
Exceptions::fake();
196+
197+
fakeFormFor('person', new class() extends PersonForm
198+
{
199+
public function update(mixed $id, array $data) {}
200+
});
201+
202+
$this
203+
->post('/sharp/s-list/person/s-form/person')
204+
->assertRedirect()
205+
->assertSessionDoesntHaveErrors();
206+
207+
Exceptions::assertReported(SharpFormUpdateException::class);
208+
});
209+
192210
it('validates an instance before store and update with the rules() method', function () {
193211
fakeFormFor('person', new class() extends PersonForm
194212
{

0 commit comments

Comments
 (0)