Skip to content

Commit b23eaee

Browse files
yoyekbond
authored andcommitted
[LiveComponent] Add a new helper to interact with forms in functional tests
1 parent 2df113f commit b23eaee

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

src/LiveComponent/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# CHANGELOG
22

3+
- Add `submitForm()` to `TestLiveComponent`.
4+
35
## 2.18.0
46

57
- Add parameter to `TestLiveComponent::call()` to add files to the request

src/LiveComponent/doc/index.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3659,7 +3659,7 @@ uses Symfony's test client to render and make requests to your components::
36593659

36603660
// call live action with file uploads
36613661
$testComponent
3662-
->save('processUpload', files: ['file' => new UploadedFile(...)]);
3662+
->call('processUpload', files: ['file' => new UploadedFile(...)]);
36633663

36643664
// emit live events
36653665
$testComponent
@@ -3672,6 +3672,10 @@ uses Symfony's test client to render and make requests to your components::
36723672
->set('count', 99)
36733673
;
36743674

3675+
// Submit form data
3676+
$testComponent
3677+
->submitForm(['form' => ['input' => 'value']], 'save');
3678+
36753679
$this->assertStringContainsString('Count: 99', $testComponent->render());
36763680

36773681
// refresh the component

src/LiveComponent/src/Test/TestLiveComponent.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ public function response(): Response
125125
return $this->client()->getResponse();
126126
}
127127

128+
public function submitForm(array $formValues, ?string $action = null): self
129+
{
130+
$flattenValues = $this->flattenFormValues($formValues);
131+
132+
return $this->request(['updated' => $flattenValues, 'validatedFields' => array_keys($flattenValues)], $action);
133+
}
134+
128135
private function request(array $content = [], ?string $action = null, array $files = []): self
129136
{
130137
$csrfToken = $this->csrfToken();
@@ -205,4 +212,19 @@ private function client(): KernelBrowser
205212

206213
return $this->client;
207214
}
215+
216+
private function flattenFormValues(array $values, string $prefix = ''): array
217+
{
218+
$result = [];
219+
220+
foreach ($values as $key => $value) {
221+
if (\is_array($value)) {
222+
$result += $this->flattenFormValues($value, $prefix.$key.'.');
223+
} else {
224+
$result[$prefix.$key] = $value;
225+
}
226+
}
227+
228+
return $result;
229+
}
208230
}

src/LiveComponent/tests/Functional/Test/InteractsWithLiveComponentsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,14 @@ public function testActingAs(): void
169169

170170
$this->assertStringContainsString('Username: kevin', $testComponent->render());
171171
}
172+
173+
public function testCanSubmitForm(): void
174+
{
175+
$testComponent = $this->createLiveComponent('form_with_many_different_fields_type');
176+
177+
$response = $testComponent->submitForm(['form' => ['text' => 'foobar']])->response();
178+
179+
$this->assertSame(200, $response->getStatusCode());
180+
$this->assertStringContainsString('foobar', $testComponent->render());
181+
}
172182
}

0 commit comments

Comments
 (0)