diff --git a/src/LiveComponent/CHANGELOG.md b/src/LiveComponent/CHANGELOG.md index 15e2c613ffb..1d30d5c4f19 100644 --- a/src/LiveComponent/CHANGELOG.md +++ b/src/LiveComponent/CHANGELOG.md @@ -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. diff --git a/src/LiveComponent/doc/index.rst b/src/LiveComponent/doc/index.rst index 0d6c04957d1..06e15144e9d 100644 --- a/src/LiveComponent/doc/index.rst +++ b/src/LiveComponent/doc/index.rst @@ -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') diff --git a/src/LiveComponent/src/Test/TestLiveComponent.php b/src/LiveComponent/src/Test/TestLiveComponent.php index ac9404a7203..98d9126ad15 100644 --- a/src/LiveComponent/src/Test/TestLiveComponent.php +++ b/src/LiveComponent/src/Test/TestLiveComponent.php @@ -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; @@ -76,11 +77,12 @@ public function actingAs(object $user, string $firewallContext = 'main'): self } /** - * @param array $arguments + * @param array $arguments + * @param array $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); } /** @@ -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(); @@ -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] : [], );