Skip to content

Commit

Permalink
Merge pull request #567 from ndench/login-client
Browse files Browse the repository at this point in the history
Create WebTestCase::loginClient to allow working with a single kernel
  • Loading branch information
alexislefebvre authored May 29, 2020
2 parents 337a59c + 6db746a commit efa6519
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
14 changes: 7 additions & 7 deletions doc/logged.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You have three alternatives to create an already logged in client:

1. Use the `liip_functional_test.authentication` key in the `config_test.yml` file;
2. Pass an array with login parameters directly when you call the method;
3. Use the method `WebTestCase::loginAs()`;
3. Use the method `WebTestCase::loginClient()`;

### Logging in a user from the `config_test.yml` file

Expand Down Expand Up @@ -37,22 +37,22 @@ $credentials = array(
$client = $this->makeClient($credentials);
```

### Logging in a user using `WebTestCase::loginAs()`
### Logging in a user using `WebTestCase::loginClient()`

To use the method `WebTestCase::loginAs()` you have to [return the repository containing all references set in the
To use the method `WebTestCase::loginClient()` you have to [return the repository containing all references set in the
fixtures](#referencing-fixtures-in-tests) using the method `getReferenceRepository()` and pass the reference of the `User`
object to the method `WebTestCase::loginAs()`.
object to the method `WebTestCase::loginClient()`.

```php
$client = $this->makeClient();
$fixtures = $this->loadFixtures(array(
'AppBundle\DataFixtures\ORM\LoadUserData'
))->getReferenceRepository();
$this->loginAs($fixtures->getReference('account-alpha'), 'main');
$client = $this->makeClient();
$this->loginClient($client, $fixtures->getReference('account-alpha'), 'main');
```

Remember that `WebTestCase::loginAs()` accepts objects that implement the interface `Symfony\Component\Security\Core\User\UserInterface`.
Remember that `WebTestCase::loginClient()` accepts objects that implement the interface `Symfony\Component\Security\Core\User\UserInterface`.

**If you get the error message *"Missing session.storage.options#name"***, you have to simply add to your
[`config_test.yml`](https://github.com/liip/LiipFunctionalTestBundle/blob/master/Tests/App/config.yml#L16)
Expand Down
26 changes: 26 additions & 0 deletions src/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,37 @@ public function fetchCrawler(string $path, string $method = 'GET', bool $authent
*/
public function loginAs(UserInterface $user, string $firewallName): self
{
@trigger_error(sprintf('"%s()" is deprecated, use loginClient() after creating a client.', __METHOD__), E_USER_DEPRECATED);

$this->firewallLogins[$firewallName] = $user;

return $this;
}

public function loginClient(KernelBrowser $client, UserInterface $user, string $firewallName): void
{
// has to be set otherwise "hasPreviousSession" in Request returns false.
$options = $client->getContainer()->getParameter('session.storage.options');

if (!$options || !isset($options['name'])) {
throw new \InvalidArgumentException('Missing session.storage.options#name');
}

$session = $client->getContainer()->get('session');
$session->setId(uniqid());

$client->getCookieJar()->set(new Cookie($options['name'], $session->getId()));

$token = $this->createUserToken($user, $firewallName);

$tokenStorage = $client->getContainer()->get('security.token_storage');

$tokenStorage->setToken($token);
$session->set('_security_'.$firewallName, serialize($token));

$session->save();
}

/**
* Asserts that the HTTP response code of the last request performed by
* $client matches the expected code. If not, raises an error with more
Expand Down
52 changes: 46 additions & 6 deletions tests/Test/WebTestCaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public function testIndexClientWithCredentials(): void

$this->assertStatusCode(200, $this->client);

$this->assertSame(1,
$crawler->filter('html > body')->count());
$this->assertSame(
1,
$crawler->filter('html > body')->count()
);

$this->assertSame(
'Logged in as foobar.',
Expand Down Expand Up @@ -88,8 +90,10 @@ public function testIndexAuthenticatedClient(): void

$this->assertStatusCode(200, $this->client);

$this->assertSame(1,
$crawler->filter('html > body')->count());
$this->assertSame(
1,
$crawler->filter('html > body')->count()
);

$this->assertSame(
'Logged in as foobar.',
Expand Down Expand Up @@ -125,8 +129,44 @@ public function testIndexAuthenticationLoginAs(): void

$this->assertStatusCode(200, $this->client);

$this->assertSame(1,
$crawler->filter('html > body')->count());
$this->assertSame(
1,
$crawler->filter('html > body')->count()
);

$this->assertSame(
'Logged in as foo bar.',
$crawler->filter('p#user')->text()
);

$this->assertSame(
'LiipFunctionalTestBundle',
$crawler->filter('h1')->text()
);
}

/**
* Log in as the user defined in the Data Fixture.
*/
public function testIndexAuthenticationLoginClient(): void
{
$this->client = static::makeClient();

$this->schemaUpdate();
$user = $this->loadTestFixtures();

$this->loginClient($this->client, $user, 'secured_area');

$path = '/';

$crawler = $this->client->request('GET', $path);

$this->assertStatusCode(200, $this->client);

$this->assertSame(
1,
$crawler->filter('html > body')->count()
);

$this->assertSame(
'Logged in as foo bar.',
Expand Down

0 comments on commit efa6519

Please sign in to comment.