Merge pull request #133 from acelaya/feature/1.8.0

1.8.0
This commit is contained in:
Alejandro Celaya 2018-03-29 09:50:58 +02:00 committed by GitHub
commit 7130425896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
71 changed files with 533 additions and 437 deletions

View File

@ -5,7 +5,6 @@ branches:
- /.*/ - /.*/
php: php:
- 7
- 7.1 - 7.1
- 7.2 - 7.2
@ -16,12 +15,10 @@ before_install:
before_script: before_script:
- composer self-update - composer self-update
- composer install --no-interaction - composer install --no-interaction
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then composer global require --dev phpstan/phpstan:0.9.*; fi
script: script:
- mkdir build - mkdir build
- composer check - composer check
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then ~/.composer/vendor/bin/phpstan analyse module/*/src/ --level=6 -c phpstan.neon; fi
after_script: after_script:
- vendor/bin/phpcov merge build --clover build/clover.xml - vendor/bin/phpcov merge build --clover build/clover.xml

View File

@ -1,5 +1,21 @@
## CHANGELOG ## CHANGELOG
### 1.8.1
**Features**
* [125: Implement a path which returns a 1px image instead of a redirection](https://github.com/shlinkio/shlink/issues/125)
**Enhancements:**
* [130: Update to Expressive 3](https://github.com/shlinkio/shlink/issues/130)
* [137: Update symfony packages to v4](https://github.com/shlinkio/shlink/issues/137)
**Tasks**
* [131: Drop support for PHP 7](https://github.com/shlinkio/shlink/issues/131)
* [132: Add infection to improve tests](https://github.com/shlinkio/shlink/issues/132)
### 1.7.2 ### 1.7.2
**Bugs:** **Bugs:**

View File

@ -12,8 +12,8 @@
} }
], ],
"require": { "require": {
"php": "^7.0", "php": "^7.1",
"acelaya/ze-content-based-error-handler": "^2.0", "acelaya/ze-content-based-error-handler": "^2.2",
"cocur/slugify": "^3.0", "cocur/slugify": "^3.0",
"doctrine/annotations": "^1.4", "doctrine/annotations": "^1.4",
"doctrine/cache": "^1.6", "doctrine/cache": "^1.6",
@ -22,23 +22,23 @@
"doctrine/dbal": "^2.5", "doctrine/dbal": "^2.5",
"doctrine/migrations": "^1.4", "doctrine/migrations": "^1.4",
"doctrine/orm": "^2.5", "doctrine/orm": "^2.5",
"endroid/qrcode": "^1.7", "endroid/qr-code": "^1.7",
"firebase/php-jwt": "^4.0", "firebase/php-jwt": "^4.0",
"guzzlehttp/guzzle": "^6.2", "guzzlehttp/guzzle": "^6.2",
"http-interop/http-middleware": "^0.4.1",
"mikehaertl/phpwkhtmltopdf": "^2.2", "mikehaertl/phpwkhtmltopdf": "^2.2",
"monolog/monolog": "^1.21", "monolog/monolog": "^1.21",
"roave/security-advisories": "dev-master", "roave/security-advisories": "dev-master",
"symfony/console": "^3.4", "symfony/console": "^4.0",
"symfony/filesystem": "^3.0", "symfony/filesystem": "^4.0",
"symfony/process": "^3.0", "symfony/process": "^4.0",
"theorchard/monolog-cascade": "^0.4", "theorchard/monolog-cascade": "^0.4",
"zendframework/zend-config": "^3.0", "zendframework/zend-config": "^3.0",
"zendframework/zend-config-aggregator": "^1.0", "zendframework/zend-config-aggregator": "^1.0",
"zendframework/zend-expressive": "^2.0", "zendframework/zend-diactoros": "^1.7",
"zendframework/zend-expressive-fastroute": "^2.0", "zendframework/zend-expressive": "^3.0",
"zendframework/zend-expressive-helpers": "^4.2", "zendframework/zend-expressive-fastroute": "^3.0",
"zendframework/zend-expressive-platesrenderer": "^1.3", "zendframework/zend-expressive-helpers": "^5.0",
"zendframework/zend-expressive-platesrenderer": "^2.0",
"zendframework/zend-i18n": "^2.7", "zendframework/zend-i18n": "^2.7",
"zendframework/zend-inputfilter": "^2.8", "zendframework/zend-inputfilter": "^2.8",
"zendframework/zend-paginator": "^2.6", "zendframework/zend-paginator": "^2.6",
@ -47,14 +47,16 @@
}, },
"require-dev": { "require-dev": {
"filp/whoops": "^2.0", "filp/whoops": "^2.0",
"phpunit/dbunit": "^3.0", "infection/infection": "^0.8.1",
"phpunit/phpcov": "^4.0", "phpstan/phpstan": "0.9",
"phpunit/phpunit": "^6.0", "phpunit/phpcov": "^5.0",
"phpunit/phpunit": "^7.0",
"slevomat/coding-standard": "^4.0", "slevomat/coding-standard": "^4.0",
"squizlabs/php_codesniffer": "^3.1 <3.2", "squizlabs/php_codesniffer": "^3.1 <3.2",
"symfony/dotenv": "^3.4", "symfony/dotenv": "^4.0",
"symfony/var-dumper": "^3.0", "symfony/var-dumper": "^4.0",
"zendframework/zend-expressive-tooling": "^0.4" "zendframework/zend-component-installer": "^2.1",
"zendframework/zend-expressive-tooling": "^1.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -84,8 +86,10 @@
"scripts": { "scripts": {
"check": [ "check": [
"@cs", "@cs",
"@stan",
"@test", "@test",
"@func-test" "@func-test",
"@infect"
], ],
"cs": "phpcs", "cs": "phpcs",
"cs-fix": "phpcbf", "cs-fix": "phpcbf",
@ -97,13 +101,17 @@
"@test", "@test",
"@func-test", "@func-test",
"phpcov merge build --html build/html" "phpcov merge build --html build/html"
] ],
"stan": "phpstan analyse module/*/src/ --level=6 -c phpstan.neon",
"infect": "infection --threads=4 --min-msi=65 --only-covered --log-verbosity=2",
"infect-show": "infection --threads=4 --min-msi=65 --only-covered --log-verbosity=2 --show-mutations",
"expressive": "expressive"
}, },
"config": { "config": {
"process-timeout": 0, "process-timeout": 0,
"sort-packages": true, "sort-packages": true,
"platform": { "platform": {
"php": "7.0.8" "php": "7.1.8"
} }
} }
} }

View File

