-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathRunBefore.php
More file actions
157 lines (135 loc) · 4.19 KB
/
RunBefore.php
File metadata and controls
157 lines (135 loc) · 4.19 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
<?php
declare(strict_types=1);
namespace Codeception\Extension;
use Codeception\Events;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
use Symfony\Component\Process\Process;
use function array_shift;
use function class_exists;
use function count;
use function is_array;
use function sleep;
/**
* 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 array $config = [];
/**
* @var array<string, string>
*/
protected static array $events = [
Events::SUITE_BEFORE => 'runBefore'
];
private array $processes = [];
public function _initialize(): void
{
if (!class_exists(Process::class)) {
throw new ExtensionException($this, 'symfony/process package is required');
}
}
public function runBefore(): void
{
$this->runProcesses();
$this->processMonitoring();
}
private function runProcesses(): void
{
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);
}
}
private function runProcess(string $command): Process
{
$this->output->debug('[RunBefore] Starting ' . $command);
$process = Process::fromShellCommandline($command, $this->getRootDir());
$process->start();
return $process;
}
/**
* @param string[] $followingCommands
*/
private function addProcessToMonitoring(Process $process, array $followingCommands): void
{
$this->processes[] = [
'instance' => $process,
'following' => $followingCommands
];
}
private function removeProcessFromMonitoring(int $index): void
{
unset($this->processes[$index]);
}
private function processMonitoring(): void
{
while ($this->processes !== []) {
$this->checkProcesses();
sleep(1);
}
}
private function checkProcesses(): void
{
foreach ($this->processes as $index => $process) {
/**
* @var Process $processInstance
*/
$processInstance = $process['instance'];
if (!$this->isRunning($processInstance)) {
if (!$processInstance->isSuccessful()) {
$this->output->debug('[RunBefore] Failed ' . $processInstance->getCommandLine());
$this->output->writeln('<error>' . $processInstance->getErrorOutput() . '</error>');
exit(1);
}
$this->output->debug('[RunBefore] Completed ' . $processInstance->getCommandLine());
$this->runFollowingCommand($process['following']);
$this->removeProcessFromMonitoring($index);
}
}
}
/**
* @param string[] $followingCommands
*/
private function runFollowingCommand(array $followingCommands): void
{
if ($followingCommands !== []) {
$process = $this->runProcess(array_shift($followingCommands));
$this->addProcessToMonitoring($process, $followingCommands);
}
}
private function isRunning(Process $process): bool
{
return $process->isRunning();
}
}