Skip to content

Commit 44f5193

Browse files
committed
feature #1832 [LiveComponent] add support for testing file uploads in 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
2 parents c9f55bd + cfa1e6f commit 44f5193

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/LiveComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.18.0
4+
5+
- Add parameter to `TestLiveComponent::call()` to add files to the request
6+
37
## 2.17.0
48

59
- Add `modifier` option in `LiveProp` so options can be modified at runtime.

src/LiveComponent/doc/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3657,6 +3657,10 @@ uses Symfony's test client to render and make requests to your components::
36573657

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

3660+
// call live action with file uploads
3661+
$testComponent
3662+
->save('processUpload', files: ['file' => new UploadedFile(...)]);
3663+
36603664
// emit live events
36613665
$testComponent
36623666
->emit('increaseEvent')

src/LiveComponent/src/Test/TestLiveComponent.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Test;
1313

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

7879
/**
79-
* @param array<string,mixed> $arguments
80+
* @param array<string,mixed> $arguments
81+
* @param array<string, UploadedFile> $files
8082
*/
81-
public function call(string $action, array $arguments = []): self
83+
public function call(string $action, array $arguments = [], array $files = []): self
8284
{
83-
return $this->request(['args' => $arguments], $action);
85+
return $this->request(['args' => $arguments], $action, $files);
8486
}
8587

8688
/**
@@ -123,7 +125,7 @@ public function response(): Response
123125
return $this->client()->getResponse();
124126
}
125127

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

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

0 commit comments

Comments
 (0)