Skip to content

Commit d2febe0

Browse files
committed
wip
1 parent 43066c2 commit d2febe0

9 files changed

Lines changed: 225 additions & 124 deletions

File tree

docs/guide/testing.md

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@ Sharp provides a fluent testing API to help you test your Sharp code. These asse
44

55
## The `SharpAssertions` trait
66

7-
To use Sharp's testing helpers, include the `Code16\Sharp\Utils\Testing\SharpAssertions` trait in your test class:
7+
To use Sharp's testing helpers, include the `Code16\Sharp\Utils\Testing\SharpAssertions` trait in your TestCase class:
88

99
```php
1010
use Code16\Sharp\Utils\Testing\SharpAssertions;
1111

12-
class PostFormTest extends TestCase
12+
abstract class TestCase extends BaseTestCase
1313
{
1414
use SharpAssertions;
1515

1616
// ...
1717
}
1818
```
1919

20+
or in `Pest.php`:
21+
22+
```php
23+
use Code16\Sharp\Utils\Testing\SharpAssertions;
24+
25+
pest()
26+
->extend(\Tests\TestCase::class)
27+
->use(SharpAssertions::class)
28+
```
29+
2030
## Authentication
2131

2232
### `loginAsSharpUser($user)`
@@ -68,31 +78,46 @@ You can call an Entity Command directly from the list:
6878

6979
```php
7080
$this->sharpList(Post::class)
71-
->callEntityCommand(ExportPosts::class, ['format' => 'csv'])
81+
->entityCommand(ExportPosts::class)
82+
->post()
7283
->assertOk()
7384
->assertReturnsDownload('posts.csv');
7485
```
7586

87+
If the command has a form, you can test it:
88+
89+
```php
90+
$this->sharpList(Post::class)
91+
->entityCommand(ExportPosts::class)
92+
->getForm()
93+
->post(['format' => 'csv'])
94+
->assertOk();
95+
```
96+
7697
### Instance Commands
7798

7899
Similarly, you can call an Instance Command:
79100

80101
```php
81102
$this->sharpList(Post::class)
82-
->callInstanceCommand(1, PublishPost::class)
103+
->instanceCommand(PublishPost::class, 1)
104+
->post()
83105
->assertOk()
84106
->assertReturnsReload();
85107
```
86108

87109
### Multi-step Commands (Wizards)
88110

89-
For commands that have multiple steps, you can use `callNextStep()`:
111+
For commands that have multiple steps, you can use `getNextStepForm()`:
90112

91113
```php
92114
$this->sharpList(Post::class)
93-
->callEntityCommand(MyWizardCommand::class, ['step1_data' => 'value'])
115+
->entityCommand(MyWizardCommand::class)
116+
->getForm()
117+
->post(['step1_data' => 'value'])
94118
->assertReturnsStep('step2')
95-
->callNextStep(['step2_data' => 'value'])
119+
->getNextStepForm()
120+
->post(['step2_data' => 'value'])
96121
->assertOk();
97122
```
98123

@@ -118,7 +143,8 @@ $this->sharpShow(Post::class, 1)
118143

119144
```php
120145
$this->sharpShow(Post::class, 1)
121-
->callInstanceCommand(PublishPost::class)
146+
->instanceCommand(PublishPost::class)
147+
->post()
122148
->assertOk();
123149
```
124150

@@ -193,7 +219,8 @@ $this->sharpDashboard(MyDashboard::class)
193219

194220
```php
195221
$this->sharpDashboard(MyDashboard::class)
196-
->callDashboardCommand(RefreshStats::class)
222+
->dashboardCommand(RefreshStats::class)
223+
->post()
197224
->assertOk();
198225
```
199226

