4 * This file is part of the Behat Testwork.
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\Testwork\Environment;
13 use Behat\Testwork\Call\Callee;
14 use Behat\Testwork\Environment\Exception\EnvironmentBuildException;
15 use Behat\Testwork\Environment\Exception\EnvironmentIsolationException;
16 use Behat\Testwork\Environment\Handler\EnvironmentHandler;
17 use Behat\Testwork\Environment\Reader\EnvironmentReader;
18 use Behat\Testwork\Suite\Suite;
21 * Builds, isolates and reads environments using registered handlers and readers.
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
25 final class EnvironmentManager
28 * @var EnvironmentHandler[]
30 private $handlers = array();
32 * @var EnvironmentReader[]
34 private $readers = array();
37 * Registers environment handler.
39 * @param EnvironmentHandler $handler
41 public function registerEnvironmentHandler(EnvironmentHandler $handler)
43 $this->handlers[] = $handler;
47 * Registers environment reader.
49 * @param EnvironmentReader $reader
51 public function registerEnvironmentReader(EnvironmentReader $reader)
53 $this->readers[] = $reader;
57 * Builds new environment for provided test suite.
63 * @throws EnvironmentBuildException
65 public function buildEnvironment(Suite $suite)
67 foreach ($this->handlers as $handler) {
68 if ($handler->supportsSuite($suite)) {
69 return $handler->buildEnvironment($suite);
73 throw new EnvironmentBuildException(sprintf(
74 'None of the registered environment handlers seem to support `%s` suite.',
80 * Creates new isolated test environment using built one.
82 * @param Environment $environment
83 * @param mixed $testSubject
87 * @throws EnvironmentIsolationException If appropriate environment handler is not found
89 public function isolateEnvironment(Environment $environment, $testSubject = null)
91 foreach ($this->handlers as $handler) {
92 if ($handler->supportsEnvironmentAndSubject($environment, $testSubject)) {
93 return $handler->isolateEnvironment($environment, $testSubject);
97 throw new EnvironmentIsolationException(sprintf(
98 'None of the registered environment handlers seem to support `%s` environment.',
99 get_class($environment)
100 ), $environment, $testSubject);
104 * Reads all callees from environment using registered environment readers.
106 * @param Environment $environment
110 public function readEnvironmentCallees(Environment $environment)
113 foreach ($this->readers as $reader) {
114 if ($reader->supportsEnvironment($environment)) {
115 $callees = array_merge($callees, $reader->readEnvironmentCallees($environment));