Created
August 13, 2011 15:00
-
-
Save everzet/1143930 to your computer and use it in GitHub Desktop.
How to use MinkContext inside BehatBundle as subcontext
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
<?php | |
namespace Acme\DemoBundle\Features\Context; | |
use Behat\BehatBundle\Context\BehatContext, | |
Behat\Behat\Exception\PendingException; | |
use Behat\Gherkin\Node\PyStringNode, | |
Behat\Gherkin\Node\TableNode; | |
/** | |
* Feature context. | |
*/ | |
class FeatureContext extends BehatContext | |
{ | |
/** | |
* BehatBundle's main context constructor gets | |
* KernelInterface instance as single parameter | |
*/ | |
public function __construct($kernel) | |
{ | |
// Instantiate and use BehatBundle's MinkContext with provided $kernel | |
$this->useContext('mink', new MinkContext($kernel)); | |
} | |
/** | |
* Get Mink session from MinkContext | |
*/ | |
public function getSession($name = null) | |
{ | |
reutrn $this->getSubcontext('mink')->getSession($name); | |
} | |
} |
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
<?php | |
namespace Acme\DemoBundle\Features\Context; | |
use Behat\BehatBundle\Context\MinkContext as BaseMinkContext, | |
Behat\Behat\Exception\PendingException; | |
use Behat\Gherkin\Node\PyStringNode, | |
Behat\Gherkin\Node\TableNode; | |
/** | |
* Mink-enabled context. | |
*/ | |
class MinkContext extends BaseMinkContext | |
{ | |
} |
How is it that you are able to avoid calling parent constructor within each of those classes? I get a "Call to a member function getContainer() on a non-object" when I don't call parent::__construct($kernel);
You have a typo in
public function getSession($name = null)
{
reutrn $this->getSubcontext('mink')->getSession($name); // Should be 'return'
}
Also thank you for your work on Behat/Mink
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanation: https://github.com/Behat/Mink/issues/68#issuecomment-1797567
If you use BehatBundle - use it's contexts. If you don't use BehatBundle - use Behat or Mink context as parent. Where's the confusion???
The main difference between them is that BehatBundle's contexts gets kernel instance injected into constructor instead of array of parameters. That's all. BehatBundle provides extensions for both
MinkContext
andBehatContext
which:__constructor()
That's all.
So, if you use BehatBundle - extend
Behat\BehatBundle\Context\BehatContext
, it'll give you access to kernel and container in child class. BUT don't forget to provide kernel instance into it's constructor.BehatContext
with basic functionalityMinkContext
layer on top of theBehatContext
functionalityBehatContext
andMinkContext
with kernel, container and services knowledge, so you can talk with your Symfony2 app through themAlso, in counterpart with
Behat\BehatBundle\Context\BehatContext
, which does nothing except defining some helper methods,Behat\BehatBundle\Context\MinkContext
describes step definitions and hooks, needed for Mink. And as you remember - you can't have 2 same definitions in suite. So, you canextend
ORuse
Behat\*\Context\MinkContext
only once!