Skip to content

Commit 3877b50

Browse files
authored
feat(test): add Client::loginUser() (#4588)
1 parent 7d835cb commit 3877b50

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* A new configuration is available to keep old services (IriConverter, IdentifiersExtractor and OpenApiFactory) `metadata_backward_compatibility_layer` (defaults to false) (#4351)
4646
* Add support for `security_post_validation` attribute
4747
* Mark the GraphQL subsystem as stable (#4500)
48+
* feat(test): add `Client::loginUser()`
4849

4950
## 2.6.7
5051

src/Symfony/Bundle/Test/Client.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\HttpClient\HttpClientTrait;
2121
use Symfony\Component\HttpKernel\KernelInterface;
2222
use Symfony\Component\HttpKernel\Profiler\Profile;
23+
use Symfony\Component\Security\Core\User\UserInterface;
2324
use Symfony\Contracts\HttpClient\HttpClientInterface;
2425
use Symfony\Contracts\HttpClient\ResponseInterface;
2526
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
@@ -243,4 +244,15 @@ private static function extractHeaders(array $options): array
243244

244245
return $headers;
245246
}
247+
248+
public function loginUser(UserInterface $user, string $firewallContext = 'main'): self
249+
{
250+
if (!method_exists($this->kernelBrowser, 'loginUser')) {
251+
throw new \LogicException(sprintf('"%s" requires symfony/framework-bundle 5.1+ to be installed.', __METHOD__));
252+
}
253+
254+
$this->kernelBrowser->loginUser($user, $firewallContext);
255+
256+
return $this;
257+
}
246258
}

tests/Symfony/Bundle/Test/ClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use Doctrine\ORM\EntityManagerInterface;
1919
use Doctrine\ORM\Tools\SchemaTool;
2020
use PHPUnit\Runner\Version;
21+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
2122
use Symfony\Component\HttpKernel\Profiler\Profile;
23+
use Symfony\Component\Security\Core\User\InMemoryUser;
2224

2325
/**
2426
* @group legacy
@@ -162,4 +164,17 @@ public function testStream(): void
162164
$client = self::createClient();
163165
$client->stream([]);
164166
}
167+
168+
public function testLoginUser(): void
169+
{
170+
if (!method_exists(KernelBrowser::class, 'loginUser')) {
171+
$this->markTestSkipped('symfony/framework-bundle 5.1 is required to test this function');
172+
}
173+
174+
$client = self::createClient();
175+
$client->loginUser(new InMemoryUser('dunglas', 'kevin', ['ROLE_USER']));
176+
177+
$client->request('GET', '/secured_dummies');
178+
$this->assertResponseIsSuccessful();
179+
}
165180
}

0 commit comments

Comments
 (0)