-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest
executable file
·138 lines (109 loc) · 3.19 KB
/
test
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
#!/usr/bin/env php
<?php
use Symfony\Component\Process\Process;
require __DIR__ . '/vendor/autoload.php';
function getTestingDirectories(string $dir)
{
return array_filter(
array_map(
function ($directory) use ($dir) {
return "$dir/tests/$directory";
},
array_diff(scandir($dir . '/tests'), ['.', '..'])
),
function ($directory) {
return is_dir($directory);
}
);
}
function getTestName(string $test)
{
$matches = [];
if (preg_match('/^.*\/tests\/(.*?)\/.*\Test.php$/', $test, $matches)) {
return $matches[1];
}
return '';
}
function printLabel(string $label, string $colour)
{
$label = " $label ";
if (strlen($label) < 80) {
$padding = str_repeat(' ', floor((80 - strlen($label)) / 2));
$label = "$padding$label$padding";
$label .= str_repeat(' ', strlen($label) % 2);
}
$blankLine = str_repeat(' ', strlen($label));
echo "$colour$blankLine\033[0m\n";
echo "$colour$label\033[0m\n";
echo "$colour$blankLine\033[0m\n";
}
function printTestLabel(string $test)
{
$testName = getTestName($test);
printLabel("Running $testName tests", "\033[30;106m");
}
function printTestResult(string $test, int $result)
{
echo "\n";
$testName = getTestName($test);
if ($result == 0) {
printLabel("$testName tests passed!", "\033[30;42m");
} else {
printLabel("$testName tests failed!", "\033[30;41m");
}
}
$file = $argv[1];
$testsToRun = [];
$matches = [];
if (preg_match('/^(.*)\/tests\/.*?\/(.*\Test.php)$/', $file, $matches)) {
$testsToRun[] = $file;
$rootDir = $matches[1];
$test = $matches[2];
$testName = substr($file, strrpos($file, '/') + 1);
$fileName = str_replace('Test.php', '.php', $testName);
$appDir = is_dir("$rootDir/app") ? 'app' : 'src';
$file = "$rootDir/$appDir/" . str_replace($testName, $fileName, $test);
}
if (preg_match('/^(.*)\/(?:(?:app)|(?:src))\/(.*\.php)$/', $file, $matches)) {
$rootDir = $matches[1];
$file = $matches[2];
$fileName = substr($file, strrpos($file, '/') + 1);
$testName = str_replace('.php', 'Test.php', $fileName);
$test = str_replace($fileName, $testName, $file);
$files = array_filter(
array_map(
function ($directory) use ($test) {
return "$directory/$test";
},
getTestingDirectories($rootDir)
),
function ($file) {
return is_file($file);
}
);
$testsToRun = array_unique(array_merge($testsToRun, $files));
if (empty($testsToRun)) {
system('clear');
echo "\033[1;31mNo tests found!\033[0m\n\n";
return;
}
}
if (empty($testsToRun)) {
return;
}
$phpunit = "$rootDir/vendor/bin/phpunit";
if (!is_file($phpunit)) {
$phpunit = 'phpunit';
}
system('clear');
foreach ($testsToRun as $test) {
printTestLabel($test);
echo "\nUsing: $phpunit\n";
echo "Running: $test\n";
$process = new Process("$phpunit --color=always $test");
$process->run(function ($type, $buffer) {
echo $buffer;
});
printTestResult($test, $process->getExitCode());
echo "\n";
}