shlink/config/test/test_config.global.php

193 lines
6.1 KiB
PHP
Raw Normal View History

<?php
2019-10-05 18:26:10 +03:00
declare(strict_types=1);
2019-08-11 17:30:46 +03:00
namespace Shlinkio\Shlink;
2019-01-26 12:19:20 +03:00
use GuzzleHttp\Client;
2020-01-01 23:11:53 +03:00
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\Diactoros\Response\EmptyResponse;
2020-01-01 23:11:53 +03:00
use Laminas\ServiceManager\Factory\InvokableFactory;
2022-06-04 09:59:17 +03:00
use Monolog\Level;
2020-09-26 11:43:50 +03:00
use PHPUnit\Runner\Version;
use SebastianBergmann\CodeCoverage\CodeCoverage;
2020-11-02 13:50:19 +03:00
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\Filter;
2021-12-10 19:45:50 +03:00
use SebastianBergmann\CodeCoverage\Report\Html\Facade as Html;
use SebastianBergmann\CodeCoverage\Report\PHP;
2020-09-26 11:43:50 +03:00
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as Xml;
2022-08-09 20:48:43 +03:00
use Shlinkio\Shlink\Common\Logger\LoggerType;
use Shlinkio\Shlink\TestUtils\ApiTest\CoverageMiddleware;
use Shlinkio\Shlink\TestUtils\CliTest\CliCoverageDelegator;
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;
use function sprintf;
use const ShlinkioTest\Shlink\API_TESTS_HOST;
use const ShlinkioTest\Shlink\API_TESTS_PORT;
2020-09-26 11:43:50 +03:00
$isApiTest = env('TEST_ENV') === 'api';
$isCliTest = env('TEST_ENV') === 'cli';
$isE2eTest = $isApiTest || $isCliTest;
$coverageType = env('GENERATE_COVERAGE');
2023-11-30 11:13:29 +03:00
$generateCoverage = contains($coverageType, ['yes', 'pretty']);
$coverage = null;
if ($isE2eTest && $generateCoverage) {
2020-11-02 13:50:19 +03:00
$filter = new Filter();
2021-12-10 19:45:50 +03:00
$filter->includeDirectory(__DIR__ . '/../../module/Core/src');
$filter->includeDirectory(__DIR__ . '/../../module/' . ($isApiTest ? 'Rest' : 'CLI') . '/src');
2020-11-02 13:50:19 +03:00
$coverage = new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
2020-09-26 11:43:50 +03:00
}
/**
* @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');
}
};
2021-07-20 15:03:19 +03:00
$buildDbConnection = static function (): array {
$driver = env('DB_DRIVER', 'sqlite');
$isCi = env('CI', false);
2021-07-20 15:03:19 +03:00
$getCiMysqlPort = static fn (string $driver) => $driver === 'mysql' ? '3307' : '3308';
2021-07-20 15:03:19 +03:00
return match ($driver) {
'sqlite' => [
'driver' => 'pdo_sqlite',
'memory' => true,
],
'postgres' => [
'driver' => 'pdo_pgsql',
'host' => $isCi ? '127.0.0.1' : 'shlink_db_postgres',
'port' => $isCi ? '5433' : '5432',
'user' => 'postgres',
'password' => 'root',
'dbname' => 'shlink_test',
2022-01-10 15:04:16 +03:00
'charset' => 'utf8',
],
2020-02-03 23:20:40 +03:00
'mssql' => [
'driver' => 'pdo_sqlsrv',
'host' => $isCi ? '127.0.0.1' : 'shlink_db_ms',
'user' => 'sa',
'password' => 'Passw0rd!',
2020-02-03 23:20:40 +03:00
'dbname' => 'shlink_test',
'driverOptions' => [
'TrustServerCertificate' => 'true',
],
2020-02-03 23:20:40 +03:00
],
2021-07-20 15:03:19 +03:00
default => [ // mysql and maria
'driver' => 'pdo_mysql',
'host' => $isCi ? '127.0.0.1' : sprintf('shlink_db_%s', $driver),
2021-07-20 15:03:19 +03:00
'port' => $isCi ? $getCiMysqlPort($driver) : '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'shlink_test',
2022-01-10 15:04:16 +03:00
'charset' => 'utf8mb4',
2021-07-20 15:03:19 +03:00
],
};
};
2022-06-04 09:59:17 +03:00
$buildTestLoggerConfig = static fn (string $filename) => [
'level' => Level::Debug->value,
'type' => LoggerType::STREAM->value,
'destination' => sprintf('data/log/api-tests/%s', $filename),
'add_new_line' => true,
];
return [
2019-01-26 12:59:24 +03:00
'debug' => true,
ConfigAggregator::ENABLE_CACHE => false,
2019-01-27 14:35:00 +03:00
'url_shortener' => [
'domain' => [
'schema' => 'http',
'hostname' => 's.test',
2019-01-27 14:35:00 +03:00
],
],
'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'],
],
],
2021-12-10 19:45:50 +03:00
'middleware_pipeline' => !$isApiTest ? [] : [
'capture_code_coverage' => [
'middleware' => new CoverageMiddleware($coverage),
2021-12-10 19:45:50 +03:00
'priority' => 9999,
],
],
'mercure' => [
'public_hub_url' => null,
'internal_hub_url' => null,
'jwt_secret' => null,
],
'dependencies' => [
'services' => [
'shlink_test_api_client' => new Client([
'base_uri' => sprintf('http://%s:%s/', API_TESTS_HOST, API_TESTS_PORT),
'http_errors' => false,
]),
],
'factories' => [
2019-08-11 17:30:46 +03:00
TestUtils\Helper\TestHelper::class => InvokableFactory::class,
],
'delegators' => $isCliTest ? [
Application::class => [
new CliCoverageDelegator($exportCoverage(...), $coverage),
],
] : [],
],
'entity_manager' => [
'connection' => $buildDbConnection(),
],
2019-01-27 14:14:18 +03:00
'data_fixtures' => [
'paths' => [
2022-02-13 14:20:02 +03:00
// TODO These are used for CLI tests too, so maybe should be somewhere else
2019-01-27 14:14:18 +03:00
__DIR__ . '/../../module/Rest/test-api/Fixtures',
],
],
'logger' => [
2022-06-04 09:59:17 +03:00
'Shlink' => $buildTestLoggerConfig('shlink.log'),
'Access' => $buildTestLoggerConfig('access.log'),
],
];