Skip to content

Commit 2e08324

Browse files
committed
feature #1116 [Live] add TestLiveComponent::actingAs() (kbond)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Live] add `TestLiveComponent::actingAs()` | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | (slack discussion) | License | MIT ```php $this->createLiveComponent('my_component') ->actingAs($user) // UserInterface ->... ; ``` Additionally, the test client can be passed to `InteractsWithLiveComponents::createLiveComponent()`: ```php // customize the test client $client = self::getContainer()->get('test.client'); // do some stuff with the client (ie login user via form) $testComponent = $this->createLiveComponent( name: 'MyComponent', data: ['foo' => 'bar'], client: $client, ); ``` Commits ------- 3762fc4 [Live] add `TestLiveComponent::actingAs()`
2 parents c238896 + 3762fc4 commit 2e08324

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,6 +3123,20 @@ uses Symfony's test client to render and make requests to your components::
31233123
$response = $testComponent->call('redirect')->response(); // Symfony\Component\HttpFoundation\Response
31243124

31253125
$this->assertSame(302, $response->getStatusCode());
3126+
3127+
// authenticate a user ($user is instance of UserInterface)
3128+
$testComponent->actingAs($user);
3129+
3130+
// customize the test client
3131+
$client = self::getContainer()->get('test.client');
3132+
3133+
// do some stuff with the client (ie login user via form)
3134+
3135+
$testComponent = $this->createLiveComponent(
3136+
name: 'MyComponent',
3137+
data: ['foo' => 'bar'],
3138+
client: $client,
3139+
);
31263140
}
31273141
}
31283142

src/LiveComponent/src/Test/InteractsWithLiveComponents.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\LiveComponent\Test;
1313

14+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1415
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1516
use Symfony\UX\TwigComponent\ComponentFactory;
1617

@@ -19,7 +20,7 @@
1920
*/
2021
trait InteractsWithLiveComponents
2122
{
22-
protected function createLiveComponent(string $name, array $data = []): TestLiveComponent
23+
protected function createLiveComponent(string $name, array $data = [], KernelBrowser $client = null): TestLiveComponent
2324
{
2425
if (!$this instanceof KernelTestCase) {
2526
throw new \LogicException(sprintf('The "%s" trait can only be used on "%s" classes.', __TRAIT__, KernelTestCase::class));
@@ -37,7 +38,7 @@ protected function createLiveComponent(string $name, array $data = []): TestLive
3738
$metadata,
3839
$data,
3940
$factory,
40-
self::getContainer()->get('test.client'),
41+
$client ?? self::getContainer()->get('test.client'),
4142
self::getContainer()->get('ux.live_component.component_hydrator'),
4243
self::getContainer()->get('ux.live_component.metadata_factory'),
4344
self::getContainer()->get('router'),

src/LiveComponent/src/Test/TestLiveComponent.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17+
use Symfony\Component\Security\Core\User\UserInterface;
1718
use Symfony\UX\LiveComponent\LiveComponentHydrator;
1819
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
1920
use Symfony\UX\TwigComponent\ComponentFactory;
@@ -76,6 +77,16 @@ public function component(): object
7677
return (new MountedComponent($this->metadata->getName(), $component, $componentAttributes))->getComponent();
7778
}
7879

80+
/**
81+
* @param UserInterface $user
82+
*/
83+
public function actingAs(object $user, string $firewallContext = 'main'): self
84+
{
85+
$this->client->loginUser($user, $firewallContext);
86+
87+
return $this;
88+
}
89+
7990
/**
8091
* @param array<string,mixed> $arguments
8192
*/

0 commit comments

Comments
 (0)