1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Behatch\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\Step; |
6
|
|
|
use Behat\Behat\Context\Context; |
7
|
|
|
use Behat\Gherkin\Node\PyStringNode; |
8
|
|
|
|
9
|
|
|
class SystemContext implements Context |
10
|
|
|
{ |
11
|
|
|
private $root; |
12
|
|
|
private $output; |
13
|
|
|
private $lastExecutionTime; |
14
|
|
|
private $lastReturnCode; |
15
|
|
|
private $createdFiles = []; |
16
|
|
|
|
17
|
|
|
public function __construct($root = '.') |
18
|
|
|
{ |
19
|
|
|
$this->root = $root; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function getTranslationResources() |
23
|
|
|
{ |
24
|
|
|
return glob(__DIR__ . '/../../i18n/*.xliff'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Uploads a file using the specified input field |
29
|
|
|
* |
30
|
|
|
* @When (I )put the file :file into :field |
31
|
|
|
*/ |
32
|
|
|
public function putFileIntoField($file, $field) |
33
|
|
|
{ |
34
|
|
|
$path = $this->root . DIRECTORY_SEPARATOR . $file; |
35
|
|
|
|
36
|
|
|
return [ |
37
|
|
|
new Step\When("I attach the file '$path' to '$field'") |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute a command |
43
|
|
|
* |
44
|
|
|
* @Given (I )execute :command |
45
|
|
|
*/ |
46
|
|
|
public function iExecute($cmd) |
47
|
|
|
{ |
48
|
|
|
$start = microtime(true); |
49
|
|
|
|
50
|
|
|
exec($cmd, $this->output, $this->lastReturnCode); |
51
|
|
|
|
52
|
|
|
$this->lastExecutionTime = microtime(true) - $start; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Execute a command from project root |
57
|
|
|
* |
58
|
|
|
* @Given (I )execute :command from project root |
59
|
|
|
*/ |
60
|
|
|
public function iExecuteFromProjectRoot($cmd) |
61
|
|
|
{ |
62
|
|
|
$cmd = $this->root . DIRECTORY_SEPARATOR . $cmd; |
63
|
|
|
$this->iExecute($cmd); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Command should succeed |
68
|
|
|
* |
69
|
|
|
* @Then command should succeed |
70
|
|
|
*/ |
71
|
|
|
public function commandShouldSucceed() { |
72
|
|
|
if ($this->lastReturnCode !== 0) { |
73
|
|
|
throw new \Exception(sprintf("Command should succeed %b", $this->lastReturnCode)); |
74
|
|
|
}; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Command should fail |
79
|
|
|
* |
80
|
|
|
* @Then command should fail |
81
|
|
|
*/ |
82
|
|
|
public function commandShouldFail() { |
83
|
|
|
if ($this->lastReturnCode === 0) { |
84
|
|
|
throw new \Exception(sprintf("Command should fail %b", $this->lastReturnCode)); |
85
|
|
|
}; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Command should last less than |
90
|
|
|
* |
91
|
|
|
* @Then command should last less than :seconds seconds |
92
|
|
|
*/ |
93
|
|
|
public function commandShouldLastLessThan($seconds) |
94
|
|
|
{ |
95
|
|
|
if ($this->lastExecutionTime > $seconds) { |
96
|
|
|
throw new \Exception(sprintf("Last command last %s which is more than %s seconds", $lastExecutionTime, $seconds)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Command should last more than |
102
|
|
|
* |
103
|
|
|
* @Then command should last more than :seconds seconds |
104
|
|
|
*/ |
105
|
|
|
public function commandShouldMoreLessThan($seconds) |
106
|
|
|
{ |
107
|
|
|
if ($this->lastExecutionTime < $seconds) { |
108
|
|
|
throw new \Exception(sprintf("Last command last %s which is less than %s seconds", $lastExecutionTime, $seconds)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Checks, that output contains specified text. |
114
|
|
|
* |
115
|
|
|
* @Then output should contain :text |
116
|
|
|
*/ |
117
|
|
|
public function outputShouldContain($text) |
118
|
|
|
{ |
119
|
|
|
$regex = '~'.$text.'~ui'; |
120
|
|
|
|
121
|
|
|
$check = false; |
122
|
|
|
foreach ($this->output as $line) { |
123
|
|
|
if (preg_match($regex, $line) === 1) { |
124
|
|
|
$check = true; |
125
|
|
|
break; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($check === false) { |
130
|
|
|
throw new \Exception(sprintf("The text '%s' was not found anywhere on output of command.\n%s", $text, implode("\n", $this->output))); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Checks, that output not contains specified text. |
136
|
|
|
* |
137
|
|
|
* @Then output should not contain :text |
138
|
|
|
*/ |
139
|
|
|
public function ouputShouldNotContain($text) |
140
|
|
|
{ |
141
|
|
|
$regex = '~'.$text.'~ui'; |
142
|
|
|
|
143
|
|
View Code Duplication |
foreach ($this->output as $line) { |
|
|
|
|
144
|
|
|
if (preg_match($regex, $line) === 1) { |
145
|
|
|
throw new \Exception(sprintf("The text '%s' was found somewhere on output of command.\n%s", $text, implode("\n", $this->output))); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @Given output should be: |
152
|
|
|
*/ |
153
|
|
|
public function outputShouldBe(PyStringNode $string) |
154
|
|
|
{ |
155
|
|
|
$expected = $string->getStrings(); |
156
|
|
View Code Duplication |
foreach ($this->output as $index => $line) { |
|
|
|
|
157
|
|
|
if ($line !== $expected[$index]) { |
158
|
|
|
throw new \Exception(sprintf("instead of\n%s", implode("\n", $this->output))); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @Given output should not be: |
165
|
|
|
*/ |
166
|
|
|
public function outputShouldNotBe(PyStringNode $string) |
167
|
|
|
{ |
168
|
|
|
$expected = $string->getStrings(); |
169
|
|
|
|
170
|
|
|
$check = false; |
171
|
|
|
foreach ($this->output as $index => $line) { |
172
|
|
|
if ($line !== $expected[$index]) { |
173
|
|
|
$check = true; |
174
|
|
|
break; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ($check === false) { |
179
|
|
|
throw new \Exception("Output should not be"); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @Given (I )create the file :filename containing: |
185
|
|
|
* @Given (I )create the file :filename contening: |
186
|
|
|
*/ |
187
|
|
|
public function iCreateTheFileContaining($filename, PyStringNode $string) |
188
|
|
|
{ |
189
|
|
|
if (!is_file($filename)) { |
190
|
|
|
file_put_contents($filename, $string); |
191
|
|
|
$this->createdFiles[] = $filename; |
192
|
|
|
} |
193
|
|
|
else { |
194
|
|
|
throw new \RuntimeException("'$filename' already exists."); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @Then print the content of :filename file |
200
|
|
|
*/ |
201
|
|
|
public function printTheContentOfFile($filename) |
202
|
|
|
{ |
203
|
|
|
if (is_file($filename)) { |
204
|
|
|
echo file_get_contents($filename); |
205
|
|
|
} |
206
|
|
|
else { |
207
|
|
|
throw new \RuntimeException("'$filename' doesn't exists."); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @AfterScenario |
213
|
|
|
*/ |
214
|
|
|
public function after() |
215
|
|
|
{ |
216
|
|
|
foreach ($this->createdFiles as $filename) { |
217
|
|
|
unlink($filename); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.