From 5bbab23ccd33cfcc438fc3ec76e588184a64e0db Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 3 Jul 2026 10:44:11 +0200 Subject: [PATCH 1/2] wip --- .../Testing/EntityList/PendingEntityList.php | 15 +++++++++++++++ src/Utils/Testing/SharpAssertions.php | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Utils/Testing/EntityList/PendingEntityList.php b/src/Utils/Testing/EntityList/PendingEntityList.php index dbf0f287b..481b3138a 100644 --- a/src/Utils/Testing/EntityList/PendingEntityList.php +++ b/src/Utils/Testing/EntityList/PendingEntityList.php @@ -14,6 +14,7 @@ use Code16\Sharp\Utils\Testing\SharpAssertions; use Code16\Sharp\Utils\Testing\Show\PendingShow; use Illuminate\Foundation\Testing\TestCase; +use Illuminate\Testing\TestResponse; use PHPUnit\Framework\Assert as PHPUnit; class PendingEntityList @@ -85,6 +86,20 @@ public function get(): AssertableEntityList ); } + public function delete(int|string $instanceId): TestResponse + { + return $this->test + ->delete( + route('code16.sharp.api.list.delete', [ + 'entityKey' => $this->entityKey, + 'instanceId' => $instanceId, + ]), + headers: [ + SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(), + ] + ); + } + public function entityCommand(string $commandKeyOrClassName): PendingCommand { $commandKey = class_exists($commandKeyOrClassName) diff --git a/src/Utils/Testing/SharpAssertions.php b/src/Utils/Testing/SharpAssertions.php index 13fd2d915..7dfb2bb7d 100644 --- a/src/Utils/Testing/SharpAssertions.php +++ b/src/Utils/Testing/SharpAssertions.php @@ -47,7 +47,7 @@ public function sharpDashboard(string $entityClassNameOrKey): PendingDashboard } /** - * @param (\Closure(BreadcrumbBuilder): BreadcrumbBuilder) $callback + * @param (Closure(BreadcrumbBuilder): BreadcrumbBuilder) $callback * @return $this */ public function withSharpBreadcrumb(Closure $callback): static @@ -57,6 +57,9 @@ public function withSharpBreadcrumb(Closure $callback): static return $this; } + /** + * @deprecated Use $this->>sharpShow()->delete() instead + */ public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanceId) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); @@ -75,6 +78,9 @@ public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanc ); } + /** + * @deprecated Use $this->sharpList()->delete() instead + */ public function deleteFromSharpList(string $entityClassNameOrKey, mixed $instanceId) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); From 5dca79058584941536b8891272dbb1c564b4b043 Mon Sep 17 00:00:00 2001 From: antoine Date: Fri, 3 Jul 2026 12:20:14 +0200 Subject: [PATCH 2/2] Deprecate legacy API --- docs/guide/testing-legacy.md | 2 +- docs/guide/testing.md | 20 ++++++++++++ docs/guide/upgrade.md | 43 ++++++++++++++++++++++++- src/Utils/Testing/SharpAssertions.php | 31 +++++++++++++++++- src/Utils/Testing/Show/PendingShow.php | 20 ++++++++++-- tests/Http/SharpAssertionsHttpTest.php | 44 ++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 5 deletions(-) diff --git a/docs/guide/testing-legacy.md b/docs/guide/testing-legacy.md index c21095edf..fa14c7263 100644 --- a/docs/guide/testing-legacy.md +++ b/docs/guide/testing-legacy.md @@ -1,7 +1,7 @@ # Testing with Sharp (legacy API) ::: warning -This page documents the old Testing API, we recommend using the new [Testing API](/guide/testing). +This page documents the old Testing API, it is deprecated in 10.x. We recommend using the new [Testing API](/guide/testing). ::: Sharp provides a few assertions and helpers to help you test your Sharp code. diff --git a/docs/guide/testing.md b/docs/guide/testing.md index 37173922e..ec6914a08 100644 --- a/docs/guide/testing.md +++ b/docs/guide/testing.md @@ -136,6 +136,16 @@ $this->sharpList(Post::class) ->assertOk(); ``` +### Delete an instance + +To test the deletion of an instance, you can use `delete()`; + +```php +$this->sharpList(Post::class) + ->delete(1) + ->assertOk(); +``` + ## Testing Show Pages Use `sharpShow()` to test your Show Pages. @@ -201,6 +211,16 @@ $this->sharpList(Post::class) ->assertOk(); ``` +### Delete the instance + +To test the deletion of the show instance, you can use `delete()`; + +```php +$this->sharpShow(Post::class, 1) + ->delete() + ->assertRedirect(); +``` + ## Testing Forms Use `sharpForm()` to test your Forms. diff --git a/docs/guide/upgrade.md b/docs/guide/upgrade.md index dc8af886f..c14bcaf23 100644 --- a/docs/guide/upgrade.md +++ b/docs/guide/upgrade.md @@ -19,12 +19,53 @@ You should update the following dependencies in your `composer.json` file: ### Forms -#### Upload `setImageCompactThumbnail()` has no impact and can be removed +Upload `setImageCompactThumbnail()` has no impact and can be removed: ```php \Code16\Sharp\Form\Fields\SharpFormUploadField::make('upload') ->setImageCompactThumbnail() // [!code --] ``` +### Testing +The legacy testing API is deprecated and will be removed in 11.x. Please refer to (cf. [Testing](testing)). Here are replacement examples: + +```php +uses(\Code16\Sharp\Utils\Testing\SharpAssertions::class) + +it('test', function () { + $this->callSharpEntityCommandFromList(PersonEntity::class, MyCommand::class, ['attr' => 'some_value']) // [!code --] + $this->sharpList(PersonEntity::class)->entityCommand(MyCommand::class)->post(['attr' => 'some_value']) // [!code ++] + + $this->callSharpInstanceCommandFromList(PersonEntity::class, 1, MyCommand::class, ['attr' => 'some_value']) // [!code --] + $this->sharpList(PersonEntity::class)->instanceCommand(MyCommand::class, 1)->post(['attr' => 'some_value']) // [!code ++] + + $this->callSharpInstanceCommandFromShow(PersonEntity::class, 1, MyCommand::class, ['attr' => 'some_value']) // [!code --] + $this->sharpShow(PersonEntity::class, 1)->instanceCommand(MyCommand::class)->post(['attr' => 'some_value']) // [!code ++] + + $this->getSharpShow(PersonEntity::class, 1) // [!code --] + $this->sharpShow(PersonEntity::class, 1)->get() // [!code ++] + + $this->getSharpForm(PersonEntity::class, 1) // [!code --] + $this->sharpForm(PersonEntity::class, 1)->get() // [!code ++] + + $this->getSingleSharpForm(PersonEntity::class) // [!code --] + $this->sharpForm(PersonEntity::class)->get() // [!code ++] + + $this->updateSharpForm(PersonEntity::class, 1, []) // [!code --] + $this->sharpForm(PersonEntity::class, 1)->update([]) // [!code ++] + + $this->storeSharpForm(PersonEntity::class, 1, []) // [!code --] + $this->sharpForm(PersonEntity::class, 1)->store([]) // [!code ++] + + $this->updateSingleSharpForm(PersonEntity::class, []) // [!code --] + $this->sharpForm(PersonEntity::class)->update([]) // [!code ++] + + $this->withSharpBreadcrumb(function (\Code16\Sharp\Utils\Links\BreadcrumbBuilder $builder) { // [!code --] + $builder->appendEntityList(PersonEntity::class)->appendShowPage(PersonEntity::class, 1) // [!code --] + })->getSharpForm(PersonEntity::class, 1) // [!code --] + $this->sharpList(PersonEntity::class)->sharpShow(PersonEntity::class, 1)->sharpForm(PersonEntity::class)->get() // [!code ++] +}) +``` + ## Removed classes and methods Several features and methods that were deprecated in Sharp 9.0 have been removed to clean up the codebase. diff --git a/src/Utils/Testing/SharpAssertions.php b/src/Utils/Testing/SharpAssertions.php index 7dfb2bb7d..9766e36d3 100644 --- a/src/Utils/Testing/SharpAssertions.php +++ b/src/Utils/Testing/SharpAssertions.php @@ -47,6 +47,8 @@ public function sharpDashboard(string $entityClassNameOrKey): PendingDashboard } /** + * @deprecated Chain $this->sharpList()->sharpShow()->sharpForm() instead + * * @param (Closure(BreadcrumbBuilder): BreadcrumbBuilder) $callback * @return $this */ @@ -58,7 +60,7 @@ public function withSharpBreadcrumb(Closure $callback): static } /** - * @deprecated Use $this->>sharpShow()->delete() instead + * @deprecated Use $this->sharpShow()->delete() instead */ public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanceId) { @@ -102,6 +104,9 @@ public function deleteFromSharpList(string $entityClassNameOrKey, mixed $instanc ); } + /** + * @deprecated Use $this->sharpForm()->get() instead + */ public function getSharpForm(string $entityClassNameOrKey, mixed $instanceId = null) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); @@ -129,6 +134,9 @@ public function getSharpForm(string $entityClassNameOrKey, mixed $instanceId = n ); } + /** + * @deprecated Use $this->sharpForm()->get() instead + */ public function getSharpSingleForm(string $entityClassNameOrKey) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); @@ -147,6 +155,9 @@ public function getSharpSingleForm(string $entityClassNameOrKey) ); } + /** + * @deprecated Use $this->sharpForm()->update() instead + */ public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array $data) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); @@ -166,6 +177,9 @@ public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array ); } + /** + * @deprecated Use $this->sharpForm()->update() instead + */ public function updateSharpSingleForm(string $entityClassNameOrKey, array $data) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); @@ -184,6 +198,9 @@ public function updateSharpSingleForm(string $entityClassNameOrKey, array $data) ); } + /** + * @deprecated Use $this->sharpShow()->get() instead + */ public function getSharpShow(string $entityClassNameOrKey, $instanceId) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); @@ -203,6 +220,9 @@ public function getSharpShow(string $entityClassNameOrKey, $instanceId) ); } + /** + * @deprecated Use $this->sharpForm()->store() instead + */ public function storeSharpForm(string $entityClassNameOrKey, array $data) { $entityKey = $this->resolveEntityKey($entityClassNameOrKey); @@ -222,6 +242,9 @@ public function storeSharpForm(string $entityClassNameOrKey, array $data) ); } + /** + * @deprecated Use $this->sharpList()->instanceCommand()->post() + */ public function callSharpInstanceCommandFromList( string $entityClassNameOrKey, $instanceId, @@ -253,6 +276,9 @@ public function callSharpInstanceCommandFromList( ); } + /** + * @deprecated Use $this->sharpShow()->instanceCommand()->post() + */ public function callSharpInstanceCommandFromShow( string $entityClassNameOrKey, $instanceId, @@ -284,6 +310,9 @@ public function callSharpInstanceCommandFromShow( ); } + /** + * @deprecated Use $this->sharpList()->entityCommand()->post() + */ public function callSharpEntityCommandFromList( string $entityClassNameOrKey, string $commandKeyOrClassName, diff --git a/src/Utils/Testing/Show/PendingShow.php b/src/Utils/Testing/Show/PendingShow.php index 5d67aa517..4d72005da 100644 --- a/src/Utils/Testing/Show/PendingShow.php +++ b/src/Utils/Testing/Show/PendingShow.php @@ -14,6 +14,7 @@ use Code16\Sharp\Utils\Testing\IsPendingComponent; use Code16\Sharp\Utils\Testing\SharpAssertions; use Illuminate\Foundation\Testing\TestCase; +use Illuminate\Testing\TestResponse; class PendingShow { @@ -37,9 +38,9 @@ public function __construct( ); } - public function sharpForm(string $entityClassNameOrKey): PendingForm + public function sharpForm(string $entityClassNameOrKey, string|int|null $instanceId = null): PendingForm { - return new PendingForm($this->test, $entityClassNameOrKey, $this->instanceId, parent: $this); + return new PendingForm($this->test, $entityClassNameOrKey, $instanceId ?: $this->instanceId, parent: $this); } public function sharpListField(string $entityClassNameOrKey): PendingEntityList @@ -69,6 +70,21 @@ public function get(): AssertableShow ); } + public function delete(): TestResponse + { + return $this->test + ->delete( + route('code16.sharp.show.delete', [ + 'parentUri' => $this->getParentUri(), + 'entityKey' => $this->entityKey, + 'instanceId' => $this->instanceId, + ]), + headers: [ + SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(), + ] + ); + } + public function instanceCommand(string $commandKeyOrClassName): PendingCommand { $commandKey = class_exists($commandKeyOrClassName) diff --git a/tests/Http/SharpAssertionsHttpTest.php b/tests/Http/SharpAssertionsHttpTest.php index 7fa9cb154..2a423c52d 100644 --- a/tests/Http/SharpAssertionsHttpTest.php +++ b/tests/Http/SharpAssertionsHttpTest.php @@ -405,6 +405,28 @@ public function execute($instanceId, array $data = []): array ->assertReturnsInfo('instance 1'); }); +it('delete an entity list instance', function () { + $deletedInstanceId = null; + + fakeListFor(PersonEntity::class, new class($deletedInstanceId) extends PersonList + { + public function __construct( + public &$deletedInstanceId + ) {} + + public function delete(mixed $id): void + { + $this->deletedInstanceId = $id; + } + }); + + $this->sharpList(PersonEntity::class) + ->delete(1) + ->assertOk(); + + expect($deletedInstanceId)->toEqual(1); +}); + test('get show', function () { fakeShowFor(PersonEntity::class, new class() extends PersonShow { @@ -516,6 +538,28 @@ public function getListData(): array expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1/s-show/person/2'); }); +test('delete show', function () { + $deletedInstanceId = null; + + fakeShowFor(PersonEntity::class, new class($deletedInstanceId) extends PersonShow + { + public function __construct( + public &$deletedInstanceId + ) {} + + public function delete($id): void + { + $this->deletedInstanceId = $id; + } + }); + + $this->sharpShow(PersonEntity::class, 1) + ->delete() + ->assertRedirect(); + + expect($deletedInstanceId)->toEqual(1); +}); + test('create & store form', function () { $postedData = [];