-
-
Notifications
You must be signed in to change notification settings - Fork 192
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 #141 from pfrenssen/subcontextbase
Provide a base class for subcontexts Signed-off-by: Jonathan Hedstrom <jhedstrom@gmail.com>
- Loading branch information
Showing
2 changed files
with
92 additions
and
12 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,16 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Contains \FooFoo. | ||
*/ | ||
|
||
use Behat\Behat\Tester\Exception\PendingException; | ||
use Drupal\DrupalExtension\Context\DrupalSubContextBase; | ||
use Drupal\DrupalExtension\Context\DrupalSubContextInterface; | ||
use Drupal\DrupalDriverManager; | ||
class FooFoo implements DrupalSubContextInterface { | ||
private $drupal; | ||
public function __construct(DrupalDriverManager $drupal) { | ||
$this->drupal = $drupal; | ||
} | ||
/** | ||
* @Then /^I should have a subcontext definition$/ | ||
*/ | ||
public function assertSubContextDefinition() { | ||
throw new PendingException(); | ||
} | ||
|
||
/** | ||
* Example subcontext. | ||
*/ | ||
class FooFoo extends DrupalSubContextBase implements DrupalSubContextInterface { | ||
|
||
/** | ||
* @Then /^I should have a subcontext definition$/ | ||
*/ | ||
public function assertSubContextDefinition() { | ||
throw new PendingException(); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/Drupal/DrupalExtension/Context/DrupalSubContextBase.php
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,73 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\DrupalExtension\Context\DrupalSubContextBase. | ||
*/ | ||
|
||
namespace Drupal\DrupalExtension\Context; | ||
|
||
use Behat\Behat\Context\Environment\InitializedContextEnvironment; | ||
use Drupal\DrupalDriverManager; | ||
|
||
/** | ||
* Base class for subcontexts that use the Drupal API. | ||
*/ | ||
abstract class DrupalSubContextBase extends RawDrupalContext implements DrupalSubContextInterface { | ||
|
||
/** | ||
* The Drupal Driver Manager. | ||
* | ||
* @var \Drupal\DrupalDriverManager $drupal | ||
*/ | ||
protected $drupal; | ||
|
||
/** | ||
* Constructs a DrupalSubContextBase object. | ||
* | ||
* @param \Drupal\DrupalDriverManager $drupal | ||
* The Drupal driver manager. | ||
*/ | ||
public function __construct(DrupalDriverManager $drupal) { | ||
$this->drupal = $drupal; | ||
} | ||
|
||
/** | ||
* Get the currently logged in user from DrupalContext. | ||
*/ | ||
protected function getUser() { | ||
/** @var DrupalContext $context */ | ||
$context = $this->getContext('\Drupal\DrupalExtension\Context\DrupalContext'); | ||
if (empty($context->user)) { | ||
throw new \Exception('No user is logged in.'); | ||
} | ||
|
||
return $context->user; | ||
} | ||
|
||
/** | ||
* Returns the Behat context that corresponds with the given class name. | ||
* | ||
* This is inspired by InitializedContextEnvironment::getContext() but also | ||
* returns subclasses of the given class name. This allows us to retrieve for | ||
* example DrupalContext even if it is overridden in a project. | ||
* | ||
* @param string $class | ||
* A fully namespaced class name. | ||
* | ||
* @return \Behat\Behat\Context\Context|false | ||
* The requested context, or FALSE if the context is not registered. | ||
*/ | ||
protected function getContext($class) { | ||
/** @var InitializedContextEnvironment $environment */ | ||
$environment = $this->drupal->getEnvironment(); | ||
foreach ($environment->getContexts() as $context) { | ||
if ($context instanceof $class) { | ||
return $context; | ||
} | ||
} | ||
|
||
return FALSE; | ||
} | ||
|
||
} |