Skip to content

Commit

Permalink
Merge pull request #141 from pfrenssen/subcontextbase
Browse files Browse the repository at this point in the history
Provide a base class for subcontexts

Signed-off-by: Jonathan Hedstrom <jhedstrom@gmail.com>
  • Loading branch information
jhedstrom committed Feb 14, 2015
1 parent 0c100d1 commit b486a75
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 12 deletions.
31 changes: 19 additions & 12 deletions doc/_static/snippets/subcontext.inc
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 src/Drupal/DrupalExtension/Context/DrupalSubContextBase.php
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;
}

}

0 comments on commit b486a75

Please sign in to comment.