-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documentation for WithAuthenticationTrait
- Loading branch information
1 parent
7c71798
commit f9c548e
Showing
1 changed file
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,13 +110,56 @@ class MyCoolTest extends WebTestCase | |
{ | ||
$client = static::createClient(); | ||
|
||
$this->authenticate($client, 'my_firewall_name'); | ||
// Authenticate with dummy username, don't need the user to exist in the database | ||
$this->authenticate($client, "fakeUserName", 'my_firewall_name'); | ||
$client->request('GET', '/protected/route'); | ||
|
||
$this->authenticate($client, 'my_firewall_name', ['ROLE_ADMIN']); | ||
// You can pass custom roles for your simulated user | ||
$this->authenticate($client, "fakeUserName", 'my_firewall_name', ['ROLE_ADMIN']); | ||
$client->request('GET', '/admin/foo'); | ||
|
||
$this->authenticateAsAdmin($client, 'my_firewall_name'); | ||
// A shortcut to authenticate as an admin | ||
$this->authenticateAsAdmin($client, "fakeUserName", 'my_firewall_name'); | ||
$client->request('GET', '/admin/foo'); | ||
} | ||
} | ||
``` | ||
|
||
You can also authenticate with a real user ! | ||
|
||
```php | ||
<?php | ||
|
||
use Liior\SymfonyTestHelpers\Concerns\WithAuthenticationTrait; | ||
use Liior\SymfonyTestHelpers\Concerns\WithDatabaseTrait; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use App\Entity\User; | ||
|
||
class MyCoolTest extends WebTestCase | ||
{ | ||
use WithAuthenticationTrait; | ||
use WithDatabaseTrait; | ||
|
||
public function testItRuns(): void | ||
{ | ||
$client = static::createClient(); | ||
|
||
// Create a user (it must be an instance of UserInterface) | ||
$user = new User; | ||
$user->setEmail("[email protected]") | ||
->setPassword("password") | ||
->setRoles(['ROLE_MANAGER', 'ROLE_AUTHOR']); | ||
|
||
// You get this from WithDatabaseTrait | ||
$this->getManager()->persist($user); | ||
$this->getManager()->flush(); | ||
|
||
// Then you can authenticate with this user | ||
$this->authenticate($client, $user, "my_firewall_name"); | ||
$client->request('GET', '/protected/route'); | ||
|
||
// You can also override roles : | ||
$this->authenticate($client, $user, "my_firewall_name", ['ROLE_MODERATOR', 'ROLE_ADMIN']); | ||
$client->request('GET', '/admin/foo'); | ||
} | ||
} | ||
|