mirror of
https://github.com/Dannecron/coverage-merger.git
synced 2025-12-26 00:02:35 +03:00
add tests
This commit is contained in:
95
tests/Unit/Clover/Handler/HandleSingleDocumentTest.php
Normal file
95
tests/Unit/Clover/Handler/HandleSingleDocumentTest.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Dannecron\CoverageMerger\Clover\Dto\FileDto;
|
||||
use Dannecron\CoverageMerger\Clover\Exceptions\HandleException;
|
||||
use Dannecron\CoverageMerger\Clover\Handler;
|
||||
use Dannecron\CoverageMerger\Clover\Parser;
|
||||
|
||||
\test('examples without files', function (string $exampleFilename): void {
|
||||
$handler = new Handler(new Parser());
|
||||
|
||||
$cloverContents = \file_get_contents(\getExamplePath($exampleFilename));
|
||||
|
||||
$accumulator = $handler->handleSingleDocument(
|
||||
\simplexml_load_string($cloverContents),
|
||||
);
|
||||
|
||||
$files = $accumulator->getFiles();
|
||||
\expect($files)->toHaveCount(0);
|
||||
})->with([
|
||||
'empty-package.xml',
|
||||
'empty-project.xml',
|
||||
'file-with-errors.xml',
|
||||
'file-with-no-name.xml',
|
||||
'minimal.xml',
|
||||
]);
|
||||
|
||||
\test('examples with single file', function (
|
||||
string $exampleFilename,
|
||||
string $expectedFilename,
|
||||
int $expectedClassesCount,
|
||||
int $expectedLinesCount,
|
||||
): void {
|
||||
$handler = new Handler(new Parser());
|
||||
|
||||
$cloverContents = \file_get_contents(\getExamplePath($exampleFilename));
|
||||
|
||||
$accumulator = $handler->handleSingleDocument(
|
||||
\simplexml_load_string($cloverContents),
|
||||
);
|
||||
|
||||
$files = $accumulator->getFiles();
|
||||
\expect($files)->toHaveCount(1)->toHaveKey($expectedFilename);
|
||||
|
||||
$file = $files[$expectedFilename];
|
||||
\expect($file)->toBeInstanceOf(FileDto::class);
|
||||
\expect($file->getClasses())->toHaveCount($expectedClassesCount);
|
||||
\expect($file->getLines())->toHaveCount($expectedLinesCount);
|
||||
})
|
||||
->with([
|
||||
['empty-file-with-package.xml', 'test.php', 0, 0],
|
||||
['file-with-package.xml', 'test.php', 0, 5],
|
||||
['file-without-package.xml', 'test.php', 0, 4],
|
||||
['metrics-and-classes.xml', '/src/Example/Namespace/Class.php', 1, 16],
|
||||
]);
|
||||
|
||||
\test('examples with two files', function (
|
||||
string $exampleFilename,
|
||||
string $expectedFilename1,
|
||||
string $expectedFilename2,
|
||||
): void {
|
||||
$handler = new Handler(new Parser());
|
||||
|
||||
$cloverContents = \file_get_contents(\getExamplePath($exampleFilename));
|
||||
|
||||
$accumulator = $handler->handleSingleDocument(
|
||||
\simplexml_load_string($cloverContents),
|
||||
);
|
||||
|
||||
$files = $accumulator->getFiles();
|
||||
\expect($files)->toHaveCount(2)
|
||||
->toHaveKey($expectedFilename1)
|
||||
->toHaveKey($expectedFilename2);
|
||||
})
|
||||
->with([
|
||||
['empty-file-without-package.xml', 'test.php', 'other.php'],
|
||||
['file-with-differences.xml', 'test.php', 'other.php'],
|
||||
]);
|
||||
|
||||
\test('examples with invalid structure', function (string $exampleFilename): void {
|
||||
$handler = new Handler(new Parser());
|
||||
|
||||
$cloverContents = \file_get_contents(\getExamplePath($exampleFilename));
|
||||
|
||||
$this->expectException(HandleException::class);
|
||||
$handler->handleSingleDocument(
|
||||
\simplexml_load_string($cloverContents),
|
||||
);
|
||||
})
|
||||
->with([
|
||||
'file-with-bad-line.xml',
|
||||
'file-with-junk.xml',
|
||||
'non-clover.xml',
|
||||
]);
|
||||
60
tests/Unit/Clover/Handler/HandleTest.php
Normal file
60
tests/Unit/Clover/Handler/HandleTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Dannecron\CoverageMerger\Clover\Dto\ClassDto;
|
||||
use Dannecron\CoverageMerger\Clover\Dto\FileDto;
|
||||
use Dannecron\CoverageMerger\Clover\Dto\LineDto;
|
||||
use Dannecron\CoverageMerger\Clover\Handler;
|
||||
use Dannecron\CoverageMerger\Clover\Parser;
|
||||
|
||||
\test('merge multiple valid files', function (): void {
|
||||
$fileWithPackage = \file_get_contents(\getExamplePath('file-with-package.xml'));
|
||||
$fileWithoutPackage = \file_get_contents(\getExamplePath('file-without-package.xml'));
|
||||
$fileWithDifferences = \file_get_contents(\getExamplePath('file-with-differences.xml'));
|
||||
$metricsAndClasses = \file_get_contents(\getExamplePath('metrics-and-classes.xml'));
|
||||
|
||||
$handler = new Handler(new Parser());
|
||||
|
||||
$accumulator = $handler->handle(
|
||||
\simplexml_load_string($fileWithPackage),
|
||||
\simplexml_load_string($fileWithoutPackage),
|
||||
\simplexml_load_string($fileWithDifferences),
|
||||
\simplexml_load_string($metricsAndClasses),
|
||||
);
|
||||
|
||||
$files = $accumulator->getFiles();
|
||||
\expect($files)->toHaveCount(3)
|
||||
->toHaveKey('test.php')
|
||||
->toHaveKey('other.php')
|
||||
->toHaveKey('/src/Example/Namespace/Class.php')
|
||||
->each->toBeInstanceOf(FileDto::class);
|
||||
|
||||
$testFile = $files['test.php'];
|
||||
\expect($testFile->getClasses())->toHaveCount(0);
|
||||
$testFileLines = $testFile->getLines();
|
||||
\expect($testFileLines)->toHaveCount(7)
|
||||
->toHaveKeys([1, 2, 3, 4, 5, 6, 8])
|
||||
->each->toBeInstanceOf(LineDto::class)
|
||||
->toMatchCallback(fn (LineDto $line): bool => match ($line->getNum()) {
|
||||
1 => $line->getCount() === 0,
|
||||
2, 8 => $line->getCount() === 3,
|
||||
3, 5 => $line->getCount() === 4,
|
||||
6 => $line->getCount() === 1,
|
||||
4 => $line->getCount() === 9,
|
||||
default => true,
|
||||
});
|
||||
|
||||
$classFile = $files['/src/Example/Namespace/Class.php'];
|
||||
\expect($classFile->getClasses())->toHaveCount(1)
|
||||
->each->toBeInstanceOf(ClassDto::class)
|
||||
->toMatchCallback(function (ClassDto $class): bool {
|
||||
$properties = $class->getProperties();
|
||||
|
||||
return $properties['name'] === 'Example\Namespace\Class'
|
||||
&& $properties['namespace'] === 'Example\Namespace';
|
||||
});
|
||||
});
|
||||
|
||||
// todo merge multiple files with empty report
|
||||
// todo merge multiple files with invalid report
|
||||
101
tests/Unit/Clover/RendererTest.php
Normal file
101
tests/Unit/Clover/RendererTest.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Dannecron\CoverageMerger\Clover\Dto\Accumulator;
|
||||
use Dannecron\CoverageMerger\Clover\Dto\ClassDto;
|
||||
use Dannecron\CoverageMerger\Clover\Dto\FileDto;
|
||||
use Dannecron\CoverageMerger\Clover\Dto\LineDto;
|
||||
use Dannecron\CoverageMerger\Clover\Dto\Metrics;
|
||||
use Dannecron\CoverageMerger\Clover\Renderer;
|
||||
|
||||
\test('test render accumulator', function (): void {
|
||||
$package = 'package';
|
||||
|
||||
$accumulator = new Accumulator();
|
||||
|
||||
$file1 = new FileDto($package);
|
||||
$file1->mergeLine(1, new LineDto(['num' => '1', 'type' => 'stmt'], 2));
|
||||
$file1->mergeLine(2, new LineDto(['num' => '2', 'type' => 'stmt'], 3));
|
||||
|
||||
$file2 = new FileDto($package);
|
||||
$file2->mergeLine(22, new LineDto([
|
||||
'num' => '22',
|
||||
'type' => 'method',
|
||||
'name' => '__construct',
|
||||
'visibility' => 'public',
|
||||
'complexity' => '7',
|
||||
'crap' => '8.23',
|
||||
], 1));
|
||||
$file2->mergeLine(24, new LineDto(['num' => '24', 'type' => 'stmt'], 3));
|
||||
$file2->mergeClass('Example\Namespace\Class', new ClassDto([
|
||||
'name' => 'Example\Namespace\Class',
|
||||
'namespace' => 'Example\Namespace',
|
||||
]));
|
||||
|
||||
$file3 = new FileDto();
|
||||
$file3->mergeLine(34, new LineDto(['num' => '34', 'type' => 'cond'], 0));
|
||||
$file3->mergeLine(38, new LineDto(['num' => '38', 'type' => 'cond'], 1));
|
||||
|
||||
$accumulator->addFile('test1.php', $file1);
|
||||
$accumulator->addFile('test2.php', $file2);
|
||||
$accumulator->addFile('test3.php', $file3);
|
||||
|
||||
$renderer = new Renderer();
|
||||
$result = $renderer->renderAccumulator($accumulator);
|
||||
|
||||
\expect($result)->not->toBeEmpty()
|
||||
->toHaveCount(2);
|
||||
|
||||
$resultXmlString = $result[0];
|
||||
\expect($resultXmlString)->toBeString()
|
||||
->not->toBeEmpty()
|
||||
->toStartWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<coverage");
|
||||
|
||||
\expect(\simplexml_load_string($resultXmlString))->toBeInstanceOf(\SimpleXMLElement::class)
|
||||
->toMatchCallback(function (\SimpleXMLElement $actualCoverage) use ($package): bool {
|
||||
$packageXpath = "/coverage/project/package[@name=\"{$package}\"]";
|
||||
$packageFiles = $actualCoverage->xpath("{$packageXpath}/file");
|
||||
\expect($packageFiles)->toBeArray()->toHaveCount(2);
|
||||
|
||||
$nonPackageFiles = $actualCoverage->xpath('/coverage/project/file');
|
||||
\expect($nonPackageFiles)->toBeArray()->toHaveCount(1);
|
||||
|
||||
$packageMetrics = $actualCoverage->xpath("{$packageXpath}/metrics")[0];
|
||||
\expect($packageMetrics)->toBeInstanceOf(\SimpleXMLElement::class);
|
||||
\expect($packageMetrics->attributes())
|
||||
->toMatchCallback(
|
||||
static fn (\SimpleXMLElement $attributes): bool => (int) $attributes->elements === 4
|
||||
&& (int) $attributes->coveredelements === 4
|
||||
&& (int) $attributes->conditionals === 0
|
||||
&& (int) $attributes->coveredconditionals === 0
|
||||
&& (int) $attributes->statements === 3
|
||||
&& (int) $attributes->coveredstatements === 3
|
||||
&& (int) $attributes->methods === 1
|
||||
&& (int) $attributes->coveredmethods === 1
|
||||
&& (int) $attributes->classes === 1,
|
||||
'invalid package metrics',
|
||||
);
|
||||
|
||||
$projectMetrics = $actualCoverage->xpath('/coverage/project/metrics')[0];
|
||||
\expect($projectMetrics)->toBeInstanceOf(\SimpleXMLElement::class);
|
||||
\expect($projectMetrics->attributes())
|
||||
->toMatchCallback(
|
||||
static fn (\SimpleXMLElement $attributes): bool => (int) $attributes->elements === 6
|
||||
&& (int) $attributes->coveredelements === 5
|
||||
&& (int) $attributes->conditionals === 2
|
||||
&& (int) $attributes->coveredconditionals === 1
|
||||
&& (int) $attributes->statements === 3
|
||||
&& (int) $attributes->coveredstatements === 3
|
||||
&& (int) $attributes->methods === 1
|
||||
&& (int) $attributes->coveredmethods === 1
|
||||
&& (int) $attributes->classes === 1,
|
||||
'invalid project metrics',
|
||||
);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
$resultMetrics = $result[1];
|
||||
\expect($resultMetrics)->toBeInstanceOf(Metrics::class);
|
||||
});
|
||||
7
tests/Unit/ExampleTest.php
Normal file
7
tests/Unit/ExampleTest.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
\test('example', function () {
|
||||
\expect(true)->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user