2019-01-26 02:42:01 -06:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2019-01-26 02:42:01 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-11 09:30:46 -05:00
|
|
|
namespace Shlinkio\Shlink;
|
2019-01-26 02:42:01 -06:00
|
|
|
|
2019-01-26 03:19:20 -06:00
|
|
|
use GuzzleHttp\Client;
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\ConfigAggregator\ConfigAggregator;
|
2020-09-26 03:43:50 -05:00
|
|
|
use Laminas\Diactoros\Response\EmptyResponse;
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\ServiceManager\Factory\InvokableFactory;
|
2021-03-14 01:49:38 -06:00
|
|
|
use Monolog\Handler\StreamHandler;
|
2021-02-13 04:40:19 -06:00
|
|
|
use Monolog\Logger;
|
2020-09-26 03:43:50 -05:00
|
|
|
use PHPUnit\Runner\Version;
|
2021-12-10 10:45:50 -06:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2020-09-26 03:43:50 -05:00
|
|
|
use SebastianBergmann\CodeCoverage\CodeCoverage;
|
2020-11-02 04:50:19 -06:00
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Selector;
|
|
|
|
use SebastianBergmann\CodeCoverage\Filter;
|
2021-12-10 10:45:50 -06:00
|
|
|
use SebastianBergmann\CodeCoverage\Report\Html\Facade as Html;
|
2020-09-26 03:49:56 -05:00
|
|
|
use SebastianBergmann\CodeCoverage\Report\PHP;
|
2020-09-26 03:43:50 -05:00
|
|
|
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as Xml;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2020-09-26 03:43:50 -05:00
|
|
|
use function Laminas\Stratigility\middleware;
|
2019-03-05 13:36:35 -06:00
|
|
|
use function Shlinkio\Shlink\Common\env;
|
2019-01-29 06:05:26 -06:00
|
|
|
use function sprintf;
|
2019-01-26 02:42:01 -06:00
|
|
|
use function sys_get_temp_dir;
|
|
|
|
|
2020-09-26 03:43:50 -05: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 04:50:19 -06:00
|
|
|
$filter = new Filter();
|
2021-12-10 10:45:50 -06:00
|
|
|
$filter->includeDirectory(__DIR__ . '/../../module/Core/src');
|
|
|
|
$filter->includeDirectory(__DIR__ . '/../../module/Rest/src');
|
2020-11-02 04:50:19 -06:00
|
|
|
$coverage = new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
|
2020-09-26 03:43:50 -05:00
|
|
|
}
|
2019-01-29 06:05:26 -06:00
|
|
|
|
2021-07-20 07:03:19 -05:00
|
|
|
$buildDbConnection = static function (): array {
|
2019-03-05 13:36:35 -06:00
|
|
|
$driver = env('DB_DRIVER', 'sqlite');
|
2020-12-13 04:07:37 -06:00
|
|
|
$isCi = env('CI', false);
|
2021-07-20 07:03:19 -05:00
|
|
|
$getMysqlHost = static fn (string $driver) => sprintf('shlink_db%s', $driver === 'mysql' ? '' : '_maria');
|
|
|
|
$getCiMysqlPort = static fn (string $driver) => $driver === 'mysql' ? '3307' : '3308';
|
2019-03-05 13:36:35 -06:00
|
|
|
|
2021-07-20 07:03:19 -05:00
|
|
|
return match ($driver) {
|
2019-10-06 04:21:41 -05:00
|
|
|
'sqlite' => [
|
|
|
|
'driver' => 'pdo_sqlite',
|
|
|
|
'path' => sys_get_temp_dir() . '/shlink-tests.db',
|
|
|
|
],
|
|
|
|
'postgres' => [
|
|
|
|
'driver' => 'pdo_pgsql',
|
|
|
|
'host' => $isCi ? '127.0.0.1' : 'shlink_db_postgres',
|
2020-05-04 12:55:03 -05:00
|
|
|
'port' => $isCi ? '5433' : '5432',
|
2019-10-06 04:21:41 -05:00
|
|
|
'user' => 'postgres',
|
2020-05-04 12:55:03 -05:00
|
|
|
'password' => 'root',
|
2019-10-06 04:21:41 -05:00
|
|
|
'dbname' => 'shlink_test',
|
|
|
|
'charset' => 'utf8',
|
|
|
|
],
|
2020-02-03 14:20:40 -06:00
|
|
|
'mssql' => [
|
|
|
|
'driver' => 'pdo_sqlsrv',
|
|
|
|
'host' => $isCi ? '127.0.0.1' : 'shlink_db_ms',
|
|
|
|
'user' => 'sa',
|
2020-05-04 12:55:03 -05:00
|
|
|
'password' => 'Passw0rd!',
|
2020-02-03 14:20:40 -06:00
|
|
|
'dbname' => 'shlink_test',
|
|
|
|
],
|
2021-07-20 07:03:19 -05:00
|
|
|
default => [ // mysql and maria
|
|
|
|
'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',
|
|
|
|
],
|
|
|
|
};
|
2019-03-05 13:36:35 -06:00
|
|
|
};
|
|
|
|
|
2021-03-14 03:01:11 -05:00
|
|
|
$buildTestLoggerConfig = fn (string $handlerName, string $filename) => [
|
|
|
|
'handlers' => [
|
|
|
|
$handlerName => [
|
|
|
|
'name' => StreamHandler::class,
|
|
|
|
'params' => [
|
|
|
|
'level' => Logger::DEBUG,
|
|
|
|
'stream' => sprintf('data/log/api-tests/%s', $filename),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2019-01-26 02:42:01 -06:00
|
|
|
return [
|
|
|
|
|
2019-01-26 03:59:24 -06:00
|
|
|
'debug' => true,
|
|
|
|
ConfigAggregator::ENABLE_CACHE => false,
|
|
|
|
|
2019-01-27 05:35:00 -06:00
|
|
|
'url_shortener' => [
|
|
|
|
'domain' => [
|
|
|
|
'schema' => 'http',
|
|
|
|
'hostname' => 'doma.in',
|
|
|
|
],
|
2019-12-31 09:29:32 -06:00
|
|
|
'validate_url' => true,
|
2019-01-27 05:35:00 -06:00
|
|
|
],
|
|
|
|
|
2020-01-01 14:11:53 -06:00
|
|
|
'mezzio-swoole' => [
|
2019-11-01 04:10:43 -05:00
|
|
|
'enable_coroutine' => false,
|
2019-01-26 02:42:01 -06:00
|
|
|
'swoole-http-server' => [
|
2020-09-26 03:43:50 -05:00
|
|
|
'host' => SWOOLE_TESTING_HOST,
|
|
|
|
'port' => SWOOLE_TESTING_PORT,
|
2019-01-26 03:19:20 -06:00
|
|
|
'process-name' => 'shlink_test',
|
2019-01-27 03:15:48 -06:00
|
|
|
'options' => [
|
|
|
|
'pid_file' => sys_get_temp_dir() . '/shlink-test-swoole.pid',
|
2019-11-01 04:10:43 -05:00
|
|
|
'enable_coroutine' => false,
|
2019-01-27 03:15:48 -06:00
|
|
|
],
|
2019-01-26 02:42:01 -06:00
|
|
|
],
|
|
|
|
],
|
|
|
|
|
2020-09-26 03:43:50 -05:00
|
|
|
'routes' => !$isApiTest ? [] : [
|
|
|
|
[
|
|
|
|
'name' => 'dump_coverage',
|
|
|
|
'path' => '/api-tests/stop-coverage',
|
|
|
|
'middleware' => middleware(static function () use (&$coverage) {
|
2021-07-19 15:50:32 -05:00
|
|
|
if ($coverage) { // @phpstan-ignore-line
|
2020-09-26 03:43:50 -05:00
|
|
|
$basePath = __DIR__ . '/../../build/coverage-api';
|
2021-12-10 10:45:50 -06:00
|
|
|
|
|
|
|
// TODO Generate these coverages dynamically based on CLI options
|
2020-09-26 03:49:56 -05:00
|
|
|
(new PHP())->process($coverage, $basePath . '.cov');
|
2020-09-26 03:43:50 -05:00
|
|
|
(new Xml(Version::getVersionString()))->process($coverage, $basePath . '/coverage-xml');
|
2021-12-10 10:45:50 -06:00
|
|
|
(new Html())->process($coverage, $basePath . '/coverage-html');
|
2020-09-26 03:43:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return new EmptyResponse();
|
|
|
|
}),
|
|
|
|
'allowed_methods' => ['GET'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
|
2021-12-10 10:45:50 -06:00
|
|
|
'middleware_pipeline' => !$isApiTest ? [] : [
|
|
|
|
'capture_code_coverage' => [
|
|
|
|
'middleware' => middleware(static function (
|
|
|
|
ServerRequestInterface $req,
|
|
|
|
RequestHandlerInterface $handler,
|
|
|
|
) use (&$coverage): ResponseInterface {
|
|
|
|
$coverage?->start($req->getHeaderLine('x-coverage-id'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $handler->handle($req);
|
|
|
|
} finally {
|
|
|
|
$coverage?->stop();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
'priority' => 9999,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
|
2020-04-12 11:39:28 -05:00
|
|
|
'mercure' => [
|
|
|
|
'public_hub_url' => null,
|
|
|
|
'internal_hub_url' => null,
|
|
|
|
'jwt_secret' => null,
|
|
|
|
],
|
|
|
|
|
2019-01-26 02:42:01 -06:00
|
|
|
'dependencies' => [
|
2019-01-29 06:05:26 -06:00
|
|
|
'services' => [
|
|
|
|
'shlink_test_api_client' => new Client([
|
2020-09-26 03:43:50 -05:00
|
|
|
'base_uri' => sprintf('http://%s:%s/', SWOOLE_TESTING_HOST, SWOOLE_TESTING_PORT),
|
2019-01-30 11:28:07 -06:00
|
|
|
'http_errors' => false,
|
2019-01-29 06:05:26 -06:00
|
|
|
]),
|
|
|
|
],
|
2019-01-26 02:42:01 -06:00
|
|
|
'factories' => [
|
2019-08-11 09:30:46 -05:00
|
|
|
TestUtils\Helper\TestHelper::class => InvokableFactory::class,
|
2019-01-26 02:42:01 -06:00
|
|
|
],
|
|
|
|
],
|
|
|
|
|
|
|
|
'entity_manager' => [
|
2019-03-05 13:36:35 -06:00
|
|
|
'connection' => $buildDbConnection(),
|
2019-01-26 02:42:01 -06:00
|
|
|
],
|
|
|
|
|
2019-01-27 05:14:18 -06:00
|
|
|
'data_fixtures' => [
|
|
|
|
'paths' => [
|
|
|
|
__DIR__ . '/../../module/Rest/test-api/Fixtures',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
|
2021-02-13 04:40:19 -06:00
|
|
|
'logger' => [
|
2021-03-14 03:01:11 -05:00
|
|
|
'Shlink' => $buildTestLoggerConfig('shlink_handler', 'shlink.log'),
|
|
|
|
'Access' => $buildTestLoggerConfig('access_handler', 'access.log'),
|
2021-02-13 04:40:19 -06:00
|
|
|
],
|
|
|
|
|
2019-01-26 02:42:01 -06:00
|
|
|
];
|