Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Strict code coverage tracing with @covers annotations #509

Merged
merged 19 commits into from
Aug 19, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
--tmp-dir option; LogInterpreter ok
Slamdunk committed Aug 18, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit b4c49650fd1fb5fe6a38843b6635a20ee47d81eb
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
"phpunit/php-file-iterator": "^3.0",
"phpunit/php-timer": "^5.0",
"phpunit/phpunit": "^9.3.5",
"sebastian/environment": "^5.1",
"symfony/console": "^4.4 || ^5.1",
"symfony/process": "^4.4 || ^5.1"
},
2 changes: 1 addition & 1 deletion src/Logging/JUnit/Reader.php
Original file line number Diff line number Diff line change
@@ -255,7 +255,7 @@ public function getTotalErrors(): int
return $this->suites[0]->errors;
}

public function getTotalWarning(): int
public function getTotalWarnings(): int
{
return $this->suites[0]->warnings;
}
22 changes: 4 additions & 18 deletions src/Logging/#terpreter.php
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@
use function array_reduce;
use function array_values;
use function count;
use function reset;

final class LogInterpreter implements MetaProvider
{
@@ -22,28 +21,15 @@ final class LogInterpreter implements MetaProvider
*
* @var Reader[]
*/
protected $readers = [];

/**
* Reset the array pointer of the internal
* readers collection.
*/
public function rewind(): void
{
reset($this->readers);
}
private $readers = [];

/**
* Add a new Reader to be included
* in the final results.
*
* @return $this
*/
public function addReader(Reader $reader): self
public function addReader(Reader $reader): void
{
$this->readers[] = $reader;

return $this;
}

/**
@@ -168,10 +154,10 @@ public function getTotalErrors(): int
}, 0);
}

public function getTotalWarning(): int
public function getTotalWarnings(): int
{
return array_reduce($this->readers, static function (int $result, Reader $reader): int {
return $result + $reader->getTotalWarning();
return $result + $reader->getTotalWarnings();
}, 0);
}

2 changes: 1 addition & 1 deletion src/Logging/MetaProvider.php
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ public function getTotalFailures(): int;

public function getTotalErrors(): int;

public function getTotalWarning(): int;
public function getTotalWarnings(): int;

public function getTotalTime(): float;

1 change: 0 additions & 1 deletion src/Runners/PHPUnit/BaseWrapperRunner.php
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ final protected function complete(): void
{
$this->setExitCode();
$this->printer->printResults();
$this->interpreter->rewind();
$this->log();
$this->logCoverage();
$readers = $this->interpreter->getReaders();
10 changes: 6 additions & 4 deletions src/Runners/PHPUnit/ExecutableTest.php
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@
use function array_map;
use function array_merge;
use function assert;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;

@@ -47,11 +46,14 @@ abstract class ExecutableTest

/** @var bool */
private $needsCoverage;
/** @var string */
private $tmpDir;

public function __construct(string $path, bool $needsCoverage)
public function __construct(string $path, bool $needsCoverage, string $tmpDir)
{
$this->path = $path;
$this->needsCoverage = $needsCoverage;
$this->tmpDir = $tmpDir;
}

/**
@@ -75,7 +77,7 @@ final public function getPath(): string
final public function getTempFile(): string
{
if ($this->temp === null) {
$temp = tempnam(sys_get_temp_dir(), 'PT_');
$temp = tempnam($this->tmpDir, 'PT_');
assert($temp !== false);

$this->temp = $temp;
@@ -165,7 +167,7 @@ final public function command(string $binary, array $options = [], ?array $passt
final public function getCoverageFileName(): string
{
if ($this->coverageFileName === null) {
$coverageFileName = tempnam(sys_get_temp_dir(), 'CV_');
$coverageFileName = tempnam($this->tmpDir, 'CV_');
assert($coverageFileName !== false);

$this->coverageFileName = $coverageFileName;
4 changes: 2 additions & 2 deletions src/Runners/PHPUnit/FullSuite.php
Original file line number Diff line number Diff line change
@@ -14,9 +14,9 @@ final class FullSuite extends ExecutableTest
/** @var string */
protected $configPath;

public function __construct(string $suiteName, string $configPath, bool $needsCoverage)
public function __construct(string $suiteName, string $configPath, bool $needsCoverage, string $tmpDir)
{
parent::__construct('', $needsCoverage);
parent::__construct('', $needsCoverage, $tmpDir);

$this->suiteName = $suiteName;
$this->configPath = $configPath;
18 changes: 18 additions & 0 deletions src/Runners/PHPUnit/Options.php
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@
use function realpath;
use function sprintf;
use function strlen;
use function sys_get_temp_dir;
use function unserialize;

use const DIRECTORY_SEPARATOR;
@@ -190,6 +191,8 @@ final class Options
private $logJunit;
/** @var string|null */
private $whitelist;
/** @var string */
private $tmpDir;

/**
* @param array<string, string> $annotations
@@ -229,6 +232,7 @@ private function __construct(
string $runner,
bool $stopOnFailure,
array $testsuite,
string $tmpDir,
int $verbose,
?string $whitelist
) {
@@ -260,6 +264,7 @@ private function __construct(
$this->runner = $runner;
$this->stopOnFailure = $stopOnFailure;
$this->testsuite = $testsuite;
$this->tmpDir = $tmpDir;
$this->verbose = $verbose;
$this->whitelist = $whitelist;
}
@@ -357,6 +362,7 @@ public static function fromConsoleInput(InputInterface $input, string $cwd): sel
$options['runner'],
$options['stop-on-failure'],
$testsuite,
$options['tmp-dir'],
(int) $options['verbose'],
$options['whitelist']
);
@@ -552,6 +558,13 @@ public static function setInputDefinition(InputDefinition $inputDefinition): voi
InputOption::VALUE_REQUIRED,
'Filter which testsuite to run'
),
new InputOption(
'tmp-dir',
null,
InputOption::VALUE_REQUIRED,
'Temporary directory for internal ParaTest files',
sys_get_temp_dir()
),
new InputOption(
'verbose',
'v',
@@ -876,6 +889,11 @@ public function logJunit(): ?string
return $this->logJunit;
}

public function tmpDir(): string
{
return $this->tmpDir;
}

public function whitelist(): ?string
{
return $this->whitelist;
1 change: 0 additions & 1 deletion src/Runners/PHPUnit/Runner.php
Original file line number Diff line number Diff line change
@@ -88,7 +88,6 @@ public function run(): void
private function complete(): void
{
$this->printer->printResults();
$this->interpreter->rewind();
$this->log();
$this->logCoverage();
$readers = $this->interpreter->getReaders();
3 changes: 1 addition & 2 deletions src/Runners/PHPUnit/SqliteRunner.php
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
use function implode;
use function realpath;
use function serialize;
use function sys_get_temp_dir;
use function tempnam;
use function uniqid;
use function unlink;
@@ -39,7 +38,7 @@ public function __construct(Options $opts, OutputInterface $output)
{
parent::__construct($opts, $output);

$this->dbFileName = (string) ($opts->filtered()['database'] ?? tempnam(sys_get_temp_dir(), 'paratest_db_'));
$this->dbFileName = (string) ($opts->filtered()['database'] ?? tempnam($opts->tmpDir(), 'paratest_db_'));
$this->db = new PDO('sqlite:' . $this->dbFileName);
}

4 changes: 2 additions & 2 deletions src/Runners/PHPUnit/Suite.php
Original file line number Diff line number Diff line change
@@ -23,9 +23,9 @@ final class Suite extends ExecutableTest
/**
* @param TestMethod[] $functions
*/
public function __construct(string $path, array $functions, bool $needsCoverage)
public function __construct(string $path, array $functions, bool $needsCoverage, string $tmpDir)
{
parent::__construct($path, $needsCoverage);
parent::__construct($path, $needsCoverage, $tmpDir);
$this->functions = $functions;
}

36 changes: 17 additions & 19 deletions src/Runners/PHPUnit/SuiteLoader.php
Original file line number Diff line number Diff line change
@@ -60,16 +60,12 @@ final class SuiteLoader
*/
private $configuration;

/** @var Options|null */
public $options;
/** @var Options */
private $options;

public function __construct(?Options $options = null)
public function __construct(Options $options)
{
$this->options = $options;
if ($options === null) {
return;
}

$this->options = $options;
$this->configuration = $options->configuration();
}

@@ -117,8 +113,7 @@ public function load(?string $path = null): void
(new Facade())->getFilesAsArray($path, ['Test.php'])
);
} elseif (
$this->options !== null
&& $this->options->parallelSuite()
$this->options->parallelSuite()
&& $this->configuration !== null
&& ! $this->configuration->testSuite()->isEmpty()
) {
@@ -130,7 +125,7 @@ public function load(?string $path = null): void
&& ! $this->configuration->testSuite()->isEmpty()
) {
$testSuiteCollection = $this->configuration->testSuite()->asArray();
if ($this->options !== null && count($this->options->testsuite()) > 0) {
if (count($this->options->testsuite()) > 0) {
$suitesName = array_map(static function (TestSuite $testSuite): string {
return $testSuite->name();
}, $testSuiteCollection);
@@ -202,7 +197,8 @@ private function executableTests(string $path, ParsedClass $class): array
$executableTests[] = new TestMethod(
$path,
$methodBatch,
$this->options !== null && $this->options->hasCoverage()
$this->options->hasCoverage(),
$this->options->tmpDir()
);
}

@@ -219,8 +215,8 @@ private function executableTests(string $path, ParsedClass $class): array
*/
private function getMethodBatches(ParsedClass $class): array
{
$classMethods = $class->getMethods($this->options !== null ? $this->options->annotations() : []);
$maxBatchSize = $this->options !== null && $this->options->functional() ? $this->options->maxBatchSize() : 0;
$classMethods = $class->getMethods($this->options->annotations());
$maxBatchSize = $this->options->functional() ? $this->options->maxBatchSize() : 0;
assert($maxBatchSize !== null);

$batches = [];
@@ -328,7 +324,7 @@ private function getMethodTests(ParsedClass $class, ParsedFunction $method): arr
*/
private function testMatchGroupOptions(array $groups): bool
{
if ($this->options === null || count($this->options->group()) === 0) {
if (count($this->options->group()) === 0) {
return true;
}

@@ -342,7 +338,7 @@ private function testMatchGroupOptions(array $groups): bool

private function testMatchFilterOptions(string $className, string $name): bool
{
if ($this->options === null || ($filter = $this->options->filter()) === null) {
if (($filter = $this->options->filter()) === null) {
return true;
}

@@ -362,7 +358,8 @@ private function createSuite(string $path, ParsedClass $class): Suite
$path,
$class
),
$this->options !== null && $this->options->hasCoverage()
$this->options->hasCoverage(),
$this->options->tmpDir()
);
}

@@ -371,7 +368,8 @@ private function createFullSuite(string $suiteName, string $configPath): FullSui
return new FullSuite(
$suiteName,
$configPath,
$this->options !== null && $this->options->hasCoverage()
$this->options->hasCoverage(),
$this->options->tmpDir()
);
}

@@ -427,7 +425,7 @@ private function loadConfiguration(): void
}

$bootstrap = null;
if ($this->options !== null && $this->options->bootstrap() !== null) {
if ($this->options->bootstrap() !== null) {
$bootstrap = $this->options->bootstrap();
} elseif ($this->configuration !== null && $this->configuration->phpunit()->hasBootstrap()) {
$bootstrap = $this->configuration->phpunit()->bootstrap();
4 changes: 2 additions & 2 deletions src/Runners/PHPUnit/TestMethod.php
Original file line number Diff line number Diff line change
@@ -35,9 +35,9 @@ final class TestMethod extends ExecutableTest
* @param string $testPath path to phpunit test case file
* @param string[] $filters array of filters or single filter
*/
public function __construct(string $testPath, array $filters, bool $needsCoverage)
public function __construct(string $testPath, array $filters, bool $needsCoverage, string $tmpDir)
{
parent::__construct($testPath, $needsCoverage);
parent::__construct($testPath, $needsCoverage, $tmpDir);
// for compatibility with other code (tests), which can pass string (one filter)
// instead of array of filters
$this->filters = $filters;
Loading