-
-
Notifications
You must be signed in to change notification settings - Fork 364
[LiveComponent] Add a new helper to interact with forms in functional tests #1992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3659,7 +3659,7 @@ uses Symfony's test client to render and make requests to your components:: | |
|
||
// call live action with file uploads | ||
$testComponent | ||
->save('processUpload', files: ['file' => new UploadedFile(...)]); | ||
->call('processUpload', files: ['file' => new UploadedFile(...)]); | ||
|
||
// emit live events | ||
$testComponent | ||
|
@@ -3672,6 +3672,10 @@ uses Symfony's test client to render and make requests to your components:: | |
->set('count', 99) | ||
; | ||
|
||
// Submit form data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should create an entire block "Test LiveComponent with Forms" (or something like that), wdyt ? If "no", i think some comment to precise what is "form" and "save" here could avoid some missunderstandings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As Test helper is only one block, I didn't want to change that. But I understand that my array is a bit confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep as it is.. Who knows, mabe if you add other nice helpers... we will be forced to add a dedicated block ? :)) |
||
$testComponent | ||
->submitForm(['form' => ['input' => 'value']], 'save'); | ||
|
||
$this->assertStringContainsString('Count: 99', $testComponent->render()); | ||
|
||
// refresh the component | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,13 @@ public function response(): Response | |
return $this->client()->getResponse(); | ||
} | ||
|
||
public function submitForm(array $formValues, ?string $action = null): self | ||
{ | ||
$flattenValues = $this->flattenFormValues($formValues); | ||
|
||
return $this->request(['updated' => $flattenValues, 'validatedFields' => array_keys($flattenValues)], $action); | ||
} | ||
|
||
private function request(array $content = [], ?string $action = null, array $files = []): self | ||
{ | ||
$csrfToken = $this->csrfToken(); | ||
|
@@ -205,4 +212,19 @@ private function client(): KernelBrowser | |
|
||
return $this->client; | ||
} | ||
|
||
private function flattenFormValues(array $values, string $prefix = ''): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($values as $key => $value) { | ||
if (\is_array($value)) { | ||
$result += $this->flattenFormValues($value, $prefix.$key.'.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No difference array_is_list / map ? (geniune question) Does this work with fields accepting multiple values ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't know how to use Yes, I was a little undecided to not add a test for that method. At first I've thinked to add a public static method in But this will work. public function testFlatten(): void
{
$data = [
'foo' => ['bar' => 'baz'],
'collection' => ['one', 'two', 'three'],
'collection_with_sub' => [
'sub_one' => ['child_one', 'child_two'],
'sub_two' => ['child_three', 'key' => 'child_four'],
],
];
$this->assertSame([
'foo.bar' => 'baz',
'collection.0' => 'one',
'collection.1' => 'two',
'collection.2' => 'three',
'collection_with_sub.sub_one.0' => 'child_one',
'collection_with_sub.sub_one.1' => 'child_two',
'collection_with_sub.sub_two.0' => 'child_three',
'collection_with_sub.sub_two.key' => 'child_four',
], TestLiveComponent::flattenFormValues($data));
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good for me then, thank you for taking time to answer :) |
||
} else { | ||
$result[$prefix.$key] = $value; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well spotted!