mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-02 05:03:12 +08:00
Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35950a6294 | ||
|
|
c104eee2b1 | ||
|
|
f0972c6220 | ||
|
|
42a5145895 | ||
|
|
8d412e7d4c | ||
|
|
f45e34cfcf | ||
|
|
320c8e2d6b | ||
|
|
988de0b96e | ||
|
|
25a785dfa7 | ||
|
|
c993bbd993 | ||
|
|
479760c0ee | ||
|
|
e186237410 | ||
|
|
4084e3f0d8 | ||
|
|
dddf64031f | ||
|
|
8f1477e893 | ||
|
|
4866fe241e | ||
|
|
6613cb5c60 | ||
|
|
0f48dd567f | ||
|
|
b24511b7b5 | ||
|
|
df40199134 | ||
|
|
935562acc9 | ||
|
|
feb67e76f0 | ||
|
|
fdbe93f0fb | ||
|
|
f27058e255 | ||
|
|
6ddbbb4ba0 | ||
|
|
ef32f2c129 | ||
|
|
760bb2db2a | ||
|
|
68f38fd9fe | ||
|
|
5c6829fb62 | ||
|
|
91c48919c6 | ||
|
|
72313800fa | ||
|
|
478d5a16fd | ||
|
|
b8909d8043 | ||
|
|
c2c659b0fe | ||
|
|
20c3bde036 | ||
|
|
e77e37076f | ||
|
|
734fdf83c1 | ||
|
|
2906d42f97 | ||
|
|
0135f205df | ||
|
|
781c6e94a0 | ||
|
|
1d64dc8a26 | ||
|
|
34ff831473 | ||
|
|
3734160cb4 | ||
|
|
21234cacfb | ||
|
|
eb4dc85006 | ||
|
|
249b8a4768 | ||
|
|
1a1868c7f4 | ||
|
|
487659d5b4 | ||
|
|
f46de4d3e1 | ||
|
|
6314315db7 | ||
|
|
a22beeed08 | ||
|
|
840e377245 | ||
|
|
6fa255386b |
6
.github/ISSUE_TEMPLATE.md
vendored
Normal file
6
.github/ISSUE_TEMPLATE.md
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
<!--
|
||||
Before opening an issue, just take into account that this is a completely free of charge open source project.
|
||||
I'm always happy to help and provide support, but some understanding will be required.
|
||||
I do this in my own free time, so expect some delays when implementing new features and fixing bugs, and don't take it personal if an issue gets eventually closed.
|
||||
Try to be polite, and understand it is impossible for a project to cover all use cases.
|
||||
-->
|
||||
16
.travis.yml
16
.travis.yml
@@ -5,12 +5,14 @@ branches:
|
||||
- /.*/
|
||||
|
||||
php:
|
||||
- 7.1
|
||||
- 7.2
|
||||
- 7.3
|
||||
|
||||
services:
|
||||
- mysql
|
||||
- postgresql
|
||||
|
||||
before_install:
|
||||
- echo 'extension = memcached.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
|
||||
- echo 'extension = apcu.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
|
||||
- yes | pecl install swoole
|
||||
- phpenv config-rm xdebug.ini || return 0
|
||||
@@ -19,8 +21,12 @@ install:
|
||||
- composer self-update
|
||||
- composer install --no-interaction
|
||||
|
||||
script:
|
||||
before_script:
|
||||
- mysql -e 'CREATE DATABASE shlink_test;'
|
||||
- psql -c 'create database shlink_test;' -U postgres
|
||||
- mkdir build
|
||||
|
||||
script:
|
||||
- composer ci
|
||||
|
||||
after_success:
|
||||
@@ -42,10 +48,10 @@ deploy:
|
||||
skip_cleanup: true
|
||||
on:
|
||||
tags: true
|
||||
php: 7.1
|
||||
php: 7.2
|
||||
- provider: script
|
||||
script: bash data/travis/trigger_docker_build.sh
|
||||
skip_cleanup: true
|
||||
on:
|
||||
tags: true
|
||||
php: 7.1
|
||||
php: 7.2
|
||||
|
||||
55
CHANGELOG.md
55
CHANGELOG.md
@@ -4,6 +4,61 @@ All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
|
||||
|
||||
## 1.17.0 - 2019-05-13
|
||||
|
||||
#### Added
|
||||
|
||||
* [#377](https://github.com/shlinkio/shlink/issues/377) Updated `visit:locate` command (formerly `visit:process`) to automatically update the GeoLite2 database if it is too old or it does not exist.
|
||||
|
||||
This simplifies processing visits in a container-based infrastructure, since a fresh container is capable of getting an updated version of the file by itself.
|
||||
|
||||
It also removes the need of asynchronously and programmatically updating the file, which deprecates the `visit:update-db` command.
|
||||
|
||||
* [#373](https://github.com/shlinkio/shlink/issues/373) Added support for a simplified config. Specially useful to use with the docker container.
|
||||
|
||||
#### Changed
|
||||
|
||||
* [#56](https://github.com/shlinkio/shlink/issues/56) Simplified supported cache, requiring APCu always.
|
||||
|
||||
### Deprecated
|
||||
|
||||
* [#406](https://github.com/shlinkio/shlink/issues/406) Deprecated `PUT /short-urls/{shortCode}` REST endpoint in favor of `PATCH /short-urls/{shortCode}`.
|
||||
|
||||
#### Removed
|
||||
|
||||
* [#385](https://github.com/shlinkio/shlink/issues/385) Dropped support for PHP 7.1
|
||||
* [#379](https://github.com/shlinkio/shlink/issues/379) Removed copyright from error templates.
|
||||
|
||||
#### Fixed
|
||||
|
||||
* *Nothing*
|
||||
|
||||
|
||||
## 1.16.3 - 2019-03-30
|
||||
|
||||
#### Added
|
||||
|
||||
* *Nothing*
|
||||
|
||||
#### Changed
|
||||
|
||||
* [#153](https://github.com/shlinkio/shlink/issues/153) Updated to [doctrine/migrations](https://github.com/doctrine/migrations) version 2.0.0
|
||||
* [#376](https://github.com/shlinkio/shlink/issues/376) Allowed `visit:update-db` command to not return an error exit code even if download fails, by passing the `-i` flag.
|
||||
* [#341](https://github.com/shlinkio/shlink/issues/341) Improved database tests so that they are executed against all supported database engines.
|
||||
|
||||
#### Deprecated
|
||||
|
||||
* *Nothing*
|
||||
|
||||
#### Removed
|
||||
|
||||
* *Nothing*
|
||||
|
||||
#### Fixed
|
||||
|
||||
* [#382](https://github.com/shlinkio/shlink/issues/382) Fixed existing short URLs not properly checked when providing the `findIfExists` flag.
|
||||
|
||||
|
||||
## 1.16.2 - 2019-03-05
|
||||
|
||||
#### Added
|
||||
|
||||
24
README.md
24
README.md
@@ -21,7 +21,7 @@ A PHP-based self-hosted URL shortener that can be used to serve shortened URLs u
|
||||
|
||||
First make sure the host where you are going to run shlink fulfills these requirements:
|
||||
|
||||
* PHP 7.1 or greater with JSON, APCu, intl, curl, PDO and gd extensions enabled.
|
||||
* PHP 7.2 or greater with JSON, APCu, intl, curl, PDO and gd extensions enabled.
|
||||
* MySQL, PostgreSQL or SQLite.
|
||||
* The web server of your choice with PHP integration (Apache or Nginx recommended).
|
||||
|
||||
@@ -74,7 +74,7 @@ Despite how you built the project, you are going to need to install it now, by f
|
||||
|
||||
location ~ \.php$ {
|
||||
fastcgi_split_path_info ^(.+\.php)(/.+)$;
|
||||
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
|
||||
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
|
||||
fastcgi_index index.php;
|
||||
include fastcgi.conf;
|
||||
}
|
||||
@@ -188,21 +188,21 @@ There are a couple of time-consuming tasks that shlink expects you to do manuall
|
||||
|
||||
Those tasks can be performed using shlink's CLI, so it should be easy to schedule them to be run in the background (for example, using cron jobs):
|
||||
|
||||
* Resolve IP address locations: `/path/to/shlink/bin/cli visit:process`
|
||||
* Resolve IP address locations: `/path/to/shlink/bin/cli visit:locate`
|
||||
|
||||
If you don't run this command regularly, the stats will say all visits come from *unknown* locations.
|
||||
|
||||
* Update IP geolocation database: `/path/to/shlink/bin/cli visit:update-db`
|
||||
|
||||
When shlink is installed it downloads a fresh [GeoLite2](https://dev.maxmind.com/geoip/geoip2/geolite2/) db file. Running this command will update this file.
|
||||
|
||||
The file is updated the first Tuesday of every month, so it should be enough running this command the first Wednesday.
|
||||
|
||||
* Generate website previews: `/path/to/shlink/bin/cli short-url:process-previews`
|
||||
|
||||
Running this will improve the performance of the `doma.in/abc123/preview` URLs, which return a preview of the site.
|
||||
|
||||
*Any of those commands accept the `-q` flag, which makes it not display any output. This is recommended when configuring the commands as cron jobs.*
|
||||
* **For shlink older than v1.17.0**: Update IP geolocation database: `/path/to/shlink/bin/cli visit:update-db`
|
||||
|
||||
When shlink is installed it downloads a fresh [GeoLite2](https://dev.maxmind.com/geoip/geoip2/geolite2/) db file. Running this command will update this file.
|
||||
|
||||
The file is updated the first Tuesday of every month, so it should be enough running this command the first Wednesday.
|
||||
|
||||
*Any of these commands accept the `-q` flag, which makes it not display any output. This is recommended when configuring the commands as cron jobs.*
|
||||
|
||||
In future versions, it is planed that, when using **swoole** to serve shlink, some of these tasks are automatically run without blocking the request and also, without having to configure cron jobs. Probably resolving IP locations and generating previews.
|
||||
|
||||
@@ -281,8 +281,8 @@ Available commands:
|
||||
tag:list Lists existing tags.
|
||||
tag:rename Renames one existing tag.
|
||||
visit
|
||||
visit:process Processes visits where location is not set yet
|
||||
visit:update-db Updates the GeoLite2 database file used to geolocate IP addresses
|
||||
visit:locate [visit:process] Resolves visits origin locations.
|
||||
visit:update-db [DEPRECATED] Updates the GeoLite2 database file used to geolocate IP addresses
|
||||
```
|
||||
|
||||
> This product includes GeoLite2 data created by MaxMind, available from [https://www.maxmind.com](https://www.maxmind.com)
|
||||
|
||||
3
build.sh
3
build.sh
@@ -35,7 +35,8 @@ rsync -av * "${builtcontent}" \
|
||||
--exclude=config/autoload/*local* \
|
||||
--exclude=config/test \
|
||||
--exclude=**/test* \
|
||||
--exclude=build*
|
||||
--exclude=build* \
|
||||
--exclude=.github
|
||||
cd "${builtcontent}"
|
||||
|
||||
# Install dependencies
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.1",
|
||||
"php": "^7.2",
|
||||
"ext-json": "*",
|
||||
"ext-pdo": "*",
|
||||
"acelaya/ze-content-based-error-handler": "^2.2",
|
||||
@@ -20,7 +20,7 @@
|
||||
"cakephp/chronos": "^1.2",
|
||||
"cocur/slugify": "^3.0",
|
||||
"doctrine/cache": "^1.6",
|
||||
"doctrine/migrations": "^1.4",
|
||||
"doctrine/migrations": "^2.0",
|
||||
"doctrine/orm": "^2.5",
|
||||
"endroid/qr-code": "^1.7",
|
||||
"firebase/php-jwt": "^4.0",
|
||||
@@ -42,7 +42,7 @@
|
||||
"zendframework/zend-expressive-fastroute": "^3.0",
|
||||
"zendframework/zend-expressive-helpers": "^5.0",
|
||||
"zendframework/zend-expressive-platesrenderer": "^2.0",
|
||||
"zendframework/zend-expressive-swoole": "^2.2",
|
||||
"zendframework/zend-expressive-swoole": "^2.4",
|
||||
"zendframework/zend-i18n": "^2.7",
|
||||
"zendframework/zend-inputfilter": "^2.8",
|
||||
"zendframework/zend-paginator": "^2.6",
|
||||
@@ -55,8 +55,8 @@
|
||||
"filp/whoops": "^2.0",
|
||||
"infection/infection": "^0.12.2",
|
||||
"phpstan/phpstan": "^0.11.2",
|
||||
"phpunit/phpcov": "^6.0@dev || ^5.0",
|
||||
"phpunit/phpunit": "^8.0 || ^7.5",
|
||||
"phpunit/phpcov": "^6.0",
|
||||
"phpunit/phpunit": "^8.0",
|
||||
"roave/security-advisories": "dev-master",
|
||||
"shlinkio/php-coding-standard": "~1.1.0",
|
||||
"symfony/dotenv": "^4.2",
|
||||
@@ -110,11 +110,15 @@
|
||||
"test:ci": [
|
||||
"@test:unit:ci",
|
||||
"@test:db",
|
||||
"@test:db:mysql",
|
||||
"@test:db:postgres",
|
||||
"@test:api"
|
||||
],
|
||||
"test:unit": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov --testdox",
|
||||
"test:unit:ci": "phpdbg -qrr vendor/bin/phpunit --order-by=random --coverage-php build/coverage-unit.cov --coverage-clover=build/clover.xml --coverage-xml=build/coverage-xml --log-junit=build/phpunit.junit.xml --testdox",
|
||||
"test:db": "APP_ENV=test phpdbg -qrr vendor/bin/phpunit --order-by=random -c phpunit-db.xml --coverage-php build/coverage-db.cov --testdox",
|
||||
"test:db:mysql": "DB_DRIVER=mysql composer test:db",
|
||||
"test:db:postgres": "DB_DRIVER=postgres composer test:db",
|
||||
"test:api": "bin/test/run-api-tests.sh",
|
||||
|
||||
"test:pretty": [
|
||||
@@ -141,7 +145,9 @@
|
||||
"test:ci": "<fg=blue;options=bold>Runs all test suites, generating all needed reports and logs for CI envs</>",
|
||||
"test:unit": "<fg=blue;options=bold>Runs unit test suites</>",
|
||||
"test:unit:ci": "<fg=blue;options=bold>Runs unit test suites, generating all needed reports and logs for CI envs</>",
|
||||
"test:db": "<fg=blue;options=bold>Runs database test suites (covering entity repositories)</>",
|
||||
"test:db": "<fg=blue;options=bold>Runs database test suites on a SQLite database</>",
|
||||
"test:db:mysql": "<fg=blue;options=bold>Runs database test suites on a MySQL database</>",
|
||||
"test:db:postgres": "<fg=blue;options=bold>Runs database test suites on a PostgreSQL database</>",
|
||||
"test:api": "<fg=blue;options=bold>Runs API test suites</>",
|
||||
"test:pretty": "<fg=blue;options=bold>Runs all test suites and generates an HTML code coverage report</>",
|
||||
"test:unit:pretty": "<fg=blue;options=bold>Runs unit test suites and generates an HTML code coverage report</>",
|
||||
|
||||
@@ -28,7 +28,7 @@ return [
|
||||
'max_files' => 30,
|
||||
'formatter' => 'dashed',
|
||||
],
|
||||
'swoole_access_handler' => [
|
||||
'access_handler' => [
|
||||
'class' => StreamHandler::class,
|
||||
'level' => Logger::INFO,
|
||||
'stream' => 'php://stdout',
|
||||
@@ -49,9 +49,9 @@ return [
|
||||
'handlers' => ['shlink_rotating_handler'],
|
||||
'processors' => ['exception_with_new_line', 'psr3'],
|
||||
],
|
||||
'Swoole' => [
|
||||
'handlers' => ['swoole_access_handler'],
|
||||
'processors' => ['psr3'],
|
||||
'Access' => [
|
||||
'handlers' => ['access_handler'],
|
||||
'processors' => ['exception_with_new_line', 'psr3'],
|
||||
],
|
||||
],
|
||||
],
|
||||
@@ -59,14 +59,14 @@ return [
|
||||
'dependencies' => [
|
||||
'factories' => [
|
||||
'Logger_Shlink' => Common\Factory\LoggerFactory::class,
|
||||
'Logger_Swoole' => Common\Factory\LoggerFactory::class,
|
||||
'Logger_Access' => Common\Factory\LoggerFactory::class,
|
||||
],
|
||||
],
|
||||
|
||||
'zend-expressive-swoole' => [
|
||||
'swoole-http-server' => [
|
||||
'logger' => [
|
||||
'logger-name' => 'Logger_Swoole',
|
||||
'logger-name' => 'Logger_Access',
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
21
config/autoload/swoole.local.php.dist
Normal file
21
config/autoload/swoole.local.php.dist
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Zend\Expressive\Swoole\HotCodeReload\FileWatcher\InotifyFileWatcher;
|
||||
use Zend\ServiceManager\Factory\InvokableFactory;
|
||||
|
||||
return [
|
||||
|
||||
'zend-expressive-swoole' => [
|
||||
'hot-code-reload' => [
|
||||
'enable' => true,
|
||||
],
|
||||
],
|
||||
|
||||
'dependencies' => [
|
||||
'factories' => [
|
||||
InotifyFileWatcher::class => InvokableFactory::class,
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
@@ -25,4 +25,6 @@ return (new ConfigAggregator\ConfigAggregator([
|
||||
env('APP_ENV') === 'test'
|
||||
? new ConfigAggregator\PhpFileProvider('config/test/*.global.php')
|
||||
: new ConfigAggregator\ArrayProvider([]),
|
||||
], 'data/cache/app_config.php'))->getMergedConfig();
|
||||
], 'data/cache/app_config.php', [
|
||||
Core\ConfigPostProcessor::class,
|
||||
]))->getMergedConfig();
|
||||
|
||||
@@ -20,7 +20,7 @@ $testHelper = $container->get(TestHelper::class);
|
||||
$config = $container->get('config');
|
||||
$em = $container->get(EntityManager::class);
|
||||
|
||||
$testHelper->createTestDb($config['entity_manager']['connection']['path']);
|
||||
$testHelper->createTestDb();
|
||||
ApiTest\ApiTestCase::setApiClient($container->get('shlink_test_api_client'));
|
||||
ApiTest\ApiTestCase::setSeedFixturesCallback(function () use ($testHelper, $em, $config) {
|
||||
$testHelper->seedFixtures($em, $config['data_fixtures'] ?? []);
|
||||
|
||||
@@ -15,7 +15,5 @@ if (! file_exists('.env')) {
|
||||
|
||||
/** @var ContainerInterface $container */
|
||||
$container = require __DIR__ . '/../container.php';
|
||||
$config = $container->get('config');
|
||||
|
||||
$container->get(TestHelper::class)->createTestDb($config['entity_manager']['connection']['path']);
|
||||
$container->get(TestHelper::class)->createTestDb();
|
||||
DbTest\DatabaseTestCase::setEntityManager($container->get('em'));
|
||||
|
||||
@@ -4,15 +4,53 @@ declare(strict_types=1);
|
||||
namespace ShlinkioTest\Shlink;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use PDO;
|
||||
use Zend\ConfigAggregator\ConfigAggregator;
|
||||
use Zend\ServiceManager\Factory\InvokableFactory;
|
||||
|
||||
use function Shlinkio\Shlink\Common\env;
|
||||
use function sprintf;
|
||||
use function sys_get_temp_dir;
|
||||
|
||||
$swooleTestingHost = '127.0.0.1';
|
||||
$swooleTestingPort = 9999;
|
||||
|
||||
$buildDbConnection = function () {
|
||||
$driver = env('DB_DRIVER', 'sqlite');
|
||||
$isCi = env('TRAVIS', false);
|
||||
|
||||
switch ($driver) {
|
||||
case 'sqlite':
|
||||
return [
|
||||
'driver' => 'pdo_sqlite',
|
||||
'path' => sys_get_temp_dir() . '/shlink-tests.db',
|
||||
];
|
||||
case 'mysql':
|
||||
return [
|
||||
'driver' => 'pdo_mysql',
|
||||
'host' => $isCi ? '127.0.0.1' : 'shlink_db',
|
||||
'user' => 'root',
|
||||
'password' => $isCi ? '' : 'root',
|
||||
'dbname' => 'shlink_test',
|
||||
'charset' => 'utf8',
|
||||
'driverOptions' => [
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
|
||||
],
|
||||
];
|
||||
case 'postgres':
|
||||
return [
|
||||
'driver' => 'pdo_pgsql',
|
||||
'host' => $isCi ? '127.0.0.1' : 'shlink_db_postgres',
|
||||
'user' => 'postgres',
|
||||
'password' => $isCi ? '' : 'root',
|
||||
'dbname' => 'shlink_test',
|
||||
'charset' => 'utf8',
|
||||
];
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
return [
|
||||
|
||||
'debug' => true,
|
||||
@@ -49,11 +87,7 @@ return [
|
||||
],
|
||||
|
||||
'entity_manager' => [
|
||||
'connection' => [
|
||||
'driver' => 'pdo_sqlite',
|
||||
'path' => sys_get_temp_dir() . '/shlink-tests.db',
|
||||
// 'path' => __DIR__ . '/../../data/shlink-tests.db',
|
||||
],
|
||||
'connection' => $buildDbConnection(),
|
||||
],
|
||||
|
||||
'data_fixtures' => [
|
||||
|
||||
@@ -11,7 +11,7 @@ server {
|
||||
|
||||
location ~ \.php$ {
|
||||
fastcgi_split_path_info ^(.+\.php)(/.+)$;
|
||||
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
|
||||
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
|
||||
fastcgi_index index.php;
|
||||
include fastcgi.conf;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
FROM php:7.3.1-fpm-alpine3.8
|
||||
MAINTAINER Alejandro Celaya <alejandro@alejandrocelaya.com>
|
||||
|
||||
ENV PREDIS_VERSION 4.2.0
|
||||
ENV MEMCACHED_VERSION 3.1.3
|
||||
ENV APCU_VERSION 5.1.16
|
||||
ENV APCU_BC_VERSION 1.0.4
|
||||
ENV XDEBUG_VERSION "2.7.0RC1"
|
||||
@@ -31,28 +29,6 @@ RUN docker-php-ext-install gd
|
||||
RUN apk add --no-cache postgresql-dev
|
||||
RUN docker-php-ext-install pdo_pgsql
|
||||
|
||||
# Install redis extension
|
||||
ADD https://github.com/phpredis/phpredis/archive/$PREDIS_VERSION.tar.gz /tmp/phpredis.tar.gz
|
||||
RUN mkdir -p /usr/src/php/ext/redis\
|
||||
&& tar xf /tmp/phpredis.tar.gz -C /usr/src/php/ext/redis --strip-components=1
|
||||
# configure and install
|
||||
RUN docker-php-ext-configure redis\
|
||||
&& docker-php-ext-install redis
|
||||
# cleanup
|
||||
RUN rm /tmp/phpredis.tar.gz
|
||||
|
||||
# Install memcached extension
|
||||
RUN apk add --no-cache --virtual cyrus-sasl-dev
|
||||
RUN apk add --no-cache --virtual libmemcached-dev
|
||||
ADD https://github.com/php-memcached-dev/php-memcached/archive/v$MEMCACHED_VERSION.tar.gz /tmp/memcached.tar.gz
|
||||
RUN mkdir -p /usr/src/php/ext/memcached\
|
||||
&& tar xf /tmp/memcached.tar.gz -C /usr/src/php/ext/memcached --strip-components=1
|
||||
# configure and install
|
||||
RUN docker-php-ext-configure memcached\
|
||||
&& docker-php-ext-install memcached
|
||||
# cleanup
|
||||
RUN rm /tmp/memcached.tar.gz
|
||||
|
||||
# Install APCu extension
|
||||
ADD https://pecl.php.net/get/apcu-$APCU_VERSION.tgz /tmp/apcu.tar.gz
|
||||
RUN mkdir -p /usr/src/php/ext/apcu\
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
FROM php:7.3.1-cli-alpine3.8
|
||||
MAINTAINER Alejandro Celaya <alejandro@alejandrocelaya.com>
|
||||
|
||||
ENV PREDIS_VERSION 4.2.0
|
||||
ENV MEMCACHED_VERSION 3.1.3
|
||||
ENV APCU_VERSION 5.1.16
|
||||
ENV APCU_BC_VERSION 1.0.4
|
||||
ENV INOTIFY_VERSION 2.0.0
|
||||
|
||||
RUN apk update
|
||||
|
||||
@@ -30,28 +29,6 @@ RUN docker-php-ext-install gd
|
||||
RUN apk add --no-cache postgresql-dev
|
||||
RUN docker-php-ext-install pdo_pgsql
|
||||
|
||||
# Install redis extension
|
||||
ADD https://github.com/phpredis/phpredis/archive/$PREDIS_VERSION.tar.gz /tmp/phpredis.tar.gz
|
||||
RUN mkdir -p /usr/src/php/ext/redis\
|
||||
&& tar xf /tmp/phpredis.tar.gz -C /usr/src/php/ext/redis --strip-components=1
|
||||
# configure and install
|
||||
RUN docker-php-ext-configure redis\
|
||||
&& docker-php-ext-install redis
|
||||
# cleanup
|
||||
RUN rm /tmp/phpredis.tar.gz
|
||||
|
||||
# Install memcached extension
|
||||
RUN apk add --no-cache --virtual cyrus-sasl-dev
|
||||
RUN apk add --no-cache --virtual libmemcached-dev
|
||||
ADD https://github.com/php-memcached-dev/php-memcached/archive/v$MEMCACHED_VERSION.tar.gz /tmp/memcached.tar.gz
|
||||
RUN mkdir -p /usr/src/php/ext/memcached\
|
||||
&& tar xf /tmp/memcached.tar.gz -C /usr/src/php/ext/memcached --strip-components=1
|
||||
# configure and install
|
||||
RUN docker-php-ext-configure memcached\
|
||||
&& docker-php-ext-install memcached
|
||||
# cleanup
|
||||
RUN rm /tmp/memcached.tar.gz
|
||||
|
||||
# Install APCu extension
|
||||
ADD https://pecl.php.net/get/apcu-$APCU_VERSION.tgz /tmp/apcu.tar.gz
|
||||
RUN mkdir -p /usr/src/php/ext/apcu\
|
||||
@@ -76,6 +53,16 @@ RUN rm /tmp/apcu_bc.tar.gz
|
||||
RUN rm /usr/local/etc/php/conf.d/docker-php-ext-apcu.ini
|
||||
RUN echo extension=apcu.so > /usr/local/etc/php/conf.d/20-php-ext-apcu.ini
|
||||
|
||||
# Install inotify extension
|
||||
ADD https://pecl.php.net/get/inotify-$INOTIFY_VERSION.tgz /tmp/inotify.tar.gz
|
||||
RUN mkdir -p /usr/src/php/ext/inotify\
|
||||
&& tar xf /tmp/inotify.tar.gz -C /usr/src/php/ext/inotify --strip-components=1
|
||||
# configure and install
|
||||
RUN docker-php-ext-configure inotify\
|
||||
&& docker-php-ext-install inotify
|
||||
# cleanup
|
||||
RUN rm /tmp/inotify.tar.gz
|
||||
|
||||
# Install swoole
|
||||
# First line fixes an error when installing pecl extensions. Found in https://github.com/docker-library/php/issues/233
|
||||
RUN apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS && \
|
||||
|
||||
@@ -3,21 +3,24 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkMigrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\SchemaException;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
class Version20160819142757 extends AbstractMigration
|
||||
{
|
||||
const MYSQL = 'mysql';
|
||||
const SQLITE = 'sqlite';
|
||||
private const MYSQL = 'mysql';
|
||||
private const SQLITE = 'sqlite';
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
* @throws DBALException
|
||||
* @throws SchemaException
|
||||
*/
|
||||
public function up(Schema $schema)
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$db = $this->connection->getDatabasePlatform()->getName();
|
||||
$table = $schema->getTable('short_urls');
|
||||
@@ -31,9 +34,9 @@ class Version20160819142757 extends AbstractMigration
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
* @throws DBALException
|
||||
*/
|
||||
public function down(Schema $schema)
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$db = $this->connection->getDatabasePlatform()->getName();
|
||||
}
|
||||
|
||||
@@ -3,19 +3,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkMigrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
class Version20160820191203 extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function up(Schema $schema)
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// Check if the tables already exist
|
||||
$tables = $schema->getTables();
|
||||
@@ -29,7 +26,7 @@ class Version20160820191203 extends AbstractMigration
|
||||
$this->createShortUrlsInTagsTable($schema);
|
||||
}
|
||||
|
||||
protected function createTagsTable(Schema $schema)
|
||||
private function createTagsTable(Schema $schema): void
|
||||
{
|
||||
$table = $schema->createTable('tags');
|
||||
$table->addColumn('id', Type::BIGINT, [
|
||||
@@ -46,7 +43,7 @@ class Version20160820191203 extends AbstractMigration
|
||||
$table->setPrimaryKey(['id']);
|
||||
}
|
||||
|
||||
protected function createShortUrlsInTagsTable(Schema $schema)
|
||||
private function createShortUrlsInTagsTable(Schema $schema): void
|
||||
{
|
||||
$table = $schema->createTable('short_urls_in_tags');
|
||||
$table->addColumn('short_url_id', Type::BIGINT, [
|
||||
@@ -70,10 +67,7 @@ class Version20160820191203 extends AbstractMigration
|
||||
$table->setPrimaryKey(['short_url_id', 'tag_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema)
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$schema->dropTable('short_urls_in_tags');
|
||||
$schema->dropTable('tags');
|
||||
|
||||
@@ -3,10 +3,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkMigrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\SchemaException;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
@@ -14,10 +14,9 @@ use Doctrine\DBAL\Types\Type;
|
||||
class Version20171021093246 extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* @param Schema $schema
|
||||
* @throws SchemaException
|
||||
*/
|
||||
public function up(Schema $schema)
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$shortUrls = $schema->getTable('short_urls');
|
||||
if ($shortUrls->hasColumn('valid_since')) {
|
||||
@@ -33,10 +32,9 @@ class Version20171021093246 extends AbstractMigration
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
* @throws SchemaException
|
||||
*/
|
||||
public function down(Schema $schema)
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$shortUrls = $schema->getTable('short_urls');
|
||||
if (! $shortUrls->hasColumn('valid_since')) {
|
||||
|
||||
@@ -3,10 +3,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace ShlinkMigrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\SchemaException;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
@@ -14,10 +14,9 @@ use Doctrine\DBAL\Types\Type;
|
||||
class Version20171022064541 extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* @param Schema $schema
|
||||
* @throws SchemaException
|
||||
*/
|
||||
public function up(Schema $schema)
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$shortUrls = $schema->getTable('short_urls');
|
||||
if ($shortUrls->hasColumn('max_visits')) {
|
||||
@@ -31,10 +30,9 @@ class Version20171022064541 extends AbstractMigration
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
* @throws SchemaException
|
||||
*/
|
||||
public function down(Schema $schema)
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$shortUrls = $schema->getTable('short_urls');
|
||||
if (! $shortUrls->hasColumn('max_visits')) {
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
}
|
||||
},
|
||||
|
||||
"put": {
|
||||
"patch": {
|
||||
"operationId": "editShortUrl",
|
||||
"tags": [
|
||||
"Short URLs"
|
||||
@@ -169,6 +169,95 @@
|
||||
}
|
||||
},
|
||||
|
||||
"put": {
|
||||
"deprecated": true,
|
||||
"operationId": "editShortUrlPut",
|
||||
"tags": [
|
||||
"Short URLs"
|
||||
],
|
||||
"summary": "[DEPRECATED] Edit short URL",
|
||||
"description": "**[DEPRECATED]** Use [editShortUrl](#/Short_URLs/getShortUrl) instead",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "shortCode",
|
||||
"in": "path",
|
||||
"description": "The short code to edit.",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"description": "Request body.",
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"validSince": {
|
||||
"description": "The date (in ISO-8601 format) from which this short code will be valid",
|
||||
"type": "string"
|
||||
},
|
||||
"validUntil": {
|
||||
"description": "The date (in ISO-8601 format) until which this short code will be valid",
|
||||
"type": "string"
|
||||
},
|
||||
"maxVisits": {
|
||||
"description": "The maximum number of allowed visits for this short code",
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"ApiKey": []
|
||||
},
|
||||
{
|
||||
"Bearer": []
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"204": {
|
||||
"description": "The short code has been properly updated."
|
||||
},
|
||||
"400": {
|
||||
"description": "Provided meta arguments are invalid.",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "../definitions/Error.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "No short URL was found for provided short code.",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "../definitions/Error.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"500": {
|
||||
"description": "Unexpected error.",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "../definitions/Error.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"delete": {
|
||||
"operationId": "deleteShortUrl",
|
||||
"tags": [
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
"name": "X-Api-Key"
|
||||
},
|
||||
"Bearer": {
|
||||
"description": "**[Deprecated]** The JWT identifying a previously authenticated API key",
|
||||
"description": "**[DEPRECATED]** The JWT identifying a previously authenticated API key",
|
||||
"type": "http",
|
||||
"scheme": "bearer",
|
||||
"bearerFormat": "JWT"
|
||||
@@ -66,7 +66,7 @@
|
||||
},
|
||||
{
|
||||
"name": "Authentication",
|
||||
"description": "**[Deprecated]** Authentication-related endpoints"
|
||||
"description": "**[DEPRECATED]** Authentication-related endpoints"
|
||||
}
|
||||
],
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ return [
|
||||
Command\ShortUrl\GeneratePreviewCommand::NAME => Command\ShortUrl\GeneratePreviewCommand::class,
|
||||
Command\ShortUrl\DeleteShortUrlCommand::NAME => Command\ShortUrl\DeleteShortUrlCommand::class,
|
||||
|
||||
Command\Visit\ProcessVisitsCommand::NAME => Command\Visit\ProcessVisitsCommand::class,
|
||||
Command\Visit\LocateVisitsCommand::NAME => Command\Visit\LocateVisitsCommand::class,
|
||||
Command\Visit\UpdateDbCommand::NAME => Command\Visit\UpdateDbCommand::class,
|
||||
|
||||
Command\Config\GenerateCharsetCommand::NAME => Command\Config\GenerateCharsetCommand::class,
|
||||
|
||||
@@ -3,6 +3,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI;
|
||||
|
||||
use GeoIp2\Database\Reader;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
|
||||
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
||||
@@ -19,6 +21,8 @@ return [
|
||||
'factories' => [
|
||||
Application::class => Factory\ApplicationFactory::class,
|
||||
|
||||
GeolocationDbUpdater::class => ConfigAbstractFactory::class,
|
||||
|
||||
Command\ShortUrl\GenerateShortUrlCommand::class => ConfigAbstractFactory::class,
|
||||
Command\ShortUrl\ResolveUrlCommand::class => ConfigAbstractFactory::class,
|
||||
Command\ShortUrl\ListShortUrlsCommand::class => ConfigAbstractFactory::class,
|
||||
@@ -26,7 +30,7 @@ return [
|
||||
Command\ShortUrl\GeneratePreviewCommand::class => ConfigAbstractFactory::class,
|
||||
Command\ShortUrl\DeleteShortUrlCommand::class => ConfigAbstractFactory::class,
|
||||
|
||||
Command\Visit\ProcessVisitsCommand::class => ConfigAbstractFactory::class,
|
||||
Command\Visit\LocateVisitsCommand::class => ConfigAbstractFactory::class,
|
||||
Command\Visit\UpdateDbCommand::class => ConfigAbstractFactory::class,
|
||||
|
||||
Command\Config\GenerateCharsetCommand::class => InvokableFactory::class,
|
||||
@@ -44,6 +48,8 @@ return [
|
||||
],
|
||||
|
||||
ConfigAbstractFactory::class => [
|
||||
GeolocationDbUpdater::class => [DbUpdater::class, Reader::class],
|
||||
|
||||
Command\ShortUrl\GenerateShortUrlCommand::class => [Service\UrlShortener::class, 'config.url_shortener.domain'],
|
||||
Command\ShortUrl\ResolveUrlCommand::class => [Service\UrlShortener::class],
|
||||
Command\ShortUrl\ListShortUrlsCommand::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
|
||||
@@ -51,10 +57,11 @@ return [
|
||||
Command\ShortUrl\GeneratePreviewCommand::class => [Service\ShortUrlService::class, PreviewGenerator::class],
|
||||
Command\ShortUrl\DeleteShortUrlCommand::class => [Service\ShortUrl\DeleteShortUrlService::class],
|
||||
|
||||
Command\Visit\ProcessVisitsCommand::class => [
|
||||
Command\Visit\LocateVisitsCommand::class => [
|
||||
Service\VisitService::class,
|
||||
IpLocationResolverInterface::class,
|
||||
Lock\Factory::class,
|
||||
GeolocationDbUpdater::class,
|
||||
],
|
||||
Command\Visit\UpdateDbCommand::class => [DbUpdater::class],
|
||||
|
||||
|
||||
@@ -3,7 +3,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Command\Visit;
|
||||
|
||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
||||
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
|
||||
@@ -13,6 +15,7 @@ use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
||||
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
||||
use Shlinkio\Shlink\Core\Service\VisitServiceInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
@@ -20,9 +23,10 @@ use Symfony\Component\Lock\Factory as Locker;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
class ProcessVisitsCommand extends Command
|
||||
class LocateVisitsCommand extends Command
|
||||
{
|
||||
public const NAME = 'visit:process';
|
||||
public const NAME = 'visit:locate';
|
||||
public const ALIASES = ['visit:process'];
|
||||
|
||||
/** @var VisitServiceInterface */
|
||||
private $visitService;
|
||||
@@ -30,39 +34,48 @@ class ProcessVisitsCommand extends Command
|
||||
private $ipLocationResolver;
|
||||
/** @var Locker */
|
||||
private $locker;
|
||||
/** @var OutputInterface */
|
||||
private $output;
|
||||
/** @var GeolocationDbUpdaterInterface */
|
||||
private $dbUpdater;
|
||||
|
||||
/** @var SymfonyStyle */
|
||||
private $io;
|
||||
/** @var ProgressBar */
|
||||
private $progressBar;
|
||||
|
||||
public function __construct(
|
||||
VisitServiceInterface $visitService,
|
||||
IpLocationResolverInterface $ipLocationResolver,
|
||||
Locker $locker
|
||||
Locker $locker,
|
||||
GeolocationDbUpdaterInterface $dbUpdater
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->visitService = $visitService;
|
||||
$this->ipLocationResolver = $ipLocationResolver;
|
||||
$this->locker = $locker;
|
||||
$this->dbUpdater = $dbUpdater;
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setName(self::NAME)
|
||||
->setDescription('Processes visits where location is not set yet');
|
||||
->setAliases(self::ALIASES)
|
||||
->setDescription('Resolves visits origin locations.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): ?int
|
||||
{
|
||||
$this->output = $output;
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$this->io = new SymfonyStyle($input, $output);
|
||||
|
||||
$lock = $this->locker->createLock(self::NAME);
|
||||
if (! $lock->acquire()) {
|
||||
$io->warning(sprintf('There is already an instance of the "%s" command in execution', self::NAME));
|
||||
$this->io->warning(sprintf('There is already an instance of the "%s" command in execution', self::NAME));
|
||||
return ExitCodes::EXIT_WARNING;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->checkDbUpdate();
|
||||
|
||||
$this->visitService->locateUnlocatedVisits(
|
||||
[$this, 'getGeolocationDataForVisit'],
|
||||
function (VisitLocation $location) use ($output) {
|
||||
@@ -74,7 +87,7 @@ class ProcessVisitsCommand extends Command
|
||||
}
|
||||
);
|
||||
|
||||
$io->success('Finished processing all IPs');
|
||||
$this->io->success('Finished processing all IPs');
|
||||
} finally {
|
||||
$lock->release();
|
||||
return ExitCodes::EXIT_SUCCESS;
|
||||
@@ -84,7 +97,7 @@ class ProcessVisitsCommand extends Command
|
||||
public function getGeolocationDataForVisit(Visit $visit): Location
|
||||
{
|
||||
if (! $visit->hasRemoteAddr()) {
|
||||
$this->output->writeln(
|
||||
$this->io->writeln(
|
||||
'<comment>Ignored visit with no IP address</comment>',
|
||||
OutputInterface::VERBOSITY_VERBOSE
|
||||
);
|
||||
@@ -92,21 +105,51 @@ class ProcessVisitsCommand extends Command
|
||||
}
|
||||
|
||||
$ipAddr = $visit->getRemoteAddr();
|
||||
$this->output->write(sprintf('Processing IP <fg=blue>%s</>', $ipAddr));
|
||||
$this->io->write(sprintf('Processing IP <fg=blue>%s</>', $ipAddr));
|
||||
if ($ipAddr === IpAddress::LOCALHOST) {
|
||||
$this->output->writeln(' [<comment>Ignored localhost address</comment>]');
|
||||
$this->io->writeln(' [<comment>Ignored localhost address</comment>]');
|
||||
throw IpCannotBeLocatedException::forLocalhost();
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->ipLocationResolver->resolveIpLocation($ipAddr);
|
||||
} catch (WrongIpException $e) {
|
||||
$this->output->writeln(' [<fg=red>An error occurred while locating IP. Skipped</>]');
|
||||
if ($this->output->isVerbose()) {
|
||||
$this->getApplication()->renderException($e, $this->output);
|
||||
$this->io->writeln(' [<fg=red>An error occurred while locating IP. Skipped</>]');
|
||||
if ($this->io->isVerbose()) {
|
||||
$this->getApplication()->renderException($e, $this->io);
|
||||
}
|
||||
|
||||
throw IpCannotBeLocatedException::forError($e);
|
||||
}
|
||||
}
|
||||
|
||||
private function checkDbUpdate(): void
|
||||
{
|
||||
try {
|
||||
$this->dbUpdater->checkDbUpdate(function (bool $olderDbExists) {
|
||||
$this->io->writeln(
|
||||
sprintf('<fg=blue>%s GeoLite2 database...</>', $olderDbExists ? 'Updating' : 'Downloading')
|
||||
);
|
||||
$this->progressBar = new ProgressBar($this->io);
|
||||
}, function (int $total, int $downloaded) {
|
||||
$this->progressBar->setMaxSteps($total);
|
||||
$this->progressBar->setProgress($downloaded);
|
||||
});
|
||||
|
||||
if ($this->progressBar !== null) {
|
||||
$this->progressBar->finish();
|
||||
$this->io->newLine();
|
||||
}
|
||||
} catch (GeolocationDbUpdateFailedException $e) {
|
||||
if (! $e->olderDbExists()) {
|
||||
$this->io->error('GeoLite2 database download failed. It is not possible to locate visits.');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->io->newLine();
|
||||
$this->io->writeln(
|
||||
'<fg=yellow;options=bold>[Warning] GeoLite2 database update failed. Proceeding with old version.</>'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,13 @@ use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
/** @deprecated */
|
||||
class UpdateDbCommand extends Command
|
||||
{
|
||||
public const NAME = 'visit:update-db';
|
||||
@@ -29,10 +33,16 @@ class UpdateDbCommand extends Command
|
||||
{
|
||||
$this
|
||||
->setName(self::NAME)
|
||||
->setDescription('Updates the GeoLite2 database file used to geolocate IP addresses')
|
||||
->setDescription('[DEPRECATED] Updates the GeoLite2 database file used to geolocate IP addresses')
|
||||
->setHelp(
|
||||
'The GeoLite2 database is updated first Tuesday every month, so this command should be ideally run '
|
||||
. 'every first Wednesday'
|
||||
)
|
||||
->addOption(
|
||||
'ignoreErrors',
|
||||
'i',
|
||||
InputOption::VALUE_NONE,
|
||||
'Makes the command success even iof the update fails.'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -49,19 +59,32 @@ class UpdateDbCommand extends Command
|
||||
});
|
||||
|
||||
$progressBar->finish();
|
||||
$io->writeln('');
|
||||
$io->newLine();
|
||||
|
||||
$io->success('GeoLite2 database properly updated');
|
||||
return ExitCodes::EXIT_SUCCESS;
|
||||
} catch (RuntimeException $e) {
|
||||
$progressBar->finish();
|
||||
$io->writeln('');
|
||||
$io->newLine();
|
||||
|
||||
$io->error('An error occurred while updating GeoLite2 database');
|
||||
if ($io->isVerbose()) {
|
||||
$this->getApplication()->renderException($e, $output);
|
||||
}
|
||||
return ExitCodes::EXIT_FAILURE;
|
||||
return $this->handleError($e, $io, $input);
|
||||
}
|
||||
}
|
||||
|
||||
private function handleError(RuntimeException $e, SymfonyStyle $io, InputInterface $input): int
|
||||
{
|
||||
$ignoreErrors = $input->getOption('ignoreErrors');
|
||||
$baseErrorMsg = 'An error occurred while updating GeoLite2 database';
|
||||
|
||||
if ($ignoreErrors) {
|
||||
$io->warning(sprintf('%s, but it was ignored', $baseErrorMsg));
|
||||
return ExitCodes::EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
$io->error($baseErrorMsg);
|
||||
if ($io->isVerbose()) {
|
||||
$this->getApplication()->renderException($e, $io);
|
||||
}
|
||||
return ExitCodes::EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
10
module/CLI/src/Exception/ExceptionInterface.php
Normal file
10
module/CLI/src/Exception/ExceptionInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
interface ExceptionInterface extends Throwable
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
class GeolocationDbUpdateFailedException extends RuntimeException implements ExceptionInterface
|
||||
{
|
||||
/** @var bool */
|
||||
private $olderDbExists;
|
||||
|
||||
public function __construct(bool $olderDbExists, string $message = '', int $code = 0, Throwable $previous = null)
|
||||
{
|
||||
$this->olderDbExists = $olderDbExists;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
public static function create(bool $olderDbExists, Throwable $prev = null): self
|
||||
{
|
||||
return new self(
|
||||
$olderDbExists,
|
||||
'An error occurred while updating geolocation database, and an older version could not be found',
|
||||
0,
|
||||
$prev
|
||||
);
|
||||
}
|
||||
|
||||
public function olderDbExists(): bool
|
||||
{
|
||||
return $this->olderDbExists;
|
||||
}
|
||||
}
|
||||
67
module/CLI/src/Util/GeolocationDbUpdater.php
Normal file
67
module/CLI/src/Util/GeolocationDbUpdater.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Util;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use GeoIp2\Database\Reader;
|
||||
use InvalidArgumentException;
|
||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
||||
|
||||
class GeolocationDbUpdater implements GeolocationDbUpdaterInterface
|
||||
{
|
||||
/** @var DbUpdaterInterface */
|
||||
private $dbUpdater;
|
||||
/** @var Reader */
|
||||
private $geoLiteDbReader;
|
||||
|
||||
public function __construct(DbUpdaterInterface $dbUpdater, Reader $geoLiteDbReader)
|
||||
{
|
||||
$this->dbUpdater = $dbUpdater;
|
||||
$this->geoLiteDbReader = $geoLiteDbReader;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GeolocationDbUpdateFailedException
|
||||
*/
|
||||
public function checkDbUpdate(callable $mustBeUpdated = null, callable $handleProgress = null): void
|
||||
{
|
||||
try {
|
||||
$meta = $this->geoLiteDbReader->metadata();
|
||||
if ($this->buildIsTooOld($meta->__get('buildEpoch'))) {
|
||||
$this->downloadNewDb(true, $mustBeUpdated, $handleProgress);
|
||||
}
|
||||
} catch (InvalidArgumentException $e) {
|
||||
// This is the exception thrown by the reader when the database file does not exist
|
||||
$this->downloadNewDb(false, $mustBeUpdated, $handleProgress);
|
||||
}
|
||||
}
|
||||
|
||||
private function buildIsTooOld(int $buildTimestamp): bool
|
||||
{
|
||||
$buildDate = Chronos::createFromTimestamp($buildTimestamp);
|
||||
$now = Chronos::now();
|
||||
return $now->gt($buildDate->addDays(35));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GeolocationDbUpdateFailedException
|
||||
*/
|
||||
private function downloadNewDb(
|
||||
bool $olderDbExists,
|
||||
callable $mustBeUpdated = null,
|
||||
callable $handleProgress = null
|
||||
): void {
|
||||
if ($mustBeUpdated !== null) {
|
||||
$mustBeUpdated($olderDbExists);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->dbUpdater->downloadFreshCopy($handleProgress);
|
||||
} catch (RuntimeException $e) {
|
||||
throw GeolocationDbUpdateFailedException::create($olderDbExists, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
module/CLI/src/Util/GeolocationDbUpdaterInterface.php
Normal file
14
module/CLI/src/Util/GeolocationDbUpdaterInterface.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Util;
|
||||
|
||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||
|
||||
interface GeolocationDbUpdaterInterface
|
||||
{
|
||||
/**
|
||||
* @throws GeolocationDbUpdateFailedException
|
||||
*/
|
||||
public function checkDbUpdate(callable $mustBeUpdated = null, callable $handleProgress = null): void;
|
||||
}
|
||||
@@ -34,7 +34,6 @@ class DisableKeyCommandTest extends TestCase
|
||||
$this->apiKeyService->disable($apiKey)->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'api-key:disable',
|
||||
'apiKey' => $apiKey,
|
||||
]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
@@ -49,7 +48,6 @@ class DisableKeyCommandTest extends TestCase
|
||||
$disable = $this->apiKeyService->disable($apiKey)->willThrow(InvalidArgumentException::class);
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'api-key:disable',
|
||||
'apiKey' => $apiKey,
|
||||
]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
@@ -34,9 +34,7 @@ class GenerateKeyCommandTest extends TestCase
|
||||
{
|
||||
$create = $this->apiKeyService->create(null)->willReturn(new ApiKey());
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'api-key:generate',
|
||||
]);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
$this->assertStringContainsString('Generated API key: ', $output);
|
||||
@@ -49,7 +47,6 @@ class GenerateKeyCommandTest extends TestCase
|
||||
$this->apiKeyService->create(Argument::type(Chronos::class))->shouldBeCalledOnce()
|
||||
->willReturn(new ApiKey());
|
||||
$this->commandTester->execute([
|
||||
'command' => 'api-key:generate',
|
||||
'--expirationDate' => '2016-01-01',
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -31,9 +31,7 @@ class GenerateCharsetCommandTest extends TestCase
|
||||
{
|
||||
$prefix = 'Character set: ';
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'config:generate-charset',
|
||||
]);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
// Both default character set and the new one should have the same length
|
||||
|
||||
@@ -54,9 +54,7 @@ class GeneratePreviewCommandTest extends TestCase
|
||||
$generatePreview2 = $this->previewGenerator->generatePreview('https://bar.com')->willReturn('');
|
||||
$generatePreview3 = $this->previewGenerator->generatePreview('http://baz.com/something')->willReturn('');
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:process-previews',
|
||||
]);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
$this->assertStringContainsString('Processing URL http://foo.com', $output);
|
||||
@@ -81,9 +79,7 @@ class GeneratePreviewCommandTest extends TestCase
|
||||
$this->previewGenerator->generatePreview(Argument::any())->willThrow(PreviewGenerationException::class)
|
||||
->shouldBeCalledTimes(count($items));
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:process-previews',
|
||||
]);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertEquals(count($items), substr_count($output, 'Error'));
|
||||
}
|
||||
|
||||
@@ -42,7 +42,6 @@ class GenerateShortUrlCommandTest extends TestCase
|
||||
);
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:generate',
|
||||
'longUrl' => 'http://domain.com/foo/bar',
|
||||
'--maxVisits' => '3',
|
||||
]);
|
||||
@@ -58,10 +57,7 @@ class GenerateShortUrlCommandTest extends TestCase
|
||||
$this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(new InvalidUrlException())
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:generate',
|
||||
'longUrl' => 'http://domain.com/invalid',
|
||||
]);
|
||||
$this->commandTester->execute(['longUrl' => 'http://domain.com/invalid']);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertStringContainsString(
|
||||
'Provided URL "http://domain.com/invalid" is invalid.',
|
||||
@@ -82,7 +78,6 @@ class GenerateShortUrlCommandTest extends TestCase
|
||||
)->willReturn((new ShortUrl(''))->setShortCode('abc123'));
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:generate',
|
||||
'longUrl' => 'http://domain.com/foo/bar',
|
||||
'--tags' => ['foo,bar', 'baz', 'boo,zar,baz'],
|
||||
]);
|
||||
|
||||
@@ -45,10 +45,7 @@ class GetVisitsCommandTest extends TestCase
|
||||
new Paginator(new ArrayAdapter([]))
|
||||
)->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:visits',
|
||||
'shortCode' => $shortCode,
|
||||
]);
|
||||
$this->commandTester->execute(['shortCode' => $shortCode]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -65,7 +62,6 @@ class GetVisitsCommandTest extends TestCase
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:visits',
|
||||
'shortCode' => $shortCode,
|
||||
'--startDate' => $startDate,
|
||||
'--endDate' => $endDate,
|
||||
@@ -84,10 +80,7 @@ class GetVisitsCommandTest extends TestCase
|
||||
]))
|
||||
)->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:visits',
|
||||
'shortCode' => $shortCode,
|
||||
]);
|
||||
$this->commandTester->execute(['shortCode' => $shortCode]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertStringContainsString('foo', $output);
|
||||
$this->assertStringContainsString('Spain', $output);
|
||||
|
||||
@@ -37,7 +37,7 @@ class ListShortUrlsCommandTest extends TestCase
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->setInputs(['n']);
|
||||
$this->commandTester->execute(['command' => 'shortcode:list']);
|
||||
$this->commandTester->execute([]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -54,7 +54,7 @@ class ListShortUrlsCommandTest extends TestCase
|
||||
})->shouldBeCalledTimes(3);
|
||||
|
||||
$this->commandTester->setInputs(['y', 'y', 'n']);
|
||||
$this->commandTester->execute(['command' => 'shortcode:list']);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
$this->assertStringContainsString('Continue with page 2?', $output);
|
||||
@@ -75,7 +75,7 @@ class ListShortUrlsCommandTest extends TestCase
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->setInputs(['n']);
|
||||
$this->commandTester->execute(['command' => 'shortcode:list']);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
$this->assertStringContainsString('url_1', $output);
|
||||
@@ -95,10 +95,7 @@ class ListShortUrlsCommandTest extends TestCase
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->setInputs(['y']);
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:list',
|
||||
'--page' => $page,
|
||||
]);
|
||||
$this->commandTester->execute(['--page' => $page]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
@@ -108,10 +105,7 @@ class ListShortUrlsCommandTest extends TestCase
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->setInputs(['y']);
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:list',
|
||||
'--showTags' => true,
|
||||
]);
|
||||
$this->commandTester->execute(['--showTags' => true]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertStringContainsString('Tags', $output);
|
||||
}
|
||||
|
||||
@@ -41,10 +41,7 @@ class ResolveUrlCommandTest extends TestCase
|
||||
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($shortUrl)
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:parse',
|
||||
'shortCode' => $shortCode,
|
||||
]);
|
||||
$this->commandTester->execute(['shortCode' => $shortCode]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output);
|
||||
}
|
||||
@@ -56,10 +53,7 @@ class ResolveUrlCommandTest extends TestCase
|
||||
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:parse',
|
||||
'shortCode' => $shortCode,
|
||||
]);
|
||||
$this->commandTester->execute(['shortCode' => $shortCode]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertStringContainsString('Provided short code "' . $shortCode . '" could not be found.', $output);
|
||||
}
|
||||
@@ -71,10 +65,7 @@ class ResolveUrlCommandTest extends TestCase
|
||||
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(new InvalidShortCodeException())
|
||||
->shouldBeCalledOnce();
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'shortcode:parse',
|
||||
'shortCode' => $shortCode,
|
||||
]);
|
||||
$this->commandTester->execute(['shortCode' => $shortCode]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertStringContainsString('Provided short code "' . $shortCode . '" has an invalid format.', $output);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ namespace ShlinkioTest\Shlink\CLI\Command\Visit;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\CLI\Command\Visit\ProcessVisitsCommand;
|
||||
use Shlinkio\Shlink\CLI\Command\Visit\LocateVisitsCommand;
|
||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
||||
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\IpApiLocationResolver;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
|
||||
@@ -24,7 +26,7 @@ use Symfony\Component\Lock;
|
||||
use function array_shift;
|
||||
use function sprintf;
|
||||
|
||||
class ProcessVisitsCommandTest extends TestCase
|
||||
class LocateVisitsCommandTest extends TestCase
|
||||
{
|
||||
/** @var CommandTester */
|
||||
private $commandTester;
|
||||
@@ -36,11 +38,14 @@ class ProcessVisitsCommandTest extends TestCase
|
||||
private $locker;
|
||||
/** @var ObjectProphecy */
|
||||
private $lock;
|
||||
/** @var ObjectProphecy */
|
||||
private $dbUpdater;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->visitService = $this->prophesize(VisitService::class);
|
||||
$this->ipResolver = $this->prophesize(IpApiLocationResolver::class);
|
||||
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
||||
|
||||
$this->locker = $this->prophesize(Lock\Factory::class);
|
||||
$this->lock = $this->prophesize(Lock\LockInterface::class);
|
||||
@@ -49,10 +54,11 @@ class ProcessVisitsCommandTest extends TestCase
|
||||
});
|
||||
$this->locker->createLock(Argument::type('string'))->willReturn($this->lock->reveal());
|
||||
|
||||
$command = new ProcessVisitsCommand(
|
||||
$command = new LocateVisitsCommand(
|
||||
$this->visitService->reveal(),
|
||||
$this->ipResolver->reveal(),
|
||||
$this->locker->reveal()
|
||||
$this->locker->reveal(),
|
||||
$this->dbUpdater->reveal()
|
||||
);
|
||||
$app = new Application();
|
||||
$app->add($command);
|
||||
@@ -79,9 +85,7 @@ class ProcessVisitsCommandTest extends TestCase
|
||||
Location::emptyInstance()
|
||||
);
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'visit:process',
|
||||
]);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
$this->assertStringContainsString('Processing IP 1.2.3.0', $output);
|
||||
@@ -111,9 +115,7 @@ class ProcessVisitsCommandTest extends TestCase
|
||||
Location::emptyInstance()
|
||||
);
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'visit:process',
|
||||
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$this->assertStringContainsString($message, $output);
|
||||
@@ -150,9 +152,7 @@ class ProcessVisitsCommandTest extends TestCase
|
||||
);
|
||||
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willThrow(WrongIpException::class);
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'visit:process',
|
||||
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
@@ -170,16 +170,51 @@ class ProcessVisitsCommandTest extends TestCase
|
||||
});
|
||||
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]);
|
||||
|
||||
$this->commandTester->execute([
|
||||
'command' => 'visit:process',
|
||||
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
$this->assertStringContainsString(
|
||||
sprintf('There is already an instance of the "%s" command', ProcessVisitsCommand::NAME),
|
||||
sprintf('There is already an instance of the "%s" command', LocateVisitsCommand::NAME),
|
||||
$output
|
||||
);
|
||||
$locateVisits->shouldNotHaveBeenCalled();
|
||||
$resolveIpLocation->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideParams
|
||||
*/
|
||||
public function showsProperMessageWhenGeoLiteUpdateFails(bool $olderDbExists, string $expectedMessage): void
|
||||
{
|
||||
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function () {
|
||||
});
|
||||
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
|
||||
function (array $args) use ($olderDbExists) {
|
||||
[$mustBeUpdated, $handleProgress] = $args;
|
||||
|
||||
$mustBeUpdated($olderDbExists);
|
||||
$handleProgress(100, 50);
|
||||
|
||||
throw GeolocationDbUpdateFailedException::create($olderDbExists);
|
||||
}
|
||||
);
|
||||
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
$this->assertStringContainsString(
|
||||
sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading'),
|
||||
$output
|
||||
);
|
||||
$this->assertStringContainsString($expectedMessage, $output);
|
||||
$locateVisits->shouldHaveBeenCalledTimes((int) $olderDbExists);
|
||||
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideParams(): iterable
|
||||
{
|
||||
yield [true, '[Warning] GeoLite2 database update failed. Proceeding with old version.'];
|
||||
yield [false, 'GeoLite2 database download failed. It is not possible to locate visits.'];
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\CLI\Command\Visit\UpdateDbCommand;
|
||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
||||
use Symfony\Component\Console\Application;
|
||||
@@ -31,27 +32,45 @@ class UpdateDbCommandTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function successMessageIsPrintedIfEverythingWorks()
|
||||
public function successMessageIsPrintedIfEverythingWorks(): void
|
||||
{
|
||||
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->will(function () {
|
||||
});
|
||||
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$exitCode = $this->commandTester->getStatusCode();
|
||||
|
||||
$this->assertStringContainsString('GeoLite2 database properly updated', $output);
|
||||
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
|
||||
$download->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function errorMessageIsPrintedIfAnExceptionIsThrown()
|
||||
public function errorMessageIsPrintedIfAnExceptionIsThrown(): void
|
||||
{
|
||||
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
|
||||
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$exitCode = $this->commandTester->getStatusCode();
|
||||
|
||||
$this->assertStringContainsString('An error occurred while updating GeoLite2 database', $output);
|
||||
$this->assertEquals(ExitCodes::EXIT_FAILURE, $exitCode);
|
||||
$download->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function warningMessageIsPrintedIfAnExceptionIsThrownAndErrorsAreIgnored(): void
|
||||
{
|
||||
$download = $this->dbUpdater->downloadFreshCopy(Argument::type('callable'))->willThrow(RuntimeException::class);
|
||||
|
||||
$this->commandTester->execute(['--ignoreErrors' => true]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
$exitCode = $this->commandTester->getStatusCode();
|
||||
|
||||
$this->assertStringContainsString('ignored', $output);
|
||||
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $exitCode);
|
||||
$download->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\CLI\Exception;
|
||||
|
||||
use Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||
use Throwable;
|
||||
|
||||
class GeolocationDbUpdateFailedExceptionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideOlderDbExists
|
||||
*/
|
||||
public function constructCreatesExceptionWithDefaultArgs(bool $olderDbExists): void
|
||||
{
|
||||
$e = new GeolocationDbUpdateFailedException($olderDbExists);
|
||||
|
||||
$this->assertEquals($olderDbExists, $e->olderDbExists());
|
||||
$this->assertEquals('', $e->getMessage());
|
||||
$this->assertEquals(0, $e->getCode());
|
||||
$this->assertNull($e->getPrevious());
|
||||
}
|
||||
|
||||
public function provideOlderDbExists(): iterable
|
||||
{
|
||||
yield 'with older DB' => [true];
|
||||
yield 'without older DB' => [false];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideConstructorArgs
|
||||
*/
|
||||
public function constructCreatesException(bool $olderDbExists, string $message, int $code, ?Throwable $prev): void
|
||||
{
|
||||
$e = new GeolocationDbUpdateFailedException($olderDbExists, $message, $code, $prev);
|
||||
|
||||
$this->assertEquals($olderDbExists, $e->olderDbExists());
|
||||
$this->assertEquals($message, $e->getMessage());
|
||||
$this->assertEquals($code, $e->getCode());
|
||||
$this->assertEquals($prev, $e->getPrevious());
|
||||
}
|
||||
|
||||
public function provideConstructorArgs(): iterable
|
||||
{
|
||||
yield [true, 'This is a nice error message', 99, new Exception('prev')];
|
||||
yield [false, 'Another message', 0, new RuntimeException('prev')];
|
||||
yield [true, 'An yet another message', -50, null];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideCreateArgs
|
||||
*/
|
||||
public function createBuildsException(bool $olderDbExists, ?Throwable $prev): void
|
||||
{
|
||||
$e = GeolocationDbUpdateFailedException::create($olderDbExists, $prev);
|
||||
|
||||
$this->assertEquals($olderDbExists, $e->olderDbExists());
|
||||
$this->assertEquals(
|
||||
'An error occurred while updating geolocation database, and an older version could not be found',
|
||||
$e->getMessage()
|
||||
);
|
||||
$this->assertEquals(0, $e->getCode());
|
||||
$this->assertEquals($prev, $e->getPrevious());
|
||||
}
|
||||
|
||||
public function provideCreateArgs(): iterable
|
||||
{
|
||||
yield 'older DB and no prev' => [true, null];
|
||||
yield 'older DB and prev' => [true, new RuntimeException('prev')];
|
||||
yield 'no older DB and no prev' => [false, null];
|
||||
yield 'no older DB and prev' => [false, new Exception('prev')];
|
||||
}
|
||||
}
|
||||
139
module/CLI/test/Util/GeolocationDbUpdaterTest.php
Normal file
139
module/CLI/test/Util/GeolocationDbUpdaterTest.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\CLI\Util;
|
||||
|
||||
use Cake\Chronos\Chronos;
|
||||
use GeoIp2\Database\Reader;
|
||||
use InvalidArgumentException;
|
||||
use MaxMind\Db\Reader\Metadata;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
||||
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater;
|
||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
||||
use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdaterInterface;
|
||||
use Throwable;
|
||||
|
||||
use function Functional\map;
|
||||
use function range;
|
||||
|
||||
class GeolocationDbUpdaterTest extends TestCase
|
||||
{
|
||||
/** @var GeolocationDbUpdater */
|
||||
private $geolocationDbUpdater;
|
||||
/** @var ObjectProphecy */
|
||||
private $dbUpdater;
|
||||
/** @var ObjectProphecy */
|
||||
private $geoLiteDbReader;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->dbUpdater = $this->prophesize(DbUpdaterInterface::class);
|
||||
$this->geoLiteDbReader = $this->prophesize(Reader::class);
|
||||
|
||||
$this->geolocationDbUpdater = new GeolocationDbUpdater(
|
||||
$this->dbUpdater->reveal(),
|
||||
$this->geoLiteDbReader->reveal()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function exceptionIsThrownWhenOlderDbDoesNotExistAndDownloadFails(): void
|
||||
{
|
||||
$mustBeUpdated = function () {
|
||||
$this->assertTrue(true);
|
||||
};
|
||||
$getMeta = $this->geoLiteDbReader->metadata()->willThrow(InvalidArgumentException::class);
|
||||
$prev = new RuntimeException('');
|
||||
$download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev);
|
||||
|
||||
try {
|
||||
$this->geolocationDbUpdater->checkDbUpdate($mustBeUpdated);
|
||||
$this->assertTrue(false); // If this is reached, the test will fail
|
||||
} catch (Throwable $e) {
|
||||
/** @var GeolocationDbUpdateFailedException $e */
|
||||
$this->assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
||||
$this->assertSame($prev, $e->getPrevious());
|
||||
$this->assertFalse($e->olderDbExists());
|
||||
}
|
||||
|
||||
$getMeta->shouldHaveBeenCalledOnce();
|
||||
$download->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideBigDays
|
||||
*/
|
||||
public function exceptionIsThrownWhenOlderDbIsTooOldAndDownloadFails(int $days): void
|
||||
{
|
||||
$getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([
|
||||
'binary_format_major_version' => '',
|
||||
'binary_format_minor_version' => '',
|
||||
'build_epoch' => Chronos::now()->subDays($days)->getTimestamp(),
|
||||
'database_type' => '',
|
||||
'languages' => '',
|
||||
'description' => '',
|
||||
'ip_version' => '',
|
||||
'node_count' => 1,
|
||||
'record_size' => 4,
|
||||
]));
|
||||
$prev = new RuntimeException('');
|
||||
$download = $this->dbUpdater->downloadFreshCopy(null)->willThrow($prev);
|
||||
|
||||
try {
|
||||
$this->geolocationDbUpdater->checkDbUpdate();
|
||||
$this->assertTrue(false); // If this is reached, the test will fail
|
||||
} catch (Throwable $e) {
|
||||
/** @var GeolocationDbUpdateFailedException $e */
|
||||
$this->assertInstanceOf(GeolocationDbUpdateFailedException::class, $e);
|
||||
$this->assertSame($prev, $e->getPrevious());
|
||||
$this->assertTrue($e->olderDbExists());
|
||||
}
|
||||
|
||||
$getMeta->shouldHaveBeenCalledOnce();
|
||||
$download->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideBigDays(): iterable
|
||||
{
|
||||
yield [36];
|
||||
yield [50];
|
||||
yield [75];
|
||||
yield [100];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideSmallDays
|
||||
*/
|
||||
public function databaseIsNotUpdatedIfItIsYoungerThanOneWeek(int $days): void
|
||||
{
|
||||
$getMeta = $this->geoLiteDbReader->metadata()->willReturn(new Metadata([
|
||||
'binary_format_major_version' => '',
|
||||
'binary_format_minor_version' => '',
|
||||
'build_epoch' => Chronos::now()->subDays($days)->getTimestamp(),
|
||||
'database_type' => '',
|
||||
'languages' => '',
|
||||
'description' => '',
|
||||
'ip_version' => '',
|
||||
'node_count' => 1,
|
||||
'record_size' => 4,
|
||||
]));
|
||||
$download = $this->dbUpdater->downloadFreshCopy(null)->will(function () {
|
||||
});
|
||||
|
||||
$this->geolocationDbUpdater->checkDbUpdate();
|
||||
|
||||
$getMeta->shouldHaveBeenCalledOnce();
|
||||
$download->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function provideSmallDays(): iterable
|
||||
{
|
||||
return map(range(0, 34), function (int $days) {
|
||||
return [$days];
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,98 +5,19 @@ namespace Shlinkio\Shlink\Common\Factory;
|
||||
|
||||
use Doctrine\Common\Cache;
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Interop\Container\Exception\ContainerException;
|
||||
use Memcached;
|
||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
use function Functional\contains;
|
||||
use function Shlinkio\Shlink\Common\env;
|
||||
use function sys_get_temp_dir;
|
||||
|
||||
class CacheFactory implements FactoryInterface
|
||||
{
|
||||
private const VALID_CACHE_ADAPTERS = [
|
||||
Cache\ApcuCache::class,
|
||||
Cache\ArrayCache::class,
|
||||
Cache\FilesystemCache::class,
|
||||
Cache\PhpFileCache::class,
|
||||
Cache\MemcachedCache::class,
|
||||
];
|
||||
private const DEFAULT_MEMCACHED_PORT = 11211;
|
||||
|
||||
/**
|
||||
* Create an object
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
* @param string $requestedName
|
||||
* @param null|array $options
|
||||
* @return object
|
||||
* @throws ServiceNotFoundException if unable to resolve the service.
|
||||
* @throws ServiceNotCreatedException if an exception is raised when
|
||||
* creating a service.
|
||||
* @throws ContainerException if any other error occurs
|
||||
*/
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
|
||||
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Cache\Cache
|
||||
{
|
||||
$appOptions = $container->get(AppOptions::class);
|
||||
$adapter = $this->getAdapter($container);
|
||||
$adapter = env('APP_ENV', 'pro') === 'pro' ? new Cache\ApcuCache() : new Cache\ArrayCache();
|
||||
$adapter->setNamespace((string) $appOptions);
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
private function getAdapter(ContainerInterface $container): Cache\CacheProvider
|
||||
{
|
||||
// Try to get the adapter from config
|
||||
$config = $container->get('config');
|
||||
if (isset($config['cache']['adapter']) && contains(self::VALID_CACHE_ADAPTERS, $config['cache']['adapter'])) {
|
||||
return $this->resolveCacheAdapter($config['cache']);
|
||||
}
|
||||
|
||||
// If the adapter has not been set in config, create one based on environment
|
||||
return env('APP_ENV', 'pro') === 'pro' ? new Cache\ApcuCache() : new Cache\ArrayCache();
|
||||
}
|
||||
|
||||
private function resolveCacheAdapter(array $cacheConfig): Cache\CacheProvider
|
||||
{
|
||||
switch ($cacheConfig['adapter']) {
|
||||
case Cache\ArrayCache::class:
|
||||
case Cache\ApcuCache::class:
|
||||
return new $cacheConfig['adapter']();
|
||||
case Cache\FilesystemCache::class:
|
||||
case Cache\PhpFileCache::class:
|
||||
return new $cacheConfig['adapter']($cacheConfig['options']['dir'] ?? sys_get_temp_dir());
|
||||
case Cache\MemcachedCache::class:
|
||||
$cache = new Cache\MemcachedCache();
|
||||
$cache->setMemcached($this->buildMemcached($cacheConfig));
|
||||
return $cache;
|
||||
default:
|
||||
return new Cache\ArrayCache();
|
||||
}
|
||||
}
|
||||
|
||||
private function buildMemcached(array $cacheConfig): Memcached
|
||||
{
|
||||
$memcached = new Memcached();
|
||||
$servers = $cacheConfig['options']['servers'] ?? [];
|
||||
|
||||
foreach ($servers as $server) {
|
||||
$this->addMemcachedServer($memcached, $server);
|
||||
}
|
||||
|
||||
return $memcached;
|
||||
}
|
||||
|
||||
private function addMemcachedServer(Memcached $memcached, array $server): void
|
||||
{
|
||||
if (! isset($server['host'])) {
|
||||
return;
|
||||
}
|
||||
$port = (int) ($server['port'] ?? self::DEFAULT_MEMCACHED_PORT);
|
||||
|
||||
$memcached->addServer($server['host'], $port);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,13 @@ use Doctrine\Common\DataFixtures\Purger\ORMPurger;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
use function file_exists;
|
||||
use function unlink;
|
||||
|
||||
class TestHelper
|
||||
{
|
||||
public function createTestDb(string $shlinkDbPath): void
|
||||
public function createTestDb(): void
|
||||
{
|
||||
if (file_exists($shlinkDbPath)) {
|
||||
unlink($shlinkDbPath);
|
||||
}
|
||||
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:drop', '--force', '--no-interaction', '-q']);
|
||||
$process->inheritEnvironmentVariables()
|
||||
->mustRun();
|
||||
|
||||
$process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q']);
|
||||
$process->inheritEnvironmentVariables()
|
||||
|
||||
@@ -5,27 +5,26 @@ namespace ShlinkioTest\Shlink\Common\Factory;
|
||||
|
||||
use Doctrine\Common\Cache\ApcuCache;
|
||||
use Doctrine\Common\Cache\ArrayCache;
|
||||
use Doctrine\Common\Cache\FilesystemCache;
|
||||
use Doctrine\Common\Cache\MemcachedCache;
|
||||
use Doctrine\Common\Cache\RedisCache;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Common\Factory\CacheFactory;
|
||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||
use Zend\ServiceManager\ServiceManager;
|
||||
|
||||
use function count;
|
||||
use function putenv;
|
||||
use function realpath;
|
||||
use function sys_get_temp_dir;
|
||||
|
||||
class CacheFactoryTest extends TestCase
|
||||
{
|
||||
/** @var CacheFactory */
|
||||
private $factory;
|
||||
/** @var ServiceManager */
|
||||
private $sm;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->factory = new CacheFactory();
|
||||
$this->sm = new ServiceManager(['services' => [
|
||||
AppOptions::class => new AppOptions(),
|
||||
]]);
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass(): void
|
||||
@@ -34,76 +33,18 @@ class CacheFactoryTest extends TestCase
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function productionReturnsApcAdapter()
|
||||
public function productionReturnsApcAdapter(): void
|
||||
{
|
||||
putenv('APP_ENV=pro');
|
||||
$instance = $this->factory->__invoke($this->createSM(), '');
|
||||
$instance = ($this->factory)($this->sm, '');
|
||||
$this->assertInstanceOf(ApcuCache::class, $instance);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function developmentReturnsArrayAdapter()
|
||||
public function developmentReturnsArrayAdapter(): void
|
||||
{
|
||||
putenv('APP_ENV=dev');
|
||||
$instance = $this->factory->__invoke($this->createSM(), '');
|
||||
$instance = ($this->factory)($this->sm, '');
|
||||
$this->assertInstanceOf(ArrayCache::class, $instance);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function adapterDefinedInConfigIgnoresEnvironment()
|
||||
{
|
||||
putenv('APP_ENV=pro');
|
||||
$instance = $this->factory->__invoke($this->createSM(ArrayCache::class), '');
|
||||
$this->assertInstanceOf(ArrayCache::class, $instance);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function invalidAdapterDefinedInConfigFallbacksToEnvironment()
|
||||
{
|
||||
putenv('APP_ENV=pro');
|
||||
$instance = $this->factory->__invoke($this->createSM(RedisCache::class), '');
|
||||
$this->assertInstanceOf(ApcuCache::class, $instance);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function filesystemCacheAdaptersReadDirOption()
|
||||
{
|
||||
$dir = realpath(sys_get_temp_dir());
|
||||
/** @var FilesystemCache $instance */
|
||||
$instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), '');
|
||||
$this->assertInstanceOf(FilesystemCache::class, $instance);
|
||||
$this->assertEquals($dir, $instance->getDirectory());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function memcachedCacheAdaptersReadServersOption()
|
||||
{
|
||||
$servers = [
|
||||
[
|
||||
'host' => '1.2.3.4',
|
||||
'port' => 123,
|
||||
],
|
||||
[
|
||||
'host' => '4.3.2.1',
|
||||
'port' => 321,
|
||||
],
|
||||
];
|
||||
/** @var MemcachedCache $instance */
|
||||
$instance = $this->factory->__invoke($this->createSM(MemcachedCache::class, ['servers' => $servers]), '');
|
||||
$this->assertInstanceOf(MemcachedCache::class, $instance);
|
||||
$this->assertEquals(count($servers), count($instance->getMemcached()->getServerList()));
|
||||
}
|
||||
|
||||
private function createSM($cacheAdapter = null, array $options = [])
|
||||
{
|
||||
return new ServiceManager(['services' => [
|
||||
'config' => isset($cacheAdapter) ? [
|
||||
'cache' => [
|
||||
'adapter' => $cacheAdapter,
|
||||
'options' => $options,
|
||||
],
|
||||
] : [],
|
||||
AppOptions::class => new AppOptions(),
|
||||
]]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,6 @@ $builder->createOneToMany('visits', Entity\Visit::class)
|
||||
|
||||
$builder->createManyToMany('tags', Entity\Tag::class)
|
||||
->setJoinTable('short_urls_in_tags')
|
||||
->addInverseJoinColumn('tag_id', 'id')
|
||||
->addJoinColumn('short_url_id', 'id')
|
||||
->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE')
|
||||
->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE')
|
||||
->build();
|
||||
|
||||
@@ -43,10 +43,10 @@ $builder->createField('userAgent', Type::STRING)
|
||||
->build();
|
||||
|
||||
$builder->createManyToOne('shortUrl', Entity\ShortUrl::class)
|
||||
->addJoinColumn('short_url_id', 'id', false)
|
||||
->addJoinColumn('short_url_id', 'id', false, false, 'CASCADE')
|
||||
->build();
|
||||
|
||||
$builder->createManyToOne('visitLocation', Entity\VisitLocation::class)
|
||||
->addJoinColumn('visit_location_id', 'id')
|
||||
->addJoinColumn('visit_location_id', 'id', true, false, 'Set NULL')
|
||||
->cascadePersist()
|
||||
->build();
|
||||
|
||||
Binary file not shown.
@@ -1,15 +1,15 @@
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: Shlink 1.0\n"
|
||||
"POT-Creation-Date: 2017-10-13 12:29+0200\n"
|
||||
"PO-Revision-Date: 2017-10-13 12:30+0200\n"
|
||||
"POT-Creation-Date: 2019-04-14 08:58+0200\n"
|
||||
"PO-Revision-Date: 2019-04-14 08:58+0200\n"
|
||||
"Last-Translator: Alejandro Celaya <alejandro@alejandrocelaya.com>\n"
|
||||
"Language-Team: \n"
|
||||
"Language: es_ES\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
"Content-Type: text/plain; charset=UTF-8\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
"X-Generator: Poedit 2.0.1\n"
|
||||
"X-Generator: Poedit 2.1.1\n"
|
||||
"X-Poedit-Basepath: ..\n"
|
||||
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
||||
"X-Poedit-SourceCharset: UTF-8\n"
|
||||
@@ -39,3 +39,6 @@ msgstr "Esta URL acortada no parece ser válida."
|
||||
|
||||
msgid "Make sure you included all the characters, with no extra punctuation."
|
||||
msgstr "Asegúrate de haber incluído todos los caracteres, sin puntuación extra."
|
||||
|
||||
msgid "Invalid URL"
|
||||
msgstr "URL inválida"
|
||||
|
||||
50
module/Core/src/ConfigPostProcessor.php
Normal file
50
module/Core/src/ConfigPostProcessor.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core;
|
||||
|
||||
use Shlinkio\Shlink\Installer\Util\PathCollection;
|
||||
use Zend\Stdlib\ArrayUtils;
|
||||
|
||||
use function array_intersect_key;
|
||||
use function array_key_exists;
|
||||
use function Functional\contains;
|
||||
use function Functional\reduce_left;
|
||||
|
||||
class ConfigPostProcessor
|
||||
{
|
||||
private const SIMPLIFIED_CONFIG_MAPPING = [
|
||||
'disable_track_param' => ['app_options', 'disable_track_param'],
|
||||
'short_domain_schema' => ['url_shortener', 'domain', 'schema'],
|
||||
'short_domain_host' => ['url_shortener', 'domain', 'hostname'],
|
||||
'validate_url' => ['url_shortener', 'validate_url'],
|
||||
'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'redirect_to'],
|
||||
'db_config' => ['entity_manager', 'connection'],
|
||||
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
||||
'locale' => ['translator', 'locale'],
|
||||
];
|
||||
private const SIMPLIFIED_CONFIG_TOGGLES = [
|
||||
'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'enable_redirection'],
|
||||
'delete_short_url_threshold' => ['delete_short_urls', 'check_visits_threshold'],
|
||||
];
|
||||
private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config'];
|
||||
|
||||
public function __invoke(array $config): array
|
||||
{
|
||||
$existingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
|
||||
|
||||
return reduce_left($existingKeys, function ($value, string $key, $c, PathCollection $collection) {
|
||||
$path = self::SIMPLIFIED_CONFIG_MAPPING[$key];
|
||||
if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) {
|
||||
$value = ArrayUtils::merge($collection->getValueInPath($path), $value);
|
||||
}
|
||||
|
||||
$collection->setValueInPath($value, $path);
|
||||
if (array_key_exists($key, self::SIMPLIFIED_CONFIG_TOGGLES)) {
|
||||
$collection->setValueInPath(true, self::SIMPLIFIED_CONFIG_TOGGLES[$key]);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}, new PathCollection($config))->toArray();
|
||||
}
|
||||
}
|
||||
@@ -112,32 +112,39 @@ class UrlShortener implements UrlShortenerInterface
|
||||
if ($meta->hasCustomSlug()) {
|
||||
$criteria['shortCode'] = $meta->getCustomSlug();
|
||||
}
|
||||
/** @var ShortUrl|null $shortUrl */
|
||||
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy($criteria);
|
||||
if ($shortUrl === null) {
|
||||
/** @var ShortUrl[] $shortUrls */
|
||||
$shortUrls = $this->em->getRepository(ShortUrl::class)->findBy($criteria);
|
||||
if (empty($shortUrls)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($meta->hasMaxVisits() && $meta->getMaxVisits() !== $shortUrl->getMaxVisits()) {
|
||||
return null;
|
||||
}
|
||||
if ($meta->hasValidSince() && ! $meta->getValidSince()->eq($shortUrl->getValidSince())) {
|
||||
return null;
|
||||
}
|
||||
if ($meta->hasValidUntil() && ! $meta->getValidUntil()->eq($shortUrl->getValidUntil())) {
|
||||
return null;
|
||||
}
|
||||
// Iterate short URLs until one that matches is found, or return null otherwise
|
||||
return array_reduce($shortUrls, function (?ShortUrl $found, ShortUrl $shortUrl) use ($tags, $meta) {
|
||||
if ($found) {
|
||||
return $found;
|
||||
}
|
||||
|
||||
$shortUrlTags = invoke($shortUrl->getTags(), '__toString');
|
||||
$hasAllTags = count($shortUrlTags) === count($tags) && array_reduce(
|
||||
$tags,
|
||||
function (bool $hasAllTags, string $tag) use ($shortUrlTags) {
|
||||
return $hasAllTags && contains($shortUrlTags, $tag);
|
||||
},
|
||||
true
|
||||
);
|
||||
if ($meta->hasMaxVisits() && $meta->getMaxVisits() !== $shortUrl->getMaxVisits()) {
|
||||
return null;
|
||||
}
|
||||
if ($meta->hasValidSince() && ! $meta->getValidSince()->eq($shortUrl->getValidSince())) {
|
||||
return null;
|
||||
}
|
||||
if ($meta->hasValidUntil() && ! $meta->getValidUntil()->eq($shortUrl->getValidUntil())) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $hasAllTags ? $shortUrl : null;
|
||||
$shortUrlTags = invoke($shortUrl->getTags(), '__toString');
|
||||
$hasAllTags = count($shortUrlTags) === count($tags) && array_reduce(
|
||||
$tags,
|
||||
function (bool $hasAllTags, string $tag) use ($shortUrlTags) {
|
||||
return $hasAllTags && contains($shortUrlTags, $tag);
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
return $hasAllTags ? $shortUrl : null;
|
||||
});
|
||||
}
|
||||
|
||||
private function checkUrlExists(string $url): void
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php $this->layout('ShlinkCore::layout/default') ?>
|
||||
|
||||
<?php $this->start('title') ?>
|
||||
<?= $this->translate('URL Not Found') ?>
|
||||
<?= $this->translate('Invalid URL') ?>
|
||||
<?php $this->end() ?>
|
||||
|
||||
<?php $this->start('stylesheets') ?>
|
||||
|
||||
@@ -5,28 +5,19 @@
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
|
||||
<link rel="shortcut icon" href="/favicon.ico">
|
||||
<style>
|
||||
body {padding-top: 60px;}
|
||||
.app {display: flex; min-height: 100vh; flex-direction: column;}
|
||||
.app-content {flex: 1;}
|
||||
.app-footer p {margin-bottom: 20px;}
|
||||
html, body {height: 100%}
|
||||
.app {height: 100vh; display: flex; align-items: center; justify-content: center; flex-flow: column;}
|
||||
</style>
|
||||
<?= $this->section('stylesheets', '') ?>
|
||||
</head>
|
||||
<body class="app">
|
||||
<div class="app-content">
|
||||
<body>
|
||||
<div class="app">
|
||||
<main class="container">
|
||||
<?= $this->section('main', '') ?>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<footer class="app-footer">
|
||||
<div class="container">
|
||||
<hr>
|
||||
<p>© <?= date('Y') ?> <a href="https://shlink.io">Shlink</a></p>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -18,9 +18,9 @@ use function count;
|
||||
class ShortUrlRepositoryTest extends DatabaseTestCase
|
||||
{
|
||||
protected const ENTITIES_TO_EMPTY = [
|
||||
ShortUrl::class,
|
||||
Visit::class,
|
||||
Tag::class,
|
||||
Visit::class,
|
||||
ShortUrl::class,
|
||||
];
|
||||
|
||||
/** @var ShortUrlRepository */
|
||||
|
||||
93
module/Core/test/ConfigPostProcessorTest.php
Normal file
93
module/Core/test/ConfigPostProcessorTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\ConfigPostProcessor;
|
||||
|
||||
use function array_merge;
|
||||
|
||||
class ConfigPostProcessorTest extends TestCase
|
||||
{
|
||||
private $postProcessor;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->postProcessor = new ConfigPostProcessor();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function properlyMapsSimplifiedConfig(): void
|
||||
{
|
||||
$config = [
|
||||
'app_options' => [
|
||||
'disable_track_param' => 'foo',
|
||||
],
|
||||
|
||||
'entity_manager' => [
|
||||
'connection' => [
|
||||
'driver' => 'mysql',
|
||||
'host' => 'shlink_db',
|
||||
'port' => '3306',
|
||||
],
|
||||
],
|
||||
];
|
||||
$simplified = [
|
||||
'disable_track_param' => 'bar',
|
||||
'short_domain_schema' => 'https',
|
||||
'short_domain_host' => 'doma.in',
|
||||
'validate_url' => false,
|
||||
'delete_short_url_threshold' => 50,
|
||||
'locale' => 'es',
|
||||
'not_found_redirect_to' => 'foobar.com',
|
||||
'db_config' => [
|
||||
'dbname' => 'shlink',
|
||||
'user' => 'foo',
|
||||
'password' => 'bar',
|
||||
'port' => '1234',
|
||||
],
|
||||
];
|
||||
$expected = [
|
||||
'app_options' => [
|
||||
'disable_track_param' => 'bar',
|
||||
],
|
||||
|
||||
'entity_manager' => [
|
||||
'connection' => [
|
||||
'driver' => 'mysql',
|
||||
'host' => 'shlink_db',
|
||||
'dbname' => 'shlink',
|
||||
'user' => 'foo',
|
||||
'password' => 'bar',
|
||||
'port' => '1234',
|
||||
],
|
||||
],
|
||||
|
||||
'url_shortener' => [
|
||||
'domain' => [
|
||||
'schema' => 'https',
|
||||
'hostname' => 'doma.in',
|
||||
],
|
||||
'validate_url' => false,
|
||||
'not_found_short_url' => [
|
||||
'redirect_to' => 'foobar.com',
|
||||
'enable_redirection' => true,
|
||||
],
|
||||
],
|
||||
|
||||
'translator' => [
|
||||
'locale' => 'es',
|
||||
],
|
||||
|
||||
'delete_short_urls' => [
|
||||
'visits_threshold' => 50,
|
||||
'check_visits_threshold' => true,
|
||||
],
|
||||
];
|
||||
|
||||
$result = ($this->postProcessor)(array_merge($config, $simplified));
|
||||
|
||||
$this->assertEquals(array_merge($expected, $simplified), $result);
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,8 @@ use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
||||
use Zend\Diactoros\Uri;
|
||||
|
||||
use function array_map;
|
||||
|
||||
class UrlShortenerTest extends TestCase
|
||||
{
|
||||
/** @var UrlShortener */
|
||||
@@ -121,7 +123,7 @@ class UrlShortenerTest extends TestCase
|
||||
{
|
||||
$repo = $this->prophesize(ShortUrlRepository::class);
|
||||
$countBySlug = $repo->count(['shortCode' => 'custom-slug'])->willReturn(1);
|
||||
$repo->findOneBy(Argument::cetera())->willReturn(null);
|
||||
$repo->findBy(Argument::cetera())->willReturn([]);
|
||||
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||
|
||||
$countBySlug->shouldBeCalledOnce();
|
||||
@@ -146,20 +148,23 @@ class UrlShortenerTest extends TestCase
|
||||
?ShortUrl $expected
|
||||
): void {
|
||||
$repo = $this->prophesize(ShortUrlRepository::class);
|
||||
$findExisting = $repo->findOneBy(Argument::any())->willReturn($expected);
|
||||
$findExisting = $repo->findBy(Argument::any())->willReturn($expected !== null ? [$expected] : []);
|
||||
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||
|
||||
$result = $this->urlShortener->urlToShortCode(new Uri($url), $tags, $meta);
|
||||
|
||||
$this->assertSame($expected, $result);
|
||||
$findExisting->shouldHaveBeenCalledOnce();
|
||||
$getRepo->shouldHaveBeenCalledOnce();
|
||||
if ($expected) {
|
||||
$this->assertSame($expected, $result);
|
||||
}
|
||||
}
|
||||
|
||||
public function provideExistingShortUrls(): iterable
|
||||
{
|
||||
$url = 'http://foo.com';
|
||||
|
||||
yield [$url, [], ShortUrlMeta::createFromRawData(['findIfExists' => true]), null];
|
||||
yield [$url, [], ShortUrlMeta::createFromRawData(['findIfExists' => true]), new ShortUrl($url)];
|
||||
yield [$url, [], ShortUrlMeta::createFromRawData(
|
||||
['findIfExists' => true, 'customSlug' => 'foo']
|
||||
@@ -203,6 +208,37 @@ class UrlShortenerTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function properExistingShortUrlIsReturnedWhenMultipleMatch(): void
|
||||
{
|
||||
$url = 'http://foo.com';
|
||||
$tags = ['baz', 'foo', 'bar'];
|
||||
$meta = ShortUrlMeta::createFromRawData([
|
||||
'findIfExists' => true,
|
||||
'validUntil' => Chronos::parse('2017-01-01'),
|
||||
'maxVisits' => 4,
|
||||
]);
|
||||
$tagsCollection = new ArrayCollection(array_map(function (string $tag) {
|
||||
return new Tag($tag);
|
||||
}, $tags));
|
||||
$expected = (new ShortUrl($url, $meta))->setTags($tagsCollection);
|
||||
|
||||
$repo = $this->prophesize(ShortUrlRepository::class);
|
||||
$findExisting = $repo->findBy(Argument::any())->willReturn([
|
||||
new ShortUrl($url),
|
||||
new ShortUrl($url, $meta),
|
||||
$expected,
|
||||
(new ShortUrl($url))->setTags($tagsCollection),
|
||||
]);
|
||||
$getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
|
||||
|
||||
$result = $this->urlShortener->urlToShortCode(new Uri($url), $tags, $meta);
|
||||
|
||||
$this->assertSame($expected, $result);
|
||||
$findExisting->shouldHaveBeenCalledOnce();
|
||||
$getRepo->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function shortCodeIsProperlyParsed(): void
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@ use function sprintf;
|
||||
class EditShortUrlAction extends AbstractRestAction
|
||||
{
|
||||
protected const ROUTE_PATH = '/short-urls/{shortCode}';
|
||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT];
|
||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH, self::METHOD_PUT];
|
||||
|
||||
/** @var ShortUrlServiceInterface */
|
||||
private $shortUrlService;
|
||||
|
||||
@@ -4,29 +4,19 @@ declare(strict_types=1);
|
||||
namespace Shlinkio\Shlink\Rest\Middleware;
|
||||
|
||||
use Fig\Http\Message\RequestMethodInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Rest\Authentication;
|
||||
use Zend\Expressive\Router\RouteResult;
|
||||
|
||||
use function implode;
|
||||
|
||||
class CrossDomainMiddleware implements MiddlewareInterface, RequestMethodInterface
|
||||
{
|
||||
/**
|
||||
* Process an incoming server request and return a response, optionally delegating
|
||||
* to the next middleware component to create the response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param RequestHandlerInterface $handler
|
||||
*
|
||||
* @return Response
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function process(Request $request, RequestHandlerInterface $handler): Response
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
/** @var Response $response */
|
||||
$response = $handler->handle($request);
|
||||
if (! $request->hasHeader('Origin')) {
|
||||
return $response;
|
||||
@@ -42,13 +32,28 @@ class CrossDomainMiddleware implements MiddlewareInterface, RequestMethodInterfa
|
||||
return $response;
|
||||
}
|
||||
|
||||
// Add OPTIONS-specific headers
|
||||
foreach ([
|
||||
'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS', // TODO Should be dynamic
|
||||
// 'Access-Control-Allow-Methods' => $response->getHeaderLine('Allow'),
|
||||
return $this->addOptionsHeaders($request, $response);
|
||||
}
|
||||
|
||||
private function addOptionsHeaders(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
/** @var RouteResult|null $matchedRoute */
|
||||
$matchedRoute = $request->getAttribute(RouteResult::class);
|
||||
$matchedMethods = $matchedRoute !== null ? $matchedRoute->getAllowedMethods() : [
|
||||
self::METHOD_GET,
|
||||
self::METHOD_POST,
|
||||
self::METHOD_PUT,
|
||||
self::METHOD_PATCH,
|
||||
self::METHOD_DELETE,
|
||||
self::METHOD_OPTIONS,
|
||||
];
|
||||
$corsHeaders = [
|
||||
'Access-Control-Allow-Methods' => implode(',', $matchedMethods),
|
||||
'Access-Control-Max-Age' => '1000',
|
||||
'Access-Control-Allow-Headers' => $request->getHeaderLine('Access-Control-Request-Headers'),
|
||||
] as $key => $value) {
|
||||
];
|
||||
|
||||
foreach ($corsHeaders as $key => $value) {
|
||||
$response = $response->withHeader($key, $value);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,63 +10,108 @@ use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequest;
|
||||
use Zend\Expressive\Router\Route;
|
||||
use Zend\Expressive\Router\RouteResult;
|
||||
|
||||
use function Zend\Stratigility\middleware;
|
||||
|
||||
class CrossDomainMiddlewareTest extends TestCase
|
||||
{
|
||||
/** @var CrossDomainMiddleware */
|
||||
private $middleware;
|
||||
/** @var ObjectProphecy */
|
||||
private $delegate;
|
||||
private $handler;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->middleware = new CrossDomainMiddleware();
|
||||
$this->delegate = $this->prophesize(RequestHandlerInterface::class);
|
||||
$this->handler = $this->prophesize(RequestHandlerInterface::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function nonCrossDomainRequestsAreNotAffected()
|
||||
public function nonCrossDomainRequestsAreNotAffected(): void
|
||||
{
|
||||
$originalResponse = new Response();
|
||||
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
||||
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
||||
|
||||
$response = $this->middleware->process(new ServerRequest(), $this->delegate->reveal());
|
||||
$response = $this->middleware->process(new ServerRequest(), $this->handler->reveal());
|
||||
$this->assertSame($originalResponse, $response);
|
||||
|
||||
$headers = $response->getHeaders();
|
||||
$this->assertArrayNotHasKey('Access-Control-Allow-Origin', $headers);
|
||||
$this->assertArrayNotHasKey('Access-Control-Expose-Headers', $headers);
|
||||
$this->assertArrayNotHasKey('Access-Control-Allow-Methods', $headers);
|
||||
$this->assertArrayNotHasKey('Access-Control-Max-Age', $headers);
|
||||
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function anyRequestIncludesTheAllowAccessHeader()
|
||||
public function anyRequestIncludesTheAllowAccessHeader(): void
|
||||
{
|
||||
$originalResponse = new Response();
|
||||
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
||||
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
||||
|
||||
$response = $this->middleware->process(
|
||||
(new ServerRequest())->withHeader('Origin', 'local'),
|
||||
$this->delegate->reveal()
|
||||
$this->handler->reveal()
|
||||
);
|
||||
$this->assertNotSame($originalResponse, $response);
|
||||
|
||||
$headers = $response->getHeaders();
|
||||
$this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
|
||||
$this->assertArrayHasKey('Access-Control-Expose-Headers', $headers);
|
||||
$this->assertArrayNotHasKey('Access-Control-Allow-Methods', $headers);
|
||||
$this->assertArrayNotHasKey('Access-Control-Max-Age', $headers);
|
||||
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function optionsRequestIncludesMoreHeaders()
|
||||
public function optionsRequestIncludesMoreHeaders(): void
|
||||
{
|
||||
$originalResponse = new Response();
|
||||
$request = (new ServerRequest())->withMethod('OPTIONS')->withHeader('Origin', 'local');
|
||||
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
||||
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
||||
|
||||
$response = $this->middleware->process($request, $this->delegate->reveal());
|
||||
$response = $this->middleware->process($request, $this->handler->reveal());
|
||||
$this->assertNotSame($originalResponse, $response);
|
||||
|
||||
$headers = $response->getHeaders();
|
||||
$this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
|
||||
$this->assertArrayHasKey('Access-Control-Expose-Headers', $headers);
|
||||
$this->assertArrayHasKey('Access-Control-Allow-Methods', $headers);
|
||||
$this->assertArrayHasKey('Access-Control-Max-Age', $headers);
|
||||
$this->assertArrayHasKey('Access-Control-Allow-Headers', $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideRouteResults
|
||||
*/
|
||||
public function optionsRequestParsesRouteMatchToDetermineAllowedMethods(
|
||||
?RouteResult $result,
|
||||
string $expectedAllowedMethods
|
||||
): void {
|
||||
$originalResponse = new Response();
|
||||
$request = (new ServerRequest())->withAttribute(RouteResult::class, $result)
|
||||
->withMethod('OPTIONS')
|
||||
->withHeader('Origin', 'local');
|
||||
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
||||
|
||||
$response = $this->middleware->process($request, $this->handler->reveal());
|
||||
|
||||
$this->assertEquals($response->getHeaderLine('Access-Control-Allow-Methods'), $expectedAllowedMethods);
|
||||
}
|
||||
|
||||
public function provideRouteResults(): iterable
|
||||
{
|
||||
yield 'with no route result' => [null, 'GET,POST,PUT,PATCH,DELETE,OPTIONS'];
|
||||
yield 'with failed route result' => [RouteResult::fromRouteFailure(['POST', 'GET']), 'POST,GET'];
|
||||
yield 'with success route result' => [
|
||||
RouteResult::fromRoute(
|
||||
new Route('/', middleware(function () {
|
||||
}), ['DELETE', 'PATCH', 'PUT'])
|
||||
),
|
||||
'DELETE,PATCH,PUT',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user