1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MyTester; |
5
|
|
|
|
6
|
|
|
use Nette\Utils\Finder; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Testing Environment |
10
|
|
|
* |
11
|
|
|
* @author Jakub Konečný |
12
|
|
|
*/ |
13
|
|
|
final class Environment { |
14
|
|
|
use \Nette\StaticClass; |
15
|
|
|
|
16
|
|
|
public const NAME = "My Tester"; |
17
|
|
|
public const VERSION = "2.0.0-dev"; |
18
|
|
|
|
19
|
|
|
private static int $taskCount = 0; |
20
|
|
|
private static bool $set = false; |
21
|
|
|
/** @var SkippedTest[] */ |
22
|
|
|
private static array $skipped = []; |
23
|
|
|
public static string $currentJob = ""; |
24
|
|
|
public static bool $shouldFail = false; |
25
|
|
|
private static string $results = ""; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Prints result of a test |
29
|
|
|
*/ |
30
|
|
|
public static function testResult(string $text, bool $success = true): void { |
31
|
|
|
static::incCounter(); |
32
|
|
|
if($success) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
static::printLine("Test " . static::$taskCount . " failed. $text"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Checks if environment was set |
40
|
|
|
*/ |
41
|
|
|
public static function isSetUp(): bool { |
42
|
|
|
return static::$set; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Increases task counter |
47
|
|
|
* |
48
|
|
|
* @internal |
49
|
|
|
*/ |
50
|
|
|
public static function incCounter(): void { |
51
|
|
|
static::$taskCount++; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Resets task counter |
56
|
|
|
* |
57
|
|
|
* @internal |
58
|
|
|
*/ |
59
|
|
|
public static function resetCounter(): void { |
60
|
|
|
static::$taskCount = 0; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getCounter(): int { |
64
|
|
|
return static::$taskCount; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Prints entered text with correct line ending |
69
|
|
|
*/ |
70
|
|
|
public static function printLine(string $text = ""): void { |
71
|
|
|
echo "$text\n"; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @internal |
76
|
|
|
*/ |
77
|
|
|
public static function addResult(string $result): void { |
78
|
|
|
static::$results .= $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @internal |
83
|
|
|
*/ |
84
|
|
|
public static function addSkipped(string $jobName, string $reason = ""): void { |
85
|
|
|
static::$skipped[] = new SkippedTest($jobName, $reason); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return SkippedTest[] |
90
|
|
|
*/ |
91
|
|
|
public static function getSkipped(): array { |
92
|
|
|
return static::$skipped; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function getShouldFail(): bool { |
96
|
|
|
return static::$shouldFail; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function setShouldFail(bool $value): void { |
100
|
|
|
static::$shouldFail = $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Print version of My Tester and PHP |
105
|
|
|
*/ |
106
|
|
|
public static function printInfo(): void { |
107
|
|
|
static::printLine(static::NAME . " " . static::VERSION); |
108
|
|
|
static::printLine(); |
109
|
|
|
static::printLine("PHP " . PHP_VERSION . "(" . PHP_SAPI . ")"); |
110
|
|
|
static::printLine(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function printResults(): void { |
114
|
|
|
$results = static::$results; |
115
|
|
|
static::printLine($results); |
116
|
|
|
static::printSkipped(); |
117
|
|
|
$failed = str_contains($results, TestCase::RESULT_FAILED); |
118
|
|
|
if(!$failed) { |
119
|
|
|
static::printLine(); |
120
|
|
|
echo "OK"; |
121
|
|
|
} else { |
122
|
|
|
static::printFailed(); |
123
|
|
|
static::printLine(); |
124
|
|
|
echo "Failed"; |
125
|
|
|
} |
126
|
|
|
$resultsLine = " (" . strlen($results) . " tests"; |
127
|
|
|
if($failed) { |
128
|
|
|
$resultsLine .= ", " . substr_count($results, TestCase::RESULT_FAILED) . " failed"; |
129
|
|
|
} |
130
|
|
|
if(str_contains($results, TestCase::RESULT_SKIPPED)) { |
131
|
|
|
$resultsLine .= ", " . substr_count($results, TestCase::RESULT_SKIPPED) . " skipped"; |
132
|
|
|
} |
133
|
|
|
$time = \Tracy\Debugger::timer(static::NAME); |
134
|
|
|
$resultsLine .= ", $time second(s))"; |
135
|
|
|
static::printLine($resultsLine); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Print info about skipped tests |
140
|
|
|
*/ |
141
|
|
|
private static function printSkipped(): void { |
142
|
|
|
foreach(static::getSkipped() as $skipped) { |
143
|
|
|
$reason = ""; |
144
|
|
|
if($skipped->reason) { |
145
|
|
|
$reason = ": {$skipped->reason}"; |
146
|
|
|
} |
147
|
|
|
static::printLine("Skipped $skipped->name$reason"); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Print info about failed tests |
153
|
|
|
*/ |
154
|
|
|
private static function printFailed(): void { |
155
|
|
|
$filenameSuffix = ".errors"; |
156
|
|
|
$files = Finder::findFiles("*$filenameSuffix")->in(\getTestsDirectory()); |
157
|
|
|
/** @var \SplFileInfo $file */ |
158
|
|
|
foreach($files as $name => $file) { |
159
|
|
|
static::printLine("--- " . $file->getBasename($filenameSuffix)); |
160
|
|
|
echo file_get_contents($name); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Sets up the environment |
166
|
|
|
*/ |
167
|
|
|
public static function setup(): void { |
168
|
|
|
if(static::$set) { |
169
|
|
|
static::printLine("Warning: Testing Environment was already set up."); |
170
|
|
|
return; |
171
|
|
|
} |
172
|
|
|
\Tracy\Debugger::timer(static::NAME); |
173
|
|
|
static::$set = true; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
?> |