-
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.
Split the context in two to not depend on the sonata user bundle (#44)
- Loading branch information
Showing
9 changed files
with
443 additions
and
197 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
composer.lock | ||
vendor | ||
.php_cs.cache | ||
.phpunit.result.cache |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Changelog | ||
|
||
[![Build Status](https://travis-ci.org/OskarStark/SonataAdminBehatContext.svg?branch=master)](https://travis-ci.org/OskarStark/SonataAdminBehatContext) | ||
|
||
## Upgrading from 1.x to 2.0 | ||
|
||
- The `friends-of-behat/symfony-extension` is used instead of `behat/symfony2-extension` (that is not maintained anymore) | ||
- The context `SonataAdminContext` has been split into two separate contexts. | ||
|
||
- `SonataAdminContext` contains step definitions for `sonata-project/admin-bundle`. | ||
- `SonataAdminUserBundleContext` contains step definitions for `sonata-project/user-bundle`. | ||
|
||
Before: | ||
```yaml | ||
# behat.yml.dist | ||
default: | ||
suites: | ||
default: | ||
contexts: | ||
- OStark\Context\SonataAdminContext: | ||
userManager: '@sonata.user.user_manager' | ||
tokenStorage: '@security.token_storage' | ||
session: '@session' | ||
``` | ||
After: | ||
```yaml | ||
# behat.yml.dist | ||
default: | ||
suites: | ||
default: | ||
contexts: | ||
- OStark\Context\SonataAdminContext: | ||
# needed only if you are using sonata-project/user-bundle | ||
- OStark\Context\SonataAdminUserBundleContext: | ||
|
||
# config/services_test.yaml | ||
services: | ||
OStark\Context\SonataAdminUserBundleContext: | ||
arguments: | ||
- '@sonata.user.user_manager' | ||
- '@security.token_storage' | ||
- '@session' | ||
- '@service_container' | ||
|
||
``` |
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
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
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
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 |
---|---|---|
|
@@ -13,72 +13,22 @@ | |
|
||
use Behat\Behat\Hook\Scope\BeforeScenarioScope; | ||
use Behat\Gherkin\Node\TableNode; | ||
use Behat\Mink\Driver\BrowserKitDriver; | ||
use Behat\Mink\Driver\Selenium2Driver; | ||
use Behat\Mink\Element\NodeElement; | ||
use Behat\Mink\Exception\ElementNotFoundException; | ||
use Behat\Mink\Exception\ExpectationException; | ||
use Behat\Mink\Exception\UnsupportedDriverActionException; | ||
use Behat\MinkExtension\Context\MinkContext; | ||
use Behat\MinkExtension\Context\RawMinkContext; | ||
use Behat\Symfony2Extension\Context\KernelAwareContext; | ||
use Sonata\UserBundle\Model\UserManagerInterface; | ||
use Symfony\Component\BrowserKit\Cookie; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* Defines features for the SonataAdmin context. | ||
*/ | ||
final class SonataAdminContext extends RawMinkContext implements KernelAwareContext | ||
final class SonataAdminContext extends RawMinkContext | ||
{ | ||
const DEFAULT_USERNAME = '[email protected]'; | ||
|
||
/** | ||
* @var UserInterface | ||
*/ | ||
private $user; | ||
|
||
/** | ||
* @var UserManagerInterface | ||
*/ | ||
protected $userManager; | ||
|
||
/** | ||
* @var TokenStorageInterface | ||
*/ | ||
protected $tokenStorage; | ||
|
||
/** | ||
* @var Session | ||
*/ | ||
protected $session; | ||
|
||
/** | ||
* @var KernelInterface | ||
*/ | ||
protected $kernel; | ||
|
||
/** | ||
* @var MinkContext | ||
*/ | ||
private $minkContext; | ||
|
||
public function __construct(UserManagerInterface $userManager, TokenStorageInterface $tokenStorage, Session $session) | ||
{ | ||
$this->userManager = $userManager; | ||
$this->tokenStorage = $tokenStorage; | ||
$this->session = $session; | ||
} | ||
|
||
public function setKernel(KernelInterface $kernel) | ||
{ | ||
$this->kernel = $kernel; | ||
} | ||
|
||
/** | ||
* @BeforeScenario | ||
*/ | ||
|
@@ -323,86 +273,6 @@ public function iCloseFlashMessage() | |
$xButton->click(); | ||
} | ||
|
||
/** | ||
* @When /^(?:|I )delete last created user$/ | ||
* | ||
* @codeCoverageIgnore Selenium2Driver needed | ||
*/ | ||
public function deleteLastCreatedUser() | ||
{ | ||
$user = $this->userManager->findBy([], ['createdAt' => 'DESC'], 1); | ||
$this->userManager->deleteUser(current($user)); | ||
} | ||
|
||
/** | ||
* @Given /^I am an authenticated User$/ | ||
* | ||
* @throws ExpectationException | ||
* @throws UnsupportedDriverActionException | ||
* | ||
* @codeCoverageIgnore Selenium2Driver needed | ||
*/ | ||
public function iAmAnAuthenticatedUser() | ||
{ | ||
$user = $this->userManager->createUser(); | ||
|
||
$user->setEmail(self::DEFAULT_USERNAME); | ||
$user->setUsername(self::DEFAULT_USERNAME); | ||
$user->setPlainPassword('foobar'); | ||
|
||
$this->userManager->updateUser($user); | ||
|
||
$this->user = $user; | ||
|
||
$this->createUserSession($user); | ||
} | ||
|
||
/** | ||
* @Given /^I have role "([^"]*)"$/ | ||
* | ||
* @param string $role | ||
* | ||
* @throws ExpectationException | ||
* @throws UnsupportedDriverActionException | ||
* | ||
* @codeCoverageIgnore Selenium2Driver needed | ||
*/ | ||
public function iHaveRole($role) | ||
{ | ||
$user = $this->getCurrentUser(); | ||
|
||
$user->setRoles([$role]); | ||
$this->userManager->updateUser($user); | ||
|
||
$this->user = $user; | ||
|
||
$this->createUserSession($user); | ||
} | ||
|
||
/** | ||
* @Given /^I am authenticated as User "([^"]*)"$/ | ||
* | ||
* @param string $username | ||
* | ||
* @throws ExpectationException | ||
* @throws UnsupportedDriverActionException | ||
* | ||
* @codeCoverageIgnore Selenium2Driver needed | ||
*/ | ||
public function iAmAuthenticatedAsUser($username) | ||
{ | ||
$driver = $this->getSession()->getDriver(); | ||
|
||
$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); | ||
} | ||
|
||
/** | ||
* @When /^(?:|I )logout User$/ | ||
* | ||
|
@@ -648,54 +518,6 @@ public function theFieldShouldNotBeEmtpy($field) | |
$this->assertSession()->fieldValueNotEquals($field, ''); | ||
} | ||
|
||
/** | ||
* @throws UnsupportedDriverActionException | ||
* | ||
* @codeCoverageIgnore Selenium2Driver needed | ||
*/ | ||
private function createUserSession(UserInterface $user) | ||
{ | ||
$providerKey = $this->kernel->getContainer()->getParameter('fos_user.firewall_name'); | ||
|
||
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles()); | ||
$this->tokenStorage->setToken($token); | ||
|
||
$authenticated = $this->tokenStorage->getToken()->isAuthenticated(); | ||
if (!$authenticated) { | ||
throw new \RuntimeException('Not authenticated!'); | ||
} | ||
|
||
$this->session->set('_security_'.$providerKey, serialize($token)); | ||
$this->session->save(); | ||
|
||
$driver = $this->getSession()->getDriver(); | ||
if ($driver instanceof BrowserKitDriver) { | ||
$client = $driver->getClient(); | ||
$cookie = new Cookie($this->session->getName(), $this->session->getId()); | ||
$client->getCookieJar()->set($cookie); | ||
} elseif ($driver instanceof Selenium2Driver) { | ||
$this->visitPath('/'); // this step is needed, otherwise the user is not logged in the first time! | ||
} else { | ||
throw new UnsupportedDriverActionException('The Driver is not supported!', $driver); | ||
} | ||
|
||
$this->getSession()->setCookie($this->session->getName(), $this->session->getId()); | ||
} | ||
|
||
private function getCurrentUser(): UserInterface | ||
{ | ||
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 "). | ||
* | ||
|
@@ -773,9 +595,4 @@ private function notFindElement(string $locator, string $type): void | |
throw new ExpectationException(sprintf('%s found, but should not!', $type), $this->getSession()->getDriver()); | ||
} | ||
} | ||
|
||
public static function getAcceptedSnippetType() | ||
{ | ||
return 'regex'; | ||
} | ||
} |
Oops, something went wrong.