Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an exception when the environment is not yet initialized #387

Merged
merged 1 commit into from
Jul 1, 2017
Merged
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 src/Drupal/DrupalExtension/Context/DrupalSubContextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,22 @@ protected function getUser() {
*
* @return \Behat\Behat\Context\Context|false
* The requested context, or FALSE if the context is not registered.
*
* @throws \Exception
* Thrown when the environment is not yet initialized, meaning that contexts
* cannot yet be retrieved.
*/
protected function getContext($class) {
/** @var InitializedContextEnvironment $environment */
$environment = $this->drupal->getEnvironment();
// Throw an exception if the environment is not yet initialized. To make
// sure state doesn't leak between test scenarios, the environment is
// reinitialized at the start of every scenario. If this code is executed
// before a test scenario starts (e.g. in a `@BeforeScenario` hook) then the
// contexts cannot yet be retrieved.
if (!$environment instanceof InitializedContextEnvironment) {
throw new \Exception('Cannot retrieve contexts when the environment is not yet initialized.');
}
foreach ($environment->getContexts() as $context) {
if ($context instanceof $class) {
return $context;
Expand Down