@ -5,30 +5,23 @@ use Shlinkio\Shlink\Common\Factory\EmptyResponseImplicitOptionsMiddlewareFactory
use Zend\Expressive; use Zend\Expressive;
use Zend\Expressive\Container; use Zend\Expressive\Container;
use Zend\Expressive\Helper; use Zend\Expressive\Helper;
use Zend\Expressive\Middleware;
use Zend\Expressive\Plates;
use Zend\Expressive\Router;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware; use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
use Zend\Expressive\Template;
use Zend\ServiceManager\Factory\InvokableFactory; use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Stratigility\Middleware\ErrorHandler;
return [ return [
'dependencies' => [ 'dependencies' => [
'factories' => [ 'factories' => [
Expressive\Application::class => Container\ApplicationFactory::class,
Template\TemplateRendererInterface::class => Plates\PlatesRendererFactory::class,
Router\RouterInterface::class => Router\FastRouteRouterFactory::class,
ErrorHandler::class => Container\ErrorHandlerFactory::class,
ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class, ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class, Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
Helper\ServerUrlHelper::class => InvokableFactory::class, Helper\ServerUrlHelper::class => InvokableFactory::class,
], ],
'aliases' => [ 'delegators' => [
Middleware\ImplicitOptionsMiddleware::class => ImplicitOptionsMiddleware::class, Expressive\Application::class => [
Container\ApplicationConfigInjectionDelegator::class,
],
], ],
], ],

View File

@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware; use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
use Shlinkio\Shlink\Core\Response\NotFoundDelegate;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware; use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware; use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware; use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
@ -48,6 +49,7 @@ return [
'post-routing' => [ 'post-routing' => [
'middleware' => [ 'middleware' => [
Expressive\Router\Middleware\DispatchMiddleware::class, Expressive\Router\Middleware\DispatchMiddleware::class,
NotFoundDelegate::class,
], ],
'priority' => 1, 'priority' => 1,
], ],

View File

@ -7,6 +7,7 @@ use Shlinkio\Shlink\Common;
use Shlinkio\Shlink\Core; use Shlinkio\Shlink\Core;
use Shlinkio\Shlink\Rest; use Shlinkio\Shlink\Rest;
use Zend\ConfigAggregator; use Zend\ConfigAggregator;
use Zend\Expressive;
/** /**
* Configuration files are loaded in a specific order. First ``global.php``, then ``*.global.php``. * Configuration files are loaded in a specific order. First ``global.php``, then ``*.global.php``.
@ -18,8 +19,11 @@ use Zend\ConfigAggregator;
*/ */
return (new ConfigAggregator\ConfigAggregator([ return (new ConfigAggregator\ConfigAggregator([
Zend\Expressive\ConfigProvider::class, Expressive\ConfigProvider::class,
Zend\Expressive\Router\ConfigProvider::class, Expressive\Router\ConfigProvider::class,
Expressive\Router\FastRouteRouter\ConfigProvider::class,
Expressive\Plates\ConfigProvider::class,
Expressive\Helper\ConfigProvider::class,
ExpressiveErrorHandler\ConfigProvider::class, ExpressiveErrorHandler\ConfigProvider::class,
Common\ConfigProvider::class, Common\ConfigProvider::class,
Core\ConfigProvider::class, Core\ConfigProvider::class,

18
infection.json Normal file
View File

@ -0,0 +1,18 @@
{
"source": {
"directories": [
"module/*/src"
],
"excludes": []
},
"timeout": 10,
"logs": {
"text": "build/infection/infection-log.txt",
"summary": "build/infection/summary-log.txt",
"debug": "build/infection/debug-log.txt"
},
"tmpDir": "build/infection/temp",
"phpUnit": {
"configDir": "."
}
}

View File

@ -134,7 +134,7 @@ class InstallCommand extends Command
if (! $this->isUpdate) { if (! $this->isUpdate) {
$this->io->write('Initializing database...'); $this->io->write('Initializing database...');
if (! $this->runCommand( if (! $this->runCommand(
'php vendor/bin/doctrine.php orm:schema-tool:create', 'php vendor/bin/doctrine orm:schema-tool:create',
'Error generating database.', 'Error generating database.',
$output $output
)) { )) {
@ -155,7 +155,7 @@ class InstallCommand extends Command
// Generate proxies // Generate proxies
$this->io->write('Generating proxies...'); $this->io->write('Generating proxies...');
if (! $this->runCommand( if (! $this->runCommand(
'php vendor/bin/doctrine.php orm:generate-proxies', 'php vendor/bin/doctrine orm:generate-proxies',
'Error generating proxies.', 'Error generating proxies.',
$output $output
)) { )) {

View File

@ -27,6 +27,8 @@ class EmptyResponseImplicitOptionsMiddlewareFactory implements FactoryInterface
*/ */
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{ {
return new ImplicitOptionsMiddleware(new EmptyResponse()); return new ImplicitOptionsMiddleware(function () {
return new EmptyResponse();
});
} }
} }

View File

@ -3,10 +3,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Middleware; namespace Shlinkio\Shlink\Common\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as DelegateInterface;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
class LocaleMiddleware implements MiddlewareInterface class LocaleMiddleware implements MiddlewareInterface
@ -32,15 +32,15 @@ class LocaleMiddleware implements MiddlewareInterface
* *
* @return Response * @return Response
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, DelegateInterface $delegate): Response
{ {
if (! $request->hasHeader('Accept-Language')) { if (! $request->hasHeader('Accept-Language')) {
return $delegate->process($request); return $delegate->handle($request);
} }
$locale = $request->getHeaderLine('Accept-Language'); $locale = $request->getHeaderLine('Accept-Language');
$this->translator->setLocale($this->normalizeLocale($locale)); $this->translator->setLocale($this->normalizeLocale($locale));
return $delegate->process($request); return $delegate->handle($request);
} }
/** /**

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Response;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
class PixelResponse extends Response
{
private const BASE_64_IMAGE = 'R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw==';
private const CONTENT_TYPE = 'image/gif';
public function __construct(int $status = 200, array $headers = [])
{
$headers['content-type'] = self::CONTENT_TYPE;
parent::__construct($this->createBody(), $status, $headers);
}
/**
* Create the message body.
*
* @return StreamInterface
*/
private function createBody(): StreamInterface
{
$body = new Stream('php://temp', 'wb+');
$body->write(\base64_decode(self::BASE_64_IMAGE));
$body->rewind();
return $body;
}
}

View File

@ -3,40 +3,17 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\DbUnit; namespace ShlinkioTest\Shlink\Common\DbUnit;
use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\DbUnit\Database\Connection as DbConn; use PHPUnit\Framework\TestCase;
use PHPUnit\DbUnit\DataSet\IDataSet as DataSet;
use PHPUnit\DbUnit\TestCase;
abstract class DatabaseTestCase extends TestCase abstract class DatabaseTestCase extends TestCase
{ {
const ENTITIES_TO_EMPTY = []; protected const ENTITIES_TO_EMPTY = [];
/** /**
* @var EntityManagerInterface * @var EntityManagerInterface
*/ */
public static $em; public static $em;
/**
* @var DbConn
*/
private static $conn;
public function getConnection(): DbConn
{
if (isset(self::$conn)) {
return self::$conn;
}
/** @var PDOConnection $pdo */
$pdo = static::$em->getConnection()->getWrappedConnection();
return self::$conn = $this->createDefaultDBConnection($pdo, static::$em->getConnection()->getDatabase());
}
public function getDataSet(): DataSet
{
return $this->createArrayDataSet([]);
}
protected function getEntityManager(): EntityManagerInterface protected function getEntityManager(): EntityManagerInterface
{ {

View File

@ -38,8 +38,8 @@ class EmptyResponseImplicitOptionsMiddlewareFactoryTest extends TestCase
$instance = $this->factory->__invoke(new ServiceManager(), ''); $instance = $this->factory->__invoke(new ServiceManager(), '');
$ref = new \ReflectionObject($instance); $ref = new \ReflectionObject($instance);
$prop = $ref->getProperty('response'); $prop = $ref->getProperty('responseFactory');
$prop->setAccessible(true); $prop->setAccessible(true);
$this->assertInstanceOf(EmptyResponse::class, $prop->getValue($instance)); $this->assertInstanceOf(EmptyResponse::class, $prop->getValue($instance)());
} }
} }

View File

@ -32,7 +32,7 @@ class LocaleMiddlewareTest extends TestCase
public function whenNoHeaderIsPresentLocaleIsNotChanged() public function whenNoHeaderIsPresentLocaleIsNotChanged()
{ {
$this->assertEquals('ru', $this->translator->getLocale()); $this->assertEquals('ru', $this->translator->getLocale());
$this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createDelegateMock()->reveal()); $this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals('ru', $this->translator->getLocale()); $this->assertEquals('ru', $this->translator->getLocale());
} }
@ -43,7 +43,7 @@ class LocaleMiddlewareTest extends TestCase
{ {
$this->assertEquals('ru', $this->translator->getLocale()); $this->assertEquals('ru', $this->translator->getLocale());
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es'); $request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es');
$this->middleware->process($request, TestUtils::createDelegateMock()->reveal()); $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals('es', $this->translator->getLocale()); $this->assertEquals('es', $this->translator->getLocale());
} }
@ -52,7 +52,7 @@ class LocaleMiddlewareTest extends TestCase
*/ */
public function localeGetsNormalized() public function localeGetsNormalized()
{ {
$delegate = TestUtils::createDelegateMock(); $delegate = TestUtils::createReqHandlerMock();
$this->assertEquals('ru', $this->translator->getLocale()); $this->assertEquals('ru', $this->translator->getLocale());

View File

@ -3,22 +3,22 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Common\Util; namespace ShlinkioTest\Shlink\Common\Util;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophet; use Prophecy\Prophet;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
class TestUtils class TestUtils
{ {
private static $prophet; private static $prophet;
public static function createDelegateMock(ResponseInterface $response = null, RequestInterface $request = null) public static function createReqHandlerMock(ResponseInterface $response = null, RequestInterface $request = null)
{ {
$argument = $request ?: Argument::any(); $argument = $request ?: Argument::any();
$delegate = static::getProphet()->prophesize(DelegateInterface::class); $delegate = static::getProphet()->prophesize(RequestHandlerInterface::class);
$delegate->process($argument)->willReturn($response ?: new Response()); $delegate->handle($argument)->willReturn($response ?: new Response());
return $delegate; return $delegate;
} }

View File

@ -28,6 +28,7 @@ return [
// Middleware // Middleware
Action\RedirectAction::class => ConfigAbstractFactory::class, Action\RedirectAction::class => ConfigAbstractFactory::class,
Action\PixelAction::class => ConfigAbstractFactory::class,
Action\QrCodeAction::class => ConfigAbstractFactory::class, Action\QrCodeAction::class => ConfigAbstractFactory::class,
Action\PreviewAction::class => ConfigAbstractFactory::class, Action\PreviewAction::class => ConfigAbstractFactory::class,
Middleware\QrCodeCacheMiddleware::class => ConfigAbstractFactory::class, Middleware\QrCodeCacheMiddleware::class => ConfigAbstractFactory::class,
@ -60,6 +61,11 @@ return [
Service\VisitsTracker::class, Service\VisitsTracker::class,
Options\AppOptions::class, Options\AppOptions::class,
], ],
Action\PixelAction::class => [
Service\UrlShortener::class,
Service\VisitsTracker::class,
Options\AppOptions::class,
],
Action\QrCodeAction::class => [RouterInterface::class, Service\UrlShortener::class, 'Logger_Shlink'], Action\QrCodeAction::class => [RouterInterface::class, Service\UrlShortener::class, 'Logger_Shlink'],
Action\PreviewAction::class => [PreviewGenerator::class, Service\UrlShortener::class], Action\PreviewAction::class => [PreviewGenerator::class, Service\UrlShortener::class],
Middleware\QrCodeCacheMiddleware::class => [Cache::class], Middleware\QrCodeCacheMiddleware::class => [Cache::class],

View File

@ -13,6 +13,12 @@ return [
'middleware' => Action\RedirectAction::class, 'middleware' => Action\RedirectAction::class,
'allowed_methods' => ['GET'], 'allowed_methods' => ['GET'],
], ],
[
'name' => 'pixel-tracking',
'path' => '/{shortCode}/track',
'middleware' => Action\PixelAction::class,
'allowed_methods' => ['GET'],
],
[ [
'name' => 'short-url-qr-code', 'name' => 'short-url-qr-code',
'path' => '/{shortCode}/qr-code[/{size:[0-9]+}]', 'path' => '/{shortCode}/qr-code[/{size:[0-9]+}]',

View File

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
abstract class AbstractTrackingAction implements MiddlewareInterface
{
use ErrorResponseBuilderTrait;
/**
* @var UrlShortenerInterface
*/
private $urlShortener;
/**
* @var VisitsTrackerInterface
*/
private $visitTracker;
/**
* @var AppOptions
*/
private $appOptions;
public function __construct(
UrlShortenerInterface $urlShortener,
VisitsTrackerInterface $visitTracker,
AppOptions $appOptions
) {
$this->urlShortener = $urlShortener;
$this->visitTracker = $visitTracker;
$this->appOptions = $appOptions;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$shortCode = $request->getAttribute('shortCode', '');
$query = $request->getQueryParams();
$disableTrackParam = $this->appOptions->getDisableTrackParam();
try {
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
// Track visit to this short code
if ($disableTrackParam === null || ! \array_key_exists($disableTrackParam, $query)) {
$this->visitTracker->track($shortCode, $request);
}
return $this->createResp($longUrl);
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) {
return $this->buildErrorResponse($request, $handler);
}
}
abstract protected function createResp(string $longUrl): ResponseInterface;
}

View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action;
use Psr\Http\Message\ResponseInterface;
use Shlinkio\Shlink\Common\Response\PixelResponse;
class PixelAction extends AbstractTrackingAction
{
protected function createResp(string $longUrl): ResponseInterface
{
return new PixelResponse();
}
}

View File

@ -3,10 +3,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action; namespace Shlinkio\Shlink\Core\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException; use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface; use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface;
use Shlinkio\Shlink\Common\Util\ResponseUtilsTrait; use Shlinkio\Shlink\Common\Util\ResponseUtilsTrait;
@ -40,11 +40,11 @@ class PreviewAction implements MiddlewareInterface
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @param RequestHandlerInterface $handler
* *
* @return Response * @return Response
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, RequestHandlerInterface $handler): Response
{ {
$shortCode = $request->getAttribute('shortCode'); $shortCode = $request->getAttribute('shortCode');
@ -52,12 +52,8 @@ class PreviewAction implements MiddlewareInterface
$url = $this->urlShortener->shortCodeToUrl($shortCode); $url = $this->urlShortener->shortCodeToUrl($shortCode);
$imagePath = $this->previewGenerator->generatePreview($url); $imagePath = $this->previewGenerator->generatePreview($url);
return $this->generateImageResponse($imagePath); return $this->generateImageResponse($imagePath);
} catch (InvalidShortCodeException $e) { } catch (InvalidShortCodeException | EntityDoesNotExistException | PreviewGenerationException $e) {
return $this->buildErrorResponse($request, $delegate); return $this->buildErrorResponse($request, $handler);
} catch (EntityDoesNotExistException $e) {
return $this->buildErrorResponse($request, $delegate);
} catch (PreviewGenerationException $e) {
return $this->buildErrorResponse($request, $delegate);
} }
} }
} }

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action; namespace Shlinkio\Shlink\Core\Action;
use Endroid\QrCode\QrCode; use Endroid\QrCode\QrCode;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
use Shlinkio\Shlink\Common\Response\QrCodeResponse; use Shlinkio\Shlink\Common\Response\QrCodeResponse;
@ -49,11 +49,11 @@ class QrCodeAction implements MiddlewareInterface
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @param RequestHandlerInterface $handler
* *
* @return Response * @return Response
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, RequestHandlerInterface $handler): Response
{ {
// Make sure the short URL exists for this short code // Make sure the short URL exists for this short code
$shortCode = $request->getAttribute('shortCode'); $shortCode = $request->getAttribute('shortCode');
@ -61,10 +61,10 @@ class QrCodeAction implements MiddlewareInterface
$this->urlShortener->shortCodeToUrl($shortCode); $this->urlShortener->shortCodeToUrl($shortCode);
} catch (InvalidShortCodeException $e) { } catch (InvalidShortCodeException $e) {
$this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e); $this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e);
return $this->buildErrorResponse($request, $delegate); return $this->buildErrorResponse($request, $handler);
} catch (EntityDoesNotExistException $e) { } catch (EntityDoesNotExistException $e) {
$this->logger->warning('Tried to create a QR code with a not found short code' . PHP_EOL . $e); $this->logger->warning('Tried to create a QR code with a not found short code' . PHP_EOL . $e);
return $this->buildErrorResponse($request, $delegate); return $this->buildErrorResponse($request, $handler);
} }
$path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]); $path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]);
@ -80,7 +80,7 @@ class QrCodeAction implements MiddlewareInterface
* @param Request $request * @param Request $request
* @return int * @return int
*/ */
protected function getSizeParam(Request $request) private function getSizeParam(Request $request): int
{ {
$size = (int) $request->getAttribute('size', 300); $size = (int) $request->getAttribute('size', 300);
if ($size < 50) { if ($size < 50) {

View File

@ -3,75 +3,15 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action; namespace Shlinkio\Shlink\Core\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
use Zend\Diactoros\Response\RedirectResponse; use Zend\Diactoros\Response\RedirectResponse;
class RedirectAction implements MiddlewareInterface class RedirectAction extends AbstractTrackingAction
{ {
use ErrorResponseBuilderTrait; protected function createResp(string $longUrl): Response
/**
* @var UrlShortenerInterface
*/
private $urlShortener;
/**
* @var VisitsTrackerInterface
*/
private $visitTracker;
/**
* @var AppOptions
*/
private $appOptions;
public function __construct(
UrlShortenerInterface $urlShortener,
VisitsTrackerInterface $visitTracker,
AppOptions $appOptions
) {
$this->urlShortener = $urlShortener;
$this->visitTracker = $visitTracker;
$this->appOptions = $appOptions;
}
/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
*
* @param Request $request
* @param DelegateInterface $delegate
*
* @return Response
*/
public function process(Request $request, DelegateInterface $delegate)
{ {
$shortCode = $request->getAttribute('shortCode', '');
$query = $request->getQueryParams();
$disableTrackParam = $this->appOptions->getDisableTrackParam();
try {
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
// Track visit to this short code
if ($disableTrackParam === null || ! \array_key_exists($disableTrackParam, $query)) {
$this->visitTracker->track($shortCode, $request);
}
// Return a redirect response to the long URL. // Return a redirect response to the long URL.
// Use a temporary redirect to make sure browsers always hit the server for analytics purposes // Use a temporary redirect to make sure browsers always hit the server for analytics purposes
return new RedirectResponse($longUrl); return new RedirectResponse($longUrl);
} catch (InvalidShortCodeException $e) {
return $this->buildErrorResponse($request, $delegate);
} catch (EntityDoesNotExistException $e) {
return $this->buildErrorResponse($request, $delegate);
}
} }
} }

View File

@ -3,16 +3,18 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Action\Util; namespace Shlinkio\Shlink\Core\Action\Util;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Response\NotFoundDelegate; use Shlinkio\Shlink\Core\Response\NotFoundDelegate;
trait ErrorResponseBuilderTrait trait ErrorResponseBuilderTrait
{ {
private function buildErrorResponse(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface private function buildErrorResponse(
{ ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$request = $request->withAttribute(NotFoundDelegate::NOT_FOUND_TEMPLATE, 'ShlinkCore::invalid-short-code'); $request = $request->withAttribute(NotFoundDelegate::NOT_FOUND_TEMPLATE, 'ShlinkCore::invalid-short-code');
return $delegate->process($request); return $handler->handle($request);
} }
} }

View File

@ -57,7 +57,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
/** /**
* @return string * @return string
*/ */
public function getReferer() public function getReferer(): string
{ {
return $this->referer; return $this->referer;
} }
@ -66,7 +66,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
* @param string $referer * @param string $referer
* @return $this * @return $this
*/ */
public function setReferer($referer) public function setReferer($referer): self
{ {
$this->referer = $referer; $this->referer = $referer;
return $this; return $this;
@ -75,7 +75,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
/** /**
* @return \DateTime * @return \DateTime
*/ */
public function getDate() public function getDate(): \DateTime
{ {
return $this->date; return $this->date;
} }
@ -84,7 +84,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
* @param \DateTime $date * @param \DateTime $date
* @return $this * @return $this
*/ */
public function setDate($date) public function setDate($date): self
{ {
$this->date = $date; $this->date = $date;
return $this; return $this;
@ -93,7 +93,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
/** /**
* @return ShortUrl * @return ShortUrl
*/ */
public function getShortUrl() public function getShortUrl(): ShortUrl
{ {
return $this->shortUrl; return $this->shortUrl;
} }
@ -102,7 +102,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
* @param ShortUrl $shortUrl * @param ShortUrl $shortUrl
* @return $this * @return $this
*/ */
public function setShortUrl($shortUrl) public function setShortUrl($shortUrl): self
{ {
$this->shortUrl = $shortUrl; $this->shortUrl = $shortUrl;
return $this; return $this;
@ -111,7 +111,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
/** /**
* @return string * @return string
*/ */
public function getRemoteAddr() public function getRemoteAddr(): string
{ {
return $this->remoteAddr; return $this->remoteAddr;
} }
@ -120,7 +120,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
* @param string $remoteAddr * @param string $remoteAddr
* @return $this * @return $this
*/ */
public function setRemoteAddr($remoteAddr) public function setRemoteAddr($remoteAddr): self
{ {
$this->remoteAddr = $remoteAddr; $this->remoteAddr = $remoteAddr;
return $this; return $this;
@ -129,7 +129,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
/** /**
* @return string * @return string
*/ */
public function getUserAgent() public function getUserAgent(): string
{ {
return $this->userAgent; return $this->userAgent;
} }
@ -138,7 +138,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
* @param string $userAgent * @param string $userAgent
* @return $this * @return $this
*/ */
public function setUserAgent($userAgent) public function setUserAgent($userAgent): self
{ {
$this->userAgent = $userAgent; $this->userAgent = $userAgent;
return $this; return $this;
@ -147,7 +147,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
/** /**
* @return VisitLocation * @return VisitLocation
*/ */
public function getVisitLocation() public function getVisitLocation(): VisitLocation
{ {
return $this->visitLocation; return $this->visitLocation;
} }
@ -156,7 +156,7 @@ class Visit extends AbstractEntity implements \JsonSerializable
* @param VisitLocation $visitLocation * @param VisitLocation $visitLocation
* @return $this * @return $this
*/ */
public function setVisitLocation($visitLocation) public function setVisitLocation($visitLocation): self
{ {
$this->visitLocation = $visitLocation; $this->visitLocation = $visitLocation;
return $this; return $this;
@ -165,11 +165,11 @@ class Visit extends AbstractEntity implements \JsonSerializable
/** /**
* Specify data which should be serialized to JSON * Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>, * @return array data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource. * which is a value of any type other than a resource.
* @since 5.4.0 * @since 5.4.0
*/ */
public function jsonSerialize() public function jsonSerialize(): array
{ {
return [ return [
'referer' => $this->referer, 'referer' => $this->referer,

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Middleware; namespace Shlinkio\Shlink\Core\Middleware;
use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Cache;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response as DiactResp; use Zend\Diactoros\Response as DiactResp;
class QrCodeCacheMiddleware implements MiddlewareInterface class QrCodeCacheMiddleware implements MiddlewareInterface
@ -27,11 +27,11 @@ class QrCodeCacheMiddleware implements MiddlewareInterface
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @param RequestHandlerInterface $handler
* *
* @return Response * @return Response
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, RequestHandlerInterface $handler): Response
{ {
$cacheKey = $request->getUri()->getPath(); $cacheKey = $request->getUri()->getPath();
@ -45,7 +45,7 @@ class QrCodeCacheMiddleware implements MiddlewareInterface
// If not, call the next middleware and cache it // If not, call the next middleware and cache it
/** @var Response $resp */ /** @var Response $resp */
$resp = $delegate->process($request); $resp = $handler->handle($request);
$this->cache->save($cacheKey, [ $this->cache->save($cacheKey, [
'body' => $resp->getBody()->__toString(), 'body' => $resp->getBody()->__toString(),
'content-type' => $resp->getHeaderLine('Content-Type'), 'content-type' => $resp->getHeaderLine('Content-Type'),

View File

@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Response; namespace Shlinkio\Shlink\Core\Response;
use Fig\Http\Message\StatusCodeInterface; use Fig\Http\Message\StatusCodeInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface as DelegateInterface;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Expressive\Template\TemplateRendererInterface; use Zend\Expressive\Template\TemplateRendererInterface;
@ -37,14 +37,14 @@ class NotFoundDelegate implements DelegateInterface
* @return ResponseInterface * @return ResponseInterface
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(ServerRequestInterface $request): ResponseInterface public function handle(ServerRequestInterface $request): ResponseInterface
{ {
$accepts = explode(',', $request->getHeaderLine('Accept')); $accepts = \explode(',', $request->getHeaderLine('Accept'));
$accept = array_shift($accepts); $accept = \array_shift($accepts);
$status = StatusCodeInterface::STATUS_NOT_FOUND; $status = StatusCodeInterface::STATUS_NOT_FOUND;
// If the first accepted type is json, return a json response // If the first accepted type is json, return a json response
if (in_array($accept, ['application/json', 'text/json', 'application/x-json'], true)) { if (\in_array($accept, ['application/json', 'text/json', 'application/x-json'], true)) {
return new Response\JsonResponse([ return new Response\JsonResponse([
'error' => 'NOT_FOUND', 'error' => 'NOT_FOUND',
'message' => 'Not found', 'message' => 'Not found',

View File

@ -14,7 +14,7 @@ trait TagManagerTrait
* @param string[] $tags * @param string[] $tags
* @return Collections\Collection|Tag[] * @return Collections\Collection|Tag[]
*/ */
protected function tagNamesToEntities(EntityManagerInterface $em, array $tags) private function tagNamesToEntities(EntityManagerInterface $em, array $tags)
{ {
$entities = []; $entities = [];
foreach ($tags as $tagName) { foreach ($tags as $tagName) {
@ -33,8 +33,8 @@ trait TagManagerTrait
* @param string $tagName * @param string $tagName
* @return string * @return string
*/ */
protected function normalizeTagName($tagName) private function normalizeTagName($tagName): string
{ {
return str_replace(' ', '-', strtolower(trim($tagName))); return \str_replace(' ', '-', \strtolower(\trim($tagName)));
} }
} }

View File

@ -9,7 +9,7 @@ use Zend\InputFilter\Input;
trait InputFactoryTrait trait InputFactoryTrait
{ {
public function createInput($name, $required = true): Input private function createInput($name, $required = true): Input
{ {
$input = new Input($name); $input = new Input($name);
$input->setRequired($required) $input->setRequired($required)

View File

@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Response\PixelResponse;
use Shlinkio\Shlink\Core\Action\PixelAction;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Options\AppOptions;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Core\Service\VisitsTracker;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory;
class PixelActionTest extends TestCase
{
/**
* @var RedirectAction
*/
protected $action;
/**
* @var ObjectProphecy
*/
protected $urlShortener;
/**
* @var ObjectProphecy
*/
protected $visitTracker;
public function setUp()
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$this->visitTracker = $this->prophesize(VisitsTracker::class);
$this->action = new PixelAction(
$this->urlShortener->reveal(),
$this->visitTracker->reveal(),
new AppOptions()
);
}
/**
* @test
*/
public function imageIsReturned()
{
$shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn('http://domain.com/foo/bar')
->shouldBeCalledTimes(1);
$this->visitTracker->track(Argument::cetera())->willReturn(null)
->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertInstanceOf(PixelResponse::class, $response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('image/gif', $response->getHeaderLine('content-type'));
}
}

View File

@ -3,11 +3,11 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action; namespace ShlinkioTest\Shlink\Core\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Common\Service\PreviewGenerator; use Shlinkio\Shlink\Common\Service\PreviewGenerator;
use Shlinkio\Shlink\Core\Action\PreviewAction; use Shlinkio\Shlink\Core\Action\PreviewAction;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
@ -47,8 +47,8 @@ class PreviewActionTest extends TestCase
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class) $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$delegate->process(Argument::cetera())->shouldBeCalledTimes(1) $delegate->handle(Argument::cetera())->shouldBeCalledTimes(1)
->willReturn(new Response()); ->willReturn(new Response());
$this->action->process( $this->action->process(
@ -70,7 +70,7 @@ class PreviewActionTest extends TestCase
$resp = $this->action->process( $resp = $this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
TestUtils::createDelegateMock()->reveal() TestUtils::createReqHandlerMock()->reveal()
); );
$this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length')); $this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
@ -85,9 +85,9 @@ class PreviewActionTest extends TestCase
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process(Argument::any())->willReturn(new Response()); $process = $delegate->handle(Argument::any())->willReturn(new Response());
$this->action->process( $this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),

View File

@ -3,11 +3,11 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action; namespace ShlinkioTest\Shlink\Core\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Common\Response\QrCodeResponse; use Shlinkio\Shlink\Common\Response\QrCodeResponse;
use Shlinkio\Shlink\Core\Action\QrCodeAction; use Shlinkio\Shlink\Core\Action\QrCodeAction;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
@ -46,8 +46,8 @@ class QrCodeActionTest extends TestCase
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class) $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->process(Argument::any())->willReturn(new Response()); $process = $delegate->handle(Argument::any())->willReturn(new Response());
$this->action->process( $this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
@ -65,9 +65,9 @@ class QrCodeActionTest extends TestCase
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process(Argument::any())->willReturn(new Response()); $process = $delegate->handle(Argument::any())->willReturn(new Response());
$this->action->process( $this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
@ -84,7 +84,7 @@ class QrCodeActionTest extends TestCase
{ {
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn('')->shouldBeCalledTimes(1); $this->urlShortener->shortCodeToUrl($shortCode)->willReturn('')->shouldBeCalledTimes(1);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$resp = $this->action->process( $resp = $this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
@ -93,6 +93,6 @@ class QrCodeActionTest extends TestCase
$this->assertInstanceOf(QrCodeResponse::class, $resp); $this->assertInstanceOf(QrCodeResponse::class, $resp);
$this->assertEquals(200, $resp->getStatusCode()); $this->assertEquals(200, $resp->getStatusCode());
$delegate->process(Argument::any())->shouldHaveBeenCalledTimes(0); $delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
} }
} }

View File

@ -3,11 +3,11 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Action; namespace ShlinkioTest\Shlink\Core\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction; use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Options\AppOptions; use Shlinkio\Shlink\Core\Options\AppOptions;
@ -57,7 +57,7 @@ class RedirectActionTest extends TestCase
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertInstanceOf(Response\RedirectResponse::class, $response); $this->assertInstanceOf(Response\RedirectResponse::class, $response);
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());
@ -76,9 +76,9 @@ class RedirectActionTest extends TestCase
$this->visitTracker->track(Argument::cetera())->willReturn(null) $this->visitTracker->track(Argument::cetera())->willReturn(null)
->shouldNotBeCalled(); ->shouldNotBeCalled();
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process(Argument::any())->willReturn(new Response()); $process = $delegate->handle(Argument::any())->willReturn(new Response());
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$this->action->process($request, $delegate->reveal()); $this->action->process($request, $delegate->reveal());
@ -100,7 +100,7 @@ class RedirectActionTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode) $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
->withQueryParams(['foobar' => true]); ->withQueryParams(['foobar' => true]);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertInstanceOf(Response\RedirectResponse::class, $response); $this->assertInstanceOf(Response\RedirectResponse::class, $response);
$this->assertEquals(302, $response->getStatusCode()); $this->assertEquals(302, $response->getStatusCode());

View File

@ -5,9 +5,9 @@ namespace ShlinkioTest\Shlink\Core\Middleware;
use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache; use Doctrine\Common\Cache\Cache;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Middleware\QrCodeCacheMiddleware; use Shlinkio\Shlink\Core\Middleware\QrCodeCacheMiddleware;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
@ -35,8 +35,8 @@ class QrCodeCacheMiddlewareTest extends TestCase
*/ */
public function noCachedPathFallsBackToNextMiddleware() public function noCachedPathFallsBackToNextMiddleware()
{ {
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$delegate->process(Argument::any())->willReturn(new Response())->shouldBeCalledTimes(1); $delegate->handle(Argument::any())->willReturn(new Response())->shouldBeCalledTimes(1);
$this->middleware->process(ServerRequestFactory::fromGlobals()->withUri( $this->middleware->process(ServerRequestFactory::fromGlobals()->withUri(
new Uri('/foo/bar') new Uri('/foo/bar')
@ -53,7 +53,7 @@ class QrCodeCacheMiddlewareTest extends TestCase
$isCalled = false; $isCalled = false;
$uri = (new Uri())->withPath('/foo'); $uri = (new Uri())->withPath('/foo');
$this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']); $this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$resp = $this->middleware->process( $resp = $this->middleware->process(
ServerRequestFactory::fromGlobals()->withUri($uri), ServerRequestFactory::fromGlobals()->withUri($uri),
@ -64,6 +64,6 @@ class QrCodeCacheMiddlewareTest extends TestCase
$resp->getBody()->rewind(); $resp->getBody()->rewind();
$this->assertEquals('the body', $resp->getBody()->getContents()); $this->assertEquals('the body', $resp->getBody()->getContents());
$this->assertEquals('image/png', $resp->getHeaderLine('Content-Type')); $this->assertEquals('image/png', $resp->getHeaderLine('Content-Type'));
$delegate->process(Argument::any())->shouldHaveBeenCalledTimes(0); $delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
} }
} }

View File

@ -43,7 +43,7 @@ class NotFoundDelegateTest extends TestCase
/** @var MethodProphecy $render */ /** @var MethodProphecy $render */
$render = $this->renderer->render(Argument::cetera())->willReturn(''); $render = $this->renderer->render(Argument::cetera())->willReturn('');
$resp = $this->delegate->process($request); $resp = $this->delegate->handle($request);
$this->assertInstanceOf($expectedResponse, $resp); $this->assertInstanceOf($expectedResponse, $resp);
$render->shouldHaveBeenCalledTimes($renderCalls); $render->shouldHaveBeenCalledTimes($renderCalls);

View File

@ -5,11 +5,11 @@ namespace Shlinkio\Shlink\Rest\Action;
use Fig\Http\Message\RequestMethodInterface; use Fig\Http\Message\RequestMethodInterface;
use Fig\Http\Message\StatusCodeInterface; use Fig\Http\Message\StatusCodeInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
abstract class AbstractRestAction implements MiddlewareInterface, RequestMethodInterface, StatusCodeInterface abstract class AbstractRestAction implements RequestHandlerInterface, RequestMethodInterface, StatusCodeInterface
{ {
/** /**
* @var LoggerInterface * @var LoggerInterface

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -43,11 +42,10 @@ class AuthenticateAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @return Response
* @return null|Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function handle(Request $request): Response
{ {
$authData = $request->getParsedBody(); $authData = $request->getParsedBody();
if (! isset($authData['apiKey'])) { if (! isset($authData['apiKey'])) {
@ -61,7 +59,7 @@ class AuthenticateAction extends AbstractRestAction
// Authenticate using provided API key // Authenticate using provided API key
$apiKey = $this->apiKeyService->getByKey($authData['apiKey']); $apiKey = $this->apiKeyService->getByKey($authData['apiKey']);
if (! isset($apiKey) || ! $apiKey->isValid()) { if ($apiKey === null || ! $apiKey->isValid()) {
return new JsonResponse([ return new JsonResponse([
'error' => RestUtils::INVALID_API_KEY_ERROR, 'error' => RestUtils::INVALID_API_KEY_ERROR,
'message' => $this->translator->translate('Provided API key does not exist or is invalid.'), 'message' => $this->translator->translate('Provided API key does not exist or is invalid.'),

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -44,11 +43,10 @@ class CreateShortcodeAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @return Response
* @return null|Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function handle(Request $request): Response
{ {
$postData = (array) $request->getParsedBody(); $postData = (array) $request->getParsedBody();
if (! isset($postData['longUrl'])) { if (! isset($postData['longUrl'])) {

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -41,12 +40,11 @@ class EditShortCodeAction extends AbstractRestAction
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* *
* @return ResponseInterface * @return ResponseInterface
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface public function handle(ServerRequestInterface $request): ResponseInterface
{ {
$postData = (array) $request->getParsedBody(); $postData = (array) $request->getParsedBody();
$shortCode = $request->getAttribute('shortCode', ''); $shortCode = $request->getAttribute('shortCode', '');

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -36,11 +35,10 @@ class EditShortcodeTagsAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @return Response
* @return null|Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function handle(Request $request): Response
{ {
$shortCode = $request->getAttribute('shortCode'); $shortCode = $request->getAttribute('shortCode');
$bodyParams = $request->getParsedBody(); $bodyParams = $request->getParsedBody();

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -37,11 +36,10 @@ class GetVisitsAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @return Response
* @return null|Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function handle(Request $request): Response
{ {
$shortCode = $request->getAttribute('shortCode'); $shortCode = $request->getAttribute('shortCode');
$startDate = $this->getDateQueryParam($request, 'startDate'); $startDate = $this->getDateQueryParam($request, 'startDate');

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -38,11 +37,10 @@ class ListShortcodesAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @return Response
* @return null|Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function handle(Request $request): Response
{ {
try { try {
$params = $this->queryToListParams($request->getQueryParams()); $params = $this->queryToListParams($request->getQueryParams());
@ -61,13 +59,13 @@ class ListShortcodesAction extends AbstractRestAction
* @param array $query * @param array $query
* @return array * @return array
*/ */
public function queryToListParams(array $query) private function queryToListParams(array $query): array
{ {
return [ return [
isset($query['page']) ? $query['page'] : 1, $query['page'] ?? 1,
isset($query['searchTerm']) ? $query['searchTerm'] : null, $query['searchTerm'] ?? null,
isset($query['tags']) ? $query['tags'] : [], $query['tags'] ?? [],
isset($query['orderBy']) ? $query['orderBy'] : null, $query['orderBy'] ?? null,
]; ];
} }
} }

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action; namespace Shlinkio\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -37,11 +36,10 @@ class ResolveUrlAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @return Response
* @return null|Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function handle(Request $request): Response
{ {
$shortCode = $request->getAttribute('shortCode'); $shortCode = $request->getAttribute('shortCode');

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Tag; namespace Shlinkio\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -29,15 +28,14 @@ class CreateTagsAction extends AbstractRestAction
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* *
* @return ResponseInterface * @return ResponseInterface
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(ServerRequestInterface $request, DelegateInterface $delegate) public function handle(ServerRequestInterface $request): ResponseInterface
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
$tags = isset($body['tags']) ? $body['tags'] : []; $tags = $body['tags'] ?? [];
return new JsonResponse([ return new JsonResponse([
'tags' => [ 'tags' => [

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Tag; namespace Shlinkio\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -29,14 +28,13 @@ class DeleteTagsAction extends AbstractRestAction
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* *
* @return ResponseInterface * @return ResponseInterface
*/ */
public function process(ServerRequestInterface $request, DelegateInterface $delegate) public function handle(ServerRequestInterface $request): ResponseInterface
{ {
$query = $request->getQueryParams(); $query = $request->getQueryParams();
$tags = isset($query['tags']) ? $query['tags'] : []; $tags = $query['tags'] ?? [];
$this->tagService->deleteTags($tags); $this->tagService->deleteTags($tags);
return new EmptyResponse(); return new EmptyResponse();

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Tag; namespace Shlinkio\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -29,12 +28,11 @@ class ListTagsAction extends AbstractRestAction
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* *
* @return ResponseInterface * @return ResponseInterface
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(ServerRequestInterface $request, DelegateInterface $delegate) public function handle(ServerRequestInterface $request): ResponseInterface
{ {
return new JsonResponse([ return new JsonResponse([
'tags' => [ 'tags' => [

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Tag; namespace Shlinkio\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -41,12 +40,11 @@ class UpdateTagAction extends AbstractRestAction
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* *
* @return ResponseInterface * @return ResponseInterface
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(ServerRequestInterface $request, DelegateInterface $delegate) public function handle(ServerRequestInterface $request): ResponseInterface
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
if (! isset($body['oldName'], $body['newName'])) { if (! isset($body['oldName'], $body['newName'])) {

View File

@ -92,7 +92,7 @@ class JWTService implements JWTServiceInterface
* @param array $data * @param array $data
* @return string * @return string
*/ */
protected function encode(array $data): string private function encode(array $data): string
{ {
return JWT::encode($data, $this->appOptions->getSecretKey(), self::DEFAULT_ENCRYPTION_ALG); return JWT::encode($data, $this->appOptions->getSecretKey(), self::DEFAULT_ENCRYPTION_ALG);
} }
@ -101,7 +101,7 @@ class JWTService implements JWTServiceInterface
* @param string $jwt * @param string $jwt
* @return array * @return array
*/ */
protected function decode(string $jwt): array private function decode(string $jwt): array
{ {
return (array) JWT::decode($jwt, $this->appOptions->getSecretKey(), [self::DEFAULT_ENCRYPTION_ALG]); return (array) JWT::decode($jwt, $this->appOptions->getSecretKey(), [self::DEFAULT_ENCRYPTION_ALG]);
} }

View File

@ -44,7 +44,7 @@ class ApiKey extends AbstractEntity
/** /**
* @return string * @return string
*/ */
public function getKey() public function getKey(): string
{ {
return $this->key; return $this->key;
} }
@ -53,7 +53,7 @@ class ApiKey extends AbstractEntity
* @param string $key * @param string $key
* @return $this * @return $this
*/ */
public function setKey($key) public function setKey($key): self
{ {
$this->key = $key; $this->key = $key;
return $this; return $this;
@ -62,7 +62,7 @@ class ApiKey extends AbstractEntity
/** /**
* @return \DateTime|null * @return \DateTime|null
*/ */
public function getExpirationDate() public function getExpirationDate(): ?\DateTime
{ {
return $this->expirationDate; return $this->expirationDate;
} }
@ -71,7 +71,7 @@ class ApiKey extends AbstractEntity
* @param \DateTime $expirationDate * @param \DateTime $expirationDate
* @return $this * @return $this
*/ */
public function setExpirationDate($expirationDate) public function setExpirationDate($expirationDate): self
{ {
$this->expirationDate = $expirationDate; $this->expirationDate = $expirationDate;
return $this; return $this;
@ -80,9 +80,9 @@ class ApiKey extends AbstractEntity
/** /**
* @return bool * @return bool
*/ */
public function isExpired() public function isExpired(): bool
{ {
if (! isset($this->expirationDate)) { if ($this->expirationDate === null) {
return false; return false;
} }
@ -92,7 +92,7 @@ class ApiKey extends AbstractEntity
/** /**
* @return boolean * @return boolean
*/ */
public function isEnabled() public function isEnabled(): bool
{ {
return $this->enabled; return $this->enabled;
} }
@ -101,7 +101,7 @@ class ApiKey extends AbstractEntity
* @param boolean $enabled * @param boolean $enabled
* @return $this * @return $this
*/ */
public function setEnabled($enabled) public function setEnabled($enabled): self
{ {
$this->enabled = $enabled; $this->enabled = $enabled;
return $this; return $this;
@ -112,7 +112,7 @@ class ApiKey extends AbstractEntity
* *
* @return $this * @return $this
*/ */
public function disable() public function disable(): self
{ {
return $this->setEnabled(false); return $this->setEnabled(false);
} }
@ -122,17 +122,17 @@ class ApiKey extends AbstractEntity
* *
* @return bool * @return bool
*/ */
public function isValid() public function isValid(): bool
{ {
return $this->isEnabled() && ! $this->isExpired(); return $this->isEnabled() && ! $this->isExpired();
} }
/** /**
* The string repesentation of an API key is the key itself * The string representation of an API key is the key itself
* *
* @return string * @return string
*/ */
public function __toString() public function __toString(): string
{ {
return $this->getKey(); return $this->getKey();
} }

View File

@ -20,7 +20,7 @@ class JsonErrorResponseGenerator implements ErrorResponseGeneratorInterface, Sta
* @return Response * @return Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function __invoke($e, Request $request, Response $response) public function __invoke(?\Throwable $e, Request $request, Response $response)
{ {
$status = $response->getStatusCode(); $status = $response->getStatusCode();
$responsePhrase = $status < 400 ? 'Internal Server Error' : $response->getReasonPhrase(); $responsePhrase = $status < 400 ? 'Internal Server Error' : $response->getReasonPhrase();
@ -32,8 +32,8 @@ class JsonErrorResponseGenerator implements ErrorResponseGeneratorInterface, Sta
], $status); ], $status);
} }
protected function responsePhraseToCode(string $responsePhrase): string private function responsePhraseToCode(string $responsePhrase): string
{ {
return strtoupper(str_replace(' ', '_', $responsePhrase)); return \strtoupper(\str_replace(' ', '_', $responsePhrase));
} }
} }

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware; namespace Shlinkio\Shlink\Rest\Middleware;
use Fig\Http\Message\RequestMethodInterface; use Fig\Http\Message\RequestMethodInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Exception\RuntimeException; use Shlinkio\Shlink\Rest\Exception\RuntimeException;
class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface
@ -17,11 +17,11 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @param RequestHandlerInterface $handler
* *
* @return Response * @return Response
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, RequestHandlerInterface $handler): Response
{ {
$method = $request->getMethod(); $method = $request->getMethod();
$currentParams = $request->getParsedBody(); $currentParams = $request->getParsedBody();
@ -32,16 +32,16 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
self::METHOD_HEAD, self::METHOD_HEAD,
self::METHOD_OPTIONS, self::METHOD_OPTIONS,
], true)) { ], true)) {
return $delegate->process($request); return $handler->handle($request);
} }
// If the accepted content is JSON, try to parse the body from JSON // If the accepted content is JSON, try to parse the body from JSON
$contentType = $this->getRequestContentType($request); $contentType = $this->getRequestContentType($request);
if (\in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) { if (\in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) {
return $delegate->process($this->parseFromJson($request)); return $handler->handle($this->parseFromJson($request));
} }
return $delegate->process($this->parseFromUrlEncoded($request)); return $handler->handle($this->parseFromUrlEncoded($request));
} }
/** /**

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware; namespace Shlinkio\Shlink\Rest\Middleware;
use Fig\Http\Message\StatusCodeInterface; use Fig\Http\Message\StatusCodeInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
use Shlinkio\Shlink\Rest\Action\AuthenticateAction; use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
@ -21,7 +21,7 @@ use Zend\Stdlib\ErrorHandler;
class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface
{ {
const AUTHORIZATION_HEADER = 'Authorization'; public const AUTHORIZATION_HEADER = 'Authorization';
/** /**
* @var TranslatorInterface * @var TranslatorInterface
@ -51,13 +51,13 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @param RequestHandlerInterface $handler
* *
* @return Response * @return Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws \ErrorException * @throws \ErrorException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, RequestHandlerInterface $handler): Response
{ {
// If current route is the authenticate route or an OPTIONS request, continue to the next middleware // If current route is the authenticate route or an OPTIONS request, continue to the next middleware
/** @var RouteResult|null $routeResult */ /** @var RouteResult|null $routeResult */
@ -67,7 +67,7 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
|| $routeResult->getMatchedRouteName() === AuthenticateAction::class || $routeResult->getMatchedRouteName() === AuthenticateAction::class
|| $request->getMethod() === 'OPTIONS' || $request->getMethod() === 'OPTIONS'
) { ) {
return $delegate->process($request); return $handler->handle($request);
} }
// Check that the auth header was provided, and that it belongs to a non-expired token // Check that the auth header was provided, and that it belongs to a non-expired token
@ -77,22 +77,22 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
// Get token making sure the an authorization type is provided // Get token making sure the an authorization type is provided
$authToken = $request->getHeaderLine(self::AUTHORIZATION_HEADER); $authToken = $request->getHeaderLine(self::AUTHORIZATION_HEADER);
$authTokenParts = explode(' ', $authToken); $authTokenParts = \explode(' ', $authToken);
if (count($authTokenParts) === 1) { if (\count($authTokenParts) === 1) {
return new JsonResponse([ return new JsonResponse([
'error' => RestUtils::INVALID_AUTHORIZATION_ERROR, 'error' => RestUtils::INVALID_AUTHORIZATION_ERROR,
'message' => sprintf($this->translator->translate( 'message' => \sprintf($this->translator->translate(
'You need to provide the Bearer type in the %s header.' 'You need to provide the Bearer type in the %s header.'
), self::AUTHORIZATION_HEADER), ), self::AUTHORIZATION_HEADER),
], self::STATUS_UNAUTHORIZED); ], self::STATUS_UNAUTHORIZED);
} }
// Make sure the authorization type is Bearer // Make sure the authorization type is Bearer
list($authType, $jwt) = $authTokenParts; [$authType, $jwt] = $authTokenParts;
if (strtolower($authType) !== 'bearer') { if (\strtolower($authType) !== 'bearer') {
return new JsonResponse([ return new JsonResponse([
'error' => RestUtils::INVALID_AUTHORIZATION_ERROR, 'error' => RestUtils::INVALID_AUTHORIZATION_ERROR,
'message' => sprintf($this->translator->translate( 'message' => \sprintf($this->translator->translate(
'Provided authorization type %s is not supported. Use Bearer instead.' 'Provided authorization type %s is not supported. Use Bearer instead.'
), $authType), ), $authType),
], self::STATUS_UNAUTHORIZED); ], self::STATUS_UNAUTHORIZED);
@ -107,7 +107,7 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
// Update the token expiration and continue to next middleware // Update the token expiration and continue to next middleware
$jwt = $this->jwtService->refresh($jwt); $jwt = $this->jwtService->refresh($jwt);
$response = $delegate->process($request); $response = $handler->handle($request);
// Return the response with the updated token on it // Return the response with the updated token on it
return $response->withHeader(self::AUTHORIZATION_HEADER, 'Bearer ' . $jwt); return $response->withHeader(self::AUTHORIZATION_HEADER, 'Bearer ' . $jwt);
@ -119,11 +119,15 @@ class CheckAuthenticationMiddleware implements MiddlewareInterface, StatusCodeIn
} }
} }
protected function createTokenErrorResponse() /**
* @return JsonResponse
* @throws \InvalidArgumentException
*/
private function createTokenErrorResponse(): JsonResponse
{ {
return new JsonResponse([ return new JsonResponse([
'error' => RestUtils::INVALID_AUTH_TOKEN_ERROR, 'error' => RestUtils::INVALID_AUTH_TOKEN_ERROR,
'message' => sprintf( 'message' => \sprintf(
$this->translator->translate( $this->translator->translate(
'Missing or invalid auth token provided. Perform a new authentication request and send provided ' 'Missing or invalid auth token provided. Perform a new authentication request and send provided '
. 'token on every new request on the "%s" header' . 'token on every new request on the "%s" header'

View File

@ -4,10 +4,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware; namespace Shlinkio\Shlink\Rest\Middleware;
use Fig\Http\Message\RequestMethodInterface; use Fig\Http\Message\RequestMethodInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class CrossDomainMiddleware implements MiddlewareInterface, RequestMethodInterface class CrossDomainMiddleware implements MiddlewareInterface, RequestMethodInterface
{ {
@ -16,15 +16,15 @@ class CrossDomainMiddleware implements MiddlewareInterface, RequestMethodInterfa
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @param RequestHandlerInterface $handler
* *
* @return Response * @return Response
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, RequestHandlerInterface $handler): Response
{ {
/** @var Response $response */ /** @var Response $response */
$response = $delegate->process($request); $response = $handler->handle($request);
if (! $request->hasHeader('Origin')) { if (! $request->hasHeader('Origin')) {
return $response; return $response;
} }

View File

@ -3,10 +3,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware; namespace Shlinkio\Shlink\Rest\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class PathVersionMiddleware implements MiddlewareInterface class PathVersionMiddleware implements MiddlewareInterface
{ {
@ -15,18 +15,18 @@ class PathVersionMiddleware implements MiddlewareInterface
* to the next middleware component to create the response. * to the next middleware component to create the response.
* *
* @param Request $request * @param Request $request
* @param DelegateInterface $delegate * @param RequestHandlerInterface $handler
* *
* @return Response * @return Response
*/ */
public function process(Request $request, DelegateInterface $delegate) public function process(Request $request, RequestHandlerInterface $handler): Response
{ {
$uri = $request->getUri(); $uri = $request->getUri();
$path = $uri->getPath(); $path = $uri->getPath();
// TODO Workaround... Do not process the request if it does not start with rest // TODO Workaround... Do not process the request if it does not start with rest
if (\strpos($path, '/rest') !== 0) { if (\strpos($path, '/rest') !== 0) {
return $delegate->process($request); return $handler->handle($request);
} }
// If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes // If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes
@ -41,6 +41,6 @@ class PathVersionMiddleware implements MiddlewareInterface
$request = $request->withUri($uri->withPath(\implode('/', $parts))); $request = $request->withUri($uri->withPath(\implode('/', $parts)));
} }
return $delegate->process($request); return $handler->handle($request);
} }
} }

View File

@ -9,16 +9,16 @@ use Shlinkio\Shlink\Rest\Exception as Rest;
class RestUtils class RestUtils
{ {
const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE'; public const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE';
const INVALID_URL_ERROR = 'INVALID_URL'; public const INVALID_URL_ERROR = 'INVALID_URL';
const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT'; public const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT';
const INVALID_SLUG_ERROR = 'INVALID_SLUG'; public const INVALID_SLUG_ERROR = 'INVALID_SLUG';
const INVALID_CREDENTIALS_ERROR = 'INVALID_CREDENTIALS'; public const INVALID_CREDENTIALS_ERROR = 'INVALID_CREDENTIALS';
const INVALID_AUTH_TOKEN_ERROR = 'INVALID_AUTH_TOKEN'; public const INVALID_AUTH_TOKEN_ERROR = 'INVALID_AUTH_TOKEN';
const INVALID_AUTHORIZATION_ERROR = 'INVALID_AUTHORIZATION'; public const INVALID_AUTHORIZATION_ERROR = 'INVALID_AUTHORIZATION';
const INVALID_API_KEY_ERROR = 'INVALID_API_KEY'; public const INVALID_API_KEY_ERROR = 'INVALID_API_KEY';
const NOT_FOUND_ERROR = 'NOT_FOUND'; public const NOT_FOUND_ERROR = 'NOT_FOUND';
const UNKNOWN_ERROR = 'UNKNOWN_ERROR'; public const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
public static function getRestErrorCodeFromException(\Throwable $e) public static function getRestErrorCodeFromException(\Throwable $e)
{ {

View File

@ -10,7 +10,6 @@ use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
use Shlinkio\Shlink\Rest\Authentication\JWTService; use Shlinkio\Shlink\Rest\Authentication\JWTService;
use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyService; use Shlinkio\Shlink\Rest\Service\ApiKeyService;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
@ -47,7 +46,7 @@ class AuthenticateActionTest extends TestCase
*/ */
public function notProvidingAuthDataReturnsError() public function notProvidingAuthDataReturnsError()
{ {
$resp = $this->action->process(ServerRequestFactory::fromGlobals(), TestUtils::createDelegateMock()->reveal()); $resp = $this->action->handle(ServerRequestFactory::fromGlobals());
$this->assertEquals(400, $resp->getStatusCode()); $this->assertEquals(400, $resp->getStatusCode());
} }
@ -62,7 +61,7 @@ class AuthenticateActionTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ $request = ServerRequestFactory::fromGlobals()->withParsedBody([
'apiKey' => 'foo', 'apiKey' => 'foo',
]); ]);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$response->getBody()->rewind(); $response->getBody()->rewind();
@ -80,7 +79,7 @@ class AuthenticateActionTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ $request = ServerRequestFactory::fromGlobals()->withParsedBody([
'apiKey' => 'foo', 'apiKey' => 'foo',
]); ]);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(401, $response->getStatusCode()); $this->assertEquals(401, $response->getStatusCode());
} }
} }

View File

@ -11,7 +11,6 @@ use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Rest\Action\CreateShortcodeAction; use Shlinkio\Shlink\Rest\Action\CreateShortcodeAction;
use Shlinkio\Shlink\Rest\Util\RestUtils; use Shlinkio\Shlink\Rest\Util\RestUtils;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
@ -41,10 +40,7 @@ class CreateShortcodeActionTest extends TestCase
*/ */
public function missingLongUrlParamReturnsError() public function missingLongUrlParamReturnsError()
{ {
$response = $this->action->process( $response = $this->action->handle(ServerRequestFactory::fromGlobals());
ServerRequestFactory::fromGlobals(),
TestUtils::createDelegateMock()->reveal()
);
$this->assertEquals(400, $response->getStatusCode()); $this->assertEquals(400, $response->getStatusCode());
} }
@ -60,7 +56,7 @@ class CreateShortcodeActionTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ $request = ServerRequestFactory::fromGlobals()->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar', 'longUrl' => 'http://www.domain.com/foo/bar',
]); ]);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), 'http://foo.com/abc123') > 0); $this->assertTrue(strpos($response->getBody()->getContents(), 'http://foo.com/abc123') > 0);
} }
@ -77,7 +73,7 @@ class CreateShortcodeActionTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ $request = ServerRequestFactory::fromGlobals()->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar', 'longUrl' => 'http://www.domain.com/foo/bar',
]); ]);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(400, $response->getStatusCode()); $this->assertEquals(400, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_URL_ERROR) > 0); $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_URL_ERROR) > 0);
} }
@ -100,7 +96,7 @@ class CreateShortcodeActionTest extends TestCase
'longUrl' => 'http://www.domain.com/foo/bar', 'longUrl' => 'http://www.domain.com/foo/bar',
'customSlug' => 'foo', 'customSlug' => 'foo',
]); ]);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(400, $response->getStatusCode()); $this->assertEquals(400, $response->getStatusCode());
$this->assertContains(RestUtils::INVALID_SLUG_ERROR, (string) $response->getBody()); $this->assertContains(RestUtils::INVALID_SLUG_ERROR, (string) $response->getBody());
} }
@ -117,7 +113,7 @@ class CreateShortcodeActionTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withParsedBody([ $request = ServerRequestFactory::fromGlobals()->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar', 'longUrl' => 'http://www.domain.com/foo/bar',
]); ]);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(500, $response->getStatusCode()); $this->assertEquals(500, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0); $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
} }

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action; namespace ShlinkioTest\Shlink\Rest\Action;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
@ -43,7 +42,7 @@ class EditShortCodeActionTest extends TestCase
]); ]);
/** @var JsonResponse $resp */ /** @var JsonResponse $resp */
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $resp = $this->action->handle($request);
$payload = $resp->getPayload(); $payload = $resp->getPayload();
$this->assertEquals(400, $resp->getStatusCode()); $this->assertEquals(400, $resp->getStatusCode());
@ -65,7 +64,7 @@ class EditShortCodeActionTest extends TestCase
); );
/** @var JsonResponse $resp */ /** @var JsonResponse $resp */
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $resp = $this->action->handle($request);
$payload = $resp->getPayload(); $payload = $resp->getPayload();
$this->assertEquals(404, $resp->getStatusCode()); $this->assertEquals(404, $resp->getStatusCode());
@ -85,7 +84,7 @@ class EditShortCodeActionTest extends TestCase
]); ]);
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(new ShortUrl()); $updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(new ShortUrl());
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $resp = $this->action->handle($request);
$this->assertEquals(204, $resp->getStatusCode()); $this->assertEquals(204, $resp->getStatusCode());
$updateMeta->shouldHaveBeenCalled(); $updateMeta->shouldHaveBeenCalled();

View File

@ -9,7 +9,6 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\EditShortcodeTagsAction; use Shlinkio\Shlink\Rest\Action\EditShortcodeTagsAction;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
@ -35,10 +34,7 @@ class EditShortcodeTagsActionTest extends TestCase
*/ */
public function notProvidingTagsReturnsError() public function notProvidingTagsReturnsError()
{ {
$response = $this->action->process( $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123'));
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123'),
TestUtils::createDelegateMock()->reveal()
);
$this->assertEquals(400, $response->getStatusCode()); $this->assertEquals(400, $response->getStatusCode());
} }
@ -51,10 +47,9 @@ class EditShortcodeTagsActionTest extends TestCase
$this->shortUrlService->setTagsByShortCode($shortCode, [])->willThrow(InvalidShortCodeException::class) $this->shortUrlService->setTagsByShortCode($shortCode, [])->willThrow(InvalidShortCodeException::class)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123') ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
->withParsedBody(['tags' => []]), ->withParsedBody(['tags' => []])
TestUtils::createDelegateMock()->reveal()
); );
$this->assertEquals(404, $response->getStatusCode()); $this->assertEquals(404, $response->getStatusCode());
} }
@ -68,10 +63,9 @@ class EditShortcodeTagsActionTest extends TestCase
$this->shortUrlService->setTagsByShortCode($shortCode, [])->willReturn(new ShortUrl()) $this->shortUrlService->setTagsByShortCode($shortCode, [])->willReturn(new ShortUrl())
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123') ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
->withParsedBody(['tags' => []]), ->withParsedBody(['tags' => []])
TestUtils::createDelegateMock()->reveal()
); );
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }

View File

@ -10,7 +10,6 @@ use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Service\VisitsTracker; use Shlinkio\Shlink\Core\Service\VisitsTracker;
use Shlinkio\Shlink\Rest\Action\GetVisitsAction; use Shlinkio\Shlink\Rest\Action\GetVisitsAction;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
@ -40,10 +39,7 @@ class GetVisitsActionTest extends TestCase
$this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willReturn([]) $this->visitsTracker->info($shortCode, Argument::type(DateRange::class))->willReturn([])
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
TestUtils::createDelegateMock()->reveal()
);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -57,10 +53,7 @@ class GetVisitsActionTest extends TestCase
InvalidArgumentException::class InvalidArgumentException::class
)->shouldBeCalledTimes(1); )->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
TestUtils::createDelegateMock()->reveal()
);
$this->assertEquals(404, $response->getStatusCode()); $this->assertEquals(404, $response->getStatusCode());
} }
@ -74,10 +67,7 @@ class GetVisitsActionTest extends TestCase
\Exception::class \Exception::class
)->shouldBeCalledTimes(1); )->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
TestUtils::createDelegateMock()->reveal()
);
$this->assertEquals(500, $response->getStatusCode()); $this->assertEquals(500, $response->getStatusCode());
} }
@ -91,10 +81,9 @@ class GetVisitsActionTest extends TestCase
->willReturn([]) ->willReturn([])
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode) ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
->withQueryParams(['endDate' => '2016-01-01 00:00:00']), ->withQueryParams(['endDate' => '2016-01-01 00:00:00'])
TestUtils::createDelegateMock()->reveal()
); );
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }

