-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPrinter7.php
102 lines (79 loc) · 2.59 KB
/
Printer7.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
<?php
namespace mheap\GithubActionsReporter;
use PHPUnit\TextUI\ResultPrinter;
use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestResult;
class Printer7 extends ResultPrinter
{
protected $currentType = null;
protected function printHeader(): void
{
}
protected function writeProgress(string $progress): void
{
}
protected function printFooter(TestResult $result): void
{
}
protected function printDefects(array $defects, string $type): void
{
$this->currentType = $type;
foreach ($defects as $i => $defect) {
$this->printDefect($defect, $i);
}
}
protected function printDefectHeader(TestFailure $defect, int $count): void
{
}
protected function printDefectTrace(TestFailure $defect): void
{
$e = $defect->thrownException();
$errorLines = array_filter(
explode("\n", (string)$e),
function ($l) {
return $l;
}
);
$error = end($errorLines);
$lineIndex = strrpos($error, ":");
$path = substr($error, 0, $lineIndex);
$line = substr($error, $lineIndex + 1);
list($reflectedPath, $reflectedLine) = $this->getReflectionFromTest(
$defect->getTestName()
);
if ($path !== $reflectedPath) {
$path = $reflectedPath;
$line = $reflectedLine;
}
$message = explode("\n", $defect->getExceptionAsString());
$message = implode('%0A', $message);
// Some messages might contain paths. Let's convert thost to relative paths too
$message = $this->relativePath($message);
$message = preg_replace('/%0A$/', '', $message);
$type = $this->getCurrentType();
$file = "file={$this->relativePath($path)}";
$line = "line={$line}";
$this->write("::{$type} $file,$line::{$message}\n");
}
protected function getCurrentType()
{
if (in_array($this->currentType, ['error', 'failure'])) {
return 'error';
}
return 'warning';
}
protected function relativePath(string $path)
{
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path);
// Translate \ in to / for Windows
$relative = str_replace('\\', '/', $relative);
return $relative;
}
protected function getReflectionFromTest(string $name)
{
list($klass, $method) = explode('::', $name);
$c = new \ReflectionClass($klass);
$m = $c->getMethod($method);
return [$m->getFileName(), $m->getStartLine()];
}
}