-
Notifications
You must be signed in to change notification settings - Fork 20
/
SharedSessionStrategy.php
126 lines (108 loc) · 2.6 KB
/
SharedSessionStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* This file is part of the phpunit-mink library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
* @link https://github.com/aik099/phpunit-mink
*/
namespace aik099\PHPUnit\Session;
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
use aik099\PHPUnit\BrowserTestCase;
use Behat\Mink\Session;
use ConsoleHelpers\PHPUnitCompat\Framework\IncompleteTestError;
use ConsoleHelpers\PHPUnitCompat\Framework\SkippedTestError;
/**
* Keeps a Session object shared between test runs to save time.
*
* @method \Mockery\Expectation shouldReceive(string $name)
*/
class SharedSessionStrategy extends AbstractSessionStrategy
{
/**
* Original session strategy.
*
* @var ISessionStrategy
*/
private $_originalStrategy;
/**
* Reference to created session.
*
* @var Session
*/
private $_session;
/**
* Remembers if last test failed.
*
* @var boolean
*/
private $_lastTestFailed = false;
/**
* Remembers original session strategy upon shared strategy creation.
*
* @param ISessionStrategy $original_strategy Original session strategy.
*/
public function __construct(ISessionStrategy $original_strategy)
{
$this->_originalStrategy = $original_strategy;
}
/**
* @inheritDoc
*/
public function session(BrowserConfiguration $browser)
{
if ( $this->_lastTestFailed ) {
$this->stopAndForgetSession();
$this->_lastTestFailed = false;
}
if ( $this->_session === null ) {
$this->_session = $this->_originalStrategy->session($browser);
$this->isFreshSession = $this->_originalStrategy->isFreshSession();
}
else {
$this->isFreshSession = false;
$this->_switchToMainWindow();
}
return $this->_session;
}
/**
* Stops and forgets a session.
*
* @return void
*/
protected function stopAndForgetSession()
{
if ( $this->_session !== null ) {
$this->stopSession($this->_session);
$this->_session = null;
}
}
/**
* Switches to window, that was created upon session creation.
*
* @return void
*/
private function _switchToMainWindow()
{
$this->_session->switchToWindow(null);
}
/**
* @inheritDoc
*/
public function onTestFailed(BrowserTestCase $test_case, $exception)
{
if ( $exception instanceof IncompleteTestError || $exception instanceof SkippedTestError ) {
return;
}
$this->_lastTestFailed = true;
}
/**
* @inheritDoc
*/
public function onTestSuiteEnded(BrowserTestCase $test_case)
{
$session = $test_case->getSession(false);
$this->stopSession($session);
}
}