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
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Manages session strategies used across browser tests. |
18
|
|
|
* |
19
|
|
|
* @method \Mockery\Expectation shouldReceive(string $name) |
20
|
|
|
*/ |
21
|
|
|
class SessionStrategyManager |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Browser configuration used in last executed test. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $lastUsedSessionStrategyHash; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Session strategy, that was requested in browser configuration. |
33
|
|
|
* |
34
|
|
|
* @var ISessionStrategy[] |
35
|
|
|
*/ |
36
|
|
|
protected $sessionStrategiesInUse = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Session strategy, that will be used by default. |
40
|
|
|
* |
41
|
|
|
* @var ISessionStrategy |
42
|
|
|
*/ |
43
|
|
|
protected $defaultSessionStrategy; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Session strategy factory. |
47
|
|
|
* |
48
|
|
|
* @var ISessionStrategyFactory |
49
|
|
|
*/ |
50
|
|
|
private $_sessionStrategyFactory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates session strategy manager instance. |
54
|
|
|
* |
55
|
|
|
* @param ISessionStrategyFactory $session_strategy_factory Session strategy factory. |
56
|
|
|
*/ |
57
|
10 |
|
public function __construct(ISessionStrategyFactory $session_strategy_factory) |
58
|
|
|
{ |
59
|
10 |
|
$this->_sessionStrategyFactory = $session_strategy_factory; |
60
|
10 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates default session strategy. |
64
|
|
|
* |
65
|
|
|
* @return ISessionStrategy |
66
|
|
|
*/ |
67
|
2 |
|
public function getDefaultSessionStrategy() |
68
|
|
|
{ |
69
|
2 |
|
if ( !$this->defaultSessionStrategy ) { |
70
|
2 |
|
$this->defaultSessionStrategy = $this->_sessionStrategyFactory->createStrategy( |
71
|
|
|
ISessionStrategyFactory::TYPE_ISOLATED |
72
|
2 |
|
); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
2 |
|
return $this->defaultSessionStrategy; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initializes session strategy using given browser test case. |
80
|
|
|
* |
81
|
|
|
* @param BrowserConfiguration $browser Browser configuration. |
82
|
|
|
* |
83
|
|
|
* @return ISessionStrategy |
84
|
|
|
*/ |
85
|
6 |
|
public function getSessionStrategy(BrowserConfiguration $browser) |
86
|
|
|
{ |
87
|
|
|
/* |
88
|
|
|
* This logic creates separate strategy for: |
89
|
|
|
* - each browser configuration in BrowserTestCase::$browsers (for isolated strategy) |
90
|
|
|
* - each browser configuration in BrowserTestCase::$browsers for each test case class (for shared strategy) |
91
|
|
|
*/ |
92
|
6 |
|
$strategy_type = $browser->getSessionStrategy(); |
93
|
6 |
|
$strategy_hash = $browser->getSessionStrategyHash(); |
94
|
|
|
|
95
|
6 |
|
if ( $strategy_hash !== $this->lastUsedSessionStrategyHash ) { |
96
|
6 |
|
$this->sessionStrategiesInUse[$strategy_hash] = $this->_sessionStrategyFactory->createStrategy( |
97
|
|
|
$strategy_type |
98
|
6 |
|
); |
99
|
6 |
|
} |
100
|
|
|
|
101
|
6 |
|
$this->lastUsedSessionStrategyHash = $strategy_hash; |
102
|
|
|
|
103
|
6 |
|
return $this->sessionStrategiesInUse[$strategy_hash]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|