View File

@ -7,7 +7,6 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Service\ShortUrlService; use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ListShortcodesAction; use Shlinkio\Shlink\Rest\Action\ListShortcodesAction;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
use Zend\Paginator\Adapter\ArrayAdapter; use Zend\Paginator\Adapter\ArrayAdapter;
@ -39,12 +38,9 @@ class ListShortCodesActionTest extends TestCase
$this->service->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter())) $this->service->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withQueryParams([
ServerRequestFactory::fromGlobals()->withQueryParams([
'page' => $page, 'page' => $page,
]), ]));
TestUtils::createDelegateMock()->reveal()
);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
} }
@ -57,12 +53,9 @@ class ListShortCodesActionTest extends TestCase
$this->service->listShortUrls($page, null, [], null)->willThrow(\Exception::class) $this->service->listShortUrls($page, null, [], null)->willThrow(\Exception::class)
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$response = $this->action->process( $response = $this->action->handle(ServerRequestFactory::fromGlobals()->withQueryParams([
ServerRequestFactory::fromGlobals()->withQueryParams([
'page' => $page, 'page' => $page,
]), ]));
TestUtils::createDelegateMock()->reveal()
);
$this->assertEquals(500, $response->getStatusCode()); $this->assertEquals(500, $response->getStatusCode());
} }
} }

