|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\BehatExtension\Context\Initializer; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Initializer\InitializerInterface; |
|
6
|
|
|
use Behat\Behat\Context\ContextInterface; |
|
7
|
|
|
|
|
8
|
|
|
use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface; |
|
9
|
|
|
|
|
10
|
|
|
/* |
|
11
|
|
|
* This file is part of the Behat/SilverStripeExtension |
|
12
|
|
|
* |
|
13
|
|
|
* (c) Michał Ochman <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
16
|
|
|
* file that was distributed with this source code. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* SilverStripe aware contexts initializer. |
|
21
|
|
|
* Sets SilverStripe instance to the SilverStripeAware contexts. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Michał Ochman <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class SilverStripeAwareInitializer implements InitializerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
private $databaseName; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $ajaxSteps; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Int Timeout in milliseconds |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $ajaxTimeout; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var String {@link see SilverStripeContext} |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $adminUrl; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var String {@link see SilverStripeContext} |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $loginUrl; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var String {@link see SilverStripeContext} |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $screenshotPath; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var object {@link TestSessionEnvironment} |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $testSessionEnvironment; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Initializes initializer. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct($frameworkPath) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->bootstrap($frameworkPath); |
|
66
|
|
|
|
|
67
|
|
|
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL); |
|
68
|
|
|
|
|
69
|
|
|
$testEnv = \Injector::inst()->get('TestSessionEnvironment'); |
|
70
|
|
|
$testEnv->startTestSession(array( |
|
71
|
|
|
'createDatabase' => true |
|
72
|
|
|
)); |
|
73
|
|
|
|
|
74
|
|
|
$state = $testEnv->getState(); |
|
75
|
|
|
|
|
76
|
|
|
$this->databaseName = $state->database; |
|
77
|
|
|
$this->testSessionEnvironment = $testEnv; |
|
78
|
|
|
|
|
79
|
|
|
file_put_contents('php://stdout', "Temp Database: $this->databaseName" . PHP_EOL . PHP_EOL); |
|
80
|
|
|
|
|
81
|
|
|
register_shutdown_function(array($this, '__destruct')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function __destruct() |
|
85
|
|
|
{ |
|
86
|
|
|
// Add condition here as register_shutdown_function() also calls this in __construct() |
|
87
|
|
|
if ($this->testSessionEnvironment) { |
|
88
|
|
|
file_put_contents('php://stdout', "Killing test session environment..."); |
|
89
|
|
|
$this->testSessionEnvironment->endTestSession(); |
|
90
|
|
|
$this->testSessionEnvironment = null; |
|
91
|
|
|
file_put_contents('php://stdout', " done!" . PHP_EOL); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Checks if initializer supports provided context. |
|
97
|
|
|
* |
|
98
|
|
|
* @param ContextInterface $context |
|
99
|
|
|
* |
|
100
|
|
|
* @return Boolean |
|
101
|
|
|
*/ |
|
102
|
|
|
public function supports(ContextInterface $context) |
|
103
|
|
|
{ |
|
104
|
|
|
return $context instanceof SilverStripeAwareContextInterface; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Initializes provided context. |
|
109
|
|
|
* |
|
110
|
|
|
* @param ContextInterface $context |
|
111
|
|
|
*/ |
|
112
|
|
|
public function initialize(ContextInterface $context) |
|
113
|
|
|
{ |
|
114
|
|
|
$context->setDatabase($this->databaseName); |
|
|
|
|
|
|
115
|
|
|
$context->setAjaxSteps($this->ajaxSteps); |
|
|
|
|
|
|
116
|
|
|
$context->setAjaxTimeout($this->ajaxTimeout); |
|
|
|
|
|
|
117
|
|
|
$context->setScreenshotPath($this->screenshotPath); |
|
|
|
|
|
|
118
|
|
|
$context->setRegionMap($this->regionMap); |
|
|
|
|
|
|
119
|
|
|
$context->setAdminUrl($this->adminUrl); |
|
|
|
|
|
|
120
|
|
|
$context->setLoginUrl($this->loginUrl); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function setAjaxSteps($ajaxSteps) |
|
124
|
|
|
{ |
|
125
|
|
|
if ($ajaxSteps) { |
|
126
|
|
|
$this->ajaxSteps = $ajaxSteps; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getAjaxSteps() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->ajaxSteps; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function setAjaxTimeout($ajaxTimeout) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->ajaxTimeout = $ajaxTimeout; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function getAjaxTimeout() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->ajaxTimeout; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function setAdminUrl($adminUrl) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->adminUrl = $adminUrl; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function getAdminUrl() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->adminUrl; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function setLoginUrl($loginUrl) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->loginUrl = $loginUrl; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function getLoginUrl() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->loginUrl; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function setScreenshotPath($screenshotPath) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->screenshotPath = $screenshotPath; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function getScreenshotPath() |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->screenshotPath; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function getRegionMap() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->regionMap; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function setRegionMap($regionMap) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->regionMap = $regionMap; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param String Absolute path to 'framework' module |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function bootstrap($frameworkPath) |
|
189
|
|
|
{ |
|
190
|
|
|
file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL); |
|
191
|
|
|
|
|
192
|
|
|
// Connect to database and build manifest |
|
193
|
|
|
$_GET['flush'] = 1; |
|
194
|
|
|
require_once $frameworkPath . '/core/Core.php'; |
|
195
|
|
|
unset($_GET['flush']); |
|
196
|
|
|
|
|
197
|
|
|
// Remove the error handler so that PHPUnit can add its own |
|
198
|
|
|
restore_error_handler(); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: