Skip to content

Commit

Permalink
Use named constructors as requested
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed May 14, 2020
1 parent ea95590 commit 118ec2d
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private function addUncoveredFilesFromWhitelist(): void
}
}

$this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST');
$this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST');
}

private function getLinesToBeIgnored(string $fileName): array
Expand Down Expand Up @@ -887,7 +887,7 @@ private function initializeData(): void
$data[$file] = $fileCoverage;
}

$this->append(new RawCodeCoverageData($data), 'UNCOVERED_FILES_FROM_WHITELIST');
$this->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage($data), 'UNCOVERED_FILES_FROM_WHITELIST');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Driver/PCOV.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public function stop(): RawCodeCoverageData

\pcov\clear();

return new RawCodeCoverageData($collect);
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($collect);
}
}
2 changes: 1 addition & 1 deletion src/Driver/PHPDBG.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function stop(): RawCodeCoverageData

$fetchedLines = \array_merge($fetchedLines, $sourceLines);

return new RawCodeCoverageData($this->detectExecutedLines($fetchedLines, $dbgData));
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($this->detectExecutedLines($fetchedLines, $dbgData));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Xdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public function stop(): RawCodeCoverageData

\xdebug_stop_code_coverage();

return new RawCodeCoverageData($data);
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($data);
}
}
26 changes: 0 additions & 26 deletions src/Exception/UnknownCoverageDataFormatException.php

This file was deleted.

28 changes: 18 additions & 10 deletions src/RawCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,27 @@ final class RawCodeCoverageData
*/
private $lineCoverage = [];

