Skip to content

[LiveComponent] add support for testing file uploads in actions #1832

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading