Skip to content

Commit f70775f

Browse files
committed
feature #2124 [LiveComponent] Add setRouteLocale in TestLiveComponent (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Add `setRouteLocale` in `TestLiveComponent` | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Issues | Fix #1565 | License | MIT The `live_component` route can be localized. It then requires the `{_locale}` the parameter to be defined. ```diff # config/routes/ux_live_component.yaml live_component: resource: '`@LiveComponentBundle`/config/routes.php' - prefix: /_components + prefix: /{_locale}/_components ``` This PR adds a `setRouteLocale` method in `TestLiveComponent` to help in this situation. ```php $testComponent = $this->createLiveComponent('Acme:Foo'); $testComponent->setRouteLocale('de'); // $testComponent is rendered with the 'de' locale ``` Thank you `@javiereguiluz` for the idea and your help :) Commits ------- 2f00e3f [LiveComponent] Add `setRouteLocale` in `TestLiveComponent`
2 parents eedeadc + 2f00e3f commit f70775f

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,6 +3718,9 @@ uses Symfony's test client to render and make requests to your components::
37183718
// authenticate a user ($user is instance of UserInterface)
37193719
$testComponent->actingAs($user);
37203720

3721+
// set the '_locale' route parameter (if the component route is localized)
3722+
$testComponent->setRouteLocale('fr');
3723+
37213724
// customize the test client
37223725
$client = self::getContainer()->get('test.client');
37233726

src/LiveComponent/src/Test/TestLiveComponent.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class TestLiveComponent
3131
{
3232
private bool $performedInitialRequest = false;
3333

34+
private ?string $locale = null;
35+
3436
/**
3537
* @internal
3638
*/
@@ -132,6 +134,17 @@ public function submitForm(array $formValues, ?string $action = null): self
132134
return $this->request(['updated' => $flattenValues, 'validatedFields' => array_keys($flattenValues)], $action);
133135
}
134136

137+
/**
138+
* @experimental
139+
*/
140+
public function setRouteLocale(string $locale): self
141+
{
142+
$this->performedInitialRequest = false;
143+
$this->locale = $locale;
144+
145+
return $this;
146+
}
147+
135148
private function request(array $content = [], ?string $action = null, array $files = []): self
136149
{
137150
$csrfToken = $this->csrfToken();
@@ -143,7 +156,8 @@ private function request(array $content = [], ?string $action = null, array $fil
143156
array_filter([
144157
'_live_component' => $this->metadata->getName(),
145158
'_live_action' => $action,
146-
])
159+
'_locale' => $this->locale,
160+
], static fn (mixed $v): bool => null !== $v),
147161
),
148162
parameters: ['data' => json_encode(array_merge($content, ['props' => $this->props()]))],
149163
files: $files,
@@ -191,20 +205,22 @@ private function client(): KernelBrowser
191205
if ('POST' === strtoupper($this->metadata->get('method'))) {
192206
$this->client->request(
193207
'POST',
194-
$this->router->generate($this->metadata->get('route'), [
208+
$this->router->generate($this->metadata->get('route'), array_filter([
195209
'_live_component' => $this->metadata->getName(),
196-
]),
210+
'_locale' => $this->locale,
211+
], static fn (mixed $v): bool => null !== $v)),
197212
[
198213
'data' => json_encode(['props' => $props->getProps()], flags: \JSON_THROW_ON_ERROR),
199214
],
200215
);
201216
} else {
202217
$this->client->request('GET', $this->router->generate(
203218
$this->metadata->get('route'),
204-
[
219+
array_filter([
205220
'_live_component' => $this->metadata->getName(),
221+
'_locale' => $this->locale,
206222
'props' => json_encode($props->getProps(), flags: \JSON_THROW_ON_ERROR),
207-
]
223+
], static fn (mixed $v): bool => null !== $v),
208224
));
209225
}
210226

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
4+
5+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
6+
use Symfony\UX\LiveComponent\DefaultActionTrait;
7+
8+
/**
9+
* @author Simon André
10+
*/
11+
#[AsLiveComponent('localized_route', route: 'localized_route')]
12+
final class LocalizedRoute
13+
{
14+
use DefaultActionTrait;
15+
}

src/LiveComponent/tests/Fixtures/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,6 @@ protected function configureRoutes(RoutingConfigurator $routes): void
214214
$routes->add('render_namespaced_template', '/render-namespaced-template/{template}')->controller('kernel::renderNamespacedTemplate');
215215
$routes->add('homepage', '/')->controller('kernel::index');
216216
$routes->add('alternate_live_route', '/alt/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
217+
$routes->add('localized_route', '/locale/{_locale}/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
217218
}
218219
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div{{ attributes }}>
2+
Localized route <em>Locale: {{ app.request.locale }}</em>
3+
</div>

src/LiveComponent/tests/Functional/Test/InteractsWithLiveComponentsTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,21 @@ public function testAccessAllLivePropsInsideOnUpdatedHook(): void
194194

195195
$this->assertStringContainsString('Total: 9', $testComponent->render());
196196
}
197+
198+
public function testSetLocaleRenderLocalizedComponent(): void
199+
{
200+
$testComponent = $this->createLiveComponent('localized_route');
201+
$testComponent->setRouteLocale('fr');
202+
$this->assertStringContainsString('Locale: fr', $testComponent->render());
203+
204+
$testComponent->refresh();
205+
$this->assertStringContainsString('Locale: fr', $testComponent->render());
206+
207+
$testComponent->setRouteLocale('es');
208+
$this->assertStringContainsString('Locale: es', $testComponent->render());
209+
210+
$testComponent = $this->createLiveComponent('localized_route');
211+
$testComponent->setRouteLocale('de');
212+
$this->assertStringContainsString('Locale: de', $testComponent->render());
213+
}
197214
}

0 commit comments

Comments
 (0)