4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\Context\Environment;
13 use Behat\Behat\Context\Context;
14 use Behat\Behat\Context\Environment\Handler\ContextEnvironmentHandler;
15 use Behat\Behat\Context\Exception\ContextNotFoundException;
16 use Behat\Behat\HelperContainer\Environment\ServiceContainerEnvironment;
17 use Behat\Testwork\Call\Callee;
18 use Behat\Testwork\Suite\Suite;
19 use Psr\Container\ContainerInterface;
22 * Context environment based on a list of instantiated context objects.
24 * @see ContextEnvironmentHandler
26 * @author Konstantin Kudryashov <ever.zet@gmail.com>
28 final class InitializedContextEnvironment implements ContextEnvironment, ServiceContainerEnvironment
35 * @var ContainerInterface
37 private $serviceContainer;
41 private $contexts = array();
44 * Initializes environment.
48 public function __construct(Suite $suite)
50 $this->suite = $suite;
54 * Registers context instance in the environment.
56 * @param Context $context
58 public function registerContext(Context $context)
60 $this->contexts[get_class($context)] = $context;
66 public function setServiceContainer(ContainerInterface $container = null)
68 $this->serviceContainer = $container;
74 public function getSuite()
82 public function hasContexts()
84 return count($this->contexts) > 0;
90 public function getContextClasses()
92 return array_keys($this->contexts);
98 public function hasContextClass($class)
100 return isset($this->contexts[$class]);
104 * Returns list of registered context instances.
108 public function getContexts()
110 return array_values($this->contexts);
114 * Returns registered context by its class name.
116 * @param string $class
120 * @throws ContextNotFoundException If context is not in the environment
122 public function getContext($class)
124 if (!$this->hasContextClass($class)) {
125 throw new ContextNotFoundException(sprintf(
126 '`%s` context is not found in the suite environment. Have you registered it?',
131 return $this->contexts[$class];
137 public function getServiceContainer()
139 return $this->serviceContainer;
145 public function bindCallee(Callee $callee)
147 $callable = $callee->getCallable();
149 if ($callee->isAnInstanceMethod()) {
150 return array($this->getContext($callable[0]), $callable[1]);