-
Notifications
You must be signed in to change notification settings - Fork 10
/
cmfiveTests.php
464 lines (397 loc) · 14.1 KB
/
cmfiveTests.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/php
<?php
if (!(isset($argc) && isset($argv))) {
echo "No action is possible.";
exit(1);
}
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
defined('TEST_DIRECTORY') || define('TEST_DIRECTORY', 'test' . DS . 'Codeception');
defined('PROJECT_MODULE_DIRECTORY') || define('PROJECT_MODULE_DIRECTORY', 'modules');
defined('SYSTEM_MODULE_DIRECTORY') || define('SYSTEM_MODULE_DIRECTORY', 'system' . DS . 'modules');
defined('WORKFLOWS_TEST_DIRECTORY') || define('WORKFLOWS_TEST_DIRECTORY', 'system' . DS . 'tests');
defined('BOILERPLATE_TEST_DIRECTORY') || define('BOILERPLATE_TEST_DIRECTORY', TEST_DIRECTORY . DS . 'tests');
defined('UNIT_DIRECTORY') || define('UNIT_DIRECTORY', DS . 'unit');
defined('UNIT_DESTINATION') || define('UNIT_DESTINATION', 'test' . DS . 'unit');
defined('SHARED_SOURCE') || define('SHARED_SOURCE', 'boilerplate');
defined('SHARED_CORE') || define('SHARED_CORE', 'workflows');
// 'shared' are explicitly declared
$sharedParam = [
'testAdminUsername' => 'admin',
'testAdminPassword' => 'admin',
'testAdminEmail' => '.@.',
'testAdminFirstname' => 'admin',
'testAdminLastname' => 'admin',
'setupCommand' => 'cmfive.php',
'DBCommand' => 'cmfiveToolsDB.php',
'cmfiveModuleList' => ''
];
$sharedParam['boilerplatePath'] = getcwd();
defined('PHPUNIT_RUN') || define('PHPUNIT_RUN', "");
ini_set('display_errors', 1);
error_reporting(E_ALL);
// before anything else happens, check testrunner is allowed!
if (allowRunner()) {
include "cmfiveToolsDB.php";
echo "\n";
if (!isset($menuMaker)) {
genericRunner($argc, $argv);
} else {
offerMenuTests();
$cmdMaker['tests'][] =
[
'request' => "unit", 'message' => "Launching UnitTest", 'function' => "genericRunner", 'args' => true,
'hint' => "moduleName or all"
];
$cmdMaker['tests'][] =
[
'request' => "module", 'message' => "Launching Tests on Module", 'function' => "genericRunner", 'args' => true,
'hint' => "moduleName"
];
$cmdMaker['testDB'][] =
[
'request' => "setup", 'message' => "Batched TestRunner DB setup", 'function' => "genericRunner", 'args' => true, 'implied' => true
];
}
}
function offerMenuTests()
{
global $menuMaker;
$menuMaker[] =
[
'option' => "Build TestRunner (show info)", 'message' => "TestRunner info", 'function' => "infoRunner", 'param' => null
];
$menuMaker[] =
[
'option' => "Setup empty TestRunner DB and Administrator", 'message' => "Batched TestRunner DB setup", 'function' => "DBRunner", 'param' => null
];
$menuMaker[] =
[
'option' => "Show Composer module dependencies", 'message' => "Showing Composer module dependencies:", 'function' => "moduleDependencies", 'param' => null
];
$found = chaseModules("all");
// we cared a lot about this for ordered module names on accpetance tests
// but more generally, it seems an oblique insistence to sort only some test catagories?
// if(!empty($found['UnitTests'])) {
// ksort($found["UnitTests"]);
// }
foreach ($found as $capabilities => $capability) {
if ($capabilities == "UnitTests") {
foreach ($capability as $module => $resources) {
$menuMaker[] =
[
'option' => "Run " . $module . " tests", 'message' => "Launching TestRunner: " . $module, 'function' => "moduleRunner", 'param' => $module
];
}
}
}
$menuMaker[] =
[
'option' => "Run all unit tests", 'message' => "Launching UnitTests", 'function' => "unitTestRunner", 'param' => "all"
];
}
function moduleRunner($runModule)
{
unitRunner($runModule);
}
function unitRunner($run_module)
{
purgeTestCode();
$run_module = ltrim($run_module);
$run_module = empty($run_module) ? "all" : $run_module;
$found = chaseModules($run_module);
foreach ($found as $capabilities => $capability) {
if ($capabilities == "UnitTests") {
foreach ($capability as $module => $resources) {
if ($module == $run_module || $run_module == "all") {
foreach ($resources as $resource) {
$file_name = PHPUNIT_RUN . " " . UNIT_DESTINATION . DS . $resource . " ";
$packBar = "\n*" . str_repeat("-", strlen($file_name) + 12) . "*\n";
echo $packBar . "| " . "\e[31m" . $file_name . " \e[39m| \e[31mTESTING \e[39m|" . $packBar;
$output = [];
$status_code = 0;
// Execute unit test, saving the output and status code.
executeUnitTest($file_name, $output, $status_code);
// If we received a non-zero status code, the test failed. Print the output to console.
if ($status_code !== 0) {
echo $packBar . "| " . "\e[31m" . $file_name . " \e[39m| \e[31mFAILED! \e[39m|" . $packBar;
foreach ($output as $o) {
echo "\e[31m$o\n\e[39m";
}
exit($status_code);
}
}
}
}
}
}
}
function infoRunner()
{
genericRunner(null, null);
}
function unitTestRunner()
{
genericRunner(2, ["", "unit"]);
}
function DBRunner()
{
genericRunner(2, ["", "setup"]);
}
function genericRunner($argc, $argv)
{
purgeTestCode();
$found = chaseModules("all");
reportModules($found);
echo "\n --- To launch: --- \ncmfiveTests module [module]\n\n";
if ($argc > 1) {
switch (strtolower($argv[1])) {
case "unit":
$unitModule = "";
if ($argc > 2) {
$unitModule = " " . $argv[2];
}
unitRunner($unitModule);
break;
case "module":
$module = "";
if ($argc > 2) {
$module = $argv[2];
}
moduleRunner($module);
break;
case "clean":
purgeTestCode();
break;
case "setup":
batchTestSetup();
break;
}
}
}
function allowRunner()
{
$webFind = "system/web.php";
if (chaseWeb($webFind)) {
if (!class_exists('Web')) {
// Testrunner leans heavily on config, so always reset it
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo exec('del .\cache\config.cache');
} else {
echo exec('rm -f cache/config.cache');
}
require("system/web.php");
}
$w = new Web();
if (Config::get("tests.testrunner") == "ENABLED") {
if (!checkTestEnvironment()) {
echo "Configure Test Runner and database backup in config.php per examples.\n";
return false;
}
return true;
} else {
echo "Test runner not enabled in config\n";
}
} else {
echo "system/web.php not found\n";
}
return false;
}
function checkTestEnvironment()
{
if (
Config::get("database.backups.commandPath")
&& Config::get("database.backups.backupCommand")
&& Config::get("database.backups.restoreCommand")
) {
echo "Found: TestDB configuration\n";
} else {
echo "Useful TestRunner configuration not found in config.php\n";
return false;
}
return true;
}
function chaseWeb($webFind)
{
// so we can find modules & some CM5 functions...
if (is_readable($webFind) && !is_dir($webFind)) {
return true;
}
return false;
}
function batchTestSetup()
{
global $sharedParam;
echo "Starting with backup.\n";
echo (shell_exec("php cmfive.php DB backup") . "\n");
if (checkABackup(ROOT_PATH . DS . BACKUP_DIRECTORY) == 0) {
echo "Stopping, useful backup not confirmed.\n\n";
return;
}
$Mcommand = [
//"cmfive.php DB backup",
"cmfiveToolsDB.php purge",
"cmfive.php install migrations",
"cmfive.php seed admin "
. $sharedParam['testAdminUsername'] . " "
. $sharedParam['testAdminPassword'] . " "
. $sharedParam['testAdminEmail'] . " "
. $sharedParam['testAdminFirstname'] . " "
. $sharedParam['testAdminLastname'],
"cmfive.php DB sample"
];
foreach ($Mcommand as $command) {
echo "Batching: " . $command . "\n";
echo (shell_exec("php " . $command) . "\n");
}
}
/**
* Calls exec on the $file_name parameter, and passes the $output and $status_code
* parameters back as references.
*
* @param string $file_name
* @param array<string>|null $output
* @param string $status_code
* @return void
*/
function executeUnitTest(string $file_name, ?array &$output, string &$status_code): void
{
try {
exec("cd " . ROOT_PATH . " && phpunit " . $file_name, $output, $status_code);
} catch (Exception $e) {
echo $e->getMessage();
}
}
function purgeTestCode()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
exec('del ' . UNIT_DESTINATION . DS . '*.php');
} else {
exec('rm -f ' . UNIT_DESTINATION . DS . '*.php');
}
}
function moduleDependencies()
{
$w = new Web();
$w->initDB();
foreach ($w->modules() as $module) {
$dependencies = Config::get("{$module}.dependencies");
if (!empty($dependencies)) {
echo "Module: " . $module;
foreach ($dependencies as $req => $ver) {
echo " / " . $req . "->" . $ver;
}
echo "\n";
}
}
echo "\n";
}
function reportModules($moduleCapabilities)
{
echo "\nRefreshed:";
foreach ($moduleCapabilities as $capabilities => $capability) {
if ($capabilities != "Paths") {
echo "\n --- " . $capabilities . " ---";
foreach ($capability as $module => $resources) {
echo "\n" . $module . ":";
foreach ($resources as $resource) {
echo " " . $resource;
}
}
}
}
echo "\n";
}
function chaseModules($module_name)
{
global $sharedParam;
$moduleCapabilities = [];
$w = new Web();
$w->initDB();
// Read all modules directories for any tests that need to be copied over
if ($module_name === 'all') {
foreach ($w->modules() as $module) {
$availableTests[] = getTestsForModule($module);
$sharedParam['cmfiveModuleList'] .= ":" . $module;
}
} else {
$availableTests[] = getTestsForModule($module_name);
}
$sharedParam['cmfiveModuleList'] .= ":";
$availableTests[] = getTestsForModule(SHARED_SOURCE);
$availableTests[] = getTestsForModule(SHARED_CORE);
foreach ($availableTests as $moduleTest) {
foreach ($moduleTest as $module => $fileShift) {
foreach ($fileShift as $from => $file) {
if (!empty($fileShift)) {
try {
copy($file['source'], $file['dest']);
if (isset($file['unit'])) {
$moduleCapabilities['UnitTests'][$module][] = $file['unit'];
}
$moduleCapabilities['Paths'][$module][] = $from;
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
}
}
return $moduleCapabilities;
}
function getTestsForModule($module)
{
$availableTests = [];
// Check modules folder
$module_path = PROJECT_MODULE_DIRECTORY . DS . $module . DS . "tests";
$system_module_path = SYSTEM_MODULE_DIRECTORY . DS . $module . DS . "tests";
$boiler_path = BOILERPLATE_TEST_DIRECTORY . DS . $module;
$workflow_path = WORKFLOWS_TEST_DIRECTORY . DS . $module;
$test_paths = [$module_path, $system_module_path, $boiler_path, $workflow_path];
$extended_paths = [UNIT_DIRECTORY];
$dest_paths = [
UNIT_DIRECTORY => UNIT_DESTINATION
];
if (empty($availableTests[$module])) {
$availableTests[$module] = [];
}
foreach ($test_paths as $base_path) {
foreach ($extended_paths as $ext) {
$test_path = $base_path . $ext;
$full_path = ROOT_PATH . DS . $test_path;
if (is_dir($full_path)) {
foreach (scandir($full_path) as $file) {
if ((!is_dir($full_path . DS . $file))
&& (strtolower(pathinfo($file, PATHINFO_EXTENSION) == "php"))
) {
$modFile = $file;
$details = [];
if ($ext == UNIT_DIRECTORY) {
$modFile = $module . "/" . $file;
$details['unit'] = $modFile;
//Create $module directory if it doesn't exist
if (!is_dir(ROOT_PATH . DS . $dest_paths[$ext] . DS . $module)) {
mkdir(ROOT_PATH . DS . $dest_paths[$ext] . DS . $module);
}
}
$details['source'] = $full_path . DS . $file;
$details['dest'] = ROOT_PATH . DS . $dest_paths[$ext] . DS . $modFile;
$availableTests[$module][$test_path . "::" . $file] = $details;
}
}
}
}
}
return $availableTests;
}
// // unused now, no silent option!
// function getUserInput($prompt = "Command: ")
// {
// $command = '';
// if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// echo $prompt;
// $command = stream_get_line(STDIN, 1024, PHP_EOL);
// } else {
// $command = readline($prompt);
// }
// return $command;
// }