Skip to content

Commit

Permalink
feature #1832 [LiveComponent] add support for testing file uploads in…
Browse files Browse the repository at this point in the history
… actions (daFish)

This PR was merged into the 2.x branch.

Discussion
----------

[LiveComponent] add support for testing file uploads in actions

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Issues        | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT

Commits
-------

cfa1e6f feat: add support for testing file uploads in actions
  • Loading branch information
kbond committed May 8, 2024
2 parents c9f55bd + cfa1e6f commit 44f5193
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/LiveComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.18.0

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

## 2.17.0

- Add `modifier` option in `LiveProp` so options can be modified at runtime.
Expand Down
4 changes: 4 additions & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3657,6 +3657,10 @@ uses Symfony's test client to render and make requests to your components::

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

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

// emit live events
$testComponent
->emit('increaseEvent')
Expand Down
11 changes: 7 additions & 4 deletions src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\LiveComponent\Test;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\User\UserInterface;
Expand Down Expand Up @@ -76,11 +77,12 @@ public function actingAs(object $user, string $firewallContext = 'main'): self
}

/**
* @param array<string,mixed> $arguments
* @param array<string,mixed> $arguments
* @param array<string, UploadedFile> $files
*/
public function call(string $action, array $arguments = []): self
public function call(string $action, array $arguments = [], array $files = []): self
{
return $this->request(['args' => $arguments], $action);
return $this->request(['args' => $arguments], $action, $files);
}

/**
Expand Down Expand Up @@ -123,7 +125,7 @@ public function response(): Response
return $this->client()->getResponse();
}

private function request(array $content = [], ?string $action = null): self
private function request(array $content = [], ?string $action = null, array $files = []): self
{
$csrfToken = $this->csrfToken();

Expand All @@ -137,6 +139,7 @@ private function request(array $content = [], ?string $action = null): self
])
),
parameters: ['data' => json_encode(array_merge($content, ['props' => $this->props()]))],
files: $files,
server: $csrfToken ? ['HTTP_X_CSRF_TOKEN' => $csrfToken] : [],
);

Expand Down

0 comments on commit 44f5193

Please sign in to comment.