Skip to content

Commit

Permalink
Merge pull request #65 from stof/context_communication
Browse files Browse the repository at this point in the history
Add a cookbook about accessing contexts from each other
  • Loading branch information
everzet committed Jan 3, 2015
2 parents 1067a4d + fb15372 commit 3d662e6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cookbooks/context_communication.rst
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.
1 change: 1 addition & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Learn specific solutions for specific needs:
:maxdepth: 1

cookbooks/1.symfony2_integration
cookbooks/context_communication

More about Behaviour Driven Development
---------------------------------------
Expand Down

0 comments on commit 3d662e6

Please sign in to comment.