Skip to content

[LiveComponent] Add setRouteLocale in TestLiveComponent #2124

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
3 changes: 3 additions & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3711,6 +3711,9 @@ uses Symfony's test client to render and make requests to your components::
// authenticate a user ($user is instance of UserInterface)
$testComponent->actingAs($user);

// set the '_locale' route parameter (if the component route is localized)
$testComponent->setRouteLocale('fr');

// customize the test client
$client = self::getContainer()->get('test.client');

Expand Down
26 changes: 21 additions & 5 deletions src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class TestLiveComponent
{
private bool $performedInitialRequest = false;

private ?string $locale = null;

/**
* @internal
*/
Expand Down Expand Up @@ -132,6 +134,17 @@ public function submitForm(array $formValues, ?string $action = null): self
return $this->request(['updated' => $flattenValues, 'validatedFields' => array_keys($flattenValues)], $action);
}

/**
* @experimental
*/
public function setRouteLocale(string $locale): self
{
$this->performedInitialRequest = false;
$this->locale = $locale;

return $this;
}

private function request(array $content = [], ?string $action = null, array $files = []): self
{
$csrfToken = $this->csrfToken();
Expand All @@ -143,7 +156,8 @@ private function request(array $content = [], ?string $action = null, array $fil
array_filter([
'_live_component' => $this->metadata->getName(),
'_live_action' => $action,
])
'_locale' => $this->locale,
], static fn (mixed $v): bool => null !== $v),
),
parameters: ['data' => json_encode(array_merge($content, ['props' => $this->props()]))],
files: $files,
Expand Down Expand Up @@ -191,20 +205,22 @@ private function client(): KernelBrowser
if ('POST' === strtoupper($this->metadata->get('method'))) {
$this->client->request(
'POST',
$this->router->generate($this->metadata->get('route'), [
$this->router->generate($this->metadata->get('route'), array_filter([
'_live_component' => $this->metadata->getName(),
]),
'_locale' => $this->locale,
], static fn (mixed $v): bool => null !== $v)),
[
'data' => json_encode(['props' => $props->getProps()], flags: \JSON_THROW_ON_ERROR),
],
);
} else {
$this->client->request('GET', $this->router->generate(
$this->metadata->get('route'),
[
array_filter([
'_live_component' => $this->metadata->getName(),
'_locale' => $this->locale,
'props' => json_encode($props->getProps(), flags: \JSON_THROW_ON_ERROR),
]
], static fn (mixed $v): bool => null !== $v),
));
}

Expand Down
15 changes: 15 additions & 0 deletions src/LiveComponent/tests/Fixtures/Component/LocalizedRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\DefaultActionTrait;

/**
* @author Simon André
*/
#[AsLiveComponent('localized_route', route: 'localized_route')]
final class LocalizedRoute
{
use DefaultActionTrait;
}
1 change: 1 addition & 0 deletions src/LiveComponent/tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,6 @@ protected function configureRoutes(RoutingConfigurator $routes): void
$routes->add('render_namespaced_template', '/render-namespaced-template/{template}')->controller('kernel::renderNamespacedTemplate');
$routes->add('homepage', '/')->controller('kernel::index');
$routes->add('alternate_live_route', '/alt/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
$routes->add('localized_route', '/locale/{_locale}/{_live_component}/{_live_action}')->defaults(['_live_action' => 'get']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div{{ attributes }}>
Localized route <em>Locale: {{ app.request.locale }}</em>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,21 @@ public function testAccessAllLivePropsInsideOnUpdatedHook(): void

$this->assertStringContainsString('Total: 9', $testComponent->render());
}

public function testSetLocaleRenderLocalizedComponent(): void
{
$testComponent = $this->createLiveComponent('localized_route');
$testComponent->setRouteLocale('fr');
$this->assertStringContainsString('Locale: fr', $testComponent->render());

$testComponent->refresh();
$this->assertStringContainsString('Locale: fr', $testComponent->render());

$testComponent->setRouteLocale('es');
$this->assertStringContainsString('Locale: es', $testComponent->render());

$testComponent = $this->createLiveComponent('localized_route');
$testComponent->setRouteLocale('de');
$this->assertStringContainsString('Locale: de', $testComponent->render());
}
}
Loading