Skip to content

Commit

Permalink
feature symfony#1992 [LiveComponent] Add a new helper to interact wit…
Browse files Browse the repository at this point in the history
…h forms in functional tests (yoye)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[LiveComponent] Add a new helper to interact with forms in functional tests

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| License       | MIT

Add a new method in `TestLiveComponent` to interact with live components that contains forms. Submitted data must be passed as an array like you'll do with `KernelBrowser`.

```php
$component = $this->createLiveComponent(name: Component::class);
$component->submitForm(['foo' => ['bar' => 'baz']]); // The method will change this values in ['foo.bar' => 'baz'] as expected by the component.
```

Commits
-------

b23eaee [LiveComponent] Add a new helper to interact with forms in functional tests
  • Loading branch information
kbond committed Jul 24, 2024
2 parents bffdae8 + b23eaee commit 2b35ecb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/LiveComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGELOG

- Add `submitForm()` to `TestLiveComponent`.

## 2.18.0

- Add parameter to `TestLiveComponent::call()` to add files to the request
Expand Down
6 changes: 5 additions & 1 deletion src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -3672,6 +3672,10 @@ uses Symfony's test client to render and make requests to your components::
->set('count', 99)
;

// Submit form data
$testComponent
->submitForm(['form' => ['input' => 'value']], 'save');

$this->assertStringContainsString('Count: 99', $testComponent->render());

// refresh the component
Expand Down
22 changes: 22 additions & 0 deletions src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.'.');
} else {
$result[$prefix.$key] = $value;
}
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,14 @@ public function testActingAs(): void

$this->assertStringContainsString('Username: kevin', $testComponent->render());
}

public function testCanSubmitForm(): void
{
$testComponent = $this->createLiveComponent('form_with_many_different_fields_type');

$response = $testComponent->submitForm(['form' => ['text' => 'foobar']])->response();

$this->assertSame(200, $response->getStatusCode());
$this->assertStringContainsString('foobar', $testComponent->render());
}
}

0 comments on commit 2b35ecb

Please sign in to comment.