From e9c7053ef5b10e1a2b30f7476f191a19c8ebcf60 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 17 Feb 2024 08:41:17 +0100 Subject: [PATCH 1/2] Move code around generating code coverage to test-utils lib --- composer.json | 2 +- config/test/test_config.global.php | 62 +++++++++--------------------- 2 files changed, 19 insertions(+), 45 deletions(-) diff --git a/composer.json b/composer.json index 1ffb6ab2..f9e918c7 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index ba0d8f2b..94d723b6 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -9,19 +9,12 @@ use Laminas\ConfigAggregator\ConfigAggregator; 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; @@ -33,41 +26,13 @@ use const ShlinkioTest\Shlink\API_TESTS_PORT; $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'); @@ -133,10 +98,14 @@ return [ [ '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'], @@ -168,7 +137,12 @@ return [ ], 'delegators' => $isCliTest ? [ Application::class => [ - new CliCoverageDelegator($exportCoverage(...), $coverage), + new CliCoverageDelegator(fn () => CoverageHelper::exportCoverage( + $coverage, + __DIR__ . '/../../build/coverage-cli', + pretty: $coverageType === 'pretty', + mergeWithExisting: true, + ), $coverage), ], ] : [], ], From 13f9f106be298f9110ca9e663a8b3cc1c4190917 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 17 Feb 2024 08:41:53 +0100 Subject: [PATCH 2/2] Update to event-dispatcher without swoole --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f9e918c7..89d65058 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ "ramsey/uuid": "^4.7", "shlinkio/shlink-common": "dev-main#2323ff3 as 6.0", "shlinkio/shlink-config": "dev-main#6b287b3 as 2.6", - "shlinkio/shlink-event-dispatcher": "^3.1", + "shlinkio/shlink-event-dispatcher": "dev-main#46f5e21 as 4.0", "shlinkio/shlink-importer": "^5.2.1", "shlinkio/shlink-installer": "dev-develop#2dee7db as 9.0", "shlinkio/shlink-ip-geolocation": "^3.4",