|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Behat\Behat\Context\Context; |
|
4
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
|
5
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
|
6
|
|
|
use Behat\Gherkin\Node\TableNode; |
|
7
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
|
8
|
|
|
use Symfony\Component\Process\Process; |
|
9
|
|
|
|
|
10
|
|
|
class ProxyFeatureContext implements Context, SnippetAcceptingContext { |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
private $phpBin = ''; |
|
13
|
|
|
private $process; |
|
14
|
|
|
private $workingDir = ''; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Initializes context. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct() { |
|
22
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Cleans test folders in the temporary directory. |
|
29
|
|
|
* |
|
30
|
|
|
* @BeforeSuite |
|
31
|
|
|
* @AfterSuite |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function cleanTestFolders() { |
|
34
|
|
|
if (is_dir($dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat')) { |
|
35
|
|
|
self::clearDirectory($dir); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Prepares test folders in the temporary directory. |
|
41
|
|
|
* |
|
42
|
|
|
* @BeforeScenario |
|
43
|
|
|
*/ |
|
44
|
|
|
public function prepareTestFolders() { |
|
45
|
|
|
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . md5(microtime() * rand(0, 10000)); |
|
46
|
|
|
|
|
47
|
|
|
mkdir($dir . '/features/bootstrap/i18n', 0777, true); |
|
48
|
|
|
|
|
49
|
|
|
$phpFinder = new PhpExecutableFinder(); |
|
50
|
|
|
if (false === $php = $phpFinder->find()) { |
|
51
|
|
|
throw new \RuntimeException('Unable to find the PHP executable.'); |
|
52
|
|
|
} |
|
53
|
|
|
$this->workingDir = $dir; |
|
54
|
|
|
$this->phpBin = $php; |
|
55
|
|
|
$this->process = new Process(null); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @Given /^(?:there is )?a file named "([^"]*)" with:$/ |
|
62
|
|
|
*/ |
|
63
|
|
|
public function aFileNamedWith($filename, PyStringNode $content) { |
|
64
|
|
|
$content = strtr((string) $content, array("'''" => '"""')); |
|
65
|
|
|
$this->createFile($this->workingDir . '/' . $filename, $content); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Runs behat command with provided parameters |
|
70
|
|
|
* |
|
71
|
|
|
* @When /^I run "behat(?: ((?:\"|[^"])*))?"$/ |
|
72
|
|
|
* @When /^I run behat$/ |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $argumentsString |
|
75
|
|
|
*/ |
|
76
|
|
|
public function iRunBehat($argumentsString = '') { |
|
77
|
|
|
$argumentsString = strtr($argumentsString, array('\'' => '"')); |
|
78
|
|
|
|
|
79
|
|
|
$this->process->setWorkingDirectory($this->workingDir); |
|
80
|
|
|
$command = sprintf( |
|
81
|
|
|
'%s %s %s %s', |
|
82
|
|
|
$this->phpBin, |
|
83
|
|
|
escapeshellarg(BEHAT_BIN_PATH), |
|
84
|
|
|
$argumentsString, |
|
85
|
|
|
strtr('--no-colors --no-snippets --strict --format=progress --format-settings=\'{"timer": false}\'', array( |
|
86
|
|
|
'\'' => '"', |
|
87
|
|
|
'"' => '\"', |
|
88
|
|
|
)) |
|
89
|
|
|
); |
|
90
|
|
|
$this->process->setCommandLine($command); |
|
91
|
|
|
|
|
92
|
|
|
// Don't reset the LANG variable on HHVM, because it breaks HHVM itself |
|
93
|
|
|
if (!defined('HHVM_VERSION')) { |
|
94
|
|
|
$env = $this->process->getEnv(); |
|
95
|
|
|
$env['LANG'] = 'en'; // Ensures that the default language is en, whatever the OS locale is. |
|
96
|
|
|
$this->process->setEnv($env); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->process->start(); |
|
100
|
|
|
$this->process->wait(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @Then /^it should (pass|fail) with:$/ |
|
105
|
|
|
*/ |
|
106
|
|
|
public function itShouldPassWith($shouldPassFail, PyStringNode $string) { |
|
107
|
|
|
$shouldPass = $shouldPassFail == 'pass'; |
|
108
|
|
|
$passed = $this->getExitCode() === 0; |
|
109
|
|
|
|
|
110
|
|
|
$output = trim($this->getOutput()); |
|
111
|
|
|
|
|
112
|
|
|
$did = $passed ? 'passed' : 'failed'; |
|
113
|
|
|
$should = $shouldPass ? 'pass' : 'fail'; |
|
114
|
|
|
|
|
115
|
|
|
if ($this->getExitCode() == (int) $shouldPass) { |
|
116
|
|
|
echo $output; |
|
117
|
|
|
throw new \Exception("Scenario $did, but should $should"); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($this->cleanOutput($output) != $this->cleanOutput($string)) { |
|
121
|
|
|
echo $output; |
|
122
|
|
|
throw new \Exception("Scenario $did, but with wrong output"); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function cleanOutput($output) { |
|
132
|
|
|
return preg_replace('#([\r\n]+)[ \t]+#', '$1', trim($output)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* |
|
137
|
|
|
*/ |
|
138
|
|
|
private function getOutput() { |
|
139
|
|
|
$output = $this->process->getErrorOutput() . $this->process->getOutput(); |
|
140
|
|
|
|
|
141
|
|
|
// Normalize the line endings in the output |
|
142
|
|
|
if ("\n" !== PHP_EOL) { |
|
143
|
|
|
$output = str_replace(PHP_EOL, "\n", $output); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// Replace wrong warning message of HHVM |
|
147
|
|
|
$output = str_replace('Notice: Undefined index: ', 'Notice: Undefined offset: ', $output); |
|
148
|
|
|
|
|
149
|
|
|
return trim(preg_replace("/ +$/m", '', $output)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* |
|
154
|
|
|
*/ |
|
155
|
|
|
private function getExitCode() { |
|
156
|
|
|
return $this->process->getExitCode(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* |
|
161
|
|
|
*/ |
|
162
|
|
|
private function createFile($filename, $content) { |
|
163
|
|
|
$path = dirname($filename); |
|
164
|
|
|
if (!is_dir($path)) { |
|
165
|
|
|
mkdir($path, 0777, true); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
file_put_contents($filename, $content); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* |
|
173
|
|
|
*/ |
|
174
|
|
|
private static function clearDirectory($path) { |
|
175
|
|
|
$files = scandir($path); |
|
176
|
|
|
array_shift($files); |
|
177
|
|
|
array_shift($files); |
|
178
|
|
|
|
|
179
|
|
|
foreach ($files as $file) { |
|
180
|
|
|
$file = $path . DIRECTORY_SEPARATOR . $file; |
|
181
|
|
|
if (is_dir($file)) { |
|
182
|
|
|
self::clearDirectory($file); |
|
183
|
|
|
} else { |
|
184
|
|
|
unlink($file); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
rmdir($path); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.