@@ -219,22 +246,3 @@ $this->sharpShow(User::class, 1)
219246
->get()
220247
->assertOk();
221248
```
222-
223-
## Advanced: Breadcrumbs and Context
224-
225-
Most of the time, Sharp handles the breadcrumb automatically. However, you might need to simulate a specific breadcrumb context.
226-
227-
### `withSharpBreadcrumb(Closure $callback)`
228-
229-
```php
230-
use Code16\Sharp\Utils\Links\BreadcrumbBuilder;
231-
232-
$this->withSharpBreadcrumb(function (BreadcrumbBuilder $builder) {
233-
return $builder
234-
->appendEntityList(Category::class)
235-
->appendShowPage(Category::class, 1);
236-
})
237-
->sharpShow(Post::class, 1)
238-
->get()
239-
->assertOk();
240-
```

src/EntityList/Traits/HandleEntityCommands.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ protected function appendEntityCommandsToConfig(array &$config): void
4545

4646
final public function getEntityCommandsHandlers(): Collection
4747
{
48-
ray($this->entityCommandHandlers);
49-
ray()->trace();
5048
if ($this->entityCommandHandlers === null) {
5149
$groupIndex = 0;
5250
$this->entityCommandHandlers = collect($this->getEntityCommands())
@@ -75,7 +73,6 @@ final public function getEntityCommandsHandlers(): Collection
7573
$commandHandler->setCommandKey($commandKey);
7674
}
7775

78-
// ray($this->queryParams ?? null, $commandHandler)->trace();
7976
if (isset($this->queryParams)) {
8077
// We have to init query params of the command
8178
$commandHandler->initQueryParams($this->queryParams);

src/Utils/Testing/Commands/AssertableCommandForm.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public function __construct(
2626

2727
public function post(array $data = []): AssertableCommand
2828
{
29-
ray($this->formData());
30-
3129
return new AssertableCommand(
3230
postCommand: fn ($data, $step) => ($this->post)($data, $step, $this->formData()),
3331
getForm: $this->getForm,

src/Utils/Testing/Dashboard/PendingDashboard.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function dashboardCommand(string $commandKeyOrClassName): PendingCommand
8282
route(
8383
'code16.sharp.api.dashboard.command.form',
8484
[
85-
'entityKey' => $this->entityKey,
85+
'dashboardKey' => $this->entityKey,
8686
'commandKey' => $commandKey,
8787
'command_step' => $step,
8888
...$this->dashboardQueryParams(),
@@ -97,7 +97,7 @@ public function dashboardCommand(string $commandKeyOrClassName): PendingCommand
9797
->postJson(
9898
route(
9999
'code16.sharp.api.dashboard.command',
100-
['entityKey' => $this->entityKey, 'commandKey' => $commandKey]
100+
['dashboardKey' => $this->entityKey, 'commandKey' => $commandKey]
101101
),
102102
[
103103
'data' => $this->formatDataForCommand(

tests/Fixtures/Entities/DashboardEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setShow(?SharpDashboard $show): self
2323

2424
protected function getView(): SharpDashboard
2525
{
26-
return $this->fakeView ?? parent::getView();
26+
return isset($this->fakeView) ? clone $this->fakeView : parent::getView();
2727
}
2828

2929
public function setPolicy(SharpEntityPolicy $policy): self

tests/Fixtures/Entities/PersonEntity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ public function setForm(?SharpForm $form): self
5757

5858
public function getForm(): ?SharpForm
5959
{
60-
return $this->fakeForm ?? parent::getForm();
60+
return isset($this->fakeForm) ? clone $this->fakeForm : parent::getForm();
6161
}
6262

6363
protected function getShow(): ?SharpShow
6464
{
65-
return $this->fakeShow ?? parent::getShow();
65+
return isset($this->fakeShow) ? clone $this->fakeShow : parent::getShow();
6666
}
6767

6868
protected function getList(): ?SharpEntityList
6969
{
70-
return $this->fakeList ?? parent::getList();
70+
return isset($this->fakeList) ? clone $this->fakeList : parent::getList();
7171
}
7272

7373
public function setValidator(string $validatorClass, ?string $subentity = null): self

tests/Http/Api/ApiEntityListControllerTest.php

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,27 @@
2020
});
2121

2222
it('allows to reorder instances', function () {
23-
$this->withoutExceptionHandling();
23+
$ids = null;
2424

25-
$list = new class() extends PersonList
25+
fakeListFor('person', new class($ids) extends PersonList
2626
{
27-
public array $reorderedInstances = [];
27+
public function __construct(public &$ids) {}
2828

2929
public function buildListConfig(): void
3030
{
3131
$this->configureReorderable(
32-
new class($this->reorderedInstances) implements ReorderHandler
32+
new class($this->ids) implements ReorderHandler
3333
{
34-
public function __construct(public array &$reorderedInstances) {}
34+
public function __construct(public &$ids) {}
3535

3636
public function reorder(array $ids): void
3737
{
38-
$this->reorderedInstances = $ids;
38+
$this->ids = $ids;
3939
}
4040
}
4141
);
4242
}
43-
};
44-
45-
fakeListFor('person', $list);
43+
});
4644

4745
$this
4846
->postJson(
@@ -51,52 +49,45 @@ public function reorder(array $ids): void
5149
)
5250
->assertOk();
5351

54-
expect($list->reorderedInstances)->toEqual([3, 2, 1]);
52+
expect($ids)->toEqual([3, 2, 1]);
5553
});
5654

5755
it('allows to delete an instance in the entity list if delete method is implemented', function () {
58-
$list = new class() extends PersonList
56+
$deletedId = null;
57+
58+
fakeListFor('person', new class($deletedId) extends PersonList
5959
{
60-
public ?int $deletedInstance = null;
60+
public function __construct(public &$deletedId) {}
6161

6262
public function delete($id): void
6363
{
64-
$this->deletedInstance = $id;
64+
$this->deletedId = $id;
6565
}
66-
};
67-
68-
fakeListFor('person', $list);
69-
70-
$idToDelete = rand(1, 10);
66+
});
7167

72-
$this->deleteJson(route('code16.sharp.api.list.delete', ['root', 'person', $idToDelete]))
68+
$this->deleteJson(route('code16.sharp.api.list.delete', ['root', 'person', 1]))
7369
->assertOk();
7470

75-
expect($list->deletedInstance)->toEqual($idToDelete);
71+
expect($deletedId)->toEqual(1);
7672
});
7773

7874
it('delegates deletion to the show page if it exists', function () {
79-
$this->withoutExceptionHandling();
80-
81-
fakeListFor('person', new PersonList());
75+
$deletedId = null;
8276

83-
$show = new class() extends PersonShow
77+
fakeShowFor('person', new class($deletedId) extends PersonShow
8478
{
85-
public ?int $deletedInstance = null;
79+
public function __construct(public &$deletedId) {}
8680

8781
public function delete($id): void
8882
{
89-
$this->deletedInstance = $id;
83+
$this->deletedId = $id;
9084
}
91-
};
92-
fakeShowFor('person', $show);
93-
94-
$idToDelete = rand(1, 10);
85+
});
9586

96-
$this->deleteJson(route('code16.sharp.api.list.delete', ['root', 'person', $idToDelete]))
87+
$this->deleteJson(route('code16.sharp.api.list.delete', ['root', 'person', 1]))
9788
->assertOk();
9889

99-
expect($show->deletedInstance)->toEqual($idToDelete);
90+
expect($deletedId)->toEqual(1);
10091
});
10192

10293
it('checks if the entity list allows deletion', function () {

0 commit comments

Comments
 (0)