|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace mheap\GithubActionsReporter\Functions; |
| 4 | + |
| 5 | +use mheap\GithubActionsReporter\Printer6; |
| 6 | +use mheap\GithubActionsReporter\Printer7; |
| 7 | +use mheap\GithubActionsReporter\Printer8; |
| 8 | +use mheap\GithubActionsReporter\Printer9; |
| 9 | +use PHPUnit\Framework\TestFailure; |
| 10 | +use ReflectionClass; |
| 11 | + |
| 12 | +/** |
| 13 | + * @param $version |
| 14 | + * |
| 15 | + * @return string|null Fully Qualified Class Name, or null if no matching version |
| 16 | + * |
| 17 | + * @internal |
| 18 | + */ |
| 19 | +function determinePrinter($version) |
| 20 | +{ |
| 21 | + $versionMatrix = [ |
| 22 | + // greater than equals, lower than equals, printer FQCN |
| 23 | + ['6.0', '6.99.99', Printer6::class], |
| 24 | + ['7.0', '7.99.99', Printer7::class], |
| 25 | + ['8.0', '8.99.99', Printer8::class], |
| 26 | + ['9.0', true, Printer9::class], |
| 27 | + ]; |
| 28 | + |
| 29 | + foreach ($versionMatrix as list($lowerVersion, $upperVersion, $class)) { |
| 30 | + if ( |
| 31 | + version_compare($version, $lowerVersion, '>=') == true && |
| 32 | + ($upperVersion === true || version_compare($version, $upperVersion, '<=') == true) |
| 33 | + ) { |
| 34 | + return $class; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return null; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * @param TestFailure $defect |
| 43 | + * @param string $defectType |
| 44 | + * |
| 45 | + * @return string |
| 46 | + * @throws \ReflectionException |
| 47 | + * @internal |
| 48 | + */ |
| 49 | +function printDefectTrace($defect, $defectType) |
| 50 | +{ |
| 51 | + $e = $defect->thrownException(); |
| 52 | + |
| 53 | + $errorLines = array_filter( |
| 54 | + explode("\n", (string)$e), |
| 55 | + static function ($l) { |
| 56 | + return $l; |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + $error = end($errorLines); |
| 61 | + $lineIndex = strrpos($error, ":"); |
| 62 | + $path = substr($error, 0, $lineIndex); |
| 63 | + $line = substr($error, $lineIndex + 1); |
| 64 | + |
| 65 | + list($reflectedPath, $reflectedLine) = getReflectionFromTest( |
| 66 | + $defect->getTestName() |
| 67 | + ); |
| 68 | + |
| 69 | + if ($path !== $reflectedPath) { |
| 70 | + $path = $reflectedPath; |
| 71 | + $line = $reflectedLine; |
| 72 | + } |
| 73 | + |
| 74 | + $message = explode("\n", $defect->getExceptionAsString()); |
| 75 | + $message = implode('%0A', $message); |
| 76 | + |
| 77 | + // Some messages might contain paths. Let's convert thost to relative paths too |
| 78 | + $message = relativePath($message); |
| 79 | + $message = preg_replace('/%0A$/', '', $message); |
| 80 | + |
| 81 | + $path = relativePath($path); |
| 82 | + $file = "file={$path}"; |
| 83 | + $line = "line={$line}"; |
| 84 | + |
| 85 | + return "::{$defectType} $file,$line::{$message}\n"; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param string $path |
| 90 | + * |
| 91 | + * @return mixed |
| 92 | + * @internal |
| 93 | + */ |
| 94 | +function relativePath($path) |
| 95 | +{ |
| 96 | + $relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path); |
| 97 | + |
| 98 | + // Translate \ in to / for Windows |
| 99 | + return str_replace('\\', '/', $relative); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * @param string $name |
| 104 | + * |
| 105 | + * @return array |
| 106 | + * @throws \ReflectionException |
| 107 | + * @internal |
| 108 | + */ |
| 109 | +function getReflectionFromTest($name) |
| 110 | +{ |
| 111 | + list($klass, $method) = explode('::', $name); |
| 112 | + |
| 113 | + // Handle data providers |
| 114 | + $parts = explode(" ", $method, 2); |
| 115 | + if (count($parts) > 1) { |
| 116 | + $method = $parts[0]; |
| 117 | + } |
| 118 | + |
| 119 | + $c = new ReflectionClass($klass); |
| 120 | + $m = $c->getMethod($method); |
| 121 | + |
| 122 | + return [$m->getFileName(), $m->getStartLine()]; |
| 123 | +} |
0 commit comments