Recover prev approach to generate API tests coverage

This commit is contained in:
Alejandro Celaya 2024-02-16 23:32:20 +01:00
parent 01846657d1
commit 0a6a794e23
2 changed files with 31 additions and 9 deletions

View file

@ -7,6 +7,12 @@ namespace Shlinkio\Shlink\TestUtils;
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;
use function register_shutdown_function;
use function sprintf;
use const ShlinkioTest\Shlink\API_TESTS_HOST;
use const ShlinkioTest\Shlink\API_TESTS_PORT;
/** @var ContainerInterface $container */
$container = require __DIR__ . '/../container.php';
$testHelper = $container->get(Helper\TestHelper::class);
@ -14,6 +20,14 @@ $config = $container->get('config');
$em = $container->get(EntityManager::class);
$httpClient = $container->get('shlink_test_api_client');
// Dump code coverage when process shuts down
register_shutdown_function(function () use ($httpClient): void {
$httpClient->request(
'GET',
sprintf('http://%s:%s/api-tests/stop-coverage', API_TESTS_HOST, API_TESTS_PORT),
);
});
$testHelper->createTestDb(
createDbCommand: ['bin/cli', 'db:create'],
migrateDbCommand: ['bin/cli', 'db:migrate'],

View file

@ -6,6 +6,7 @@ namespace Shlinkio\Shlink;
use GuzzleHttp\Client;
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Monolog\Level;
use PHPUnit\Runner\Version;
@ -21,7 +22,7 @@ use Shlinkio\Shlink\TestUtils\CliTest\CliCoverageDelegator;
use Symfony\Component\Console\Application;
use function file_exists;
use function register_shutdown_function;
use function Laminas\Stratigility\middleware;
use function Shlinkio\Shlink\Config\env;
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
use function sprintf;
@ -29,7 +30,7 @@ use function sprintf;
use const ShlinkioTest\Shlink\API_TESTS_HOST;
use const ShlinkioTest\Shlink\API_TESTS_PORT;
$isApiTest = env('TEST_ENV') === 'api' && env('RR_MODE') === 'http';
$isApiTest = env('TEST_ENV') === 'api';
$isCliTest = env('TEST_ENV') === 'cli';
$isE2eTest = $isApiTest || $isCliTest;
$coverageType = env('GENERATE_COVERAGE');
@ -68,13 +69,6 @@ $exportCoverage = static function (string $type = 'api') use (&$coverage, $cover
}
};
// Dump code coverage when process shuts down, only if running in HTTP mode
register_shutdown_function(function () use ($exportCoverage, $isApiTest): void {
if ($isApiTest) {
$exportCoverage();
}
});
$buildDbConnection = static function (): array {
$driver = env('DB_DRIVER', 'sqlite');
$isCi = env('CI', false);
@ -135,6 +129,20 @@ return [
],
],
'routes' => !$isApiTest ? [] : [
[
'name' => 'dump_coverage',
'path' => '/api-tests/stop-coverage',
'middleware' => middleware(static function () use ($exportCoverage) {
// 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();
return new EmptyResponse();
}),
'allowed_methods' => ['GET'],
],
],
'middleware_pipeline' => !$isApiTest ? [] : [
'capture_code_coverage' => [
'middleware' => new CoverageMiddleware($coverage),