|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Behat\SpawnerExtension\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
7
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class SuiteListener implements EventSubscriberInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array */ |
|
12
|
|
|
private $commands; |
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
private $winPrefix; |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private $nixPrefix; |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $workingDirectory; |
|
19
|
|
|
/** @var array|\Symfony\Component\Process\Process[] */ |
|
20
|
|
|
private $processes = array(); |
|
21
|
|
|
/** @var int */ |
|
22
|
|
|
private $sleep = 0; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Construct listener |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $commands commands in array format |
|
28
|
|
|
* @param null $workingDirectory working directory for commands |
|
29
|
|
|
* @param string $nixPrefix prefix for *nix-based OS, default "exec" |
|
30
|
|
|
* @param string $winPrefix prefix for Windows OS |
|
31
|
|
|
* @param int $sleep sleep after spawn (in milliseconds) |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( |
|
34
|
|
|
$commands = array(), |
|
35
|
|
|
$workingDirectory = null, |
|
36
|
|
|
$nixPrefix = "exec", |
|
37
|
|
|
$winPrefix = "", |
|
38
|
|
|
$sleep = 0 |
|
39
|
|
|
) { |
|
40
|
|
|
$this->commands = $commands; |
|
41
|
|
|
$this->nixPrefix = $nixPrefix; |
|
42
|
|
|
$this->winPrefix = $winPrefix; |
|
43
|
|
|
$this->workingDirectory = $workingDirectory; |
|
44
|
|
|
$this->sleep = $sleep; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
|
49
|
|
|
* |
|
50
|
|
|
* The array keys are event names and the value can be: |
|
51
|
|
|
* |
|
52
|
|
|
* * The method name to call (priority defaults to 0) |
|
53
|
|
|
* * An array composed of the method name to call and the priority |
|
54
|
|
|
* * An array of arrays composed of the method names to call and respective |
|
55
|
|
|
* priorities, or 0 if unset |
|
56
|
|
|
* |
|
57
|
|
|
* For instance: |
|
58
|
|
|
* |
|
59
|
|
|
* * array('eventName' => 'methodName') |
|
60
|
|
|
* * array('eventName' => array('methodName', $priority)) |
|
61
|
|
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')) |
|
62
|
|
|
* |
|
63
|
|
|
* @return array The event names to listen to |
|
64
|
|
|
* |
|
65
|
|
|
* @api |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getSubscribedEvents() |
|
68
|
|
|
{ |
|
69
|
|
|
return array( |
|
70
|
|
|
ExerciseCompleted::BEFORE => array('spawnProcesses', -20), |
|
71
|
|
|
ExerciseCompleted::AFTER => array('stopProcesses', -20), |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getCommands() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->commands; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getWinPrefix() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->winPrefix; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getNixPrefix() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->nixPrefix; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string|null |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getWorkingDirectory() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->workingDirectory; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Spawns processes |
|
109
|
|
|
*/ |
|
110
|
|
|
public function spawnProcesses() |
|
111
|
|
|
{ |
|
112
|
|
|
if (count($this->processes)) { |
|
113
|
|
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$workingDirectory = $this->getNormalizedWorkdir(); |
|
117
|
|
|
$execPrefix = $this->getPlatformPrefix(); |
|
118
|
|
|
|
|
119
|
|
|
foreach ($this->commands as $arguments) { |
|
120
|
|
|
$process = $this->createProcess($arguments, $execPrefix, $workingDirectory); |
|
121
|
|
|
$process->start(); |
|
122
|
|
|
$this->processes[] = $process; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->sleepIfSpawned(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Normalize working dir (set to '.' if empty) |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
private function getNormalizedWorkdir() |
|
134
|
|
|
{ |
|
135
|
|
|
if ($this->workingDirectory) { |
|
136
|
|
|
return $this->workingDirectory; |
|
137
|
|
|
} else { |
|
138
|
|
|
return "."; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get prefix based on current platform (Windows/*-nix) |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
private function getPlatformPrefix() |
|
148
|
|
|
{ |
|
149
|
|
|
if (defined('PHP_WINDOWS_VERSION_BUILD')) { |
|
150
|
|
|
return $this->winPrefix; |
|
151
|
|
|
} else { |
|
152
|
|
|
return $this->nixPrefix; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param array $arguments |
|
158
|
|
|
* @param string $execPrefix |
|
159
|
|
|
* @param string $workingDirectory |
|
160
|
|
|
* |
|
161
|
|
|
* @return \Symfony\Component\Process\Process |
|
162
|
|
|
*/ |
|
163
|
|
|
private function createProcess($arguments, $execPrefix, $workingDirectory) |
|
164
|
|
|
{ |
|
165
|
|
|
$builder = new ProcessBuilder(); |
|
166
|
|
|
$builder->setWorkingDirectory($workingDirectory); |
|
167
|
|
|
|
|
168
|
|
|
if ($execPrefix) { |
|
169
|
|
|
$builder->setPrefix($execPrefix); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
foreach ($arguments as $arg) { |
|
173
|
|
|
$builder->add($arg); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $builder->getProcess(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Sleep if processes has been spawned and sleep option configured |
|
181
|
|
|
*/ |
|
182
|
|
|
private function sleepIfSpawned() |
|
183
|
|
|
{ |
|
184
|
|
|
if ($this->sleep > 0 && count($this->processes)) { |
|
185
|
|
|
usleep(1000 * $this->sleep); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Stops processes |
|
191
|
|
|
*/ |
|
192
|
|
|
public function stopProcesses() |
|
193
|
|
|
{ |
|
194
|
|
|
foreach ($this->processes as $process) { |
|
195
|
|
|
$process->stop(); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$this->processes = array(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return array|\Symfony\Component\Process\Process[] |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getProcesses() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->processes; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return int |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getSleep() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->sleep; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|