11# Testing
22
3+ ::: tip INFO
4+ This page documents the new Testing API. If you use the legacy one, please refer to [ Testing (legacy)] ( ../legacy/testing.md ) .
5+ :::
6+
37Sharp provides a fluent testing API to help you test your Sharp code. These assertions and helpers are designed to be used in Feature tests.
48
59## The ` SharpAssertions ` trait
@@ -24,7 +28,7 @@ use Code16\Sharp\Utils\Testing\SharpAssertions;
2428
2529pest()
2630 ->extend(\Tests\TestCase::class)
27- ->use(SharpAssertions::class)
31+ ->use(SharpAssertions::class);
2832```
2933
3034## Authentication
@@ -57,8 +61,11 @@ Starts a fluent interaction with an Entity List.
5761$this->sharpList(Post::class)
5862 ->get()
5963 ->assertOk()
60- ->assertListCount(3)
61- ->assertListContains(['title' => 'My first post']);
64+ ->assertListData(fn (AssertableJson $data) => $data
65+ ->count(3)
66+ ->has('0.title', 'My first post')
67+ ->etc()
68+ );
6269```
6370
6471### Filtering the list
@@ -85,11 +92,15 @@ $this->sharpList(Post::class)
8592```
8693
8794If the command has a form, you can test it:
88-
95+
8996``` php
9097$this->sharpList(Post::class)
9198 ->entityCommand(ExportPosts::class)
9299 ->getForm()
100+ ->assertFormData(fn (AssertableJson $data) => $data
101+ ->where('format', 'xls')
102+ ->etc()
103+ )
93104 ->post(['format' => 'csv'])
94105 ->assertOk();
95106```
@@ -117,6 +128,10 @@ $this->sharpList(Post::class)
117128 ->post(['step1_data' => 'value'])
118129 ->assertReturnsStep('step2')
119130 ->getNextStepForm()
131+ ->assertFormData(fn (AssertableJson $data) => $data
132+ ->where('step2_field', 'default')
133+ ->etc()
134+ )
120135 ->post(['step2_data' => 'value'])
121136 ->assertOk();
122137```
@@ -133,10 +148,11 @@ Starts a fluent interaction with a Show Page.
133148$this->sharpShow(Post::class, 1)
134149 ->get()
135150 ->assertOk()
136- ->assertShowData([
137- 'title' => 'My first post',
138- 'author' => 'John Doe'
139- ]);
151+ ->assertShowData(fn (AssertableJson $data) => $data
152+ ->where('title', 'My first post')
153+ ->where('author', 'John Doe')
154+ ->etc()
155+ );
140156```
141157
142158### Instance Commands from Show
@@ -148,6 +164,43 @@ $this->sharpShow(Post::class, 1)
148164 ->assertOk();
149165```
150166
167+ ### List & dashboard fields
168+
169+ Show Pages can contain embedded Entity Lists or Dashboards. You can test them using ` sharpListField() ` and ` sharpDashboardField() ` .
170+
171+ #### ` sharpListField(string $entityKey) `
172+
173+ ``` php
174+ $this->sharpShow(Post::class, 1)
175+ ->sharpListField(Comment::class)
176+ ->get()
177+ ->assertOk()
178+ ->assertListData(fn (AssertableJson $data) => $data
179+ ->count(5)
180+ );
181+ ```
182+
183+ #### ` sharpDashboardField(string $entityKey) `
184+
185+ ``` php
186+ $this->sharpShow(User::class, 1)
187+ ->sharpDashboardField(UserStatsDashboard::class)
188+ ->get()
189+ ->assertOk();
190+ ```
191+
192+ ### Nested shows
193+
194+ There are some cases where you have nested shows by navigating through Show List fields. You can chain ` sharpShow() ` calls to simulate the correct breadcrumb :
195+
196+ ``` php
197+ $this->sharpList(Post::class)
198+ ->sharpShow(Post::class, 1)
199+ ->sharpListField(Comment::class)
200+ ->sharpShow(Comment::class, 1)
201+ ->assertOk();
202+ ```
203+
151204## Testing Forms
152205
153206Use ` sharpForm() ` to test your Forms.
@@ -180,7 +233,10 @@ If you want to test that the form displays correctly:
180233$this->sharpForm(Post::class, 1)
181234 ->edit()
182235 ->assertOk()
183- ->assertFormData(['title' => 'Existing Post']);
236+ ->assertFormData(fn (AssertableJson $data) => $data
237+ ->where('title', 'Existing Post')
238+ ->etc()
239+ );
184240```
185241
186242From an ` AssertableForm ` (the result of ` edit() ` or ` create() ` ), you can also call ` update() ` or ` store() ` :
@@ -223,26 +279,3 @@ $this->sharpDashboard(MyDashboard::class)
223279 ->post()
224280 ->assertOk();
225281```
226-
227- ## Testing Embedded Components
228-
229- Show Pages can contain embedded Entity Lists or Dashboards. You can test them using ` sharpListField() ` and ` sharpDashboardField() ` .
230-
231- ### ` sharpListField(string $entityKey) `
232-
233- ``` php
234- $this->sharpShow(Post::class, 1)
235- ->sharpListField(Comment::class)
236- ->get()
237- ->assertOk()
238- ->assertListCount(5);
239- ```
240-
241- ### ` sharpDashboardField(string $entityKey) `
242-
243- ``` php
244- $this->sharpShow(User::class, 1)
245- ->sharpDashboardField(UserStatsDashboard::class)
246- ->get()
247- ->assertOk();
248- ```
0 commit comments