mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Move code around generating code coverage to test-utils lib
This commit is contained in:
parent
62051c8809
commit
e9c7053ef5
@ -71,7 +71,7 @@
|
|||||||
"phpunit/phpunit": "^10.4",
|
"phpunit/phpunit": "^10.4",
|
||||||
"roave/security-advisories": "dev-master",
|
"roave/security-advisories": "dev-master",
|
||||||
"shlinkio/php-coding-standard": "~2.3.0",
|
"shlinkio/php-coding-standard": "~2.3.0",
|
||||||
"shlinkio/shlink-test-utils": "^3.9",
|
"shlinkio/shlink-test-utils": "^3.10",
|
||||||
"symfony/var-dumper": "^6.4",
|
"symfony/var-dumper": "^6.4",
|
||||||
"veewee/composer-run-parallel": "^1.3"
|
"veewee/composer-run-parallel": "^1.3"
|
||||||
},
|
},
|
||||||
|
@ -9,19 +9,12 @@ use Laminas\ConfigAggregator\ConfigAggregator;
|
|||||||
use Laminas\Diactoros\Response\EmptyResponse;
|
use Laminas\Diactoros\Response\EmptyResponse;
|
||||||
use Laminas\ServiceManager\Factory\InvokableFactory;
|
use Laminas\ServiceManager\Factory\InvokableFactory;
|
||||||
use Monolog\Level;
|
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\Common\Logger\LoggerType;
|
||||||
use Shlinkio\Shlink\TestUtils\ApiTest\CoverageMiddleware;
|
use Shlinkio\Shlink\TestUtils\ApiTest\CoverageMiddleware;
|
||||||
use Shlinkio\Shlink\TestUtils\CliTest\CliCoverageDelegator;
|
use Shlinkio\Shlink\TestUtils\CliTest\CliCoverageDelegator;
|
||||||
|
use Shlinkio\Shlink\TestUtils\Helper\CoverageHelper;
|
||||||
use Symfony\Component\Console\Application;
|
use Symfony\Component\Console\Application;
|
||||||
|
|
||||||
use function file_exists;
|
|
||||||
use function Laminas\Stratigility\middleware;
|
use function Laminas\Stratigility\middleware;
|
||||||
use function Shlinkio\Shlink\Config\env;
|
use function Shlinkio\Shlink\Config\env;
|
||||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||||
@ -33,41 +26,13 @@ use const ShlinkioTest\Shlink\API_TESTS_PORT;
|
|||||||
$isApiTest = env('TEST_ENV') === 'api';
|
$isApiTest = env('TEST_ENV') === 'api';
|
||||||
$isCliTest = env('TEST_ENV') === 'cli';
|
$isCliTest = env('TEST_ENV') === 'cli';
|
||||||
$isE2eTest = $isApiTest || $isCliTest;
|
$isE2eTest = $isApiTest || $isCliTest;
|
||||||
|
|
||||||
$coverageType = env('GENERATE_COVERAGE');
|
$coverageType = env('GENERATE_COVERAGE');
|
||||||
$generateCoverage = contains($coverageType, ['yes', 'pretty']);
|
$generateCoverage = contains($coverageType, ['yes', 'pretty']);
|
||||||
|
$coverage = $isE2eTest && $generateCoverage ? CoverageHelper::createCoverageForDirectories([
|
||||||
$coverage = null;
|
__DIR__ . '/../../module/Core/src',
|
||||||
if ($isE2eTest && $generateCoverage) {
|
__DIR__ . '/../../module/' . ($isApiTest ? 'Rest' : 'CLI') . '/src',
|
||||||
$filter = new Filter();
|
]) : null;
|
||||||
$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');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$buildDbConnection = static function (): array {
|
$buildDbConnection = static function (): array {
|
||||||
$driver = env('DB_DRIVER', 'sqlite');
|
$driver = env('DB_DRIVER', 'sqlite');
|
||||||
@ -133,10 +98,14 @@ return [
|
|||||||
[
|
[
|
||||||
'name' => 'dump_coverage',
|
'name' => 'dump_coverage',
|
||||||
'path' => '/api-tests/stop-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
|
// 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
|
// 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();
|
return new EmptyResponse();
|
||||||
}),
|
}),
|
||||||
'allowed_methods' => ['GET'],
|
'allowed_methods' => ['GET'],
|
||||||
@ -168,7 +137,12 @@ return [
|
|||||||
],
|
],
|
||||||
'delegators' => $isCliTest ? [
|
'delegators' => $isCliTest ? [
|
||||||
Application::class => [
|
Application::class => [
|
||||||
new CliCoverageDelegator($exportCoverage(...), $coverage),
|
new CliCoverageDelegator(fn () => CoverageHelper::exportCoverage(
|
||||||
|
$coverage,
|
||||||
|
__DIR__ . '/../../build/coverage-cli',
|
||||||
|
pretty: $coverageType === 'pretty',
|
||||||
|
mergeWithExisting: true,
|
||||||
|
), $coverage),
|
||||||
],
|
],
|
||||||
] : [],
|
] : [],
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user