As it is shown below you can create and use as many context files as you with in behat. With this was, you can split the logic between context files and access them from each other.


Behat.yml


default:
extensions:
BehatSymfony2Extension: ~
BehatMinkExtension:
base_url: http://inanzzz.dev:8082/app_test.php
sessions:
symfony2:
symfony2: ~
suites:
app:
type: symfony_bundle
bundle: InanzzzApplicationBundle
mink_session: symfony2
contexts:
- InanzzzApplicationBundle\Features\Context\FeatureContext
- InanzzzApplicationBundle\Features\Context\CommonContext
- InanzzzApplicationBundle\Features\Context\CountryContext

Context files


FeatureContext


namespace InanzzzApplicationBundle\Features\Context;

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\Extension\Context\MinkContext;

class FeatureContext extends MinkContext implements Context
{
/** @var CommonContext $commonContext */
private $commonContext;

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

$this->commonContext = $environment->getContext(CommonContext::class);
}

/**
* @BeforeScenario @common
*/
public function consumeCommonContext()
{
echo $this->commonContext->greet('inanzzz');
}

/**
* @Given /^I can call CommonContext method from FeatureContext$/
*/
public function iCanCallCommonContextMethodFromFeatureContext()
{
echo $this->commonContext->greet('inanzzz');
}
}

CommonContext


namespace InanzzzApplicationBundle\Features\Context;

use Behat\Behat\Context\Context;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Symfony\Component\Http\Kernel\KernelInterface;

class CommonContext implements Context, KernelAwareContext
{
/** @var KernelInterface */
private $kernel;

/**
* @param KernelInterface $kernel
*/
public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}

/**
* @return KernelInterface
*/
public function getKernel()
{
return $this->kernel;
}

/**
* @param string $name
*
* @return string
*/
public function greet($name)
{
return 'Hello '.$name;
}

/**
* @Given /^I greet you$/
*/
public function iGreetYou()
{
echo $this->greet('inanzzz');
}
}

CountryContext


namespace InanzzzApplicationBundle\Features\Context;

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;

class CountryContext implements Context
{
/** @var CommonContext $commonContext */
private $commonContext;
/** @var FeatureContext $featureContext */
private $featureContext;

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

$this->commonContext = $environment->getContext(CommonContext::class);
$this->featureContext = $environment->getContext(FeatureContext::class);
}

/**
* @Given /^I say hi$/
*/
public function iSayHi()
{
echo 'Hi inanzzz';
}

/**
* @Given /^I can call FeatureContext method from CountryContext$/
*/
public function iCanCallFeatureContextMethodFromCountryContext()
{
echo $this->featureContext->iCanCallCommonContextMethodFromFeatureContext('inanzzz');
}
}

Tests.feature


Feature: Mix of tests.

Scenario: Just shows that the methods in MinkContext are accessible via FeatureContext.
Given I am on "/countries"
Then I should see "Turkey"
And I should not see "Japan"

Scenario: I can indirectly call a CommonContext method from FeatureContext.
Given I can call CommonContext method from FeatureContext

Scenario: I can directly call a CommonContext step.
Given I greet you

Scenario: I can directly call a CountryContext step.
Given I say hi

Scenario: I can indirectly call a FeatureContext method from CountryContext.
Given I can call FeatureContext method from CountryContext

@common
Scenario: I can indirectly call a CommonContext method from FeatureContext by using tags.

Test


$ vendor/bin/behat --suite=app

Feature: Mix of tests.

Scenario: Just shows that the methods in MinkContext are accessible via FeatureContext.
Given I am on "/countries"
Then I should see "Turkey"
And I should not see "Japan"

Scenario: I can indirectly call a CommonContext method from FeatureContext.
Given I can call CommonContext method from FeatureContext
│ Hello inanzzz

Scenario: I can directly call a CommonContext step.
Given I greet you
│ Hello inanzzz

Scenario: I can directly call a CountryContext step.
Given I say hi
│ Hi inanzzz

Scenario: I can indirectly call a FeatureContext method from CountryContext.
Given I can call FeatureContext method from CountryContext
│ Hello inanzzz

┌─ @BeforeScenario @common

│ Hello inanzzz

@common
Scenario: I can indirectly call a CommonContext method from FeatureContext by using tags.

5 scenarios (5 passed)
7 steps (7 passed)
0m0.63s (26.71Mb)