|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace mheap\GithubActionsReporter; |
| 4 | + |
| 5 | +use PHPUnit\TextUI\ResultPrinter; |
| 6 | +use PHPUnit\Framework\TestFailure; |
| 7 | +use PHPUnit\Framework\TestResult; |
| 8 | + |
| 9 | +class Printer7 extends ResultPrinter |
| 10 | +{ |
| 11 | + protected $currentType = null; |
| 12 | + |
| 13 | + protected function printHeader(): void |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + protected function writeProgress(string $progress): void |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + protected function printFooter(TestResult $result): void |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + protected function printDefects(array $defects, string $type): void |
| 26 | + { |
| 27 | + $this->currentType = $type; |
| 28 | + |
| 29 | + foreach ($defects as $i => $defect) { |
| 30 | + $this->printDefect($defect, $i); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + protected function printDefectHeader(TestFailure $defect, int $count): void |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + protected function printDefectTrace(TestFailure $defect): void |
| 39 | + { |
| 40 | + $e = $defect->thrownException(); |
| 41 | + |
| 42 | + $errorLines = array_filter( |
| 43 | + explode("\n", (string)$e), |
| 44 | + function ($l) { |
| 45 | + return $l; |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + $error = end($errorLines); |
| 50 | + $lineIndex = strrpos($error, ":"); |
| 51 | + $path = substr($error, 0, $lineIndex); |
| 52 | + $line = substr($error, $lineIndex + 1); |
| 53 | + |
| 54 | + list($reflectedPath, $reflectedLine) = $this->getReflectionFromTest( |
| 55 | + $defect->getTestName() |
| 56 | + ); |
| 57 | + |
| 58 | + if ($path !== $reflectedPath) { |
| 59 | + $path = $reflectedPath; |
| 60 | + $line = $reflectedLine; |
| 61 | + } |
| 62 | + |
| 63 | + $message = explode("\n", $defect->getExceptionAsString()); |
| 64 | + $message = implode('%0A', $message); |
| 65 | + |
| 66 | + // Some messages might contain paths. Let's convert thost to relative paths too |
| 67 | + $message = $this->relativePath($message); |
| 68 | + |
| 69 | + $message = preg_replace('/%0A$/', '', $message); |
| 70 | + |
| 71 | + $type = $this->getCurrentType(); |
| 72 | + $file = "file={$this->relativePath($path)}"; |
| 73 | + $line = "line={$line}"; |
| 74 | + $this->write("::{$type} $file,$line::{$message}\n"); |
| 75 | + } |
| 76 | + |
| 77 | + protected function getCurrentType() |
| 78 | + { |
| 79 | + if (in_array($this->currentType, ['error', 'failure'])) { |
| 80 | + return 'error'; |
| 81 | + } |
| 82 | + |
| 83 | + return 'warning'; |
| 84 | + } |
| 85 | + |
| 86 | + protected function relativePath(string $path) |
| 87 | + { |
| 88 | + $relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path); |
| 89 | + // Translate \ in to / for Windows |
| 90 | + $relative = str_replace('\\', '/', $relative); |
| 91 | + return $relative; |
| 92 | + } |
| 93 | + |
| 94 | + protected function getReflectionFromTest(string $name) |
| 95 | + { |
| 96 | + list($klass, $method) = explode('::', $name); |
| 97 | + $c = new \ReflectionClass($klass); |
| 98 | + $m = $c->getMethod($method); |
| 99 | + |
| 100 | + return [$m->getFileName(), $m->getStartLine()]; |
| 101 | + } |
| 102 | +} |
0 commit comments