Skip to content

Commit

Permalink
Increase code-coverage (#487)
Browse files Browse the repository at this point in the history
* Add code-coverage core CoverageReporter

* Update PHPStan configuration for new dep update

* Add psalm.xml.dist to export-ignore

* Get rid of unused ParsedFunction visibility

* Cover passthru code
  • Loading branch information
Slamdunk committed Aug 3, 2020
1 parent e515c81 commit 92142f8
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml.dist export-ignore
/.github/ export-ignore
/test/ export-ignore
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ parameters:
excludes_analyse:
- test/fixtures/*
ignoreErrors:
# @see https://github.com/ergebnis/phpstan-rules/pull/248
- "#^Method .+__construct\\(\\) is not final, but since the containing class is abstract, it should be\\.$#"

# Known errors
- "#^Variable property access on .+$#"
- "#^Variable method call on .+$#"
Expand Down
10 changes: 1 addition & 9 deletions src/Coverage/CoverageMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use function ini_get;
use function is_array;
use function unlink;
use function unserialize;

use const PHP_SAPI;

Expand Down Expand Up @@ -50,14 +49,7 @@ private function addCoverage(CodeCoverage $coverage): void
*/
private function getCoverageObject(SplFileObject $coverageFile): CodeCoverage
{
if ($coverageFile->fread(5) === '<?php') {
return include $coverageFile->getRealPath();
}

$coverageFile->fseek(0);

// the PHPUnit 3.x and below
return unserialize($coverageFile->fread($coverageFile->getSize()));
return include $coverageFile->getRealPath();
}

/**
Expand Down
17 changes: 0 additions & 17 deletions src/Parser/ParsedFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,4 @@

final class ParsedFunction extends ParsedObject
{
/** @var string */
private $visibility;

public function __construct(string $doc, string $visibility, string $name)
{
parent::__construct($doc, $name);
$this->visibility = $visibility;
}

/**
* Returns the accessibility level of the parsed
* method - i.e public, private, protected.
*/
public function getVisibility(): string
{
return $this->visibility;
}
}
2 changes: 1 addition & 1 deletion src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private function getMethods(): array
continue;
}

$tests[] = new ParsedFunction((string) $method->getDocComment(), 'public', $method->getName());
$tests[] = new ParsedFunction((string) $method->getDocComment(), $method->getName());
}

return $tests;
Expand Down
8 changes: 7 additions & 1 deletion src/Runners/PHPUnit/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function assert;
use function count;
use function dirname;
use function escapeshellarg;
use function explode;
use function fgets;
use function file_exists;
Expand Down Expand Up @@ -456,7 +457,12 @@ private function parsePassthru(?string $param): ?array
}

$stringToArgumentProcess = Process::fromShellCommandline(
sprintf('%s -r "echo serialize(\\$argv);" -- %s', PHP_BINARY, $param)
sprintf(
'%s -r %s -- %s',
escapeshellarg(PHP_BINARY),
escapeshellarg('echo serialize($argv);'),
$param
)
);
$stringToArgumentProcess->mustRun();

Expand Down
51 changes: 51 additions & 0 deletions test/Functional/Coverage/CoverageReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use function defined;
use function mkdir;
use function ob_get_clean;
use function ob_start;
use function str_replace;
use function sys_get_temp_dir;
use function uniqid;
Expand Down Expand Up @@ -162,6 +164,55 @@ public function testGenerateHtml(array $coverageFiles): void
static::assertFileExists($target . DS . 'index.html', 'Index html file was not generated');
}

/**
* @param string[] $coverageFiles
*
* @dataProvider getReporterProvider
*/
public function testGenerateText(array $coverageFiles): void
{
$filename1 = $this->copyCoverageFile($coverageFiles[0], $this->targetDir);
$filename2 = $this->copyCoverageFile($coverageFiles[1], $this->targetDir);

$coverageMerger = new CoverageMerger();
$coverageMerger->addCoverageFromFile($filename1);
$coverageMerger->addCoverageFromFile($filename2);

ob_start();
$coverageMerger->getReporter()->text();
$output = ob_get_clean();

static::assertIsString($output);
static::assertStringContainsString('Code Coverage Report:', $output);
}

/**
* @param string[] $coverageFiles
*
* @dataProvider getReporterProvider
*/
public function testGenerateXml(array $coverageFiles): void
{
$filename1 = $this->copyCoverageFile($coverageFiles[0], $this->targetDir);
$filename2 = $this->copyCoverageFile($coverageFiles[1], $this->targetDir);

$coverageMerger = new CoverageMerger();
$coverageMerger->addCoverageFromFile($filename1);
$coverageMerger->addCoverageFromFile($filename2);

$target = $this->targetDir . DS . 'coverage.xml';

static::assertFileDoesNotExist($target);

$coverageMerger->getReporter()->xml($target);

static::assertFileExists($target);
static::assertFileExists($target . DS . 'index.xml', 'Index xml file was not generated');

$reportXml = Xml::loadFile($target . DS . 'index.xml');
static::assertTrue($reportXml->hasChildNodes(), 'Incorrect xml was generated');
}

