shlink/config/test/test_config.global.php

186 lines
5.6 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;
2020-09-26 11:43:50 +03:00
use Laminas\Diactoros\Response\EmptyResponse;
2020-01-01 23:11:53 +03:00
use Laminas\ServiceManager\Factory\InvokableFactory;
2020-09-26 11:43:50 +03:00
use Laminas\Stdlib\Glob;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use PDO;
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;
use SebastianBergmann\CodeCoverage\Report\PHP;
2020-09-26 11:43:50 +03:00
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as Xml;
2020-09-26 11:43:50 +03:00
use function Laminas\Stratigility\middleware;
use function Shlinkio\Shlink\Common\env;
use function sprintf;
use function sys_get_temp_dir;
2020-09-26 11:43:50 +03:00
use const ShlinkioTest\Shlink\SWOOLE_TESTING_HOST;
use const ShlinkioTest\Shlink\SWOOLE_TESTING_PORT;
$isApiTest = env('TEST_ENV') === 'api';
if ($isApiTest) {
2020-11-02 13:50:19 +03:00
$filter = new Filter();
2020-09-26 11:43:50 +03:00
foreach (Glob::glob(__DIR__ . '/../../module/*/src') as $item) {
2020-11-02 13:50:19 +03:00
$filter->includeDirectory($item);
2020-09-26 11:43:50 +03:00
}
2020-11-02 13:50:19 +03:00
$coverage = new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
2020-09-26 11:43:50 +03:00
}
$buildDbConnection = function (): array {
$driver = env('DB_DRIVER', 'sqlite');
$isCi = env('CI', false);
$getMysqlHost = fn (string $driver) => sprintf('shlink_db%s', $driver === 'mysql' ? '' : '_maria');
$getCiMysqlPort = fn (string $driver) => $driver === 'mysql' ? '3307' : '3308';
$driverConfigMap = [
'sqlite' => [
'driver' => 'pdo_sqlite',
'path' => sys_get_temp_dir() . '/shlink-tests.db',
],
'mysql' => [
'driver' => 'pdo_mysql',
'host' => $isCi ? '127.0.0.1' : $getMysqlHost($driver),
'port' => $isCi ? $getCiMysqlPort($driver) : '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'shlink_test',
'charset' => 'utf8',
'driverOptions' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => 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',
'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',
],
];
$driverConfigMap['maria'] = $driverConfigMap['mysql'];
return $driverConfigMap[$driver] ?? [];
};
$buildTestLoggerConfig = fn (string $handlerName, string $filename) => [
'handlers' => [
$handlerName => [
'name' => StreamHandler::class,
'params' => [
'level' => Logger::DEBUG,
'stream' => sprintf('data/log/api-tests/%s', $filename),
],
],
],
];
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' => 'doma.in',
],
'validate_url' => true,
2019-01-27 14:35:00 +03:00
],
2020-01-01 23:11:53 +03:00
'mezzio-swoole' => [
'enable_coroutine' => false,
'swoole-http-server' => [
2020-09-26 11:43:50 +03:00
'host' => SWOOLE_TESTING_HOST,
'port' => SWOOLE_TESTING_PORT,
2019-01-26 12:19:20 +03:00
'process-name' => 'shlink_test',
'options' => [
'pid_file' => sys_get_temp_dir() . '/shlink-test-swoole.pid',
'enable_coroutine' => false,
],
],
],
2020-09-26 11:43:50 +03:00
'routes' => !$isApiTest ? [] : [
[
'name' => 'start_collecting_coverage',
'path' => '/api-tests/start-coverage',
'middleware' => middleware(static function () use (&$coverage) {
if ($coverage) {
$coverage->start('API tests');
2020-09-26 11:43:50 +03:00
}
return new EmptyResponse();
}),
'allowed_methods' => ['GET'],
],
[
'name' => 'dump_coverage',
'path' => '/api-tests/stop-coverage',
'middleware' => middleware(static function () use (&$coverage) {
if ($coverage) {
$basePath = __DIR__ . '/../../build/coverage-api';
$coverage->stop();
(new PHP())->process($coverage, $basePath . '.cov');
2020-09-26 11:43:50 +03:00
(new Xml(Version::getVersionString()))->process($coverage, $basePath . '/coverage-xml');
}
return new EmptyResponse();
}),
'allowed_methods' => ['GET'],
],
],
'mercure' => [
'public_hub_url' => null,
'internal_hub_url' => null,
'jwt_secret' => null,
],
'dependencies' => [
'services' => [
'shlink_test_api_client' => new Client([
2020-09-26 11:43:50 +03:00
'base_uri' => sprintf('http://%s:%s/', SWOOLE_TESTING_HOST, SWOOLE_TESTING_PORT),
'http_errors' => false,
]),
],
'factories' => [
2019-08-11 17:30:46 +03:00
TestUtils\Helper\TestHelper::class => InvokableFactory::class,
],
],
'entity_manager' => [
'connection' => $buildDbConnection(),
],
2019-01-27 14:14:18 +03:00
'data_fixtures' => [
'paths' => [
__DIR__ . '/../../module/Rest/test-api/Fixtures',
],
],
'logger' => [
'Shlink' => $buildTestLoggerConfig('shlink_handler', 'shlink.log'),
'Access' => $buildTestLoggerConfig('access_handler', 'access.log'),
],
];