b1dda505d76941a8c52e94ead288d8afb9b234a2
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Environment / EnvironmentManager.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Testwork\Environment;
12
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;
19
20 /**
21  * Builds, isolates and reads environments using registered handlers and readers.
22  *
23  * @author Konstantin Kudryashov <ever.zet@gmail.com>
24  */
25 final class EnvironmentManager
26 {
27     /**
28      * @var EnvironmentHandler[]
29      */
30     private $handlers = array();
31     /**
32      * @var EnvironmentReader[]
33      */
34     private $readers = array();
35
36     /**
37      * Registers environment handler.
38      *
39      * @param EnvironmentHandler $handler
40      */
41     public function registerEnvironmentHandler(EnvironmentHandler $handler)
42     {
43         $this->handlers[] = $handler;
44     }
45
46     /**
47      * Registers environment reader.
48      *
49      * @param EnvironmentReader $reader
50      */
51     public function registerEnvironmentReader(EnvironmentReader $reader)
52     {
53         $this->readers[] = $reader;
54     }
55
56     /**
57      * Builds new environment for provided test suite.
58      *
59      * @param Suite $suite
60      *
61      * @return Environment
62      *
63      * @throws EnvironmentBuildException
64      */
65     public function buildEnvironment(Suite $suite)
66     {
67         foreach ($this->handlers as $handler) {
68             if ($handler->supportsSuite($suite)) {
69                 return $handler->buildEnvironment($suite);
70             }
71         }
72
73         throw new EnvironmentBuildException(sprintf(
74             'None of the registered environment handlers seem to support `%s` suite.',
75             $suite->getName()
76         ), $suite);
77     }
78
79     /**
80      * Creates new isolated test environment using built one.
81      *
82      * @param Environment $environment
83      * @param mixed       $testSubject
84      *
85      * @return Environment
86      *
87      * @throws EnvironmentIsolationException If appropriate environment handler is not found
88      */
89     public function isolateEnvironment(Environment $environment, $testSubject = null)
90     {
91         foreach ($this->handlers as $handler) {
92             if ($handler->supportsEnvironmentAndSubject($environment, $testSubject)) {
93                 return $handler->isolateEnvironment($environment, $testSubject);
94             }
95         }
96
97         throw new EnvironmentIsolationException(sprintf(
98             'None of the registered environment handlers seem to support `%s` environment.',
99             get_class($environment)
100         ), $environment, $testSubject);
101     }
102
103     /**
104      * Reads all callees from environment using registered environment readers.
105      *
106      * @param Environment $environment
107      *
108      * @return Callee[]
109      */
110     public function readEnvironmentCallees(Environment $environment)
111     {
112         $callees = array();
113         foreach ($this->readers as $reader) {
114             if ($reader->supportsEnvironment($environment)) {
115                 $callees = array_merge($callees, $reader->readEnvironmentCallees($environment));
116             }
117         }
118
119         return $callees;
120     }
121 }