Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

You can either implement KernelAwareInterface or ContainerAwareInterface so it's up to you.


FeatureContext implements KernelAwareInterface


namespace Football\TeamBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class FeatureContext extends MinkContext implements KernelAwareInterface
{
private $kernel;

public function setKernel(KernelInterface $kernelInterface)
{
$this->kernel = $kernelInterface;
}

/**
* @param string $name
*
* @return object
*/
public function getService($name)
{
return $this->kernel->getContainer()->get($name);
}

/**
* @param string $name
*
* @return mixed
*/
public function getParameter($name)
{
return $this->kernel->getContainer()->getParameter($name);
}

/**
* @param null|string $name
*
* @return EntityManagerInterface
*/
public function getEntityManager($name = null)
{
return $this->kernel->getContainer()->get('doctrine')->getManager($name);
}

/**
* @param null|string $name
*
* @return DocumentManager
*/
public function getDocumentManager($name = 'doctrine_mongodb.odm.default_document_manager')
{
return $this->kernel->getContainer()->get($name);
}
}

FeatureContext implements ContainerAwareInterface


namespace Football\TeamBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class FeatureContext extends MinkContext implements ContainerAwareInterface
{
private $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* @param string $name
*
* @return object
*/
public function getService($name)
{
return $this->container->get($name);
}

/**
* @param string $name
*
* @return mixed
*/
public function getParameter($name)
{
return $this->container->getParameter($name);
}

/**
* @param null|string $name
*
* @return EntityManagerInterface
*/
public function getEntityManager($name = null)
{
return $this->container->get('doctrine')->getManager($name);
}

/**
* @param null|string $name
*
* @return DocumentManager
*/
public function getDocumentManager($name = 'doctrine_mongodb.odm.default_document_manager')
{
return $this->container->get($name);
}
}

If you're using more than one context file then you can access public methods with code below(optional).


/** @var FeatureContext $mainContext */
$mainContext = $this->getMainContext();