-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFeatureContext.php
More file actions
258 lines (214 loc) · 6.27 KB
/
FeatureContext.php
File metadata and controls
258 lines (214 loc) · 6.27 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* Behat tests.
*
* @package ee-cli
*/
/* Start: Loading required files to enable EE::launch() in tests. */
define( 'EE_ROOT', __DIR__ . '/../..' );
include_once( EE_ROOT . '/php/class-ee.php' );
include_once( EE_ROOT . '/php/EE/Runner.php' );
include_once( EE_ROOT . '/php/utils.php' );
define( 'EE', true );
define( 'EE_VERSION', trim( file_get_contents( EE_ROOT . '/VERSION' ) ) );
define( 'EE_ROOT_DIR', '/opt/easyengine' );
require_once EE_ROOT . '/php/bootstrap.php';
if ( ! class_exists( 'EE\Runner' ) ) {
require_once EE_ROOT . '/php/EE/Runner.php';
}
if ( ! class_exists( 'EE\Configurator' ) ) {
require_once EE_ROOT . '/php/EE/Configurator.php';
}
$logger_dir = EE_ROOT . '/php/EE/Loggers';
$iterator = new \DirectoryIterator( $logger_dir );
// Make sure the base class is declared first.
include_once "$logger_dir/Base.php";
foreach ( $iterator as $filename ) {
if ( '.php' !== pathinfo( $filename, PATHINFO_EXTENSION ) ) {
continue;
}
include_once "$logger_dir/$filename";
}
$runner = \EE::get_runner();
$runner->init_logger();
/* End. Loading required files to enable EE::launch() in tests. */
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\AfterFeatureScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
define( 'EE_SITE_ROOT', EE_ROOT_DIR . '/sites' );
class FeatureContext implements Context
{
public $command;
public $webroot_path;
public $ee_path;
/**
* Initializes context.
*/
public function __construct()
{
$this->commands = [];
$this->ee_path = getcwd();
}
/**
* @Given ee phar is generated
*/
public function eePharIsPresent()
{
// Checks if phar already exists, replaces it
if( file_exists( 'ee-old.phar' ) ) {
// Below exec call is required as currenly `ee cli update` is ran with root
// which updates ee.phar with root privileges.
exec( "sudo rm ee.phar" );
copy( 'ee-old.phar','ee.phar' );
return 0;
}
exec( "php -dphar.readonly=0 utils/make-phar.php ee.phar", $output, $return_status );
if ( 0 !== $return_status ) {
throw new Exception( "Unable to generate phar" . $return_status );
}
// Cache generaed phar as it is expensive to generate one
copy( 'ee.phar','ee-old.phar' );
}
/**
* @Given :command is installed
*/
public function isInstalled( $command )
{
exec( "type " . $command, $output, $return_status );
if ( 0 !== $return_status ) {
throw new Exception( $command . " is not installed! Exit code is:" . $return_status );
}
}
/**
* @When /I run '(.*)'|"(.*)"/
*/
public function iRun( $command )
{
$this->commands[] = EE::launch($command, false, true);
}
/**
* @When I try :command
*/
public function iTry( $command )
{
$this->commands[] = EE::launch($command, false, true);
}
/**
* @Then After delay of :time seconds
*/
public function afterDelayOfSeconds( $time )
{
sleep( $time );
}
/**
* @Then /(STDOUT|STDERR) should return exactly/
*/
public function stdoutShouldReturnExactly( $output_stream, PyStringNode $expected_output )
{
$command_output = $output_stream === "STDOUT" ? $this->commands[0]->stdout : $this->commands[0]->stderr;
$command_output = str_replace(["\033[1;31m","\033[0m"],'',$command_output);
if ( $expected_output->getRaw() !== trim($command_output)) {
throw new Exception("Actual output is:\n" . $command_output);
}
}
/**
* @Then /(STDOUT|STDERR) should return something like/
*/
public function stdoutShouldReturnSomethingLike( $output_stream, PyStringNode $expected_output )
{
$command_output = $output_stream === "STDOUT" ? $this->commands[0]->stdout : $this->commands[0]->stderr;
$expected_out = isset( $expected_output->getStrings()[0] ) ? $expected_output->getStrings()[0] : '';
if ( strpos( $command_output, $expected_out ) === false ) {
throw new Exception( "Actual output is:\n" . $command_output );
}
}
/**
* @Then The ee should have admin-tools directory in root
*/
public function theEEShouldHaveToolsDir()
{
if ( ! file_exists( EE_ROOT_DIR . '/admin-tools' ) ) {
throw new Exception( "The admin-tools directory has not been created!" );
}
}
/**
* @Then The admin-tools should have index file
*/
public function theAdminToolsShouldHaveIndexFile()
{
if ( ! file_exists( EE_ROOT_DIR . '/admin-tools/index.php' ) ) {
throw new Exception( "Admin Tools data not found!" );
}
}
/**
* @Then The site :site should have index file
*/
public function theSiteShouldHaveIndexFile( $site )
{
if ( ! file_exists( EE_SITE_ROOT . '/' . $site . "/app/htdocs/index.php" ) ) {
throw new Exception( "PHP site data not found!" );
}
}
/**
* @Then The site :site should have WordPress
*/
public function theSiteShouldHaveWordpress($site)
{
if ( ! file_exists( EE_SITE_ROOT . '/' . $site . "/app/wp-config.php" ) ) {
throw new Exception("WordPress data not found!");
}
}
/**
* @Then Request on :site should contain following headers:
*/
public function requestOnShouldContainFollowingHeaders( $site, TableNode $table )
{
$url = 'http://' . $site;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_NOBODY, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_VERBOSE, true );
$headers = curl_exec( $ch );
curl_close($ch);
$rows = $table->getHash();
foreach ( $rows as $row ) {
if ( strpos( $headers, $row['header'] ) === false ) {
throw new Exception( "Unable to find " . $row['header'] . "\nActual output is : " . $headers );
}
}
}
/**
* @AfterScenario
*/
public function cleanupScenario( AfterScenarioScope $scope )
{
$this->commands = [];
chdir( $this->ee_path );
}
/**
* @AfterFeature
*/
public static function cleanup( AfterFeatureScope $scope )
{
$test_sites = [
'php.test',
'wp.test'
];
$result = EE::launch( 'sudo bin/ee site list --format=text', false, true );
$running_sites = explode( "\n", $result->stdout );
$sites_to_delete = array_intersect( $test_sites, $running_sites );
foreach ( $sites_to_delete as $site ) {
exec( "sudo bin/ee site delete $site --yes" );
}
if( file_exists( 'ee.phar' ) ) {
unlink( 'ee.phar' );
}
if( file_exists( 'ee-old.phar' ) ) {
unlink( 'ee-old.phar' );
}
}
}