/**
* @return array<string, array<string, string[]|class-string>>
*/
Expand Down
7 changes: 1 addition & 6 deletions test/Unit/Parser/ParsedClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ public function setUp(): void
'/**
* @group group1
*/',
'public',
'testFunction'
),
new ParsedFunction(
'/**
* @group group2
*/',
'public',
'testFunction2'
),
new ParsedFunction('', 'public', 'testFunction3'),
new ParsedFunction('', 'testFunction3'),
];
$this->class = new ParsedClass('', 'MyTestClass', '', $this->methods);
}
Expand All @@ -48,21 +46,18 @@ public function testGetMethodsMultipleAnnotationsReturnsMethods(): void
'/**
* @group group1
*/',
'public',
'testFunction'
);
$goodMethod2 = new ParsedFunction(
'/**
* @group group2
*/',
'public',
'testFunction2'
);
$badMethod = new ParsedFunction(
'/**
* @group group3
*/',
'public',
'testFunction2'
);
$annotatedClass = new ParsedClass('', 'MyTestClass', '', [$goodMethod, $goodMethod2, $badMethod]);
Expand Down
2 changes: 1 addition & 1 deletion test/Unit/ResultTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ final protected function getSuiteWithResult(string $result, int $methodCount): S

final protected function mockFunction(string $functionCount): ParsedFunction
{
return new ParsedFunction('doc', 'public', 'func' . $functionCount);
return new ParsedFunction('doc', 'func' . $functionCount);
}
}
26 changes: 26 additions & 0 deletions test/Unit/Runners/PHPUnit/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ParaTest\Tests\TestBase;

use function chdir;
use function defined;
use function file_put_contents;
use function getcwd;
use function glob;
Expand All @@ -33,6 +34,7 @@ public function setUp(): void
'phpunit' => 'phpunit',
'functional' => true,
'group' => 'group1',
'exclude-group' => 'group2',
'bootstrap' => '/path/to/bootstrap',
];
$this->options = new Options($this->unfiltered);
Expand Down Expand Up @@ -131,6 +133,30 @@ public function testConfigurationKeyIsNotPresentIfNoConfigGiven(): void
static::assertArrayNotHasKey('configuration', $options->filtered);
}

public function testPassthru(): void
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$options = new Options([
'passthru' => '"--prepend" "xdebug-filter.php"',
'passthru-php' => '"-d" "zend_extension=xdebug.so"',
]);
} else {
$options = new Options([
'passthru' => "'--prepend' 'xdebug-filter.php'",
'passthru-php' => "'-d' 'zend_extension=xdebug.so'",
]);
}

$expectedPassthru = ['--prepend', 'xdebug-filter.php'];
$expectedPassthruPhp = ['-d', 'zend_extension=xdebug.so'];

static::assertSame($expectedPassthru, $options->passthru);
static::assertSame($expectedPassthruPhp, $options->passthruPhp);

$emptyOptions = new Options(['passthru' => '']);
static::assertNull($emptyOptions->passthru);
}

public function testConfigurationShouldReturnXmlIfConfigSpecifiedAsDirectoryAndFileExists(): void
{
$this->assertConfigurationFileFiltered('phpunit.xml', getcwd(), getcwd());
Expand Down
6 changes: 3 additions & 3 deletions test/Unit/Runners/PHPUnit/ResultPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testStartSetsWidthAndMaxColumn(): void
{
$funcs = [];
for ($i = 0; $i < 120; ++$i) {
$funcs[] = new ParsedFunction('doc', 'public', 'function' . $i);
$funcs[] = new ParsedFunction('doc', 'function' . $i);
}

$suite = new Suite('/path', $funcs);
Expand Down Expand Up @@ -147,8 +147,8 @@ public function testStartPrintsOptionInfoWithSingularForOneProcess(): void
public function testAddSuiteAddsFunctionCountToTotalTestCases(): void
{
$suite = new Suite('/path', [
new ParsedFunction('doc', 'public', 'funcOne'),
new ParsedFunction('doc', 'public', 'funcTwo'),
new ParsedFunction('doc', 'funcOne'),
new ParsedFunction('doc', 'funcTwo'),
]);
$this->printer->addTest($suite);
static::assertEquals(2, $this->printer->getTotalCases());
Expand Down

0 comments on commit 92142f8

Please sign in to comment.