Skip to content

Commit

Permalink
Recover prev approach to generate API tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Feb 16, 2024
1 parent 0184665 commit 0a6a794
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
14 changes: 14 additions & 0 deletions config/test/bootstrap_api_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@
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);
$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'],
Expand Down
26 changes: 17 additions & 9 deletions config/test/test_config.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GuzzleHttp\Client;
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Monolog\Level;
use PHPUnit\Runner\Version;
Expand All @@ -21,15 +22,15 @@
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;

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');
Expand Down Expand Up @@ -68,13 +69,6 @@
}
};

// 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);
Expand Down Expand Up @@ -135,6 +129,20 @@
],
],

'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),
Expand Down

0 comments on commit 0a6a794

Please sign in to comment.