mirror of
https://github.com/Dannecron/coverage-merger.git
synced 2025-12-25 15:52:34 +03:00
add tests
This commit is contained in:
283
tests/Feature/Command/CloverMergeCommandTest.php
Normal file
283
tests/Feature/Command/CloverMergeCommandTest.php
Normal file
@@ -0,0 +1,283 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Ahc\Cli\IO\Interactor;
|
||||
use Ahc\Cli\Output\Writer;
|
||||
use Dannecron\CoverageMerger\Clover\Handler;
|
||||
use Dannecron\CoverageMerger\Clover\Parser;
|
||||
use Dannecron\CoverageMerger\Clover\Renderer;
|
||||
use Dannecron\CoverageMerger\Command\CloverMergeCommand;
|
||||
use Dannecron\CoverageMerger\Command\Exceptions\ExecuteException;
|
||||
use Dannecron\CoverageMerger\Command\Exceptions\InvalidArgumentException;
|
||||
use Tests\Helpers\Traits\MakeCliApplication;
|
||||
|
||||
\uses(MakeCliApplication::class);
|
||||
|
||||
\beforeEach(function (): void {
|
||||
$mergedPath = '/tmp/merged.xml';
|
||||
if (\is_file($mergedPath)) {
|
||||
\unlink($mergedPath);
|
||||
}
|
||||
});
|
||||
|
||||
\test('merge two files no workdir', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
new Handler(new Parser()),
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$mergedPath = '/tmp/merged.xml';
|
||||
\expect($mergedPath)->not->toBeFile();
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
\getExamplePath('metrics-and-classes.xml'),
|
||||
\getExamplePath('file-with-differences.xml'),
|
||||
]);
|
||||
|
||||
\expect($mergedPath)->toBeFile()->toBeReadableFile();
|
||||
\expect(\file_get_contents($mergedPath))->toBeString()
|
||||
->not->toBeEmpty();
|
||||
});
|
||||
|
||||
\test('merge two files with workdir', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
new Handler(new Parser()),
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$filename1 = 'metrics-and-classes.xml';
|
||||
$filename2 = 'file-with-differences.xml';
|
||||
|
||||
$dir = '/tmp/temp_source';
|
||||
if (\is_dir($dir) === false) {
|
||||
\mkdir($dir);
|
||||
}
|
||||
\copy(\getExamplePath($filename1), "{$dir}/{$filename1}");
|
||||
\copy(\getExamplePath($filename2), "{$dir}/{$filename2}");
|
||||
|
||||
$mergedPath = 'some_merged.xml';
|
||||
$mergedFullPath = "{$dir}/{$mergedPath}";
|
||||
\expect($mergedFullPath)->not->toBeFile();
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
'-w',
|
||||
$dir,
|
||||
$filename1,
|
||||
$filename2,
|
||||
]);
|
||||
|
||||
\expect($mergedFullPath)->toBeFile()->toBeReadableFile();
|
||||
\expect(\file_get_contents($mergedFullPath))->toBeString()
|
||||
->not->toBeEmpty();
|
||||
|
||||
\unlink($mergedFullPath);
|
||||
});
|
||||
|
||||
\test('merge two files no workdir with stats', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
|
||||
$interactorMock = \Mockery::mock(Interactor::class);
|
||||
$interactorMock->shouldReceive('info')
|
||||
->once()
|
||||
->with(
|
||||
'Files Discovered: 2',
|
||||
true
|
||||
)
|
||||
->andReturn(\Mockery::mock(Writer::class));
|
||||
$interactorMock->shouldReceive('info')
|
||||
->once()
|
||||
->with(
|
||||
'Final Coverage: 5/5 (100.00%)',
|
||||
true
|
||||
)
|
||||
->andReturn(\Mockery::mock(Writer::class));
|
||||
|
||||
$cliApp->io($interactorMock);
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
new Handler(new Parser()),
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$mergedPath = '/tmp/merged.xml';
|
||||
\expect($mergedPath)->not->toBeFile();
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
'-s',
|
||||
\getExamplePath('empty-package.xml'),
|
||||
\getExamplePath('file-with-differences.xml'),
|
||||
]);
|
||||
|
||||
\expect($mergedPath)->toBeFile()->toBeReadableFile();
|
||||
\expect(\file_get_contents($mergedPath))->toBeString()
|
||||
->not->toBeEmpty();
|
||||
});
|
||||
|
||||
\test('merge two files error file not exist', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
new Handler(new Parser()),
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$mergedPath = '/tmp/merged.xml';
|
||||
\expect($mergedPath)->not->toBeFile();
|
||||
|
||||
$badFilePath = \getExamplePath('some-very-bad-file.xml');
|
||||
|
||||
$this->expectException(ExecuteException::class);
|
||||
$this->expectExceptionMessage("File {$badFilePath} does not exists or not readable");
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
\getExamplePath('metrics-and-classes.xml'),
|
||||
$badFilePath,
|
||||
]);
|
||||
});
|
||||
|
||||
\test('merge two files error file not xml', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
new Handler(new Parser()),
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$mergedPath = '/tmp/merged.xml';
|
||||
\expect($mergedPath)->not->toBeFile();
|
||||
|
||||
$badFilePath = \getExamplePath('not-xml.json');
|
||||
|
||||
$this->expectException(ExecuteException::class);
|
||||
$this->expectExceptionMessage("Unable to parse file {$badFilePath}");
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
\getExamplePath('metrics-and-classes.xml'),
|
||||
$badFilePath,
|
||||
]);
|
||||
});
|
||||
|
||||
\test('merge two files error workdir not exist', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
new Handler(new Parser()),
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$mergedPath = 'tmp/merged.xml';
|
||||
\expect($mergedPath)->not->toBeFile();
|
||||
|
||||
$badFilePath = \getExamplePath('some-very-bad-file.xml');
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Invalid argument workdir');
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
'-w',
|
||||
'/tmp/foo/bar/baz',
|
||||
\getExamplePath('metrics-and-classes.xml'),
|
||||
\getExamplePath('file-with-differences.xml'),
|
||||
]);
|
||||
});
|
||||
|
||||
\test('merge two files error no arguments', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
new Handler(new Parser()),
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$mergedPath = '/tmp/merged.xml';
|
||||
\expect($mergedPath)->not->toBeFile();
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Invalid argument files');
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
]);
|
||||
});
|
||||
|
||||
\test('merge two files error in handler', function (): void {
|
||||
$cliApp = $this->makeCliApp();
|
||||
|
||||
$handlerMock = \Mockery::mock(Handler::class);
|
||||
$handlerMock->shouldReceive('handle')
|
||||
->once()
|
||||
->with(
|
||||
\Mockery::type(\SimpleXMLElement::class),
|
||||
\Mockery::type(\SimpleXMLElement::class),
|
||||
)
|
||||
->andThrow(new \RuntimeException('some error'));
|
||||
|
||||
$cliApp->add(
|
||||
new CloverMergeCommand(
|
||||
$handlerMock,
|
||||
new Renderer(),
|
||||
$cliApp,
|
||||
),
|
||||
);
|
||||
|
||||
$mergedPath = '/tmp/merged.xml';
|
||||
\expect($mergedPath)->not->toBeFile();
|
||||
|
||||
$this->expectException(ExecuteException::class);
|
||||
$this->expectExceptionMessage('some error');
|
||||
|
||||
$cliApp->handle([
|
||||
'./merger',
|
||||
'clover',
|
||||
'-o',
|
||||
$mergedPath,
|
||||
\getExamplePath('metrics-and-classes.xml'),
|
||||
\getExamplePath('file-with-differences.xml'),
|
||||
]);
|
||||
});
|
||||
20
tests/Helpers/Traits/MakeCliApplication.php
Normal file
20
tests/Helpers/Traits/MakeCliApplication.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Helpers\Traits;
|
||||
|
||||
use Ahc\Cli\Application;
|
||||
|
||||
trait MakeCliApplication
|
||||
{
|
||||
protected function makeCliApp(): Application
|
||||
{
|
||||
$cliApp = new \Ahc\Cli\Application('test', '0.0.1', static fn () => true);
|
||||
$cliApp->onException(static function (\Throwable $exception): void {
|
||||
throw $exception;
|
||||
});
|
||||
|
||||
return $cliApp;
|
||||
}
|
||||
}
|
||||
59
tests/Pest.php
Normal file
59
tests/Pest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Test Case
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The closure you provide to your test functions is always bound to a specific PHPUnit test
|
||||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
|
||||
| need to change it using the "uses()" function to bind a different classes or traits.
|
||||
|
|
||||
*/
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
\uses(TestCase::class)->group('unit')->in('Unit');
|
||||
\uses(TestCase::class)->group('feature')->in('Feature');
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Expectations
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| When you're writing tests, you often need to check that values meet certain conditions. The
|
||||
| "expect()" function gives you access to a set of "expectations" methods that you can use
|
||||
| to assert different things. Of course, you may extend the Expectation API at any time.
|
||||
|
|
||||
*/
|
||||
|
||||
\expect()->extend('toBeOne', function () {
|
||||
return $this->toBe(1);
|
||||
});
|
||||
|
||||
\expect()->extend('toMatchCallback', function (callable $callback, string $message = '') {
|
||||
$result = $callback($this->value);
|
||||
\expect($result)->toBeTrue($message);
|
||||
|
||||
return $this;
|
||||
});
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Functions
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
|
||||
| project that you don't want to repeat in every file. Here you can also expose helpers as
|
||||
| global functions to help you to reduce the number of lines of code in your test files.
|
||||
|
|
||||
*/
|
||||
|
||||
function getExamplePath(string $filename): string
|
||||
{
|
||||
return __DIR__ . "/data/examples/{$filename}";
|
||||
}
|
||||
11
tests/TestCase.php
Normal file
11
tests/TestCase.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
}
|
||||
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();
|
||||
});
|
||||
3
tests/data/.gitignore
vendored
Normal file
3
tests/data/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
*
|
||||
!examples/
|
||||
!.gitignore
|
||||
8
tests/data/examples/empty-file-with-package.xml
Normal file
8
tests/data/examples/empty-file-with-package.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<package name="test">
|
||||
<file name="test.php"/>
|
||||
</package>
|
||||
</project>
|
||||
</coverage>
|
||||
7
tests/data/examples/empty-file-without-package.xml
Normal file
7
tests/data/examples/empty-file-without-package.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<file name="test.php"/>
|
||||
<file name="other.php"/>
|
||||
</project>
|
||||
</coverage>
|
||||
6
tests/data/examples/empty-package.xml
Normal file
6
tests/data/examples/empty-package.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<package name="test"/>
|
||||
</project>
|
||||
</coverage>
|
||||
4
tests/data/examples/empty-project.xml
Normal file
4
tests/data/examples/empty-project.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project/>
|
||||
</coverage>
|
||||
14
tests/data/examples/file-with-bad-line.xml
Normal file
14
tests/data/examples/file-with-bad-line.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<package name="test">
|
||||
<file name="test.php">
|
||||
<line num="1" type="method" name="__construct" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="2" type="stmt" />
|
||||
<line num="3" type="stmt" count="2"/>
|
||||
<line num="4" type="stmt" count="3"/>
|
||||
<line num="5" type="stmt" count="4"/>
|
||||
</file>
|
||||
</package>
|
||||
</project>
|
||||
</coverage>
|
||||
16
tests/data/examples/file-with-differences.xml
Normal file
16
tests/data/examples/file-with-differences.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<package name="test">
|
||||
<file name="test.php">
|
||||
<line num="2" type="stmt" count="1"/>
|
||||
<line num="4" type="stmt" count="3"/>
|
||||
<line num="6" type="stmt" count="1"/>
|
||||
<line num="8" type="stmt" count="3"/>
|
||||
</file>
|
||||
<file name="other.php">
|
||||
<line num="1" type="stmt" count="1"/>
|
||||
</file>
|
||||
</package>
|
||||
</project>
|
||||
</coverage>
|
||||
10
tests/data/examples/file-with-errors.xml
Normal file
10
tests/data/examples/file-with-errors.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<package/>
|
||||
<package>
|
||||
<file/>
|
||||
<bogus/>
|
||||
</package>
|
||||
</project>
|
||||
</coverage>
|
||||
22
tests/data/examples/file-with-junk.xml
Normal file
22
tests/data/examples/file-with-junk.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<bogus/>
|
||||
<project>
|
||||
<dinosaurs>
|
||||
<dinosaur name="Tyrannosaurus"/>
|
||||
<dinosaur name="Velociraptor"/>
|
||||
<dinosaur name="Spinosaurus"/>
|
||||
</dinosaurs>
|
||||
<package name="test">
|
||||
<folder name="test folder"/>
|
||||
<file name="test.php">
|
||||
<line num="1" type="method" name="__construct" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="2" type="stmt" count="1"/>
|
||||
<line num="3" type="stmt" count="2"/>
|
||||
<line num="4" type="stmt" count="3"/>
|
||||
<line num="5" type="stmt" count="4"/>
|
||||
</file>
|
||||
</package>
|
||||
</project>
|
||||
<project/>
|
||||
</coverage>
|
||||
6
tests/data/examples/file-with-no-name.xml
Normal file
6
tests/data/examples/file-with-no-name.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<file/>
|
||||
</project>
|
||||
</coverage>
|
||||
14
tests/data/examples/file-with-package.xml
Normal file
14
tests/data/examples/file-with-package.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<package name="test">
|
||||
<file name="test.php">
|
||||
<line num="1" type="method" name="__construct" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="2" type="stmt" count="1"/>
|
||||
<line num="3" type="stmt" count="2"/>
|
||||
<line num="4" type="stmt" count="3"/>
|
||||
<line num="5" type="stmt" count="4"/>
|
||||
</file>
|
||||
</package>
|
||||
</project>
|
||||
</coverage>
|
||||
12
tests/data/examples/file-without-package.xml
Normal file
12
tests/data/examples/file-without-package.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage>
|
||||
<project>
|
||||
<file name="test.php">
|
||||
<line num="1" type="method" name="__construct" visibility="public" complexity="1" crap="2.00" count="0"/>
|
||||
<line num="2" type="stmt" count="1"/>
|
||||
<line num="3" type="stmt" count="2"/>
|
||||
<line num="4" type="stmt" count="3"/>
|
||||
</file>
|
||||
<metrics loc="719" ncloc="578" statements="141" coveredstatements="138"/>
|
||||
</project>
|
||||
</coverage>
|
||||
30
tests/data/examples/metrics-and-classes.xml
Normal file
30
tests/data/examples/metrics-and-classes.xml
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage generated="1537585816">
|
||||
<project timestamp="1537585816">
|
||||
<package name="Example\Namespace">
|
||||
<file name="/src/Example/Namespace/Class.php">
|
||||
<class name="Example\Namespace\Class" namespace="Example\Namespace">
|
||||
<metrics complexity="7" methods="1" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="15" coveredstatements="9" elements="16" coveredelements="9"/>
|
||||
</class>
|
||||
<line num="22" type="method" name="__construct" visibility="public" complexity="7" crap="10.14" count="1"/>
|
||||
<line num="28" type="stmt" count="1"/>
|
||||
<line num="29" type="stmt" count="0"/>
|
||||
<line num="31" type="stmt" count="0"/>
|
||||
<line num="32" type="stmt" count="0"/>
|
||||
<line num="35" type="stmt" count="1"/>
|
||||
<line num="36" type="stmt" count="0"/>
|
||||
<line num="37" type="stmt" count="1"/>
|
||||
<line num="38" type="stmt" count="0"/>
|
||||
<line num="39" type="stmt" count="1"/>
|
||||
<line num="40" type="stmt" count="0"/>
|
||||
<line num="43" type="stmt" count="1"/>
|
||||
<line num="44" type="stmt" count="1"/>
|
||||
<line num="45" type="stmt" count="1"/>
|
||||
<line num="49" type="stmt" count="1"/>
|
||||
<line num="50" type="stmt" count="1"/>
|
||||
<metrics loc="51" ncloc="37" classes="1" methods="1" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="15" coveredstatements="9" elements="16" coveredelements="9"/>
|
||||
</file>
|
||||
</package>
|
||||
<metrics loc="719" ncloc="578" statements="141" coveredstatements="138"/>
|
||||
</project>
|
||||
</coverage>
|
||||
2
tests/data/examples/minimal.xml
Normal file
2
tests/data/examples/minimal.xml
Normal file
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<coverage/>
|
||||
4
tests/data/examples/non-clover.xml
Normal file
4
tests/data/examples/non-clover.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<card>
|
||||
<name>John Doe</name>
|
||||
<title>CEO, Widget Inc.</title>
|
||||
</card>
|
||||
Reference in New Issue
Block a user