View File

@ -10,7 +10,6 @@ use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortener; use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Rest\Action\ResolveUrlAction; use Shlinkio\Shlink\Rest\Action\ResolveUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils; use Shlinkio\Shlink\Rest\Util\RestUtils;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
@ -41,7 +40,7 @@ class ResolveUrlActionTest extends TestCase
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(404, $response->getStatusCode()); $this->assertEquals(404, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_ARGUMENT_ERROR) > 0); $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_ARGUMENT_ERROR) > 0);
} }
@ -56,7 +55,7 @@ class ResolveUrlActionTest extends TestCase
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), 'http://domain.com/foo/bar') > 0); $this->assertTrue(strpos($response->getBody()->getContents(), 'http://domain.com/foo/bar') > 0);
} }
@ -71,7 +70,7 @@ class ResolveUrlActionTest extends TestCase
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(400, $response->getStatusCode()); $this->assertEquals(400, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_SHORTCODE_ERROR) > 0); $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_SHORTCODE_ERROR) > 0);
} }
@ -86,7 +85,7 @@ class ResolveUrlActionTest extends TestCase
->shouldBeCalledTimes(1); ->shouldBeCalledTimes(1);
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode); $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->action->handle($request);
$this->assertEquals(500, $response->getStatusCode()); $this->assertEquals(500, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0); $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
} }

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Tag; namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
@ -40,7 +39,7 @@ class CreateTagsActionTest extends TestCase
/** @var MethodProphecy $deleteTags */ /** @var MethodProphecy $deleteTags */
$deleteTags = $this->tagService->createTags($tags ?: [])->willReturn(new ArrayCollection()); $deleteTags = $this->tagService->createTags($tags ?: [])->willReturn(new ArrayCollection());
$response = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $response = $this->action->handle($request);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$deleteTags->shouldHaveBeenCalled(); $deleteTags->shouldHaveBeenCalled();

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Tag; namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
@ -39,7 +38,7 @@ class DeleteTagsActionTest extends TestCase
/** @var MethodProphecy $deleteTags */ /** @var MethodProphecy $deleteTags */
$deleteTags = $this->tagService->deleteTags($tags ?: []); $deleteTags = $this->tagService->deleteTags($tags ?: []);
$response = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $response = $this->action->handle($request);
$this->assertEquals(204, $response->getStatusCode()); $this->assertEquals(204, $response->getStatusCode());
$deleteTags->shouldHaveBeenCalled(); $deleteTags->shouldHaveBeenCalled();

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Tag; namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
@ -37,10 +36,7 @@ class ListTagsActionTest extends TestCase
/** @var MethodProphecy $listTags */ /** @var MethodProphecy $listTags */
$listTags = $this->tagService->listTags()->willReturn([new Tag('foo'), new Tag('bar')]); $listTags = $this->tagService->listTags()->willReturn([new Tag('foo'), new Tag('bar')]);
$resp = $this->action->process( $resp = $this->action->handle(ServerRequestFactory::fromGlobals());
ServerRequestFactory::fromGlobals(),
$this->prophesize(DelegateInterface::class)->reveal()
);
$this->assertEquals([ $this->assertEquals([
'tags' => [ 'tags' => [

View File

@ -3,7 +3,6 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Action\Tag; namespace ShlinkioTest\Shlink\Rest\Action\Tag;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
@ -39,7 +38,7 @@ class UpdateTagActionTest extends TestCase
public function whenInvalidParamsAreProvidedAnErrorIsReturned(array $bodyParams) public function whenInvalidParamsAreProvidedAnErrorIsReturned(array $bodyParams)
{ {
$request = ServerRequestFactory::fromGlobals()->withParsedBody($bodyParams); $request = ServerRequestFactory::fromGlobals()->withParsedBody($bodyParams);
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $resp = $this->action->handle($request);
$this->assertEquals(400, $resp->getStatusCode()); $this->assertEquals(400, $resp->getStatusCode());
} }
@ -65,7 +64,7 @@ class UpdateTagActionTest extends TestCase
/** @var MethodProphecy $rename */ /** @var MethodProphecy $rename */
$rename = $this->tagService->renameTag('foo', 'bar')->willThrow(EntityDoesNotExistException::class); $rename = $this->tagService->renameTag('foo', 'bar')->willThrow(EntityDoesNotExistException::class);
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $resp = $this->action->handle($request);
$this->assertEquals(404, $resp->getStatusCode()); $this->assertEquals(404, $resp->getStatusCode());
$rename->shouldHaveBeenCalled(); $rename->shouldHaveBeenCalled();
@ -83,7 +82,7 @@ class UpdateTagActionTest extends TestCase
/** @var MethodProphecy $rename */ /** @var MethodProphecy $rename */
$rename = $this->tagService->renameTag('foo', 'bar')->willReturn(new Tag()); $rename = $this->tagService->renameTag('foo', 'bar')->willReturn(new Tag());
$resp = $this->action->process($request, $this->prophesize(DelegateInterface::class)->reveal()); $resp = $this->action->handle($request);
$this->assertEquals(204, $resp->getStatusCode()); $this->assertEquals(204, $resp->getStatusCode());
$rename->shouldHaveBeenCalled(); $rename->shouldHaveBeenCalled();

View File

@ -3,11 +3,11 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware; namespace ShlinkioTest\Shlink\Rest\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware; use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
@ -31,9 +31,9 @@ class BodyParserMiddlewareTest extends TestCase
public function requestsFromOtherMethodsJustFallbackToNextMiddleware() public function requestsFromOtherMethodsJustFallbackToNextMiddleware()
{ {
$request = ServerRequestFactory::fromGlobals()->withMethod('GET'); $request = ServerRequestFactory::fromGlobals()->withMethod('GET');
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal()); $this->middleware->process($request, $delegate->reveal());
@ -51,9 +51,9 @@ class BodyParserMiddlewareTest extends TestCase
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT') $request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body) ->withBody($body)
->withHeader('content-type', 'application/json'); ->withHeader('content-type', 'application/json');
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process(Argument::type(ServerRequestInterface::class))->will( $process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
function (array $args) use ($test) { function (array $args) use ($test) {
/** @var ServerRequestInterface $req */ /** @var ServerRequestInterface $req */
$req = array_shift($args); $req = array_shift($args);
@ -82,9 +82,9 @@ class BodyParserMiddlewareTest extends TestCase
$body->write('foo=bar&bar[]=one&bar[]=5'); $body->write('foo=bar&bar[]=one&bar[]=5');
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT') $request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body); ->withBody($body);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process(Argument::type(ServerRequestInterface::class))->will( $process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
function (array $args) use ($test) { function (array $args) use ($test) {
/** @var ServerRequestInterface $req */ /** @var ServerRequestInterface $req */
$req = array_shift($args); $req = array_shift($args);

View File

@ -3,10 +3,10 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware; namespace ShlinkioTest\Shlink\Rest\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy; use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Action\AuthenticateAction; use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
use Shlinkio\Shlink\Rest\Authentication\JWTService; use Shlinkio\Shlink\Rest\Authentication\JWTService;
use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware; use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
@ -16,7 +16,6 @@ use Zend\Diactoros\ServerRequestFactory;
use Zend\Expressive\Router\Route; use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult; use Zend\Expressive\Router\RouteResult;
use Zend\I18n\Translator\Translator; use Zend\I18n\Translator\Translator;
use function Zend\Stratigility\middleware; use function Zend\Stratigility\middleware;
class CheckAuthenticationMiddlewareTest extends TestCase class CheckAuthenticationMiddlewareTest extends TestCase
@ -50,9 +49,9 @@ class CheckAuthenticationMiddlewareTest extends TestCase
public function someWhiteListedSituationsFallbackToNextMiddleware() public function someWhiteListedSituationsFallbackToNextMiddleware()
{ {
$request = ServerRequestFactory::fromGlobals(); $request = ServerRequestFactory::fromGlobals();
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal()); $this->middleware->process($request, $delegate->reveal());
$process->shouldHaveBeenCalledTimes(1); $process->shouldHaveBeenCalledTimes(1);
@ -61,9 +60,9 @@ class CheckAuthenticationMiddlewareTest extends TestCase
RouteResult::class, RouteResult::class,
RouteResult::fromRouteFailure(['GET']) RouteResult::fromRouteFailure(['GET'])
); );
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal()); $this->middleware->process($request, $delegate->reveal());
$process->shouldHaveBeenCalledTimes(1); $process->shouldHaveBeenCalledTimes(1);
@ -76,9 +75,9 @@ class CheckAuthenticationMiddlewareTest extends TestCase
AuthenticateAction::class AuthenticateAction::class
)) ))
); );
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal()); $this->middleware->process($request, $delegate->reveal());
$process->shouldHaveBeenCalledTimes(1); $process->shouldHaveBeenCalledTimes(1);
@ -86,9 +85,9 @@ class CheckAuthenticationMiddlewareTest extends TestCase
RouteResult::class, RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), []) RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withMethod('OPTIONS'); )->withMethod('OPTIONS');
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal()); $this->middleware->process($request, $delegate->reveal());
$process->shouldHaveBeenCalledTimes(1); $process->shouldHaveBeenCalledTimes(1);
} }
@ -102,7 +101,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
RouteResult::class, RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), []) RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
); );
$response = $this->middleware->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals(401, $response->getStatusCode()); $this->assertEquals(401, $response->getStatusCode());
} }
@ -117,7 +116,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), []) RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken); )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
$response = $this->middleware->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals(401, $response->getStatusCode()); $this->assertEquals(401, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), 'You need to provide the Bearer type') > 0); $this->assertTrue(strpos($response->getBody()->getContents(), 'You need to provide the Bearer type') > 0);
@ -134,7 +133,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), []) RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken); )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken);
$response = $this->middleware->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals(401, $response->getStatusCode()); $this->assertEquals(401, $response->getStatusCode());
$this->assertTrue( $this->assertTrue(
@ -154,7 +153,7 @@ class CheckAuthenticationMiddlewareTest extends TestCase
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken); )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken);
$this->jwtService->verify($authToken)->willReturn(false)->shouldBeCalledTimes(1); $this->jwtService->verify($authToken)->willReturn(false)->shouldBeCalledTimes(1);
$response = $this->middleware->process($request, TestUtils::createDelegateMock()->reveal()); $response = $this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals(401, $response->getStatusCode()); $this->assertEquals(401, $response->getStatusCode());
} }
@ -171,9 +170,9 @@ class CheckAuthenticationMiddlewareTest extends TestCase
$this->jwtService->verify($authToken)->willReturn(true)->shouldBeCalledTimes(1); $this->jwtService->verify($authToken)->willReturn(true)->shouldBeCalledTimes(1);
$this->jwtService->refresh($authToken)->willReturn($authToken)->shouldBeCalledTimes(1); $this->jwtService->refresh($authToken)->willReturn($authToken)->shouldBeCalledTimes(1);
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */ /** @var MethodProphecy $process */
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$resp = $this->middleware->process($request, $delegate->reveal()); $resp = $this->middleware->process($request, $delegate->reveal());
$process->shouldHaveBeenCalledTimes(1); $process->shouldHaveBeenCalledTimes(1);