public function __construct(array $rawCoverage = [])
public static function fromXdebugWithoutPathCoverage(array $rawCoverage): self
{
foreach ($rawCoverage as $file => $fileCoverageData) {
$hasOnlyIntegerKeys = \count(\array_filter(\array_keys($fileCoverageData), 'is_int')) === \count($fileCoverageData);
$coverage = new self();
$coverage->lineCoverage = $rawCoverage;

return $coverage;
}

public static function fromXdebugWithPathCoverage(array $rawCoverage): self
{
$coverage = new self();

if ($hasOnlyIntegerKeys) {
$this->lineCoverage[$file] = $fileCoverageData;
} elseif (\count($fileCoverageData) === 2 && isset($fileCoverageData['lines'], $fileCoverageData['functions'])) {
$this->lineCoverage[$file] = $fileCoverageData['lines'];
} else {
throw UnknownCoverageDataFormatException::create($file);
}
foreach ($rawCoverage as $file => $fileCoverageData) {
$coverage->lineCoverage[$file] = $fileCoverageData['lines'];
}

return $coverage;
}

private function __construct()
{
}

public function clear(): void
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function setUpBeforeClass(): void
protected function getXdebugDataForBankAccount()
{
return [
new RawCodeCoverageData([
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'BankAccount.php' => [
8 => 1,
9 => -2,
Expand All @@ -40,23 +40,23 @@ protected function getXdebugDataForBankAccount()
32 => -2,
],
]),
new RawCodeCoverageData([
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'BankAccount.php' => [
8 => 1,
13 => 1,
16 => 1,
29 => 1,
],
]),
new RawCodeCoverageData([
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'BankAccount.php' => [
8 => 1,
13 => 1,
16 => 1,
22 => 1,
],
]),
new RawCodeCoverageData([
RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'BankAccount.php' => [
8 => 1,
13 => 1,
Expand Down Expand Up @@ -314,7 +314,7 @@ protected function setUpXdebugStubForFileWithIgnoredLines(): Driver
$stub->expects($this->any())
->method('stop')
->will($this->returnValue(
new RawCodeCoverageData(
RawCodeCoverageData::fromXdebugWithoutPathCoverage(
[
TEST_FILES_PATH . 'source_with_ignore.php' => [
2 => 1,
Expand Down Expand Up @@ -352,7 +352,7 @@ protected function setUpXdebugStubForClassWithAnonymousFunction(): Driver
$stub->expects($this->any())
->method('stop')
->will($this->returnValue(
new RawCodeCoverageData(
RawCodeCoverageData::fromXdebugWithoutPathCoverage(
[
TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => [
7 => 1,
Expand Down Expand Up @@ -390,7 +390,7 @@ protected function setUpXdebugStubForCrashParsing(): Driver

$stub->expects($this->any())
->method('stop')
->will($this->returnValue(new RawCodeCoverageData([])));
->will($this->returnValue(RawCodeCoverageData::fromXdebugWithoutPathCoverage([])));

return $stub;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/CodeCoverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testCannotAppendWithInvalidArgument(): void
{
$this->expectException(Exception::class);

$this->coverage->append(new RawCodeCoverageData([]), null);
$this->coverage->append(RawCodeCoverageData::fromXdebugWithoutPathCoverage([]), null);
}

public function testCollect(): void
Expand All @@ -71,7 +71,7 @@ public function testWhitelistFiltering(): void
{
$this->coverage->filter()->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');

$data = new RawCodeCoverageData([
$data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'BankAccount.php' => [
29 => -1,
31 => -1,
Expand Down Expand Up @@ -310,7 +310,7 @@ public function testAppendThrowsExceptionIfCoveredCodeWasNotExecuted(): void
$this->coverage->filter()->addDirectoryToWhitelist(TEST_FILES_PATH);
$this->coverage->setCheckForUnexecutedCoveredCode(true);

$data = new RawCodeCoverageData([
$data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'BankAccount.php' => [
29 => -1,
31 => -1,
Expand All @@ -336,7 +336,7 @@ public function testAppendThrowsExceptionIfUsedCodeWasNotExecuted(): void
$this->coverage->filter()->addDirectoryToWhitelist(TEST_FILES_PATH);
$this->coverage->setCheckForUnexecutedCoveredCode(true);

$data = new RawCodeCoverageData([
$data = RawCodeCoverageData::fromXdebugWithoutPathCoverage([
TEST_FILES_PATH . 'BankAccount.php' => [
29 => -1,
31 => -1,
Expand Down
40 changes: 8 additions & 32 deletions tests/tests/RawCodeCoverageDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public function testLineDataFromStandardXDebugFormat(): void
],
];

$dataObject = new RawCodeCoverageData($lineDataFromDriver);
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
$this->assertEquals($lineDataFromDriver, $dataObject->getLineCoverage());
}

/**
* In the branch-check XDebug format, the line data exists inside a "lines" array key.
* In the path-coverage XDebug format, the line data exists inside a "lines" array key.
*/
public function testLineDataFromBranchCheckXDebugFormat(): void
public function testLineDataFromPathCoverageXDebugFormat(): void
{
$rawDataFromDriver = [
'/some/path/SomeClass.php' => [
Expand All @@ -54,34 +54,10 @@ public function testLineDataFromBranchCheckXDebugFormat(): void
],
];

$dataObject = new RawCodeCoverageData($rawDataFromDriver);
$dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver);
$this->assertEquals($lineData, $dataObject->getLineCoverage());
}

/**
* Coverage data that does not match a known format should throw an exception.
*/
public function testDataFromUnknownFormat(): void
{
$this->expectException(UnknownCoverageDataFormatException::class);

$lineDataFromDriver = [
'/some/path/SomeClass.php' => [
'executedLines' => [
8,
],
'unExecutedLines' => [
13,
],
'nonExecutableLines' => [
9,
],
],
];

$dataObject = new RawCodeCoverageData($lineDataFromDriver);
}

public function testClear(): void
{
$lineDataFromDriver = [
Expand All @@ -92,7 +68,7 @@ public function testClear(): void
],
];

$dataObject = new RawCodeCoverageData($lineDataFromDriver);
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
$dataObject->clear();
$this->assertEmpty($dataObject->getLineCoverage());
}
Expand Down Expand Up @@ -130,7 +106,7 @@ public function testRemoveCoverageDataForFile(): void
],
];

$dataObject = new RawCodeCoverageData($lineDataFromDriver);
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
$dataObject->removeCoverageDataForFile('/some/path/SomeOtherClass.php');
$this->assertEquals($expectedFilterResult, $dataObject->getLineCoverage());
}
Expand Down Expand Up @@ -167,7 +143,7 @@ public function testKeepCoverageDataOnlyForLines(): void
],
];

$dataObject = new RawCodeCoverageData($lineDataFromDriver);
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
$dataObject->keepCoverageDataOnlyForLines('/some/path/SomeClass.php', [9, 13]);
$dataObject->keepCoverageDataOnlyForLines('/some/path/SomeOtherClass.php', [999]);
$dataObject->keepCoverageDataOnlyForLines('/some/path/AnotherClass.php', [28]);
Expand Down Expand Up @@ -209,7 +185,7 @@ public function testRemoveCoverageDataForLines(): void
],
];

$dataObject = new RawCodeCoverageData($lineDataFromDriver);
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
$dataObject->removeCoverageDataForLines('/some/path/SomeClass.php', [9, 13]);
$dataObject->removeCoverageDataForLines('/some/path/SomeOtherClass.php', [999]);
$dataObject->removeCoverageDataForLines('/some/path/AnotherClass.php', [28]);
Expand Down

0 comments on commit 118ec2d

Please sign in to comment.