-
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.
Merge pull request #15 from OskarStark/fix-user
fix current user
- Loading branch information
Showing
1 changed file
with
30 additions
and
10 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 |
---|---|---|
|
@@ -25,6 +25,11 @@ final class SonataAdminContext extends RawMinkContext implements CustomSnippetAc | |
{ | ||
const DEFAULT_USERNAME = '[email protected]'; | ||
|
||
/** | ||
* @var UserInterface | ||
*/ | ||
private $user; | ||
|
||
/** | ||
* @var UserManagerInterface | ||
*/ | ||
|
@@ -316,6 +321,8 @@ public function iAmAnAuthenticatedUser() | |
|
||
$this->userManager->save($user); | ||
|
||
$this->user = $user; | ||
|
||
$this->createUserSession($user); | ||
} | ||
|
||
|
@@ -331,19 +338,13 @@ public function iAmAnAuthenticatedUser() | |
*/ | ||
public function iHaveRole($role) | ||
{ | ||
$driver = $this->getSession()->getDriver(); | ||
|
||
$user = $this->userManager->findOneBy(['username' => self::DEFAULT_USERNAME]); | ||
if (null === $user) { | ||
throw new ExpectationException( | ||
sprintf('User with username "%s" does not exist', self::DEFAULT_USERNAME), | ||
$driver | ||
); | ||
} | ||
$user = $this->getCurrentUser(); | ||
|
||
$user->setRoles([$role]); | ||
$this->userManager->save($user); | ||
|
||
$this->user = $user; | ||
|
||
$this->createUserSession($user); | ||
} | ||
|
||
|
@@ -361,14 +362,16 @@ public function iAmAuthenticatedAsUser($username) | |
{ | ||
$driver = $this->getSession()->getDriver(); | ||
|
||
$user = $this->userManager->findOneBy(['username' => self::DEFAULT_USERNAME]); | ||
$user = $this->userManager->findOneBy(['username' => $username]); | ||
if (null === $user) { | ||
throw new ExpectationException( | ||
sprintf('User with username "%s" does not exist', $username), | ||
$driver | ||
); | ||
} | ||
|
||
$this->user = $user; | ||
|
||
$this->createUserSession($user); | ||
} | ||
|
||
|
@@ -592,6 +595,23 @@ private function createUserSession(UserInterface $user) | |
$this->getSession()->setCookie($this->session->getName(), $this->session->getId()); | ||
} | ||
|
||
private function getCurrentUser() | ||
{ | ||
if (null != $this->user) { | ||
return $this->user; | ||
} | ||
|
||
$user = $this->userManager->findOneBy(['username' => self::DEFAULT_USERNAME]); | ||
if (null === $user) { | ||
throw new ExpectationException( | ||
sprintf('User with username "%s" does not exist', self::DEFAULT_USERNAME), | ||
$this->getSession()->getDriver() | ||
); | ||
} | ||
|
||
return $user; | ||
} | ||
|
||
/** | ||
* Returns fixed step argument (with \\" replaced back to "). | ||
* | ||
|