Skip to content

Instantly share code, notes, and snippets.

@everzet
Created August 13, 2011 15:00
Show Gist options
  • Save everzet/1143930 to your computer and use it in GitHub Desktop.
Save everzet/1143930 to your computer and use it in GitHub Desktop.
How to use MinkContext inside BehatBundle as subcontext
<?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);
}
}
<?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
{
}
@everzet
Copy link
Author

everzet commented Aug 13, 2011

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 and BehatContext which:

  1. Gets kernel instead of array of parameters into __constructor()
  2. Gets services out of service container (bound to kernel) instead of instantiating them by hands
  3. Gets parameters out of service container (bound to kernel) instead of maintaining them by hands

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.

  1. Behat provides BehatContext with basic functionality
  2. Mink provides MinkContext layer on top of the BehatContext functionality
  3. BehatBundle overrides both BehatContext and MinkContext with kernel, container and services knowledge, so you can talk with your Symfony2 app through them

Also, 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 can extend OR use Behat\*\Context\MinkContext only once!

@arrobeusa
Copy link

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);

@clemherreman
Copy link

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