forked from Codeception/Codeception
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunProcess.php
105 lines (96 loc) · 2.74 KB
/
RunProcess.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
<?php
namespace Codeception\Extension;
use Codeception\Events;
use Codeception\Exception\ExtensionException;
use Codeception\Extension;
use Symfony\Component\Process\Process;
/**
* Extension to start and stop processes per suite.
* Can be used to start/stop selenium server, chromedriver, phantomjs, mailcatcher, etc.
*
* Can be configured in suite config:
*
* ```yaml
* # acceptance.suite.yml
* extensions:
* enabled:
* - Codeception\Extension\RunProcess:
* - chromedriver
* ```
*
* Multiple parameters can be passed as array:
*
* ```yaml
* # acceptance.suite.yml
*
* extensions:
* enabled:
* - Codeception\Extension\RunProcess:
* - php -S 127.0.0.1:8000 -t tests/data/app
* - java -jar ~/selenium-server.jar
* ```
*
* In the end of a suite all launched processes will be stopped.
*
* To wait for the process to be launched use `sleep` option.
* In this case you need configuration to be specified as object:
*
* ```yaml
* extensions:
* enabled:
* - Codeception\Extension\RunProcess:
* 0: java -jar ~/selenium-server.jar
* 1: mailcatcher
* sleep: 5 # wait 5 seconds for processes to boot
* ```
*
* HINT: you can use different configurations per environment.
*/
class RunProcess extends Extension
{
public $config = ['sleep' => 0];
static $events = [
Events::SUITE_BEFORE => 'runProcess',
Events::SUITE_AFTER => 'stopProcess'
];
protected $processes = [];
public function _initialize()
{
if (!class_exists('Symfony\Component\Process\Process')) {
throw new ExtensionException($this, 'symfony/process package is required');
}
}
public function runProcess()
{
$this->processes = [];
foreach ($this->config as $key => $command) {
if (!$command) {
continue;
}
if (!is_int($key)) {
continue; // configuration options
}
$process = new Process($command, $this->getRootDir(), null, null, null);
$process->start();
$this->processes[] = $process;
$this->output->debug('[RunProcess] Starting '.$command);
}
sleep($this->config['sleep']);
}
public function __destruct()
{
$this->stopProcess();
}
public function stopProcess()
{
foreach (array_reverse($this->processes) as $process) {
/** @var $process Process **/
if (!$process->isRunning()) {
continue;
}
$this->output->debug('[RunProcess] Stopping ' . $process->getCommandLine());
$process->stop();
}
$this->processes = [];
}
}