Skip to content

Instantly share code, notes, and snippets.

@veproza
Created August 25, 2018 19:35
Show Gist options
  • Save veproza/c21b4e5cf61630eda5e973efc2b41e17 to your computer and use it in GitHub Desktop.
Save veproza/c21b4e5cf61630eda5e973efc2b41e17 to your computer and use it in GitHub Desktop.
A Setup.php file compatible with current (2.0) Nette Tester
<?php
/**
* By jiripudil, https://github.com/jiripudil/intellij-nette-tester/blob/master/resources/setup.php
* @license https://github.com/jiripudil/intellij-nette-tester/blob/master/LICENSE.md
*/
use Tester\Runner\Runner;
final class TeamCityOutputHandler implements \Tester\Runner\OutputHandler
{
/**
* @var resource
*/
private $file;
public function __construct($output = 'php://output')
{
$this->file = \fopen($output, 'w');
}
public function begin(): void
{
// \fwrite($this->file, $this->message('testCount', array('count' => 0)));
}
function finish(\Tester\Runner\Test $test): void
{
$testName = $this->getTestName($test);
$message = $test->message;
$flowId = \md5($test->getSignature());
\fwrite($this->file, $this->message('testStarted', ['name' => $testName, 'flowId' => $flowId]));
if ($test->getResult() === \Tester\Runner\Test::SKIPPED) {
\fwrite($this->file, $this->message('testIgnored', ['name' => $testName, 'flowId' => $flowId, 'message' => 'Test skipped', 'details' => $message]));
} elseif ($test->getResult() === \Tester\Runner\Test::FAILED) { // Runner::FAILED, Test::FAILED
$extraArguments = [];
if (\preg_match("/^diff \"(.*)\" \"(.*)\"$/m", $message, $matches)) { // Windows build
$expectedFile = \str_replace('""', '"', $matches[1]);
$actualFile = \str_replace('""', '"', $matches[2]);
$extraArguments = ['type' => 'comparisonFailure', 'expectedFile' => $expectedFile, 'actualFile' => $actualFile];
} elseif (\preg_match("/^diff '?(.*)'? '?(.*)'?$/m", $message, $matches)) {
$expectedFile = \trim($matches[1], "'");
$actualFile = \trim($matches[2], "'");
$extraArguments = ['type' => 'comparisonFailure', 'expectedFile' => $expectedFile, 'actualFile' => $actualFile];
} elseif (\preg_match("/Failed: (.*) should be( equal to)?\s+\.*\s*(.*) in/is", $message, $matches)) {
$expected = $matches[3];
$actual = $matches[1];
$extraArguments = ['type' => 'comparisonFailure', 'expected' => $expected, 'actual' => $actual];
}
$args = \array_merge([
'name' => $testName,
'flowId' => $flowId,
'message' => 'Test failed',
'details' => $message,
'duration' => $duration,
], $extraArguments);
\fwrite($this->file, $this->message('testFailed', $args));
}
\fwrite($this->file, $this->message('testFinished', ['name' => $testName, 'flowId' => $flowId, 'duration' => $duration]));
}
private function message($messageName, $args)
{
$argsPairs = [];
foreach ($args as $arg => $value) {
$argsPairs[] = \sprintf("%s='%s'", $arg, $this->escape($value));
}
return \sprintf(
"##teamcity[%s %s]\n\n",
$messageName,
\implode(' ', $argsPairs)
);
}
private function escape($value)
{
$replace = [
"|" => "||",
"'" => "|'",
"\n" => "|n",
"\r" => "|r",
"]" => "|]",
"[" => "|[",
];
return \strtr($value, $replace);
}
public function end(): void
{
}
function prepare(\Tester\Runner\Test $test): void
{
// noop
}
/**
* @param \Tester\Runner\Test $test
* @return null|string
*/
private function getTestName(\Tester\Runner\Test $test)
{
return $test->title
? $test->title
: str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $test->getFile());
}
}
/** @var Runner $runner */
// replace registered output handlers with TC
$runner->outputHandlers = [new TeamCityOutputHandler()];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment