forked from Codeception/Codeception
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunBefore.php
151 lines (133 loc) · 3.9 KB
/
RunBefore.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
<?php
namespace Codeception\Extension;
use Codeception\Events;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
use Symfony\Component\Process\Process;
/**
* Extension for execution of some processes before running tests.
*
* Processes can be independent and dependent.
* Independent processes run independently of each other.
* Dependent processes run sequentially one by one.
*
* Can be configured in suite config:
*
* ```yaml
* # acceptance.suite.yml
* extensions:
* enabled:
* - Codeception\Extension\RunBefore:
* - independent_process_1
* -
* - dependent_process_1_1
* - dependent_process_1_2
* - independent_process_2
* -
* - dependent_process_2_1
* - dependent_process_2_2
* ```
*
* HINT: you can use different configurations per environment.
*/
class RunBefore extends Extension
{
protected $config = [];
protected static $events = [
Events::SUITE_BEFORE => 'runBefore'
];
/** @var array[] */
private $processes = [];
public function _initialize()
{
if (!class_exists('Symfony\Component\Process\Process')) {
throw new ExtensionException($this, 'symfony/process package is required');
}
}
public function runBefore()
{
$this->runProcesses();
$this->processMonitoring();
}
private function runProcesses()
{
foreach ($this->config as $item) {
if (is_array($item)) {
$currentCommand = array_shift($item);
$followingCommands = $item;
} else {
$currentCommand = $item;
$followingCommands = [];
}
$process = $this->runProcess($currentCommand);
$this->addProcessToMonitoring($process, $followingCommands);
}
}
/**
* @param string $command
* @return Process
*/
private function runProcess($command)
{
$this->output->debug('[RunBefore] Starting ' . $command);
if (method_exists(Process::class, 'fromShellCommandline')) {
//Symfony 4.2+
$process = Process::fromShellCommandline($command, $this->getRootDir());
} else {
$process = new Process($command, $this->getRootDir());
}
$process->start();
return $process;
}
/**
* @param string[] $followingCommands
*/
private function addProcessToMonitoring(Process $process, array $followingCommands)
{
$this->processes[] = [
'instance' => $process,
'following' => $followingCommands
];
}
/**
* @param int $index
*/
private function removeProcessFromMonitoring($index)
{
unset($this->processes[$index]);
}
private function processMonitoring()
{
while (count($this->processes) !== 0) {
$this->checkProcesses();
sleep(1);
}
}
private function checkProcesses()
{
foreach ($this->processes as $index => $process) {
if (!$this->isRunning($process['instance'])) {
$this->output->debug('[RunBefore] Completing ' . $process['instance']->getCommandLine());
$this->runFollowingCommand($process['following']);
$this->removeProcessFromMonitoring($index);
}
}
}
/**
* @param string[] $followingCommands
*/
private function runFollowingCommand(array $followingCommands)
{
if (count($followingCommands) > 0) {
$process = $this->runProcess(array_shift($followingCommands));
$this->addProcessToMonitoring($process, $followingCommands);
}
}
private function isRunning(Process $process)
{
if ($process->isRunning()) {
return true;
}
return false;
}
}