Issues (90)

config/test/test_config.global.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink;
6
7
use GuzzleHttp\Client;
8
use Laminas\ConfigAggregator\ConfigAggregator;
9
use Laminas\Diactoros\Response\EmptyResponse;
10
use Laminas\ServiceManager\Factory\InvokableFactory;
11
use Laminas\Stdlib\Glob;
12
use PDO;
13
use PHPUnit\Runner\Version;
14
use SebastianBergmann\CodeCoverage\CodeCoverage;
15
use SebastianBergmann\CodeCoverage\Driver\Selector;
16
use SebastianBergmann\CodeCoverage\Filter;
17
use SebastianBergmann\CodeCoverage\Report\PHP;
18
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as Xml;
19
20
use function Laminas\Stratigility\middleware;
21
use function Shlinkio\Shlink\Common\env;
22
use function sprintf;
23
use function sys_get_temp_dir;
24
25
use const ShlinkioTest\Shlink\SWOOLE_TESTING_HOST;
0 ignored issues
show
The constant ShlinkioTest\Shlink\SWOOLE_TESTING_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26
use const ShlinkioTest\Shlink\SWOOLE_TESTING_PORT;
0 ignored issues
show
The constant ShlinkioTest\Shlink\SWOOLE_TESTING_PORT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
28
$isApiTest = env('TEST_ENV') === 'api';
29
if ($isApiTest) {
30
    $filter = new Filter();
31
    foreach (Glob::glob(__DIR__ . '/../../module/*/src') as $item) {
32
        $filter->includeDirectory($item);
33
    }
34
    $coverage = new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
35
}
36
37
$buildDbConnection = function (): array {
38
    $driver = env('DB_DRIVER', 'sqlite');
39
    $isCi = env('CI', false);
40
    $getMysqlHost = fn (string $driver) => sprintf('shlink_db%s', $driver === 'mysql' ? '' : '_maria');
41
    $getCiMysqlPort = fn (string $driver) => $driver === 'mysql' ? '3307' : '3308';
42
43
    $driverConfigMap = [
44
        'sqlite' => [
45
            'driver' => 'pdo_sqlite',
46
            'path' => sys_get_temp_dir() . '/shlink-tests.db',
47
        ],
48
        'mysql' => [
49
            'driver' => 'pdo_mysql',
50
            'host' => $isCi ? '127.0.0.1' : $getMysqlHost($driver),
51
            'port' => $isCi ? $getCiMysqlPort($driver) : '3306',
52
            'user' => 'root',
53
            'password' => 'root',
54
            'dbname' => 'shlink_test',
55
            'charset' => 'utf8',
56
            'driverOptions' => [
57
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
58
                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
59
            ],
60
        ],
61
        'postgres' => [
62
            'driver' => 'pdo_pgsql',
63
            'host' => $isCi ? '127.0.0.1' : 'shlink_db_postgres',
64
            'port' => $isCi ? '5433' : '5432',
65
            'user' => 'postgres',
66
            'password' => 'root',
67
            'dbname' => 'shlink_test',
68
            'charset' => 'utf8',
69
        ],
70
        'mssql' => [
71
            'driver' => 'pdo_sqlsrv',
72
            'host' => $isCi ? '127.0.0.1' : 'shlink_db_ms',
73
            'user' => 'sa',
74
            'password' => 'Passw0rd!',
75
            'dbname' => 'shlink_test',
76
        ],
77
    ];
78
    $driverConfigMap['maria'] = $driverConfigMap['mysql'];
79
80
    return $driverConfigMap[$driver] ?? [];
81
};
82
83
return [
84
85
    'debug' => true,
86
    ConfigAggregator::ENABLE_CACHE => false,
87
88
    'url_shortener' => [
89
        'domain' => [
90
            'schema' => 'http',
91
            'hostname' => 'doma.in',
92
        ],
93
        'validate_url' => true,
94
    ],
95
96
    'mezzio-swoole' => [
97
        'enable_coroutine' => false,
98
        'swoole-http-server' => [
99
            'host' => SWOOLE_TESTING_HOST,
100
            'port' => SWOOLE_TESTING_PORT,
101
            'process-name' => 'shlink_test',
102
            'options' => [
103
                'pid_file' => sys_get_temp_dir() . '/shlink-test-swoole.pid',
104
                'enable_coroutine' => false,
105
            ],
106
        ],
107
    ],
108
109
    'routes' => !$isApiTest ? [] : [
110
        [
111
            'name' => 'start_collecting_coverage',
112
            'path' => '/api-tests/start-coverage',
113
            'middleware' => middleware(static function () use (&$coverage) {
114
                if ($coverage) {
115
                    $coverage->start('API tests');
116
                }
117
                return new EmptyResponse();
118
            }),
119
            'allowed_methods' => ['GET'],
120
        ],
121
        [
122
            'name' => 'dump_coverage',
123
            'path' => '/api-tests/stop-coverage',
124
            'middleware' => middleware(static function () use (&$coverage) {
125
                if ($coverage) {
126
                    $basePath = __DIR__ . '/../../build/coverage-api';
127
                    $coverage->stop();
128
                    (new PHP())->process($coverage, $basePath . '.cov');
129
                    (new Xml(Version::getVersionString()))->process($coverage, $basePath . '/coverage-xml');
130
                }
131
132
                return new EmptyResponse();
133
            }),
134
            'allowed_methods' => ['GET'],
135
        ],
136
    ],
137
138
    'mercure' => [
139
        'public_hub_url' => null,
140
        'internal_hub_url' => null,
141
        'jwt_secret' => null,
142
    ],
143
144
    'dependencies' => [
145
        'services' => [
146
            'shlink_test_api_client' => new Client([
147
                'base_uri' => sprintf('http://%s:%s/', SWOOLE_TESTING_HOST, SWOOLE_TESTING_PORT),
148
                'http_errors' => false,
149
            ]),
150
        ],
151
        'factories' => [
152
            TestUtils\Helper\TestHelper::class => InvokableFactory::class,
153
        ],
154
    ],
155
156
    'entity_manager' => [
157
        'connection' => $buildDbConnection(),
158
    ],
159
160
    'data_fixtures' => [
161
        'paths' => [
162
            __DIR__ . '/../../module/Rest/test-api/Fixtures',
163
        ],
164
    ],
165
166
];
167