View File

@ -3,10 +3,10 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware; namespace ShlinkioTest\Shlink\Rest\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy; use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware; use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
@ -25,7 +25,7 @@ class CrossDomainMiddlewareTest extends TestCase
public function setUp() public function setUp()
{ {
$this->middleware = new CrossDomainMiddleware(); $this->middleware = new CrossDomainMiddleware();
$this->delegate = $this->prophesize(DelegateInterface::class); $this->delegate = $this->prophesize(RequestHandlerInterface::class);
} }
/** /**
@ -34,7 +34,7 @@ class CrossDomainMiddlewareTest extends TestCase
public function nonCrossDomainRequestsAreNotAffected() public function nonCrossDomainRequestsAreNotAffected()
{ {
$originalResponse = new Response(); $originalResponse = new Response();
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1); $this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
$response = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->delegate->reveal()); $response = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->delegate->reveal());
$this->assertSame($originalResponse, $response); $this->assertSame($originalResponse, $response);
@ -50,7 +50,7 @@ class CrossDomainMiddlewareTest extends TestCase
public function anyRequestIncludesTheAllowAccessHeader() public function anyRequestIncludesTheAllowAccessHeader()
{ {
$originalResponse = new Response(); $originalResponse = new Response();
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1); $this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
$response = $this->middleware->process( $response = $this->middleware->process(
ServerRequestFactory::fromGlobals()->withHeader('Origin', 'local'), ServerRequestFactory::fromGlobals()->withHeader('Origin', 'local'),
@ -70,7 +70,7 @@ class CrossDomainMiddlewareTest extends TestCase
{ {
$originalResponse = new Response(); $originalResponse = new Response();
$request = ServerRequestFactory::fromGlobals()->withMethod('OPTIONS')->withHeader('Origin', 'local'); $request = ServerRequestFactory::fromGlobals()->withMethod('OPTIONS')->withHeader('Origin', 'local');
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1); $this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
$response = $this->middleware->process($request, $this->delegate->reveal()); $response = $this->middleware->process($request, $this->delegate->reveal());
$this->assertNotSame($originalResponse, $response); $this->assertNotSame($originalResponse, $response);

View File

@ -3,11 +3,11 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware; namespace ShlinkioTest\Shlink\Rest\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware; use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
use Zend\Diactoros\Response; use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory; use Zend\Diactoros\ServerRequestFactory;
@ -32,8 +32,8 @@ class PathVersionMiddlewareTest extends TestCase
{ {
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo')); $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo'));
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal()); $this->middleware->process($request, $delegate->reveal());
@ -47,8 +47,8 @@ class PathVersionMiddlewareTest extends TestCase
{ {
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo')); $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo'));
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->process($request)->willReturn(new Response()); $process = $delegate->handle($request)->willReturn(new Response());
$this->middleware->process($request, $delegate->reveal()); $this->middleware->process($request, $delegate->reveal());
@ -62,8 +62,8 @@ class PathVersionMiddlewareTest extends TestCase
{ {
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz')); $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz'));
$delegate = $this->prophesize(DelegateInterface::class); $delegate = $this->prophesize(RequestHandlerInterface::class);
$delegate->process(Argument::type(Request::class))->will(function (array $args) use ($request) { $delegate->handle(Argument::type(Request::class))->will(function (array $args) use ($request) {
$req = \array_shift($args); $req = \array_shift($args);
Assert::assertNotSame($request, $req); Assert::assertNotSame($request, $req);

View File

@ -1,3 +1,4 @@
parameters: parameters:
excludes_analyse: excludes_analyse:
- module/Common/src/Template/Extension/TranslatorExtension.php - module/Common/src/Template/Extension/TranslatorExtension.php
- module/Rest/src/Util/RestUtils.php

View File

@ -1,9 +1,9 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
use Interop\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Zend\Expressive\Application; use Zend\Expressive\Application;
/** @var ContainerInterface $container */ /** @var ContainerInterface $container */
$container = include __DIR__ . '/../config/container.php'; $container = include __DIR__ . '/../config/container.php';
$app = $container->get(Application::class)->run(); $container->get(Application::class)->run();