Skip to content

Commit d42cb8c

Browse files
authored
Merge pull request #741 from code16/deprecated-legacy-assertions
[10.x] Deprecate legacy testing API
2 parents e3f02f3 + 5dca790 commit d42cb8c

7 files changed

Lines changed: 176 additions & 5 deletions

File tree

docs/guide/testing-legacy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Testing with Sharp (legacy API)
22

33
::: warning
4-
This page documents the old Testing API, we recommend using the new [Testing API](/guide/testing).
4+
This page documents the old Testing API, it is deprecated in 10.x. We recommend using the new [Testing API](/guide/testing).
55
:::
66

77
Sharp provides a few assertions and helpers to help you test your Sharp code.

docs/guide/testing.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ $this->sharpList(Post::class)
136136
->assertOk();
137137
```
138138

139+
### Delete an instance
140+
141+
To test the deletion of an instance, you can use `delete()`;
142+
143+
```php
144+
$this->sharpList(Post::class)
145+
->delete(1)
146+
->assertOk();
147+
```
148+
139149
## Testing Show Pages
140150

141151
Use `sharpShow()` to test your Show Pages.
@@ -202,6 +212,16 @@ $this->sharpList(Post::class)
202212
->assertOk();
203213
```
204214

215+
### Delete the instance
216+
217+
To test the deletion of the show instance, you can use `delete()`;
218+
219+
```php
220+
$this->sharpShow(Post::class, 1)
221+
->delete()
222+
->assertRedirect();
223+
```
224+
205225
## Testing Forms
206226

207227
Use `sharpForm()` to test your Forms.

