Skip to content

Commit

Permalink
constants are PascalCase (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 2, 2023
1 parent fa7dacc commit 9942c28
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 174 deletions.
14 changes: 7 additions & 7 deletions src/CodeCoverage/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
class Collector
{
public const
ENGINE_PCOV = 'PCOV',
ENGINE_PHPDBG = 'PHPDBG',
ENGINE_XDEBUG = 'Xdebug';
EnginePcov = 'PCOV',
EnginePhpdbg = 'PHPDBG',
EngineXdebug = 'Xdebug';

/** @var resource */
private static $file;
Expand All @@ -29,9 +29,9 @@ class Collector
public static function detectEngines(): array
{
return array_filter([
extension_loaded('pcov') ? [self::ENGINE_PCOV, phpversion('pcov')] : null,
defined('PHPDBG_VERSION') ? [self::ENGINE_PHPDBG, PHPDBG_VERSION] : null,
extension_loaded('xdebug') ? [self::ENGINE_XDEBUG, phpversion('xdebug')] : null,
extension_loaded('pcov') ? [self::EnginePcov, phpversion('pcov')] : null,
defined('PHPDBG_VERSION') ? [self::EnginePhpdbg, PHPDBG_VERSION] : null,
extension_loaded('xdebug') ? [self::EngineXdebug, phpversion('xdebug')] : null,
]);
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public static function start(string $file, string $engine): void
*/
public static function flush(): void
{
if (self::isStarted() && self::$engine === self::ENGINE_PHPDBG) {
if (self::isStarted() && self::$engine === self::EnginePhpdbg) {
self::save();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/CodeCoverage/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
abstract class AbstractGenerator
{
protected const
CODE_DEAD = -2,
CODE_UNTESTED = -1,
CODE_TESTED = 1;
LineDead = -2,
LineTested = 1,
LineUntested = -1;

public array $acceptFiles = ['php', 'phpt', 'phtml'];
protected array $data;
Expand Down
4 changes: 2 additions & 2 deletions src/CodeCoverage/Generators/CloverXMLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected function renderSelf(): void


foreach ((array) $coverageData as $line => $count) {
if ($count === self::CODE_DEAD) {
if ($count === self::LineDead) {
continue;
}

Expand Down Expand Up @@ -195,7 +195,7 @@ private static function analyzeMethod(\stdClass $info, ?array $coverageData = nu
$count = max(1, $info->end - $info->start - 2);
} else {
for ($i = $info->start; $i <= $info->end; $i++) {
if (isset($coverageData[$i]) && $coverageData[$i] !== self::CODE_DEAD) {
if (isset($coverageData[$i]) && $coverageData[$i] !== self::LineDead) {
$count++;
if ($coverageData[$i] > 0) {
$coveredCount++;
Expand Down
10 changes: 5 additions & 5 deletions src/CodeCoverage/Generators/HtmlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
class HtmlGenerator extends AbstractGenerator
{
private const Classes = [
self::CODE_TESTED => 't', // tested
self::CODE_UNTESTED => 'u', // untested
self::CODE_DEAD => 'dead', // dead code
self::LineTested => 't', // tested
self::LineUntested => 'u', // untested
self::LineDead => 'dead', // dead code
];
private ?string $title;
private array $files = [];
Expand Down Expand Up @@ -78,11 +78,11 @@ private function parse(): void
if ($loaded) {
$lines = $this->data[$entry];
foreach ($lines as $flag) {
if ($flag >= self::CODE_UNTESTED) {
if ($flag >= self::LineUntested) {
$total++;
}

if ($flag >= self::CODE_TESTED) {
if ($flag >= self::LineTested) {
$covered++;
}
}
Expand Down
41 changes: 28 additions & 13 deletions src/Framework/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@
class Environment
{
/** Should Test use console colors? */
public const COLORS = 'NETTE_TESTER_COLORS';
public const VariableColors = 'NETTE_TESTER_COLORS';

/** Test is run by Runner */
public const RUNNER = 'NETTE_TESTER_RUNNER';
public const VariableRunner = 'NETTE_TESTER_RUNNER';

/** Code coverage engine */
public const COVERAGE_ENGINE = 'NETTE_TESTER_COVERAGE_ENGINE';
public const VariableCoverageEngine = 'NETTE_TESTER_COVERAGE_ENGINE';

/** Code coverage file */
public const COVERAGE = 'NETTE_TESTER_COVERAGE';
public const VariableCoverage = 'NETTE_TESTER_COVERAGE';

/** Thread number when run tests in multi threads */
public const THREAD = 'NETTE_TESTER_THREAD';
public const VariableThread = 'NETTE_TESTER_THREAD';

/** @deprecated use Environment::VariableColors */
public const COLORS = self::VariableColors;

/** @deprecated use Environment::VariableRunner */
public const RUNNER = self::VariableRunner;

/** @deprecated use Environment::VariableCoverageEngine */
public const COVERAGE_ENGINE = self::VariableCoverageEngine;

/** @deprecated use Environment::VariableCoverage */
public const COVERAGE = self::VariableCoverage;

/** @deprecated use Environment::VariableThread */
public const THREAD = self::VariableThread;

public static bool $checkAssertions = false;
public static bool $useColors;
Expand All @@ -52,8 +67,8 @@ class_exists(Assert::class);
$annotations = self::getTestAnnotations();
self::$checkAssertions = !isset($annotations['outputmatch']) && !isset($annotations['outputmatchfile']);

if (getenv(self::COVERAGE) && getenv(self::COVERAGE_ENGINE)) {
CodeCoverage\Collector::start(getenv(self::COVERAGE), getenv(self::COVERAGE_ENGINE));
if (getenv(self::VariableCoverage) && getenv(self::VariableCoverageEngine)) {
CodeCoverage\Collector::start(getenv(self::VariableCoverage), getenv(self::VariableCoverageEngine));
}

if (getenv('TERMINAL_EMULATOR') === 'JetBrains-JediTerm') {
Expand All @@ -68,8 +83,8 @@ class_exists(Assert::class);
*/
public static function setupColors(): void
{
self::$useColors = getenv(self::COLORS) !== false
? (bool) getenv(self::COLORS)
self::$useColors = getenv(self::VariableColors) !== false
? (bool) getenv(self::VariableColors)
: (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')
&& getenv('NO_COLOR') === false // https://no-color.org
&& (getenv('FORCE_COLOR')
Expand Down Expand Up @@ -122,8 +137,8 @@ public static function setupErrors(): void
} elseif (self::$checkAssertions && !Assert::$counter) {
self::removeOutputBuffers();
echo "\n", Dumper::color('white/red', 'Error: This test forgets to execute an assertion.'), "\n";
self::exit(Runner\Job::CODE_FAIL);
} elseif (!getenv(self::RUNNER) && self::$exitCode !== Runner\Job::CODE_SKIP) {
self::exit(Runner\Job::CodeFail);
} elseif (!getenv(self::VariableRunner) && self::$exitCode !== Runner\Job::CodeSkip) {
echo "\n", (self::$exitCode ? Dumper::color('white/red', 'FAILURE') : Dumper::color('white/green', 'OK')), "\n";
}
});
Expand All @@ -139,7 +154,7 @@ public static function handleException(\Throwable $e): void
self::removeOutputBuffers();
self::$checkAssertions = false;
echo Dumper::dumpException($e);
self::exit($e instanceof AssertException ? Runner\Job::CODE_FAIL : Runner\Job::CODE_ERROR);
self::exit($e instanceof AssertException ? Runner\Job::CodeFail : Runner\Job::CodeError);
}


Expand All @@ -150,7 +165,7 @@ public static function skip(string $message = ''): void
{
self::$checkAssertions = false;
echo "\nSkipped:\n$message\n";
self::exit(Runner\Job::CODE_SKIP);
self::exit(Runner\Job::CodeSkip);
}


Expand Down
20 changes: 10 additions & 10 deletions src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function run(): ?int
}

$runner = $this->createRunner();
$runner->setEnvironmentVariable(Environment::RUNNER, '1');
$runner->setEnvironmentVariable(Environment::COLORS, (string) (int) Environment::$useColors);
$runner->setEnvironmentVariable(Environment::VariableRunner, '1');
$runner->setEnvironmentVariable(Environment::VariableColors, (string) (int) Environment::$useColors);

$this->installInterruptHandler();

Expand Down Expand Up @@ -125,14 +125,14 @@ private function loadOptions(): CommandLine

XX,
[
'-c' => [CommandLine::Realpath => true],
'--watch' => [CommandLine::Repeatable => true, CommandLine::Realpath => true],
'--setup' => [CommandLine::Realpath => true],
'-c' => [CommandLine::RealPath => true],
'--watch' => [CommandLine::Repeatable => true, CommandLine::RealPath => true],
'--setup' => [CommandLine::RealPath => true],
'--temp' => [],
'paths' => [CommandLine::Repeatable => true, CommandLine::Value => getcwd()],
'--debug' => [],
'--cider' => [],
'--coverage-src' => [CommandLine::Realpath => true, CommandLine::Repeatable => true],
'--coverage-src' => [CommandLine::RealPath => true, CommandLine::Repeatable => true],
'-o' => [CommandLine::Repeatable => true, CommandLine::Normalizer => function ($arg) use (&$outputFiles) {
[$format, $file] = explode(':', $arg, 2) + [1 => null];

Expand Down Expand Up @@ -261,14 +261,14 @@ private function prepareCodeCoverage(Runner $runner): string

[$engine, $version] = reset($engines);

$runner->setEnvironmentVariable(Environment::COVERAGE, $file);
$runner->setEnvironmentVariable(Environment::COVERAGE_ENGINE, $engine);
$runner->setEnvironmentVariable(Environment::VariableCoverage, $file);
$runner->setEnvironmentVariable(Environment::VariableCoverageEngine, $engine);

if ($engine === CodeCoverage\Collector::ENGINE_XDEBUG && version_compare($version, '3.0.0', '>=')) {
if ($engine === CodeCoverage\Collector::EngineXdebug && version_compare($version, '3.0.0', '>=')) {
$runner->addPhpIniOption('xdebug.mode', ltrim(ini_get('xdebug.mode') . ',coverage', ','));
}

if ($engine === CodeCoverage\Collector::ENGINE_PCOV && count($this->options['--coverage-src'])) {
if ($engine === CodeCoverage\Collector::EnginePcov && count($this->options['--coverage-src'])) {
$runner->addPhpIniOption('pcov.directory', Helpers::findCommonDirectory($this->options['--coverage-src']));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Runner/CommandLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CommandLine
Optional = 'optional',
Repeatable = 'repeatable',
Enum = 'enum',
Realpath = 'realpath',
RealPath = 'realpath',
Normalizer = 'normalizer',
Value = 'default';

Expand Down Expand Up @@ -171,7 +171,7 @@ public function checkArg(array $opt, mixed &$arg): void
$arg = call_user_func($opt[self::Normalizer], $arg);
}

if (!empty($opt[self::Realpath])) {
if (!empty($opt[self::RealPath])) {
$path = realpath($arg);
if ($path === false) {
throw new \Exception("File path '$arg' not found.");
Expand Down
20 changes: 10 additions & 10 deletions src/Runner/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
class Job
{
public const
CODE_NONE = -1,
CODE_OK = 0,
CODE_SKIP = 177,
CODE_FAIL = 178,
CODE_ERROR = 255;
CodeNone = -1,
CodeOk = 0,
CodeSkip = 177,
CodeFail = 178,
CodeError = 255;

/** waiting time between process activity check in microseconds */
public const RUN_USLEEP = 10000;
public const RunSleep = 10000;

private Test $test;
private PhpInterpreter $interpreter;
Expand All @@ -39,7 +39,7 @@ class Job
/** @var resource|null */
private $stdout;
private ?string $stderrFile;
private int $exitCode = self::CODE_NONE;
private int $exitCode = self::CodeNone;

/** @var string[] output headers */
private array $headers = [];
Expand All @@ -48,7 +48,7 @@ class Job

public function __construct(Test $test, PhpInterpreter $interpreter, ?array $envVars = null)
{
if ($test->getResult() !== Test::PREPARED) {
if ($test->getResult() !== Test::Prepared) {
throw new \LogicException("Test '{$test->getSignature()}' already has result '{$test->getResult()}'.");
}

Expand Down Expand Up @@ -127,7 +127,7 @@ public function run(bool $async = false): void
stream_set_blocking($this->stdout, false); // on Windows does not work with proc_open()
} else {
while ($this->isRunning()) {
usleep(self::RUN_USLEEP); // stream_select() doesn't work with proc_open()
usleep(self::RunSleep); // stream_select() doesn't work with proc_open()
}
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ public function isRunning(): bool
}

$code = proc_close($this->proc);
$this->exitCode = $code === self::CODE_NONE
$this->exitCode = $code === self::CodeNone
? $status['exitcode']
: $code;

Expand Down
22 changes: 11 additions & 11 deletions src/Runner/Output/ConsolePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function __construct(
$this->displaySkipped = $displaySkipped;
$this->file = fopen($file ?: 'php://output', 'w');
$this->symbols = [
Test::PASSED => $ciderMode ? Dumper::color('green', '🍎') : '.',
Test::SKIPPED => 's',
Test::FAILED => $ciderMode ? Dumper::color('red', '🍎') : Dumper::color('white/red', 'F'),
Test::Passed => $ciderMode ? Dumper::color('green', '🍎') : '.',
Test::Skipped => 's',
Test::Failed => $ciderMode ? Dumper::color('red', '🍎') : Dumper::color('white/red', 'F'),
];
}

Expand All @@ -56,9 +56,9 @@ public function begin(): void
$this->buffer = '';
$this->baseDir = null;
$this->results = [
Test::PASSED => 0,
Test::SKIPPED => 0,
Test::FAILED => 0,
Test::Passed => 0,
Test::Skipped => 0,
Test::Failed => 0,
];
$this->time = -microtime(true);
fwrite($this->file, $this->runner->getInterpreter()->getShortInfo()
Expand Down Expand Up @@ -99,9 +99,9 @@ public function finish(Test $test): void
$title = ($test->title ? "$test->title | " : '') . substr($test->getSignature(), strlen($this->baseDir));
$message = ' ' . str_replace("\n", "\n ", trim((string) $test->message)) . "\n\n";
$message = preg_replace('/^ $/m', '', $message);
if ($test->getResult() === Test::FAILED) {
if ($test->getResult() === Test::Failed) {
$this->buffer .= Dumper::color('red', "-- FAILED: $title") . "\n$message";
} elseif ($test->getResult() === Test::SKIPPED && $this->displaySkipped) {
} elseif ($test->getResult() === Test::Skipped && $this->displaySkipped) {
$this->buffer .= "-- Skipped: $title\n$message";
}
}
Expand All @@ -112,10 +112,10 @@ public function end(): void
$run = array_sum($this->results);
fwrite($this->file, !$this->count ? "No tests found\n" :
"\n\n" . $this->buffer . "\n"
. ($this->results[Test::FAILED] ? Dumper::color('white/red') . 'FAILURES!' : Dumper::color('white/green') . 'OK')
. ($this->results[Test::Failed] ? Dumper::color('white/red') . 'FAILURES!' : Dumper::color('white/green') . 'OK')
. " ($this->count test" . ($this->count > 1 ? 's' : '') . ', '
. ($this->results[Test::FAILED] ? $this->results[Test::FAILED] . ' failure' . ($this->results[Test::FAILED] > 1 ? 's' : '') . ', ' : '')
. ($this->results[Test::SKIPPED] ? $this->results[Test::SKIPPED] . ' skipped, ' : '')
. ($this->results[Test::Failed] ? $this->results[Test::Failed] . ' failure' . ($this->results[Test::Failed] > 1 ? 's' : '') . ', ' : '')
. ($this->results[Test::Skipped] ? $this->results[Test::Skipped] . ' skipped, ' : '')
. ($this->count !== $run ? ($this->count - $run) . ' not run, ' : '')
. sprintf('%0.1f', $this->time + microtime(true)) . ' seconds)' . Dumper::color() . "\n");

Expand Down
14 changes: 7 additions & 7 deletions src/Runner/Output/JUnitPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function begin(): void
{
$this->buffer = '';
$this->results = [
Test::PASSED => 0,
Test::SKIPPED => 0,
Test::FAILED => 0,
Test::Passed => 0,
Test::Skipped => 0,
Test::Failed => 0,
];
$this->startTime = microtime(true);
fwrite($this->file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites>\n");
Expand All @@ -54,9 +54,9 @@ public function finish(Test $test): void
$this->results[$test->getResult()]++;
$this->buffer .= "\t\t<testcase classname=\"" . htmlspecialchars($test->getSignature()) . '" name="' . htmlspecialchars($test->getSignature()) . '"';
$this->buffer .= match ($test->getResult()) {
Test::FAILED => ">\n\t\t\t<failure message=\"" . htmlspecialchars($test->message, ENT_COMPAT | ENT_HTML5) . "\"/>\n\t\t</testcase>\n",
Test::SKIPPED => ">\n\t\t\t<skipped/>\n\t\t</testcase>\n",
Test::PASSED => "/>\n",
Test::Failed => ">\n\t\t\t<failure message=\"" . htmlspecialchars($test->message, ENT_COMPAT | ENT_HTML5) . "\"/>\n\t\t</testcase>\n",
Test::Skipped => ">\n\t\t\t<skipped/>\n\t\t</testcase>\n",
Test::Passed => "/>\n",
};
}

Expand All @@ -65,7 +65,7 @@ public function end(): void
{
$time = sprintf('%0.1f', microtime(true) - $this->startTime);
$output = $this->buffer;
$this->buffer = "\t<testsuite errors=\"{$this->results[Test::FAILED]}\" skipped=\"{$this->results[Test::SKIPPED]}\" tests=\"" . array_sum($this->results) . "\" time=\"$time\" timestamp=\"" . @date('Y-m-d\TH:i:s') . "\">\n";
$this->buffer = "\t<testsuite errors=\"{$this->results[Test::Failed]}\" skipped=\"{$this->results[Test::Skipped]}\" tests=\"" . array_sum($this->results) . "\" time=\"$time\" timestamp=\"" . @date('Y-m-d\TH:i:s') . "\">\n";
$this->buffer .= $output;
$this->buffer .= "\t</testsuite>";

Expand Down
Loading

0 comments on commit 9942c28

Please sign in to comment.