1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the phpunit-mink library. |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
8
|
|
|
* @link https://github.com/aik099/phpunit-mink |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace aik099\PHPUnit\Session; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration; |
15
|
|
|
use aik099\PHPUnit\BrowserTestCase; |
16
|
|
|
use aik099\PHPUnit\Event\TestEvent; |
17
|
|
|
use Behat\Mink\Session; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Produces a new Session object shared for each test. |
22
|
|
|
* |
23
|
|
|
* @method \Mockery\Expectation shouldReceive(string $name) |
24
|
|
|
*/ |
25
|
|
|
class IsolatedSessionStrategy implements ISessionStrategy |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Session factory. |
30
|
|
|
* |
31
|
|
|
* @var ISessionFactory |
32
|
|
|
*/ |
33
|
|
|
private $_sessionFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates isolated session strategy instance. |
37
|
|
|
* |
38
|
|
|
* @param ISessionFactory $session_factory Session factory. |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct(ISessionFactory $session_factory) |
41
|
|
|
{ |
42
|
7 |
|
$this->_sessionFactory = $session_factory; |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
47
|
|
|
* |
48
|
|
|
* @return array The event names to listen to |
49
|
|
|
*/ |
50
|
7 |
|
public static function getSubscribedEvents() |
51
|
|
|
{ |
52
|
|
|
return array( |
53
|
7 |
|
BrowserTestCase::TEST_ENDED_EVENT => array('onTestEnd', 0), |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets event dispatcher. |
59
|
|
|
* |
60
|
|
|
* @param EventDispatcherInterface $event_dispatcher Event dispatcher. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
7 |
|
public function setEventDispatcher(EventDispatcherInterface $event_dispatcher) |
65
|
|
|
{ |
66
|
7 |
|
$event_dispatcher->addSubscriber($this); |
67
|
7 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns Mink session with given browser configuration. |
71
|
|
|
* |
72
|
|
|
* @param BrowserConfiguration $browser Browser configuration for a session. |
73
|
|
|
* |
74
|
|
|
* @return Session |
75
|
|
|
*/ |
76
|
1 |
|
public function session(BrowserConfiguration $browser) |
77
|
|
|
{ |
78
|
1 |
|
return $this->_sessionFactory->createSession($browser); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Called, when test ends. |
83
|
|
|
* |
84
|
|
|
* @param TestEvent $event Test event. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
3 |
|
public function onTestEnd(TestEvent $event) |
89
|
|
|
{ |
90
|
3 |
|
if ( !$this->_isEventForMe($event) ) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
$session = $event->getSession(); |
95
|
|
|
|
96
|
3 |
|
if ( $session !== null && $session->isStarted() ) { |
97
|
2 |
|
$session->stop(); |
98
|
|
|
} |
99
|
3 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Checks, that event can be handled by this class. |
103
|
|
|
* |
104
|
|
|
* @param TestEvent $event Test event. |
105
|
|
|
* |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
3 |
|
private function _isEventForMe(TestEvent $event) |
109
|
|
|
{ |
110
|
3 |
|
return $event->getTestCase()->getSessionStrategy() instanceof self; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|