docs/guide/upgrade.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,53 @@ If you depend on them, you should update the following dependencies in your `com
1919

2020
### Forms
2121

22-
#### Upload `setImageCompactThumbnail()` has no impact and can be removed
22+
Upload `setImageCompactThumbnail()` has no impact and can be removed:
2323
```php
2424
\Code16\Sharp\Form\Fields\SharpFormUploadField::make('upload')
2525
->setImageCompactThumbnail() // [!code --]
2626
```
2727

28+
### Testing
29+
The legacy testing API is deprecated and will be removed in 11.x. Please refer to (cf. [Testing](testing)). Here are replacement examples:
30+
31+
```php
32+
uses(\Code16\Sharp\Utils\Testing\SharpAssertions::class)
33+
34+
it('test', function () {
35+
$this->callSharpEntityCommandFromList(PersonEntity::class, MyCommand::class, ['attr' => 'some_value']) // [!code --]
36+
$this->sharpList(PersonEntity::class)->entityCommand(MyCommand::class)->post(['attr' => 'some_value']) // [!code ++]
37+
38+
$this->callSharpInstanceCommandFromList(PersonEntity::class, 1, MyCommand::class, ['attr' => 'some_value']) // [!code --]
39+
$this->sharpList(PersonEntity::class)->instanceCommand(MyCommand::class, 1)->post(['attr' => 'some_value']) // [!code ++]
40+
41+
$this->callSharpInstanceCommandFromShow(PersonEntity::class, 1, MyCommand::class, ['attr' => 'some_value']) // [!code --]
42+
$this->sharpShow(PersonEntity::class, 1)->instanceCommand(MyCommand::class)->post(['attr' => 'some_value']) // [!code ++]
43+
44+
$this->getSharpShow(PersonEntity::class, 1) // [!code --]
45+
$this->sharpShow(PersonEntity::class, 1)->get() // [!code ++]
46+
47+
$this->getSharpForm(PersonEntity::class, 1) // [!code --]
48+
$this->sharpForm(PersonEntity::class, 1)->get() // [!code ++]
49+
50+
$this->getSingleSharpForm(PersonEntity::class) // [!code --]
51+
$this->sharpForm(PersonEntity::class)->get() // [!code ++]
52+
53+
$this->updateSharpForm(PersonEntity::class, 1, []) // [!code --]
54+
$this->sharpForm(PersonEntity::class, 1)->update([]) // [!code ++]
55+
56+
$this->storeSharpForm(PersonEntity::class, 1, []) // [!code --]
57+
$this->sharpForm(PersonEntity::class, 1)->store([]) // [!code ++]
58+
59+
$this->updateSingleSharpForm(PersonEntity::class, []) // [!code --]
60+
$this->sharpForm(PersonEntity::class)->update([]) // [!code ++]
61+
62+
$this->withSharpBreadcrumb(function (\Code16\Sharp\Utils\Links\BreadcrumbBuilder $builder) { // [!code --]
63+
$builder->appendEntityList(PersonEntity::class)->appendShowPage(PersonEntity::class, 1) // [!code --]
64+
})->getSharpForm(PersonEntity::class, 1) // [!code --]
65+
$this->sharpList(PersonEntity::class)->sharpShow(PersonEntity::class, 1)->sharpForm(PersonEntity::class)->get() // [!code ++]
66+
})
67+
```
68+
2869
## Removed classes and methods
2970

3071
Several features and methods that were deprecated in Sharp 9.0 have been removed to clean up the codebase.

src/Utils/Testing/EntityList/PendingEntityList.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Code16\Sharp\Utils\Testing\SharpAssertions;
1515
use Code16\Sharp\Utils\Testing\Show\PendingShow;
1616
use Illuminate\Foundation\Testing\TestCase;
17+
use Illuminate\Testing\TestResponse;
1718
use PHPUnit\Framework\Assert as PHPUnit;
1819

1920
class PendingEntityList
@@ -85,6 +86,20 @@ public function get(): AssertableEntityList
8586
);
8687
}
8788

89+
public function delete(int|string $instanceId): TestResponse
90+
{
91+
return $this->test
92+
->delete(
93+
route('code16.sharp.api.list.delete', [
94+
'entityKey' => $this->entityKey,
95+
'instanceId' => $instanceId,
96+
]),
97+
headers: [
98+
SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(),
99+
]
100+
);
101+
}
102+
88103
public function entityCommand(string $commandKeyOrClassName): PendingCommand
89104
{
90105
$commandKey = class_exists($commandKeyOrClassName)

src/Utils/Testing/SharpAssertions.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public function sharpDashboard(string $entityClassNameOrKey): PendingDashboard
4747
}
4848

4949
/**
50-
* @param (\Closure(BreadcrumbBuilder): BreadcrumbBuilder) $callback
50+
* @deprecated Chain $this->sharpList()->sharpShow()->sharpForm() instead
51+
*
52+
* @param (Closure(BreadcrumbBuilder): BreadcrumbBuilder) $callback
5153
* @return $this
5254
*/
5355
public function withSharpBreadcrumb(Closure $callback): static
@@ -57,6 +59,9 @@ public function withSharpBreadcrumb(Closure $callback): static
5759
return $this;
5860
}
5961

62+
/**
63+
* @deprecated Use $this->sharpShow()->delete() instead
64+
*/
6065
public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanceId)
6166
{
6267
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -75,6 +80,9 @@ public function deleteFromSharpShow(string $entityClassNameOrKey, mixed $instanc
7580
);
7681
}
7782

83+
/**
84+
* @deprecated Use $this->sharpList()->delete() instead
85+
*/
7886
public function deleteFromSharpList(string $entityClassNameOrKey, mixed $instanceId)
7987
{
8088
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -96,6 +104,9 @@ public function deleteFromSharpList(string $entityClassNameOrKey, mixed $instanc
96104
);
97105
}
98106

107+
/**
108+
* @deprecated Use $this->sharpForm()->get() instead
109+
*/
99110
public function getSharpForm(string $entityClassNameOrKey, mixed $instanceId = null)
100111
{
101112
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -123,6 +134,9 @@ public function getSharpForm(string $entityClassNameOrKey, mixed $instanceId = n
123134
);
124135
}
125136

137+
/**
138+
* @deprecated Use $this->sharpForm()->get() instead
139+
*/
126140
public function getSharpSingleForm(string $entityClassNameOrKey)
127141
{
128142
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -141,6 +155,9 @@ public function getSharpSingleForm(string $entityClassNameOrKey)
141155
);
142156
}
143157

158+
/**
159+
* @deprecated Use $this->sharpForm()->update() instead
160+
*/
144161
public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array $data)
145162
{
146163
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -160,6 +177,9 @@ public function updateSharpForm(string $entityClassNameOrKey, $instanceId, array
160177
);
161178
}
162179

180+
/**
181+
* @deprecated Use $this->sharpForm()->update() instead
182+
*/
163183
public function updateSharpSingleForm(string $entityClassNameOrKey, array $data)
164184
{
165185
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -178,6 +198,9 @@ public function updateSharpSingleForm(string $entityClassNameOrKey, array $data)
178198
);
179199
}
180200

201+
/**
202+
* @deprecated Use $this->sharpShow()->get() instead
203+
*/
181204
public function getSharpShow(string $entityClassNameOrKey, $instanceId)
182205
{
183206
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -197,6 +220,9 @@ public function getSharpShow(string $entityClassNameOrKey, $instanceId)
197220
);
198221
}
199222

223+
/**
224+
* @deprecated Use $this->sharpForm()->store() instead
225+
*/
200226
public function storeSharpForm(string $entityClassNameOrKey, array $data)
201227
{
202228
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
@@ -216,6 +242,9 @@ public function storeSharpForm(string $entityClassNameOrKey, array $data)
216242
);
217243
}
218244

245+
/**
246+
* @deprecated Use $this->sharpList()->instanceCommand()->post()
247+
*/
219248
public function callSharpInstanceCommandFromList(
220249
string $entityClassNameOrKey,
221250
$instanceId,
@@ -247,6 +276,9 @@ public function callSharpInstanceCommandFromList(
247276
);
248277
}
249278

279+
/**
280+
* @deprecated Use $this->sharpShow()->instanceCommand()->post()
281+
*/
250282
public function callSharpInstanceCommandFromShow(
251283
string $entityClassNameOrKey,
252284
$instanceId,
@@ -278,6 +310,9 @@ public function callSharpInstanceCommandFromShow(
278310
);
279311
}
280312

313+
/**
314+
* @deprecated Use $this->sharpList()->entityCommand()->post()
315+
*/
281316
public function callSharpEntityCommandFromList(
282317
string $entityClassNameOrKey,
283318
string $commandKeyOrClassName,

src/Utils/Testing/Show/PendingShow.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Code16\Sharp\Utils\Testing\IsPendingComponent;
1515
use Code16\Sharp\Utils\Testing\SharpAssertions;
1616
use Illuminate\Foundation\Testing\TestCase;
17+
use Illuminate\Testing\TestResponse;
1718

1819
class PendingShow
1920
{
@@ -37,9 +38,9 @@ public function __construct(
3738
);
3839
}
3940

40-
public function sharpForm(string $entityClassNameOrKey): PendingForm
41+
public function sharpForm(string $entityClassNameOrKey, string|int|null $instanceId = null): PendingForm
4142
{
42-
return new PendingForm($this->test, $entityClassNameOrKey, $this->instanceId, parent: $this);
43+
return new PendingForm($this->test, $entityClassNameOrKey, $instanceId ?: $this->instanceId, parent: $this);
4344
}
4445

4546
public function sharpListField(string $entityClassNameOrKey): PendingEntityList
@@ -69,6 +70,21 @@ public function get(): AssertableShow
6970
);
7071
}
7172

73+
public function delete(): TestResponse
74+
{
75+
return $this->test
76+
->delete(
77+
route('code16.sharp.show.delete', [
78+
'parentUri' => $this->getParentUri(),
79+
'entityKey' => $this->entityKey,
80+
'instanceId' => $this->instanceId,
81+
]),
82+
headers: [
83+
SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(),
84+
]
85+
);
86+
}
87+
7288
public function instanceCommand(string $commandKeyOrClassName): PendingCommand
7389
{
7490
$commandKey = class_exists($commandKeyOrClassName)

tests/Http/SharpAssertionsHttpTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,28 @@ public function execute($instanceId, array $data = []): array
405405
->assertReturnsInfo('instance 1');
406406
});
407407

408+
it('delete an entity list instance', function () {
409+
$deletedInstanceId = null;
410+
411+
fakeListFor(PersonEntity::class, new class($deletedInstanceId) extends PersonList
412+
{
413+
public function __construct(
414+
public &$deletedInstanceId
415+
) {}
416+
417+
public function delete(mixed $id): void
418+
{
419+
$this->deletedInstanceId = $id;
420+
}
421+
});
422+
423+
$this->sharpList(PersonEntity::class)
424+
->delete(1)
425+
->assertOk();
426+
427+
expect($deletedInstanceId)->toEqual(1);
428+
});
429+
408430
test('get show', function () {
409431
fakeShowFor(PersonEntity::class, new class() extends PersonShow
410432
{
@@ -516,6 +538,28 @@ public function getListData(): array
516538
expect(sharp()->context()->breadcrumb()->getCurrentPath())->toEqual('s-list/person/s-show/person/1/s-show/person/2');
517539
});
518540

541+
test('delete show', function () {
542+
$deletedInstanceId = null;
543+
544+
fakeShowFor(PersonEntity::class, new class($deletedInstanceId) extends PersonShow
545+
{
546+
public function __construct(
547+
public &$deletedInstanceId
548+
) {}
549+
550+
public function delete($id): void
551+
{
552+
$this->deletedInstanceId = $id;
553+
}
554+
});
555+
556+
$this->sharpShow(PersonEntity::class, 1)
557+
->delete()
558+
->assertRedirect();
559+
560+
expect($deletedInstanceId)->toEqual(1);
561+
});
562+
519563
test('create & store form', function () {
520564
$postedData = [];
521565

0 commit comments

Comments
 (0)