-
Notifications
You must be signed in to change notification settings - Fork 7
PHPUnit 6 support + refactoring #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8fdacfd
c415071
ebb02cf
8e1280f
056db35
d17d8fd
476a2f1
22037ee
952dd7e
e55ddd6
1457501
def0c6b
b489c6a
36d473d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace mheap\GithubActionsReporter\Functions; | ||
|
||
use mheap\GithubActionsReporter\Printer6; | ||
use mheap\GithubActionsReporter\Printer7; | ||
use mheap\GithubActionsReporter\Printer8; | ||
use mheap\GithubActionsReporter\Printer9; | ||
use PHPUnit\Framework\TestFailure; | ||
use ReflectionClass; | ||
|
||
/** | ||
* @param $version | ||
* | ||
* @return string|null Fully Qualified Class Name, or null if no matching version | ||
* | ||
* @internal | ||
*/ | ||
function determinePrinter($version) | ||
{ | ||
$versionMatrix = [ | ||
// greater than equals, lower than equals, printer FQCN | ||
['6.0', '6.99.99', Printer6::class], | ||
['7.0', '7.99.99', Printer7::class], | ||
['8.0', '8.99.99', Printer8::class], | ||
['9.0', true, Printer9::class], | ||
]; | ||
|
||
foreach ($versionMatrix as list($lowerVersion, $upperVersion, $class)) { | ||
if ( | ||
version_compare($version, $lowerVersion, '>=') == true && | ||
($upperVersion === true || version_compare($version, $upperVersion, '<=') == true) | ||
) { | ||
return $class; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param TestFailure $defect | ||
* @param string $defectType | ||
* | ||
* @return string | ||
* @throws \ReflectionException | ||
* @internal | ||
*/ | ||
function printDefectTrace($defect, $defectType) | ||
{ | ||
$e = $defect->thrownException(); | ||
|
||
$errorLines = array_filter( | ||
explode("\n", (string)$e), | ||
static function ($l) { | ||
return $l; | ||
} | ||
); | ||
|
||
$error = end($errorLines); | ||
$lineIndex = strrpos($error, ":"); | ||
$path = substr($error, 0, $lineIndex); | ||
$line = substr($error, $lineIndex + 1); | ||
|
||
list($reflectedPath, $reflectedLine) = 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 = relativePath($message); | ||
$message = preg_replace('/%0A$/', '', $message); | ||
|
||
$path = relativePath($path); | ||
$file = "file={$path}"; | ||
$line = "line={$line}"; | ||
|
||
return "::{$defectType} $file,$line::{$message}\n"; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* | ||
* @return mixed | ||
* @internal | ||
*/ | ||
function relativePath($path) | ||
{ | ||
$relative = str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $path); | ||
|
||
// Translate \ in to / for Windows | ||
return str_replace('\\', '/', $relative); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return array | ||
* @throws \ReflectionException | ||
* @internal | ||
*/ | ||
function getReflectionFromTest($name) | ||
{ | ||
list($klass, $method) = explode('::', $name); | ||
|
||
// Handle data providers | ||
$parts = explode(" ", $method, 2); | ||
if (count($parts) > 1) { | ||
$method = $parts[0]; | ||
} | ||
|
||
$c = new ReflectionClass($klass); | ||
$m = $c->getMethod($method); | ||
|
||
return [$m->getFileName(), $m->getStartLine()]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace mheap\GithubActionsReporter; | ||
|
||
use PHPUnit\TextUI\ResultPrinter; | ||
use PHPUnit\Framework\TestFailure; | ||
use PHPUnit\Framework\TestResult; | ||
|
||
use function mheap\GithubActionsReporter\Functions\printDefectTrace; | ||
|
||
class Printer6 extends ResultPrinter | ||
{ | ||
/** | ||
* @var null|string | ||
*/ | ||
private $currentType; | ||
|
||
protected function printHeader(): void | ||
{ | ||
} | ||
|
||
protected function writeProgress($progress): void | ||
{ | ||
} | ||
|
||
protected function printFooter(TestResult $result): void | ||
{ | ||
} | ||
|
||
protected function printDefects(array $defects, $type): void | ||
{ | ||
$this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning'; | ||
|
||
foreach ($defects as $i => $defect) { | ||
$this->printDefect($defect, $i); | ||
} | ||
} | ||
|
||
protected function printDefectHeader(TestFailure $defect, $count): void | ||
{ | ||
} | ||
|
||
/** | ||
* @throws \ReflectionException | ||
*/ | ||
protected function printDefectTrace(TestFailure $defect): void | ||
{ | ||
$this->write(printDefectTrace($defect, $this->currentType)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,48 @@ | |
|
||
namespace mheap\GithubActionsReporter; | ||
|
||
use PHPUnit\Framework\TestFailure; | ||
use PHPUnit\Framework\TestResult; | ||
use PHPUnit\TextUI\ResultPrinter; | ||
|
||
use function mheap\GithubActionsReporter\Functions\getCurrentType; | ||
use function mheap\GithubActionsReporter\Functions\printDefects; | ||
use function mheap\GithubActionsReporter\Functions\printDefectTrace; | ||
|
||
class Printer8 extends ResultPrinter | ||
{ | ||
use Trait8; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was the thinking behind removing the Trait here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a trait allows us to keep the functionality DRY whilst still extending from different classes. What are the benefits/risks of using a base class rather than a trait in this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the method signatures vary over the PHPUnit versions, and traits are not actually concrete testable structures, sort of invisible magic. Trait8 & Printer7 are already repeating logic because of this. |
||
/** | ||
* @var null|string | ||
*/ | ||
private $currentType; | ||
|
||
protected function printHeader(TestResult $result): void | ||
{ | ||
} | ||
|
||
protected function writeProgress(string $progress): void | ||
{ | ||
} | ||
|
||
protected function printFooter(TestResult $result): void | ||
{ | ||
} | ||
|
||
protected function printDefects(array $defects, string $type): void | ||
{ | ||
$this->currentType = (in_array($type, ['error', 'failure']) === true) ? 'error' : 'warning'; | ||
|
||
foreach ($defects as $i => $defect) { | ||
$this->printDefect($defect, $i); | ||
} | ||
} | ||
|
||
protected function printDefectHeader(TestFailure $defect, int $count): void | ||
{ | ||
} | ||
|
||
protected function printDefectTrace(TestFailure $defect): void | ||
{ | ||
$this->write(printDefectTrace($defect, $this->currentType)); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.