Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,18 @@ Checks that the response code is 5xx
Checks that the response code 2xx


### seeUserHasRole

Check that the current user has a role

```php
<?php
$I->seeUserHasRole('ROLE_ADMIN');
```

* `param string` $role


### selectOption

Selects an option in a select tag or in radio button group.
Expand Down
38 changes: 38 additions & 0 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,42 @@ private function getPossibleKernelClasses()

return [$this->config['kernel_class']];
}

/**
* Check that the current user has a role
*
* ```php
* <?php
* $I->seeUserHasRole('ROLE_ADMIN');
* ```
*
* @param string $role
*/
public function seeUserHasRole($role)
{
$container = $this->_getContainer();

if (!$container->has('security.helper')) {
$this->fail("Symfony container doesn't have 'security.helper' service");
return;
}

$security = $this->grabService('security.helper');

$user = $security->getUser();

if (!$user) {
$this->fail('There is no user in session');
return;
}

$this->assertTrue(
$security->isGranted($role),
sprintf(
"User %s has no role %s",
$user->getUsername(),
$role
)
);
}
}