11<?php
22
33use Code16 \Sharp \EntityList \Commands \EntityCommand ;
4+ use Code16 \Sharp \EntityList \Commands \InstanceCommand ;
45use Code16 \Sharp \EntityList \Commands \Wizards \EntityWizardCommand ;
56use Code16 \Sharp \Form \Fields \SharpFormEditorField ;
67use Code16 \Sharp \Form \Fields \SharpFormTextField ;
78use Code16 \Sharp \Show \Fields \SharpShowEntityListField ;
89use Code16 \Sharp \Tests \Fixtures \Entities \PersonEntity ;
10+ use Code16 \Sharp \Tests \Fixtures \Entities \SinglePersonEntity ;
911use Code16 \Sharp \Tests \Fixtures \Sharp \PersonForm ;
1012use Code16 \Sharp \Tests \Fixtures \Sharp \PersonList ;
1113use Code16 \Sharp \Tests \Fixtures \Sharp \PersonShow ;
14+ use Code16 \Sharp \Tests \Fixtures \Sharp \SinglePersonShow ;
1215use Code16 \Sharp \Utils \Fields \FieldsContainer ;
1316use Code16 \Sharp \Utils \Testing \SharpAssertions ;
1417use Illuminate \Http \UploadedFile ;
1821beforeEach (function () {
1922 login ();
2023 sharp ()->config ()->declareEntity (PersonEntity::class);
24+ sharp ()->config ()->declareEntity (SinglePersonEntity::class);
2125});
2226
2327it ('get & assert an entity list ' , function () {
@@ -39,7 +43,7 @@ public function getListData(): array
3943});
4044
4145it ('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
214330test ('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
245363test ('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
274389test ('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
305422test ('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