05335496309e3034d6c5565910ffc078aad25d2d
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Context / Environment / InitializedContextEnvironment.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
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\Behat\Context\Environment;
12
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;
20
21 /**
22  * Context environment based on a list of instantiated context objects.
23  *
24  * @see ContextEnvironmentHandler
25  *
26  * @author Konstantin Kudryashov <ever.zet@gmail.com>
27  */
28 final class InitializedContextEnvironment implements ContextEnvironment, ServiceContainerEnvironment
29 {
30     /**
31      * @var string
32      */
33     private $suite;
34     /**
35      * @var ContainerInterface
36      */
37     private $serviceContainer;
38     /**
39      * @var Context[]
40      */
41     private $contexts = array();
42
43     /**
44      * Initializes environment.
45      *
46      * @param Suite $suite
47      */
48     public function __construct(Suite $suite)
49     {
50         $this->suite = $suite;
51     }
52
53     /**
54      * Registers context instance in the environment.
55      *
56      * @param Context $context
57      */
58     public function registerContext(Context $context)
59     {
60         $this->contexts[get_class($context)] = $context;
61     }
62
63     /**
64      * {@inheritdoc}
65      */
66     public function setServiceContainer(ContainerInterface $container = null)
67     {
68         $this->serviceContainer = $container;
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     public function getSuite()
75     {
76         return $this->suite;
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     public function hasContexts()
83     {
84         return count($this->contexts) > 0;
85     }
86
87     /**
88      * {@inheritdoc}
89      */
90     public function getContextClasses()
91     {
92         return array_keys($this->contexts);
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     public function hasContextClass($class)
99     {
100         return isset($this->contexts[$class]);
101     }
102
103     /**
104      * Returns list of registered context instances.
105      *
106      * @return Context[]
107      */
108     public function getContexts()
109     {
110         return array_values($this->contexts);
111     }
112
113     /**
114      * Returns registered context by its class name.
115      *
116      * @param string $class
117      *
118      * @return Context
119      *
120      * @throws ContextNotFoundException If context is not in the environment
121      */
122     public function getContext($class)
123     {
124         if (!$this->hasContextClass($class)) {
125             throw new ContextNotFoundException(sprintf(
126                 '`%s` context is not found in the suite environment. Have you registered it?',
127                 $class
128             ), $class);
129         }
130
131         return $this->contexts[$class];
132     }
133
134     /**
135      * {@inheritdoc}
136      */
137     public function getServiceContainer()
138     {
139         return $this->serviceContainer;
140     }
141
142     /**
143      * {@inheritdoc}
144      */
145     public function bindCallee(Callee $callee)
146     {
147         $callable = $callee->getCallable();
148
149         if ($callee->isAnInstanceMethod()) {
150             return array($this->getContext($callable[0]), $callable[1]);
151         }
152
153         return $callable;
154     }
155 }