04/03/2016 - BEHAT, SYMFONY
If you want to create a new sub context class and dedicate it to a behat feature file, you can use example below. Rather than putting everything in a FeatureContext class, it is ideal to create sub context classes and share some logic in between.
namespace Application\FrontendBundle\Features\Context;
use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeatureContext extends MinkContext implements ContainerAwareInterface
{
const SAY_HELLO = 'Hello';
private $container;
public function __construct()
{
$this->useContext('you_context', new YouContext());
$this->useContext('me_context', new MeContext());
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function sayHello()
{
return self::SAY_HELLO;
}
}
namespace Application\FrontendBundle\Features\Context;
use Behat\Behat\Context\BehatContext;
class YouContext extends BehatContext
{
/**
* @Then /^I can access YouContext step definition$/
*/
public function iCanAccessYouContextStepDefinition()
{
return true;
}
}
namespace Application\FrontendBundle\Features\Context;
use Behat\Behat\Context\BehatContext;
use Behat\Behat\Exception\BehaviorException;
class MeContext extends BehatContext
{
/**
* @Then /^I can access MeContext step definition$/
*/
public function iCanAccessMeContextStepDefinition()
{
return true;
}
/**
* @Then /^I can access FeatureContext resources from MeContext$/
*/
public function iCanAccessFeatureContextResourcesFromMeContext()
{
/** @var FeatureContext $mainContext */
$mainContext = $this->getMainContext();
if ($mainContext->sayHello() != $mainContext::SAY_HELLO) {
throw new BehaviorException('Cannot access FeatureContext resources.');
}
}
}
Feature: .......
Scenario: I can access step definitions of other context files
Given I can access YouContext step definition
And I can access MeContext step definition
And I can access FeatureContext resources from MeContext
$ bin/behat --profile=frontend
Feature: ....
Scenario: I can access step definitions of other context files
Given I can access YouContext step definition
And I can access MeContext step definition
And I can access FeatureContext resources from MeContext
1 scenarios (1 passed)
3 steps (3 passed)
0m0.304s