Version 1
[yaffs-website] / vendor / behat / mink / src / Mink.php
1 <?php
2
3 /*
4  * This file is part of the Mink package.
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\Mink;
12
13 /**
14  * Mink sessions manager.
15  *
16  * @author Konstantin Kudryashov <ever.zet@gmail.com>
17  */
18 class Mink
19 {
20     private $defaultSessionName;
21
22     /**
23      * Sessions.
24      *
25      * @var Session[]
26      */
27     private $sessions = array();
28
29     /**
30      * Initializes manager.
31      *
32      * @param Session[] $sessions
33      */
34     public function __construct(array $sessions = array())
35     {
36         foreach ($sessions as $name => $session) {
37             $this->registerSession($name, $session);
38         }
39     }
40
41     /**
42      * Stops all started sessions.
43      */
44     public function __destruct()
45     {
46         $this->stopSessions();
47     }
48
49     /**
50      * Registers new session.
51      *
52      * @param string  $name
53      * @param Session $session
54      */
55     public function registerSession($name, Session $session)
56     {
57         $name = strtolower($name);
58
59         $this->sessions[$name] = $session;
60     }
61
62     /**
63      * Checks whether session with specified name is registered.
64      *
65      * @param string $name
66      *
67      * @return Boolean
68      */
69     public function hasSession($name)
70     {
71         return isset($this->sessions[strtolower($name)]);
72     }
73
74     /**
75      * Sets default session name to use.
76      *
77      * @param string $name name of the registered session
78      *
79      * @throws \InvalidArgumentException
80      */
81     public function setDefaultSessionName($name)
82     {
83         $name = strtolower($name);
84
85         if (!isset($this->sessions[$name])) {
86             throw new \InvalidArgumentException(sprintf('Session "%s" is not registered.', $name));
87         }
88
89         $this->defaultSessionName = $name;
90     }
91
92     /**
93      * Returns default session name or null if none.
94      *
95      * @return null|string
96      */
97     public function getDefaultSessionName()
98     {
99         return $this->defaultSessionName;
100     }
101
102     /**
103      * Returns registered session by it's name or active one and automatically starts it if required.
104      *
105      * @param string $name session name
106      *
107      * @return Session
108      *
109      * @throws \InvalidArgumentException If the named session is not registered
110      */
111     public function getSession($name = null)
112     {
113         $session = $this->locateSession($name);
114
115         // start session if needed
116         if (!$session->isStarted()) {
117             $session->start();
118         }
119
120         return $session;
121     }
122
123     /**
124      * Checks whether a named session (or the default session) has already been started.
125      *
126      * @param string $name session name - if null then the default session will be checked
127      *
128      * @return bool whether the session has been started
129      *
130      * @throws \InvalidArgumentException If the named session is not registered
131      */
132     public function isSessionStarted($name = null)
133     {
134         $session = $this->locateSession($name);
135
136         return $session->isStarted();
137     }
138
139     /**
140      * Returns session asserter.
141      *
142      * @param Session|string $session session object or name
143      *
144      * @return WebAssert
145      */
146     public function assertSession($session = null)
147     {
148         if (!($session instanceof Session)) {
149             $session = $this->getSession($session);
150         }
151
152         return new WebAssert($session);
153     }
154
155     /**
156      * Resets all started sessions.
157      */
158     public function resetSessions()
159     {
160         foreach ($this->sessions as $session) {
161             if ($session->isStarted()) {
162                 $session->reset();
163             }
164         }
165     }
166
167     /**
168      * Restarts all started sessions.
169      */
170     public function restartSessions()
171     {
172         foreach ($this->sessions as $session) {
173             if ($session->isStarted()) {
174                 $session->restart();
175             }
176         }
177     }
178
179     /**
180      * Stops all started sessions.
181      */
182     public function stopSessions()
183     {
184         foreach ($this->sessions as $session) {
185             if ($session->isStarted()) {
186                 $session->stop();
187             }
188         }
189     }
190
191     /**
192      * Returns the named or default session without starting it.
193      *
194      * @param string $name session name
195      *
196      * @return Session
197      *
198      * @throws \InvalidArgumentException If the named session is not registered
199      */
200     protected function locateSession($name = null)
201     {
202         $name = strtolower($name) ?: $this->defaultSessionName;
203
204         if (null === $name) {
205             throw new \InvalidArgumentException('Specify session name to get');
206         }
207
208         if (!isset($this->sessions[$name])) {
209             throw new \InvalidArgumentException(sprintf('Session "%s" is not registered.', $name));
210         }
211
212         $session = $this->sessions[$name];
213
214         return $session;
215     }
216 }