Skip to content

Commit

Permalink
Move code around generating code coverage to test-utils lib
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Feb 17, 2024
1 parent 62051c8 commit e9c7053
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"phpunit/phpunit": "^10.4",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.3.0",
"shlinkio/shlink-test-utils": "^3.9",
"shlinkio/shlink-test-utils": "^3.10",
"symfony/var-dumper": "^6.4",
"veewee/composer-run-parallel": "^1.3"
},
Expand Down
62 changes: 18 additions & 44 deletions config/test/test_config.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Monolog\Level;
use PHPUnit\Runner\Version;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as Html;
use SebastianBergmann\CodeCoverage\Report\PHP;
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as Xml;
use Shlinkio\Shlink\Common\Logger\LoggerType;
use Shlinkio\Shlink\TestUtils\ApiTest\CoverageMiddleware;
use Shlinkio\Shlink\TestUtils\CliTest\CliCoverageDelegator;
use Shlinkio\Shlink\TestUtils\Helper\CoverageHelper;
use Symfony\Component\Console\Application;

use function file_exists;
use function Laminas\Stratigility\middleware;
use function Shlinkio\Shlink\Config\env;
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
Expand All @@ -33,41 +26,13 @@
$isApiTest = env('TEST_ENV') === 'api';
$isCliTest = env('TEST_ENV') === 'cli';
$isE2eTest = $isApiTest || $isCliTest;

$coverageType = env('GENERATE_COVERAGE');
$generateCoverage = contains($coverageType, ['yes', 'pretty']);

$coverage = null;
if ($isE2eTest && $generateCoverage) {
$filter = new Filter();
$filter->includeDirectory(__DIR__ . '/../../module/Core/src');
$filter->includeDirectory(__DIR__ . '/../../module/' . ($isApiTest ? 'Rest' : 'CLI') . '/src');
$coverage = new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
}

/**
* @param 'api'|'cli' $type
*/
$exportCoverage = static function (string $type = 'api') use (&$coverage, $coverageType): void {
if ($coverage === null) {
return;
}

$basePath = __DIR__ . '/../../build/coverage-' . $type;
$covPath = $basePath . '.cov';

// Every CLI test runs on its own process and dumps the coverage afterwards.
// Try to load it and merge it, so that we end up with the whole coverage at the end.
if ($type === 'cli' && file_exists($covPath)) {
$coverage->merge(require $covPath);
}

if ($coverageType === 'pretty') {
(new Html())->process($coverage, $basePath . '/coverage-html');
} else {
(new PHP())->process($coverage, $covPath);
(new Xml(Version::getVersionString()))->process($coverage, $basePath . '/coverage-xml');
}
};
$coverage = $isE2eTest && $generateCoverage ? CoverageHelper::createCoverageForDirectories([
__DIR__ . '/../../module/Core/src',
__DIR__ . '/../../module/' . ($isApiTest ? 'Rest' : 'CLI') . '/src',
]) : null;

$buildDbConnection = static function (): array {
$driver = env('DB_DRIVER', 'sqlite');
Expand Down Expand Up @@ -133,10 +98,14 @@
[
'name' => 'dump_coverage',
'path' => '/api-tests/stop-coverage',
'middleware' => middleware(static function () use ($exportCoverage) {
'middleware' => middleware(static function () use ($coverage, $coverageType) {
// TODO I have tried moving this block to a register_shutdown_function here, which internally checks if
// RR_MODE === 'http', but this seems to be false in CI, causing the coverage to not be generated
$exportCoverage();
CoverageHelper::exportCoverage(
$coverage,
__DIR__ . '/../../build/coverage-api',
pretty: $coverageType === 'pretty',
);
return new EmptyResponse();
}),
'allowed_methods' => ['GET'],
Expand Down Expand Up @@ -168,7 +137,12 @@
],
'delegators' => $isCliTest ? [
Application::class => [
new CliCoverageDelegator($exportCoverage(...), $coverage),
new CliCoverageDelegator(fn () => CoverageHelper::exportCoverage(
$coverage,
__DIR__ . '/../../build/coverage-cli',
pretty: $coverageType === 'pretty',
mergeWithExisting: true,
), $coverage),
],
] : [],
],
Expand Down

0 comments on commit e9c7053

Please sign in to comment.