-
Notifications
You must be signed in to change notification settings - Fork 65
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 #65 from stof/context_communication
Add a cookbook about accessing contexts from each other
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
Accessing contexts from each other | ||
================================== | ||
|
||
When splitting the definitions in multiple contexts, it might be useful to | ||
access a context from another one. This is particularly useful when migrating | ||
from Behat 2.x to replace subcontexts. | ||
|
||
Behat allows to access the environment in doc:`hooks </guides/3.hooks>`, | ||
so other contexts can be retrieved using a ``BeforeScenario`` hook: | ||
|
||
.. code-block:: php | ||
use Behat\Behat\Context\Context; | ||
use Behat\Behat\Hook\Scope\BeforeScenarioScope; | ||
class FeatureContext implements Context | ||
{ | ||
/** @var \Behat\MinkExtension\Context\MinkContext */ | ||
private $minkContext; | ||
/** @BeforeScenario */ | ||
public function gatherContexts(BeforeScenarioScope $scope) | ||
{ | ||
$environment = $scope->getEnvironment(); | ||
$this->minkContext = $environment->getContext('Behat\MinkExtension\Context\MinkContext'); | ||
} | ||
} | ||
.. caution:: | ||
|
||
Circular references in context objects would prevent the PHP reference | ||
counting from collecting contexts at the end of each scenarios, forcing | ||
to wait for the garbage collector to run. This would increase the memory | ||
usage of your Behat run. To prevent that, it is better to avoid storing | ||
the environment itself in your context classes. It is also better to | ||
avoid creating circular references between different contexts. |
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