forked from michalochman/SilverStripeExtension
-
Notifications
You must be signed in to change notification settings - Fork 30
/
SilverStripeAwareInitializer.php
178 lines (146 loc) · 4.16 KB
/
SilverStripeAwareInitializer.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace SilverStripe\BehatExtension\Context\Initializer;
use Behat\Behat\Context\Initializer\InitializerInterface,
Behat\Behat\Context\ContextInterface;
use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
/*
* This file is part of the Behat/SilverStripeExtension
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* SilverStripe aware contexts initializer.
* Sets SilverStripe instance to the SilverStripeAware contexts.
*
* @author Michał Ochman <ochman.d.michal@gmail.com>
*/
class SilverStripeAwareInitializer implements InitializerInterface
{
private $databaseName;
/**
* @var Array
*/
protected $ajaxSteps;
/**
* @var Int Timeout in milliseconds
*/
protected $ajaxTimeout;
/**
* @var String {@link see SilverStripeContext}
*/
protected $adminUrl;
/**
* @var String {@link see SilverStripeContext}
*/
protected $loginUrl;
/**
* @var String {@link see SilverStripeContext}
*/
protected $screenshotPath;
/**
* Initializes initializer.
*/
public function __construct($frameworkPath, $frameworkHost)
{
$this->bootstrap($frameworkPath, $frameworkHost);
$this->databaseName = $this->initializeTempDb();
}
public function __destruct()
{
$this->deleteTempDb();
}
/**
* Checks if initializer supports provided context.
*
* @param ContextInterface $context
*
* @return Boolean
*/
public function supports(ContextInterface $context)
{
return $context instanceof SilverStripeAwareContextInterface;
}
/**
* Initializes provided context.
*
* @param ContextInterface $context
*/
public function initialize(ContextInterface $context)
{
$context->setDatabase($this->databaseName);
$context->setAjaxSteps($this->ajaxSteps);
$context->setAjaxTimeout($this->ajaxTimeout);
$context->setScreenshotPath($this->screenshotPath);
$context->setAdminUrl($this->adminUrl);
$context->setLoginUrl($this->loginUrl);
}
public function setAjaxSteps($ajaxSteps)
{
if($ajaxSteps) $this->ajaxSteps = $ajaxSteps;
}
public function getAjaxSteps()
{
return $this->ajaxSteps;
}
public function setAjaxTimeout($ajaxTimeout)
{
$this->ajaxTimeout = $ajaxTimeout;
}
public function getAjaxTimeout()
{
return $this->ajaxTimeout;
}
public function setAdminUrl($adminUrl)
{
$this->adminUrl = $adminUrl;
}
public function getAdminUrl()
{
return $this->adminUrl;
}
public function setLoginUrl($loginUrl)
{
$this->loginUrl = $loginUrl;
}
public function getLoginUrl()
{
return $this->loginUrl;
}
public function setScreenshotPath($screenshotPath)
{
$this->screenshotPath = $screenshotPath;
}
public function getScreenshotPath()
{
return $this->screenshotPath;
}
protected function bootstrap($frameworkPath, $frameworkHost)
{
file_put_contents('php://stderr', 'Bootstrapping' . PHP_EOL);
// Set file to URL mappings
global $_FILE_TO_URL_MAPPING;
$_FILE_TO_URL_MAPPING[dirname($frameworkPath)] = $frameworkHost;
// Connect to database and build manifest
$_GET['flush'] = 1;
require_once $frameworkPath . '/core/Core.php';
unset($_GET['flush']);
// Remove the error handler so that PHPUnit can add its own
restore_error_handler();
}
protected function initializeTempDb()
{
$dbname = \SapphireTest::create_temp_db();
file_put_contents('php://stderr', "Creating temp DB $dbname" . PHP_EOL);
\DB::set_alternative_database_name($dbname);
return $dbname;
}
protected function deleteTempDb()
{
file_put_contents('php://stderr', "Killing temp DB" . PHP_EOL);
\SapphireTest::kill_temp_db();
\DB::set_alternative_database_name(null);
}
}