Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guide/testing-legacy.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
20 changes: 20 additions & 0 deletions docs/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
43 changes: 42 additions & 1 deletion docs/guide/upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/Testing/EntityList/PendingEntityList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 36 additions & 1 deletion src/Utils/Testing/SharpAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function sharpDashboard(string $entityClassNameOrKey): PendingDashboard
}

/**
* @param (\Closure(BreadcrumbBuilder): BreadcrumbBuilder) $callback
* @deprecated Chain $this->sharpList()->sharpShow()->sharpForm() instead
*
* @param (Closure(BreadcrumbBuilder): BreadcrumbBuilder) $callback
* @return $this
*/
public function withSharpBreadcrumb(Closure $callback): static
Expand All @@ -57,6 +59,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);
Expand All @@ -75,6 +80,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);
Expand All @@ -96,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);
Expand Down Expand Up @@ -123,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);
Expand All @@ -141,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);
Expand All @@ -160,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);
Expand All @@ -178,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);
Expand All @@ -197,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);
Expand All @@ -216,6 +242,9 @@ public function storeSharpForm(string $entityClassNameOrKey, array $data)
);
}

/**
* @deprecated Use $this->sharpList()->instanceCommand()->post()
*/
public function callSharpInstanceCommandFromList(
string $entityClassNameOrKey,
$instanceId,
Expand Down Expand Up @@ -247,6 +276,9 @@ public function callSharpInstanceCommandFromList(
);
}

/**
* @deprecated Use $this->sharpShow()->instanceCommand()->post()
*/
public function callSharpInstanceCommandFromShow(
string $entityClassNameOrKey,
$instanceId,
Expand Down Expand Up @@ -278,6 +310,9 @@ public function callSharpInstanceCommandFromShow(
);
}

/**
* @deprecated Use $this->sharpList()->entityCommand()->post()
*/
public function callSharpEntityCommandFromList(
string $entityClassNameOrKey,
string $commandKeyOrClassName,
Expand Down
20 changes: 18 additions & 2 deletions src/Utils/Testing/Show/PendingShow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions tests/Http/SharpAssertionsHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 = [];

Expand Down
Loading