@@ -118,6 +118,8 @@ needs::
118118 // See https://symfony.com/doc/current/reference/configuration/security.html#firewall-context
119119 $firewallContext = 'secured_area';
120120
121+ // you may need to use a different token class depending on your application.
122+ // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken
121123 $token = new UsernamePasswordToken('admin', null, $firewallName, array('ROLE_ADMIN'));
122124 $session->set('_security_'.$firewallContext, serialize($token));
123125 $session->save();
@@ -126,64 +128,3 @@ needs::
126128 $this->client->getCookieJar()->set($cookie);
127129 }
128130 }
129-
130- Note: Are you using Guard? You may adjust the token depending on your application needs.
131- For example, if you are using Guard for authentication, you would use the `PostAuthenticationGuardToken `:
132-
133- // tests/Controller/DefaultControllerTest.php
134- namespace App\T ests\C ontroller;
135-
136- use Symfony\B undle\F rameworkBundle\T est\W ebTestCase;
137- use Symfony\C omponent\B rowserKit\C ookie;
138- use Symfony\C omponent\H ttpFoundation\R esponse;
139- use Symfony\C omponent\S ecurity\G uard\T oken\P ostAuthenticationGuardToken;
140-
141- class DefaultControllerTest extends WebTestCase
142- {
143- private $client = null;
144-
145- private $user = null;
146-
147- public function setUp()
148- {
149- $this->client = static::createClient();
150- $this->user = $this->createUser()
151- }
152-
153- private function createUser() {
154-
155- // create your user, save it and return it
156- // ...
157-
158- return $user;
159- }
160-
161- public function testSecuredHello()
162- {
163- $this->logInAsUser($user);
164- $crawler = $this->client->request('GET', '/admin');
165-
166- $this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
167- $this->assertSame('Admin Dashboard', $crawler->filter('h1')->text());
168- }
169-
170-
171- protected function logInAsUser(UserInterface $user)
172- {
173- $session = $this->client->getContainer()->get('session');
174-
175- // the firewall context defaults to the firewall name
176- $firewallContext = 'secured_area';
177- $token = new PostAuthenticationGuardToken(
178- $user,
179- $firewallContext,
180- array('ROLE_ADMIN')
181- );
182- $session->set('_security_'.$firewallContext, serialize($token));
183- $session->save();
184-
185- $cookie = new Cookie($session->getName(), $session->getId());
186- $this->client->getCookieJar()->set($cookie);
187- }
188-
189-
0 commit comments