Skip to content

Commit db1c3f5

Browse files
committed
wip
1 parent 3432fda commit db1c3f5

5 files changed

Lines changed: 165 additions & 143 deletions

File tree

src/Utils/Testing/Form/PendingForm.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace Code16\Sharp\Utils\Testing\Form;
44

55
use Code16\Sharp\Form\SharpForm;
6+
use Code16\Sharp\Form\SharpSingleForm;
67
use Code16\Sharp\Utils\Entities\SharpEntityManager;
78
use Code16\Sharp\Utils\Testing\EntityList\PendingEntityList;
89
use Code16\Sharp\Utils\Testing\GeneratesGlobalFilterUrl;
910
use Code16\Sharp\Utils\Testing\IsPendingComponent;
1011
use Code16\Sharp\Utils\Testing\Show\PendingShow;
1112
use Illuminate\Foundation\Testing\TestCase;
1213
use Illuminate\Testing\TestResponse;
14+
use PHPUnit\Framework\Assert as PHPUnit;
1315

1416
class PendingForm
1517
{
@@ -36,6 +38,8 @@ public function create(): AssertableForm
3638
{
3739
$this->setGlobalFilterUrlDefault();
3840

41+
PHPUnit::assertNotInstanceOf(SharpSingleForm::class, $this->form);
42+
3943
return new AssertableForm(
4044
$this->test
4145
->get(route('code16.sharp.form.create', [
@@ -50,6 +54,10 @@ public function edit(): AssertableForm
5054
{
5155
$this->setGlobalFilterUrlDefault();
5256

57+
if (! $this->form instanceof SharpSingleForm) {
58+
PHPUnit::assertNotNull($this->instanceId, 'You can’t edit a form without an instance ID.');
59+
}
60+
5361
return new AssertableForm(
5462
$this->test
5563
->get(route('code16.sharp.form.edit', [
@@ -65,6 +73,8 @@ public function store(array $data): TestResponse
6573
{
6674
$this->setGlobalFilterUrlDefault();
6775

76+
PHPUnit::assertNotInstanceOf(SharpSingleForm::class, $this->form);
77+
6878
return $this->test
6979
->post(
7080
route('code16.sharp.form.store', [
@@ -79,6 +89,10 @@ public function update(array $data): TestResponse
7989
{
8090
$this->setGlobalFilterUrlDefault();
8191

92+
if (! $this->form instanceof SharpSingleForm) {
93+
PHPUnit::assertNotNull($this->instanceId, 'You can’t update a form without an instance ID.');
94+
}
95+
8296
return $this->test
8397
->post(
8498
route('code16.sharp.form.update', [

src/Utils/Testing/Show/PendingShow.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function get(): AssertableShow
5050
$this->test
5151
->get($this->show instanceof SharpSingleShow
5252
? route('code16.sharp.single-show', [
53-
'parentUri' => $this->getParentUri(),
5453
'entityKey' => $this->entityKey,
5554
])
5655
: route('code16.sharp.show.show', [

tests/Http/SharpAssertionsHttpTest.php

Lines changed: 151 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?php
22

33
use Code16\Sharp\EntityList\Commands\EntityCommand;
4+
use Code16\Sharp\EntityList\Commands\InstanceCommand;
45
use Code16\Sharp\EntityList\Commands\Wizards\EntityWizardCommand;
56
use Code16\Sharp\Form\Fields\SharpFormEditorField;
67
use Code16\Sharp\Form\Fields\SharpFormTextField;
78
use Code16\Sharp\Show\Fields\SharpShowEntityListField;
89
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
10+
use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity;
911
use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm;
1012
use Code16\Sharp\Tests\Fixtures\Sharp\PersonList;
1113
use Code16\Sharp\Tests\Fixtures\Sharp\PersonShow;
14+
use Code16\Sharp\Tests\Fixtures\Sharp\SinglePersonShow;
1215
use Code16\Sharp\Utils\Fields\FieldsContainer;
1316
use Code16\Sharp\Utils\Testing\SharpAssertions;
1417
use Illuminate\Http\UploadedFile;
@@ -18,6 +21,7 @@
1821
beforeEach(function () {
1922
login();
2023
sharp()->config()->declareEntity(PersonEntity::class);
24+
sharp()->config()->declareEntity(SinglePersonEntity::class);
2125
});
2226

2327
it('get & assert an entity list', function () {
@@ -39,7 +43,7 @@ public function getListData(): array
3943
});
4044

4145
it('call & assert an entity list entity command', function () {
42-
fakeListFor('person', new class() extends PersonList
46+
fakeListFor(PersonEntity::class, new class() extends PersonList
4347
{
4448
protected function getEntityCommands(): ?array
4549
{
@@ -106,8 +110,8 @@ public function execute(array $data = []): array
106110
->assertReturnsRefresh([1, 2]);
107111
});
108112

109-
it('call & assert an entity list entity wiard command', function () {
110-
fakeListFor('person', new class() extends PersonList
113+
it('call & assert an entity list entity wizard command', function () {
114+
fakeListFor(PersonEntity::class, new class() extends PersonList
111115
{
112116
protected function getEntityCommands(): ?array
113117
{
@@ -154,8 +158,71 @@ protected function executeStepSecondStep(array $data): array
154158
->assertReturnsReload();
155159
});
156160

157-
test('get & assert show', function () {
158-
fakeShowFor('person', new class() extends PersonShow
161+
it('call & assert a show instance command', function () {
162+
fakeShowFor(PersonEntity::class, new class() extends PersonShow
163+
{
164+
public function getInstanceCommands(): ?array
165+
{
166+
return [
167+
'cmd' => new class() extends InstanceCommand
168+
{
169+
public function label(): ?string
170+
{
171+
return 'entity';
172+
}
173+
174+
public function buildFormFields(FieldsContainer $formFields): void
175+
{
176+
$formFields->addField(SharpFormTextField::make('action'));
177+
}
178+
179+
public function execute($instanceId, array $data = []): array
180+
{
181+
if ($data['action'] === 'download') {
182+
Storage::fake('files');
183+
UploadedFile::fake()
184+
->create('account.pdf', 100, 'application/pdf')
185+
->storeAs('pdf', 'account.pdf', ['disk' => 'files']);
186+
}
187+
188+
return match ($data['action']) {
189+
'info' => $this->info('ok'),
190+
'link' => $this->link('https://example.org'),
191+
'view' => $this->view('fixtures::test', ['text' => 'text']),
192+
'reload' => $this->reload(),
193+
'download' => $this->download('pdf/account.pdf', 'account.pdf', 'files'),
194+
};
195+
}
196+
},
197+
];
198+
}
199+
});
200+
201+
$this->sharpShow(PersonEntity::class, 1)
202+
->callInstanceCommand('cmd', ['action' => 'info'])
203+
->assertReturnsInfo('ok');
204+
205+
$this->sharpShow(PersonEntity::class, 1)
206+
->callInstanceCommand('cmd', ['action' => 'link'])
207+
->assertReturnsLink('https://example.org');
208+
209+
$this->sharpShow(PersonEntity::class, 1)
210+
->callInstanceCommand('cmd', ['action' => 'view'])
211+
->assertReturnsView('fixtures::test', [
212+
'text' => 'text',
213+
]);
214+
215+
$this->sharpShow(PersonEntity::class, 1)
216+
->callInstanceCommand('cmd', ['action' => 'reload'])
217+
->assertReturnsReload();
218+
219+
$this->sharpShow(PersonEntity::class, 1)
220+
->callInstanceCommand('cmd', ['action' => 'download'])
221+
->assertReturnsDownload('account.pdf');
222+
});
223+
224+
test('get show', function () {
225+
fakeShowFor(PersonEntity::class, new class() extends PersonShow
159226
{
160227
public function find($id): array
161228
{
@@ -167,10 +234,30 @@ public function find($id): array
167234
->get()
168235
->assertOk()
169236
->assertShowData(['name' => 'John Doe']);
237+
238+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1');
239+
});
240+
241+
test('get single show', function () {
242+
243+
fakeShowFor(SinglePersonEntity::class, new class() extends SinglePersonShow
244+
{
245+
public function findSingle(): array
246+
{
247+
return ['name' => 'John Doe', 'age' => 31];
248+
}
249+
});
250+
251+
$this->sharpShow(SinglePersonEntity::class)
252+
->get()
253+
->assertOk()
254+
->assertShowData(['name' => 'John Doe']);
255+
256+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-show/single-person');
170257
});
171258

172-
test('get & assert show EEL', function () {
173-
fakeShowFor('person', new class() extends PersonShow
259+
test('get show EEL', function () {
260+
fakeShowFor(PersonEntity::class, new class() extends PersonShow
174261
{
175262
public function buildShowFields(FieldsContainer $showFields): void
176263
{
@@ -204,15 +291,44 @@ public function getListData(): array
204291
->assertListContains(['name' => 'Marie Curie']);
205292
});
206293

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');
294+
test('get nested show', function () {
295+
fakeShowFor(PersonEntity::class, new class() extends PersonShow
296+
{
297+
public function buildShowFields(FieldsContainer $showFields): void
298+
{
299+
$showFields->addField(
300+
SharpShowEntityListField::make(PersonEntity::class)
301+
);
302+
}
303+
304+
public function find($id): array
305+
{
306+
return ['name' => 'John Doe', 'age' => 31];
307+
}
308+
});
309+
310+
fakeListFor(PersonEntity::class, new class() extends PersonList
311+
{
312+
public function getListData(): array
313+
{
314+
return [
315+
['id' => 1, 'name' => 'Marie Curie'],
316+
['id' => 2, 'name' => 'Albert Einstein'],
317+
];
318+
}
319+
});
320+
321+
$this->sharpShow(PersonEntity::class, 1)
322+
->sharpListField(PersonEntity::class)
323+
->sharpShow(PersonEntity::class, 2)
324+
->get()
325+
->assertOk();
326+
327+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1/s-show/person/2');
212328
});
213329

214330
test('create & store form', function () {
215-
fakeFormFor('person', new class() extends PersonForm
331+
fakeFormFor(PersonEntity::class, new class() extends PersonForm
216332
{
217333
public function buildFormFields(FieldsContainer $formFields): void
218334
{
@@ -221,14 +337,14 @@ public function buildFormFields(FieldsContainer $formFields): void
221337
->addField(SharpFormEditorField::make('job'));
222338
}
223339

224-
public function find($id): array
340+
public function create(): array
225341
{
226342
return ['name' => 'John Wayne', 'job' => 'actor'];
227343
}
228344

229345
public function update($id, array $data)
230346
{
231-
expect($data)->toEqual(['name' => 'John Doe', 'job' => null]);
347+
expect($data)->toEqual(['name' => 'John Doe', 'job' => 'actor']);
232348

233349
return 1;
234350
}
@@ -240,10 +356,12 @@ public function update($id, array $data)
240356
->store(['name' => 'John Doe'])
241357
->assertValid()
242358
->assertRedirect();
359+
360+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-form/person');
243361
});
244362

245363
test('store form', function () {
246-
fakeFormFor('person', new class() extends PersonForm
364+
fakeFormFor(PersonEntity::class, new class() extends PersonForm
247365
{
248366
public function buildFormFields(FieldsContainer $formFields): void
249367
{
@@ -252,11 +370,6 @@ public function buildFormFields(FieldsContainer $formFields): void
252370
->addField(SharpFormEditorField::make('job'));
253371
}
254372

255-
public function find($id): array
256-
{
257-
return ['name' => 'John Wayne', 'job' => 'actor'];
258-
}
259-
260373
public function update($id, array $data)
261374
{
262375
expect($data)->toEqual(['name' => 'John Doe', 'job' => null]);
@@ -269,10 +382,12 @@ public function update($id, array $data)
269382
->store(['name' => 'John Doe'])
270383
->assertValid()
271384
->assertRedirect();
385+
386+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-form/person');
272387
});
273388

274389
test('edit & update form', function () {
275-
fakeFormFor('person', new class() extends PersonForm
390+
fakeFormFor(PersonEntity::class, new class() extends PersonForm
276391
{
277392
public function buildFormFields(FieldsContainer $formFields): void
278393
{
@@ -300,10 +415,12 @@ public function update($id, array $data)
300415
->update(['name' => 'John Doe'])
301416
->assertValid()
302417
->assertRedirect();
418+
419+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1/s-form/person/1');
303420
});
304421

305422
test('update form', function () {
306-
fakeFormFor('person', new class() extends PersonForm
423+
fakeFormFor(PersonEntity::class, new class() extends PersonForm
307424
{
308425
public function buildFormFields(FieldsContainer $formFields): void
309426
{
@@ -329,4 +446,15 @@ public function update($id, array $data)
329446
->update(['name' => 'John Doe'])
330447
->assertValid()
331448
->assertRedirect();
449+
450+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1/s-form/person/1');
451+
});
452+
453+
test('update single form', function () {
454+
$this->sharpForm(SinglePersonEntity::class)
455+
->update(['name' => 'John Doe'])
456+
->assertValid()
457+
->assertRedirect();
458+
459+
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-show/single-person/s-form/single-person');
332460
});

0 commit comments

Comments
 (0)