Skip to content

Commit

Permalink
Code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFeeney committed Jan 25, 2024
1 parent 6b09e8f commit 51e5fac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
14 changes: 8 additions & 6 deletions src/Data/ProcessedCodeCoverageDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
namespace SebastianBergmann\CodeCoverage\Data;

use function json_decode;
use function json_encode;

/**
* @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage
*
Expand All @@ -18,13 +21,13 @@
*/
final class ProcessedCodeCoverageDataMapper
{
const KEY_LINE_COVERAGE = 'lineCoverage';
const KEY_FUNCTION_COVERAGE = 'functionCoverage';
public const KEY_LINE_COVERAGE = 'lineCoverage';
public const KEY_FUNCTION_COVERAGE = 'functionCoverage';

public function toJson(ProcessedCodeCoverageData $processedCodeCoverageData): string
{
$arrayMapping = [
self::KEY_LINE_COVERAGE => $processedCodeCoverageData->lineCoverage(),
self::KEY_LINE_COVERAGE => $processedCodeCoverageData->lineCoverage(),
self::KEY_FUNCTION_COVERAGE => $processedCodeCoverageData->functionCoverage(),
];

Expand All @@ -35,13 +38,12 @@ public function fromJson(string $json): ProcessedCodeCoverageData
{
/** @var array<array-key, array<array-key, mixed>> */
$unserializedData = json_decode($json, true);
$processedCodeCoverageData = new ProcessedCodeCoverageData();

$processedCodeCoverageData = new ProcessedCodeCoverageData;

$processedCodeCoverageData->setLineCoverage($unserializedData[self::KEY_LINE_COVERAGE]);
$processedCodeCoverageData->setFunctionCoverage($unserializedData[self::KEY_FUNCTION_COVERAGE]);

return $processedCodeCoverageData;
}
}

43 changes: 23 additions & 20 deletions tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
*/
namespace SebastianBergmann\CodeCoverage\Data;

use function file_get_contents;
use function iterator_count;
use function json_decode;
use function unlink;
use FilesystemIterator;
use SebastianBergmann\CodeCoverage\Report\Xml\Facade;
use SebastianBergmann\CodeCoverage\TestCase;
use FilesystemIterator;

final class ProcessedCodeCoverageDataMapperTest extends TestCase
{
Expand All @@ -36,7 +40,7 @@ protected function tearDown(): void

public function testToJsonLineCoverageForBankAccount(): void
{
$coverage = $this->getLineCoverageForBankAccount()->getData();
$coverage = $this->getLineCoverageForBankAccount()->getData();
$decodedJson = $this->getDecodedJsonForProcessedCodeCoverage($coverage);

$this->assertEquals(
Expand All @@ -52,7 +56,7 @@ public function testToJsonLineCoverageForBankAccount(): void

public function testToJsonPathCoverageForBankAccount(): void
{
$coverage = $this->getPathCoverageForBankAccount()->getData();
$coverage = $this->getPathCoverageForBankAccount()->getData();
$decodedJson = $this->getDecodedJsonForProcessedCodeCoverage($coverage);

$this->assertEquals(
Expand All @@ -68,7 +72,7 @@ public function testToJsonPathCoverageForBankAccount(): void

public function testFromJsonCoverageForBankAccount(): void
{
$coverage = $this->getPathCoverageForBankAccount()->getData();
$coverage = $this->getPathCoverageForBankAccount()->getData();
$unserializedCoverage = $this->serializeAndUnserializeToJson($coverage);

$this->assertEquals(
Expand All @@ -84,7 +88,7 @@ public function testFromJsonCoverageForBankAccount(): void

public function testFromJsonPathCoverageForBankAccount(): void
{
$coverage = $this->getPathCoverageForBankAccount()->getData();
$coverage = $this->getPathCoverageForBankAccount()->getData();
$unserializedCoverage = $this->serializeAndUnserializeToJson($coverage);

$this->assertEquals(
Expand All @@ -99,13 +103,13 @@ public function testFromJsonPathCoverageForBankAccount(): void
}

/**
* I don't expect this test to survive in the PR, but I am trying to
* produce the final XML format via the JSON serialization to ensure
* that I have everything I need in the JSON format.
*/
* I don't expect this test to survive in the PR, but I am trying to
* produce the final XML format via the JSON serialization to ensure
* that I have everything I need in the JSON format.
*/
public function testFromJsonLineCoverageForBankAccountToXml(): void
{
$coverage = $this->getLineCoverageForBankAccount();
$coverage = $this->getLineCoverageForBankAccount();
$unserializedCoverage = $this->serializeAndUnserializeToJson($coverage->getData());
$coverage->setData($unserializedCoverage);

Expand All @@ -119,24 +123,24 @@ public function testFromJsonLineCoverageForBankAccountToXml(): void

private function getDecodedJsonForProcessedCodeCoverage(ProcessedCodeCoverageData $processedCodeCoverage): array
{
$dataMapper = new ProcessedCodeCoverageDataMapper();
$json = $dataMapper->toJson($processedCodeCoverage);
$dataMapper = new ProcessedCodeCoverageDataMapper;
$json = $dataMapper->toJson($processedCodeCoverage);

return json_decode($json, true);
}

/**
* Doing it this way while the JSON format is being developed, though I expect we'd have a
* fixture file in the future
**/
* Doing it this way while the JSON format is being developed, though I expect we'd have a
* fixture file in the future.
*/
private function serializeAndUnserializeToJson(ProcessedCodeCoverageData $processedCodeCoverage): ProcessedCodeCoverageData
{
$dataMapper = new ProcessedCodeCoverageDataMapper();
$json = $dataMapper->toJson($processedCodeCoverage);
$dataMapper = new ProcessedCodeCoverageDataMapper;
$json = $dataMapper->toJson($processedCodeCoverage);

// Instantiate a new data mapper out of an abundance of caution to ensure we have no
// Instantiate a new data mapper out of an abundance of caution to ensure we have no
// persisted state from the serializing instance.
$dataMapper = new ProcessedCodeCoverageDataMapper();
$dataMapper = new ProcessedCodeCoverageDataMapper;

return $dataMapper->fromJson($json);
}
Expand Down Expand Up @@ -168,4 +172,3 @@ private function assertFilesEquals(string $expectedFilesPath, string $actualFile
}
}
}

0 comments on commit 51e5fac

Please sign in to comment.