Skip to content

Commit 562faa8

Browse files
committedJan 18, 2020
Initial Commit
0 parents  commit 562faa8

File tree

5 files changed

+1652
-0
lines changed

5 files changed

+1652
-0
lines changed
 

Diff for: ‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

Diff for: ‎README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## phpunit-github-actions-printer
2+
3+
> There's a zero-config way to achieve this at [mheap/phpunit-matcher-action](https://github.com/mheap/phpunit-matcher-action)
4+
5+
This is a PHPUnit printer that uses the `::error` and `::warning` functionality of GitHub Actions to add annotiations for failing test runs. It's main differentiator to the above is that it supports adding warnings in addition to errors.
6+
7+
## Usage
8+
9+
Add this printer to your project
10+
11+
```bash
12+
composer require --dev mheap/phpunit-github-actions-printer
13+
```
14+
15+
When you run your tests, specify `mheap\GithubActionsReporter\Printer` as the printer to use
16+
17+
```bash
18+
./vendor/bin/phpunit --printer mheap\\GithubActionsReporter\\Printer /path/to/tests
19+
```

Diff for: ‎composer.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "mheap/phpunit-github-actions-printer",
3+
"description": "PHPUnit Printer for adding test failures as annotations on GitHub Actions",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Michael Heap",
9+
"email": "m@michaelheap.com"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"mheap\\GithubActionsReporter\\": "src"
15+
}
16+
},
17+
"require": {},
18+
"require-dev": {
19+
"phpunit/phpunit": "^8"
20+
}
21+
}

Diff for: ‎composer.lock

+1,534
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎src/Printer.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace mheap\GithubActionsReporter;
4+
5+
use PHPUnit\Util\Filter;
6+
use PHPUnit\Framework\Test;
7+
use PHPUnit\Runner\Version;
8+
use PHPUnit\Framework\TestResult;
9+
use PHPUnit\TextUI\ResultPrinter;
10+
use PHPUnit\Framework\TestFailure;
11+
12+
class Printer extends ResultPrinter
13+
{
14+
protected $currentType = null;
15+
16+
protected function printHeader(): void
17+
{
18+
}
19+
20+
protected function writeProgress(string $progress): void
21+
{
22+
}
23+
24+
protected function printFooter(TestResult $result): void
25+
{
26+
}
27+
28+
protected function printDefects(array $defects, string $type): void
29+
{
30+
$this->currentType = $type;
31+
32+
$i=0;
33+
foreach ($defects as $defect) {
34+
$this->printDefect($defect, $i++);
35+
}
36+
}
37+
38+
protected function printDefectHeader(TestFailure $defect, int $count): void
39+
{
40+
}
41+
42+
protected function printDefectTrace(TestFailure $defect): void
43+
{
44+
$e = $defect->thrownException();
45+
46+
$firstError = explode(PHP_EOL, (string)$e)[2];
47+
list($path, $line) = explode(":", $firstError);
48+
49+
if (!$path) {
50+
list($path, $line) = $this->getReflectionFromTest($defect->getTestName());
51+
}
52+
53+
$message = explode(PHP_EOL, $e->getMessage())[0];
54+
55+
$this->write("::{$this->getCurrentType()} file={$this->relativePath($path)},line={$line}::{$message}\n");
56+
}
57+
58+
protected function getCurrentType() {
59+
if (in_array($this->currentType, ['error', 'failure'])) {
60+
return 'error';
61+
}
62+
63+
return 'warning';
64+
}
65+
66+
protected function relativePath(string $path) {
67+
return str_replace(getcwd().'/', "", $path);
68+
}
69+
70+
protected function getReflectionFromTest(string $name) {
71+
list($klass, $method) = explode("::", $name);
72+
$c = new \ReflectionClass($klass);
73+
$m = $c->getMethod($method);
74+
75+
return [$m->getFileName(), $m->getStartLine()];
76+
}
77+
}

0 commit comments

Comments
 (0)
Please sign in to comment.