Eğer FeatureContext classını kullanan bir sub context içinde $mainContext = $this->getMainContext(); kodunu kullanırsanız, sub context içinden FeatureContext'in kaynaklarına ulaşabilirsiniz. Bunun haricinde bir sub context methoduna diğer bir sub contextten ulaşmak isterseniz, aşağıdaki örneği kullanabilirsiniz.


Sub context One


use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class SubContextOne extends FeatureContext
{
/** @var SubContextTwo */
private $subContextTwo;
/** @var SubContextThree */
private $subContextThree;

/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();

$this->subContextTwo = $environment->getContext(SubContextTwo::class);
$this->subContextThree = $environment->getContext(SubContextThree::class);
}

/**
* @BeforeScenario
*/
public function loadDataFixtures()
{
// You can manually load data fixtures here

$this->subContextTwo->doSomethingNice();
$this->subContextThree->doSomethingBad();
}

/**
* If you tag one of your step definitions with @two, it would trigger this method
*
* @BeforeScenario @two
*/
public function doSomethingNice()
{
$this->subContextTwo->doSomethingNice();
}

/**
* If you tag one of your step definitions with @three, it would trigger this method
*
* @AfterScenario @three
*/
public function doSomethingBad()
{
$this->subContextThree->doSomethingBad();
}
}