diff --git a/documentation.md b/documentation.md index a7cbf282..c1ae1b5b 100644 --- a/documentation.md +++ b/documentation.md @@ -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 +seeUserHasRole('ROLE_ADMIN'); +``` + + * `param string` $role + + ### selectOption Selects an option in a select tag or in radio button group. diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 39756533..2cb66a14 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -770,4 +770,42 @@ private function getPossibleKernelClasses() return [$this->config['kernel_class']]; } + + /** + * Check that the current user has a role + * + * ```php + * 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 + ) + ); + } }