Skip to content

[Live] add TestLiveComponent::actingAs() #1116

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 1 commit into from
Sep 18, 2023
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
14 changes: 14 additions & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3123,6 +3123,20 @@ uses Symfony's test client to render and make requests to your components::
$response = $testComponent->call('redirect')->response(); // Symfony\Component\HttpFoundation\Response

$this->assertSame(302, $response->getStatusCode());

// authenticate a user ($user is instance of UserInterface)
$testComponent->actingAs($user);

// 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,
);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/LiveComponent/src/Test/InteractsWithLiveComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\UX\LiveComponent\Test;

use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\TwigComponent\ComponentFactory;

Expand All @@ -19,7 +20,7 @@
*/
trait InteractsWithLiveComponents
{
protected function createLiveComponent(string $name, array $data = []): TestLiveComponent
protected function createLiveComponent(string $name, array $data = [], KernelBrowser $client = null): TestLiveComponent
{
if (!$this instanceof KernelTestCase) {
throw new \LogicException(sprintf('The "%s" trait can only be used on "%s" classes.', __TRAIT__, KernelTestCase::class));
Expand All @@ -37,7 +38,7 @@ protected function createLiveComponent(string $name, array $data = []): TestLive
$metadata,
$data,
$factory,
self::getContainer()->get('test.client'),
$client ?? self::getContainer()->get('test.client'),
self::getContainer()->get('ux.live_component.component_hydrator'),
self::getContainer()->get('ux.live_component.metadata_factory'),
self::getContainer()->get('router'),
Expand Down
11 changes: 11 additions & 0 deletions src/LiveComponent/src/Test/TestLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\UX\LiveComponent\LiveComponentHydrator;
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
use Symfony\UX\TwigComponent\ComponentFactory;
Expand Down Expand Up @@ -76,6 +77,16 @@ public function component(): object
return (new MountedComponent($this->metadata->getName(), $component, $componentAttributes))->getComponent();
}

/**
* @param UserInterface $user
*/
public function actingAs(object $user, string $firewallContext = 'main'): self
{
$this->client->loginUser($user, $firewallContext);

return $this;
}

/**
* @param array<string,mixed> $arguments
*/
Expand Down