Skip to content
Open
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
Add a sanity check end-to-end test producing an XML file
  • Loading branch information
AndrewFeeney committed Jan 25, 2024
commit 9b33999f903d5f66d30947cadce6ffc0e419a299
67 changes: 67 additions & 0 deletions tests/tests/Data/ProcessedCodeCoverageDataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,31 @@
*/
namespace SebastianBergmann\CodeCoverage\Data;

use SebastianBergmann\CodeCoverage\Report\Xml\Facade;
use SebastianBergmann\CodeCoverage\TestCase;
use FilesystemIterator;

final class ProcessedCodeCoverageDataMapperTest extends TestCase
{
private static string $TEST_REPORT_PATH_SOURCE;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'XML';
}

protected function tearDown(): void
{
parent::tearDown();

foreach (new FilesystemIterator(self::$TEST_TMP_PATH) as $fileInfo) {
/* @var \SplFileInfo $fileInfo */
unlink($fileInfo->getPathname());
}
}

public function testToJsonLineCoverageForBankAccount(): void
{
$coverage = $this->getLineCoverageForBankAccount()->getData();
Expand Down Expand Up @@ -77,6 +98,25 @@ 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.
*/
public function testFromJsonLineCoverageForBankAccountToXml(): void
{
$coverage = $this->getLineCoverageForBankAccount();
$unserializedCoverage = $this->serializeAndUnserializeToJson($coverage->getData());
$coverage->setData($unserializedCoverage);

$expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount';

$xml = new Facade('1.0.0');
$xml->process($coverage, self::$TEST_TMP_PATH);

$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
}

private function getDecodedJsonForProcessedCodeCoverage(ProcessedCodeCoverageData $processedCodeCoverage): array
{
$dataMapper = new ProcessedCodeCoverageDataMapper();
Expand All @@ -100,5 +140,32 @@ private function serializeAndUnserializeToJson(ProcessedCodeCoverageData $proces

return $dataMapper->fromJson($json);
}

private function assertFilesEquals(string $expectedFilesPath, string $actualFilesPath): void
{
$expectedFilesIterator = new FilesystemIterator($expectedFilesPath);
$actualFilesIterator = new FilesystemIterator($actualFilesPath);

$this->assertEquals(
iterator_count($expectedFilesIterator),
iterator_count($actualFilesIterator),
'Generated files and expected files not match',
);

foreach ($expectedFilesIterator as $fileInfo) {
/* @var \SplFileInfo $fileInfo */
$filename = $fileInfo->getFilename();

$actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename;

$this->assertFileExists($actualFile);

$this->assertStringMatchesFormatFile(
$fileInfo->getPathname(),
file_get_contents($actualFile),
"{$filename} not match",
);
}
}
}