diff --git a/composer.json b/composer.json index 36ee6ab5..627829aa 100644 --- a/composer.json +++ b/composer.json @@ -142,7 +142,7 @@ "infect:ci:unit": "@infect:ci:base --coverage=build/coverage-unit --min-msi=84", "infect:ci:db": "@infect:ci:base --coverage=build/coverage-db --min-msi=95 --configuration=infection-db.json", "infect:ci:api": "@infect:ci:base --coverage=build/coverage-api --min-msi=80 --configuration=infection-api.json", - "infect:ci:cli": "@infect:ci:base --coverage=build/coverage-cli --min-msi=95 --configuration=infection-cli.json", + "infect:ci:cli": "@infect:ci:base --coverage=build/coverage-cli --min-msi=80 --configuration=infection-cli.json", "infect:ci": "@parallel infect:ci:unit infect:ci:db infect:ci:api infect:ci:cli", "infect:test": [ "@parallel test:unit:ci test:db:sqlite:ci test:api:ci", diff --git a/config/test/bootstrap_cli_tests.php b/config/test/bootstrap_cli_tests.php index 703677d2..c3b9870c 100644 --- a/config/test/bootstrap_cli_tests.php +++ b/config/test/bootstrap_cli_tests.php @@ -6,6 +6,8 @@ namespace Shlinkio\Shlink\TestUtils; use Doctrine\ORM\EntityManager; use Psr\Container\ContainerInterface; +use function file_exists; +use function unlink; /** @var ContainerInterface $container */ $container = require __DIR__ . '/../container.php'; @@ -13,6 +15,12 @@ $testHelper = $container->get(Helper\TestHelper::class); $config = $container->get('config'); $em = $container->get(EntityManager::class); +// Delete old coverage in PHP, to avoid merging older executions with current one +$covFile = __DIR__ . '/../../build/coverage-cli.cov'; +if (file_exists($covFile)) { + unlink($covFile); +} + $testHelper->createTestDb(['bin/cli', 'db:create'], ['bin/cli', 'db:migrate']); CliTest\CliTestCase::setSeedFixturesCallback( static fn () => $testHelper->seedFixtures($em, $config['data_fixtures'] ?? []), diff --git a/config/test/test_config.global.php b/config/test/test_config.global.php index 75dd7457..e82d9da5 100644 --- a/config/test/test_config.global.php +++ b/config/test/test_config.global.php @@ -27,6 +27,7 @@ use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Event\ConsoleTerminateEvent; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; +use function file_exists; use function Laminas\Stratigility\middleware; use function Shlinkio\Shlink\Config\env; use function sprintf; @@ -57,7 +58,15 @@ $exportCoverage = static function (string $type = 'api') use (&$coverage): void } $basePath = __DIR__ . '/../../build/coverage-' . $type; - (new PHP())->process($coverage, $basePath . '.cov'); + $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); + } + + (new PHP())->process($coverage, $covPath); (new Xml(Version::getVersionString()))->process($coverage, $basePath . '/coverage-xml'); (new Html())->process($coverage, $basePath . '/coverage-html'); }; @@ -195,6 +204,7 @@ return [ $app = $callback(); $wrappedEventDispatcher = new EventDispatcher(); + // When the command starts, start collecting coverage $wrappedEventDispatcher->subscribeTo( ConsoleCommandEvent::class, static function () use (&$coverage): void { @@ -206,6 +216,7 @@ return [ $coverage?->start($id); }, ); + // When the command ends, stop collecting coverage $wrappedEventDispatcher->subscribeTo( ConsoleTerminateEvent::class, static function () use (&$coverage, $exportCoverage): void {