Created
September 17, 2011 12:14
-
-
Save beberlei/1223886 to your computer and use it in GitHub Desktop.
Easily Inject authenticated Symfony User into functional test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Liip\FunctionalTestBundle\Test\WebTestCase; | |
use Symfony\Component\HttpKernel\Profiler\Profiler; | |
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
/** | |
* @group functional | |
*/ | |
class MyWebTestCase extends WebTestCase | |
{ | |
abstract protected function getCurrentUser(); | |
/** | |
* @param string $firewallName | |
* @param array $options | |
* @param array $server | |
* @return Symfony\Component\BrowserKit\Client | |
*/ | |
protected function createClientWithAuthentication($firewallName, array $options = array(), array $server = array()) | |
{ | |
/* @var $client \Symfony\Component\BrowserKit\Client */ | |
$client = $this->createClient($options, $server); | |
// has to be set otherwise "hasPreviousSession" in Request returns false. | |
$client->getCookieJar()->set(new \Symfony\Component\BrowserKit\Cookie(session_name(), true)); | |
/* @var $user UserInterface */ | |
$user = $this->getCurrentUser(); | |
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles()); | |
self::$kernel->getContainer()->get('session')->set('_security_' . $firewallName, serialize($token)); | |
return $client; | |
} | |
} |
Hi, is there something similar for current Symfony2 versions?
See http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html
for updated version of this.
See http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html for updated version of this.
link it's possible <5.2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will this work for multiple users? E.g. different clients each with an associated user?