Skip to content

Commit

Permalink
Add PHPUnit 7 support (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap authored Sep 13, 2020
1 parent 5c2511a commit 93107fd
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
php-versions: ["7.3", "7.4"]
phpunit-version: ["8", "9"]
phpunit-version: ["7", "8", "9"]
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.os }} (PHPUnit ${{ matrix.phpunit-version }})
steps:
- name: Checkout
Expand Down
9 changes: 9 additions & 0 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
use PHPUnit\Runner\Version;
use PHPUnit_TextUI_ResultPrinter;

$low = version_compare(Version::series(), '7.0', '>=');
$high = version_compare(Version::series(), '7.99.99', '<=');

if ($low && $high) {
class Printer extends Printer7
{
}
}

$low = version_compare(Version::series(), '8.0', '>=');
$high = version_compare(Version::series(), '8.99.99', '<=');

Expand Down
102 changes: 102 additions & 0 deletions src/Printer7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()];
}
}

0 comments on commit 93107fd

Please sign in to comment.