forked from Leantime/leantime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
218 lines (189 loc) · 5.25 KB
/
Copy pathbootstrap.php
File metadata and controls
218 lines (189 loc) · 5.25 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
// Don't run the script unless using the 'run' command
if (!isset($_SERVER['argv'][1]) || $_SERVER['argv'][1] !== 'run') {
return;
}
if (!file_exists($composer = __DIR__ . '/../vendor/autoload.php')) {
throw new RuntimeException('Please run "make build-dev" to run tests.');
}
require $composer;
define('PROJECT_ROOT', realpath(__DIR__ . '/..') . '/');
define('APP_ROOT', PROJECT_ROOT . 'app/');
define('DEV_ROOT', PROJECT_ROOT . '.dev/');
$bootstrapper = get_class(new class {
/**
* @var self
*/
protected static $instance;
/**
* Get the singleton instance of this class
*
* @access public
*
* @return self
*/
public static function getInstance(): self
{
if (! isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Start the testing environment
*
* @access public
*
* @return void
*/
public function start(): void
{
$this->setFolderPermissions();
$this->createDatabase();
$this->createStep('Starting Codeception Testing Framework');
}
/**
* Destroy the testing environment
*
* @access public
*
* @return void
*/
public function destroy(): void
{
$this->createStep('Stopping Codeception Testing Framework');
}
/**
* Create the test database
*
* @access protected
*
* @return void
*/
protected function createDatabase(): void
{
$this->createStep('Dropping Test Database');
$this->executeCommand(
[
'mysql',
'--host=db',
'--user=root',
'--password=leantime',
'-e',
'DROP DATABASE IF EXISTS leantime_test;',
],
['cwd' => DEV_ROOT]
);
$this->createStep('Creating Test Database');
$this->executeCommand(
[
'mysql',
'--host=db',
'--user=root',
'--password=leantime',
'-e',
'CREATE DATABASE IF NOT EXISTS leantime_test; GRANT ALL PRIVILEGES ON leantime_test.* TO \'leantime\'@\'%\'; FLUSH PRIVILEGES;',
],
['cwd' => DEV_ROOT]
);
}
protected function setFolderPermissions(): void
{
$this->createStep('Setting folder permissions on cache folder');
//Set file permissions
$this->executeCommand(
array_filter(
[
'chown',
'-R',
'www-data:www-data',
'/var/www/html/cache/',
]
),
[
'cwd' => DEV_ROOT,
]
);
}
/**
* Create a step in the output
*
* @access protected
* @param string $message
* @return void
*/
protected function createStep(string $message): void
{
$chars = strlen($message);
$line = str_repeat('=', $chars);
echo "\n$line\n$message\n$line\n";
}
/**
* Execute a command
*
* @access protected
* @param string|array $command
* @param array $args
* @param boolean $required
* @return Process|string
*/
protected function executeCommand(
string|array $command,
array $args = [],
bool $required = true,
): Process|string {
$process = is_array($command)
? new Process($command)
: Process::fromShellCommandline($command);
if (isset($args['cwd'])) {
$process->setWorkingDirectory($args['cwd']);
}
if (isset($args['timeout'])) {
$process->setTimeout($args['timeout']);
}
if (isset($args['options'])) {
$process->setOptions($args['options']);
}
if (isset($args['background']) && $args['background']) {
$process->start();
} else {
$process->run(fn ($type, $buffer) => $this->commandOutputHandler($type, $buffer));
}
if (
$required
&& (! isset($args['background']) || ! $args['background'])
&& ! $process->isSuccessful()
) {
throw new ProcessFailedException($process);
}
if (
isset($args['getOutput'])
&& $args['getOutput']
) {
if (isset($args['background']) && $args['background']) {
throw new RuntimeException('Cannot get output from background process');
}
return $process->getOutput();
}
return $process;
}
/**
* Handle command output
*
* @access private
*
* @param string $type
* @param string $buffer
*
* @return void
*/
private function commandOutputHandler(string $type, string $buffer): void
{
echo Process::ERR === $type ? "\nSTDERR: $buffer" : "\nSTDOUT: $buffer";
}
});
register_shutdown_function(fn () => $bootstrapper::getInstance()->destroy());
$bootstrapper::getInstance()->start();