Do not use ServerRequestFactory::fromGlobals in tests

This commit is contained in:
Alejandro Celaya 2018-12-25 23:01:30 +01:00
parent 8b3324e143
commit 32070b1fa7
30 changed files with 126 additions and 140 deletions

View File

@ -10,7 +10,7 @@ use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Common\Middleware\CloseDbConnectionMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class CloseDbConnectionMiddlewareTest extends TestCase
{
@ -34,7 +34,7 @@ class CloseDbConnectionMiddlewareTest extends TestCase
*/
public function connectionIsClosedWhenMiddlewareIsProcessed()
{
$req = ServerRequestFactory::fromGlobals();
$req = new ServerRequest();
$resp = new Response();
$conn = $this->prophesize(Connection::class);

View File

@ -6,7 +6,7 @@ namespace ShlinkioTest\Shlink\Common\Middleware;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\I18n\Translator\Translator;
class LocaleMiddlewareTest extends TestCase
@ -28,7 +28,7 @@ class LocaleMiddlewareTest extends TestCase
public function whenNoHeaderIsPresentLocaleIsNotChanged()
{
$this->assertEquals('ru', $this->translator->getLocale());
$this->middleware->process(ServerRequestFactory::fromGlobals(), TestUtils::createReqHandlerMock()->reveal());
$this->middleware->process(new ServerRequest(), TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals('ru', $this->translator->getLocale());
}
@ -38,7 +38,7 @@ class LocaleMiddlewareTest extends TestCase
public function whenTheHeaderIsPresentLocaleIsChanged()
{
$this->assertEquals('ru', $this->translator->getLocale());
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', 'es');
$request = (new ServerRequest())->withHeader('Accept-Language', 'es');
$this->middleware->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertEquals('es', $this->translator->getLocale());
}
@ -53,7 +53,7 @@ class LocaleMiddlewareTest extends TestCase
$this->assertEquals('ru', $this->translator->getLocale());
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept-Language', $lang);
$request = (new ServerRequest())->withHeader('Accept-Language', $lang);
$this->middleware->process($request, $handler->reveal());
$this->assertEquals($expected, $this->translator->getLocale());
}

View File

@ -14,7 +14,7 @@ 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;
use Zend\Diactoros\ServerRequest;
class PixelActionTest extends TestCase
{
@ -48,7 +48,7 @@ class PixelActionTest extends TestCase
)->shouldBeCalledOnce();
$this->visitTracker->track(Argument::cetera())->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertInstanceOf(PixelResponse::class, $response);

View File

@ -17,7 +17,7 @@ use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use const FILEINFO_MIME;
use function filesize;
@ -49,10 +49,7 @@ class PreviewActionTest extends TestCase
$delegate->handle(Argument::cetera())->shouldBeCalledOnce()
->willReturn(new Response());
$this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
$delegate->reveal()
);
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
}
/**
@ -68,7 +65,7 @@ class PreviewActionTest extends TestCase
$this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledOnce();
$resp = $this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
(new ServerRequest())->withAttribute('shortCode', $shortCode),
TestUtils::createReqHandlerMock()->reveal()
);
@ -89,7 +86,7 @@ class PreviewActionTest extends TestCase
$process = $delegate->handle(Argument::any())->willReturn(new Response());
$this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
(new ServerRequest())->withAttribute('shortCode', $shortCode),
$delegate->reveal()
);

View File

@ -15,7 +15,7 @@ use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Expressive\Router\RouterInterface;
class QrCodeActionTest extends TestCase
@ -46,10 +46,7 @@ class QrCodeActionTest extends TestCase
$delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->handle(Argument::any())->willReturn(new Response());
$this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
$delegate->reveal()
);
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
$process->shouldHaveBeenCalledOnce();
}
@ -66,10 +63,7 @@ class QrCodeActionTest extends TestCase
/** @var MethodProphecy $process */
$process = $delegate->handle(Argument::any())->willReturn(new Response());
$this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
$delegate->reveal()
);
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
$process->shouldHaveBeenCalledOnce();
}
@ -85,7 +79,7 @@ class QrCodeActionTest extends TestCase
$delegate = $this->prophesize(RequestHandlerInterface::class);
$resp = $this->action->process(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
(new ServerRequest())->withAttribute('shortCode', $shortCode),
$delegate->reveal()
);

View File

@ -15,7 +15,7 @@ use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Core\Service\VisitsTracker;
use ShlinkioTest\Shlink\Common\Util\TestUtils;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class RedirectActionTest extends TestCase
{
@ -54,7 +54,7 @@ class RedirectActionTest extends TestCase
->shouldBeCalledOnce();
$this->visitTracker->track(Argument::cetera())->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->process($request, TestUtils::createReqHandlerMock()->reveal());
$this->assertInstanceOf(Response\RedirectResponse::class, $response);
@ -76,7 +76,7 @@ class RedirectActionTest extends TestCase
$handler = $this->prophesize(RequestHandlerInterface::class);
$handle = $handler->handle(Argument::any())->willReturn(new Response());
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$this->action->process($request, $handler->reveal());
$handle->shouldHaveBeenCalledOnce();
@ -98,7 +98,7 @@ class RedirectActionTest extends TestCase
$this->notFoundOptions->enableRedirection = true;
$this->notFoundOptions->redirectTo = 'https://shlink.io';
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$resp = $this->action->process($request, $handler->reveal());
$this->assertEquals(302, $resp->getStatusCode());
@ -119,7 +119,7 @@ class RedirectActionTest extends TestCase
->shouldBeCalledOnce();
$this->visitTracker->track(Argument::cetera())->shouldNotBeCalled();
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode)
->withQueryParams(['foobar' => true]);
$response = $this->action->process($request, TestUtils::createReqHandlerMock()->reveal());

View File

@ -10,7 +10,7 @@ use Prophecy\Argument;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Middleware\QrCodeCacheMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
class QrCodeCacheMiddlewareTest extends TestCase
@ -34,9 +34,7 @@ class QrCodeCacheMiddlewareTest extends TestCase
$delegate = $this->prophesize(RequestHandlerInterface::class);
$delegate->handle(Argument::any())->willReturn(new Response())->shouldBeCalledOnce();
$this->middleware->process(ServerRequestFactory::fromGlobals()->withUri(
new Uri('/foo/bar')
), $delegate->reveal());
$this->middleware->process((new ServerRequest())->withUri(new Uri('/foo/bar')), $delegate->reveal());
$this->assertTrue($this->cache->contains('/foo/bar'));
}
@ -51,10 +49,7 @@ class QrCodeCacheMiddlewareTest extends TestCase
$this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']);
$delegate = $this->prophesize(RequestHandlerInterface::class);
$resp = $this->middleware->process(
ServerRequestFactory::fromGlobals()->withUri($uri),
$delegate->reveal()
);
$resp = $this->middleware->process((new ServerRequest())->withUri($uri), $delegate->reveal());
$this->assertFalse($isCalled);
$resp->getBody()->rewind();

View File

@ -9,7 +9,7 @@ use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Response\NotFoundHandler;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Expressive\Template\TemplateRendererInterface;
class NotFoundHandlerTest extends TestCase
@ -35,7 +35,7 @@ class NotFoundHandlerTest extends TestCase
*/
public function properResponseTypeIsReturned(string $expectedResponse, string $accept, int $renderCalls)
{
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept', $accept);
$request = (new ServerRequest())->withHeader('Accept', $accept);
/** @var MethodProphecy $render */
$render = $this->renderer->render(Argument::cetera())->willReturn('');

View File

@ -10,7 +10,7 @@ use Shlinkio\Shlink\Rest\Action\AuthenticateAction;
use Shlinkio\Shlink\Rest\Authentication\JWTService;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use function strpos;
class AuthenticateActionTest extends TestCase
@ -36,7 +36,7 @@ class AuthenticateActionTest extends TestCase
*/
public function notProvidingAuthDataReturnsError()
{
$resp = $this->action->handle(ServerRequestFactory::fromGlobals());
$resp = $this->action->handle(new ServerRequest());
$this->assertEquals(400, $resp->getStatusCode());
}
@ -48,7 +48,7 @@ class AuthenticateActionTest extends TestCase
$this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->setId('5'))
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'apiKey' => 'foo',
]);
$response = $this->action->handle($request);
@ -66,7 +66,7 @@ class AuthenticateActionTest extends TestCase
$this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->disable())
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'apiKey' => 'foo',
]);
$response = $this->action->handle($request);

View File

@ -13,7 +13,7 @@ use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Rest\Action\ShortUrl\CreateShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
use function strpos;
@ -38,7 +38,7 @@ class CreateShortUrlActionTest extends TestCase
*/
public function missingLongUrlParamReturnsError()
{
$response = $this->action->handle(ServerRequestFactory::fromGlobals());
$response = $this->action->handle(new ServerRequest());
$this->assertEquals(400, $response->getStatusCode());
}
@ -53,7 +53,7 @@ class CreateShortUrlActionTest extends TestCase
)
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar',
]);
$response = $this->action->handle($request);
@ -70,7 +70,7 @@ class CreateShortUrlActionTest extends TestCase
->willThrow(InvalidUrlException::class)
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar',
]);
$response = $this->action->handle($request);
@ -92,7 +92,7 @@ class CreateShortUrlActionTest extends TestCase
Argument::cetera()
)->willThrow(NonUniqueSlugException::class)->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar',
'customSlug' => 'foo',
]);
@ -110,7 +110,7 @@ class CreateShortUrlActionTest extends TestCase
->willThrow(Exception::class)
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'longUrl' => 'http://www.domain.com/foo/bar',
]);
$response = $this->action->handle($request);

View File

@ -12,7 +12,7 @@ use Shlinkio\Shlink\Rest\Action\ShortUrl\DeleteShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Throwable;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class DeleteShortUrlActionTest extends TestCase
{
@ -35,7 +35,7 @@ class DeleteShortUrlActionTest extends TestCase
$deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->will(function () {
});
$resp = $this->action->handle(ServerRequestFactory::fromGlobals());
$resp = $this->action->handle(new ServerRequest());
$this->assertEquals(204, $resp->getStatusCode());
$deleteByShortCode->shouldHaveBeenCalledOnce();
@ -50,7 +50,7 @@ class DeleteShortUrlActionTest extends TestCase
$deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->willThrow($e);
/** @var JsonResponse $resp */
$resp = $this->action->handle(ServerRequestFactory::fromGlobals());
$resp = $this->action->handle(new ServerRequest());
$payload = $resp->getPayload();
$this->assertEquals($statusCode, $resp->getStatusCode());

View File

@ -12,7 +12,7 @@ use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class EditShortUrlActionTest extends TestCase
{
@ -32,7 +32,7 @@ class EditShortUrlActionTest extends TestCase
*/
public function invalidDataReturnsError()
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'maxVisits' => 'invalid',
]);
@ -50,10 +50,10 @@ class EditShortUrlActionTest extends TestCase
*/
public function incorrectShortCodeReturnsError()
{
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
->withParsedBody([
'maxVisits' => 5,
]);
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
->withParsedBody([
'maxVisits' => 5,
]);
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willThrow(
InvalidShortCodeException::class
);
@ -73,10 +73,10 @@ class EditShortUrlActionTest extends TestCase
*/
public function correctShortCodeReturnsSuccess()
{
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
->withParsedBody([
'maxVisits' => 5,
]);
$request = (new ServerRequest())->withAttribute('shortCode', 'abc123')
->withParsedBody([
'maxVisits' => 5,
]);
$updateMeta = $this->shortUrlService->updateMetadataByShortCode(Argument::cetera())->willReturn(
new ShortUrl('')
);

View File

@ -9,7 +9,7 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ShortUrl\EditShortUrlTagsAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class EditShortUrlTagsActionTest extends TestCase
{
@ -29,7 +29,7 @@ class EditShortUrlTagsActionTest extends TestCase
*/
public function notProvidingTagsReturnsError()
{
$response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123'));
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', 'abc123'));
$this->assertEquals(400, $response->getStatusCode());
}
@ -43,8 +43,8 @@ class EditShortUrlTagsActionTest extends TestCase
->shouldBeCalledOnce();
$response = $this->action->handle(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
->withParsedBody(['tags' => []])
(new ServerRequest())->withAttribute('shortCode', 'abc123')
->withParsedBody(['tags' => []])
);
$this->assertEquals(404, $response->getStatusCode());
}
@ -59,8 +59,8 @@ class EditShortUrlTagsActionTest extends TestCase
->shouldBeCalledOnce();
$response = $this->action->handle(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')
->withParsedBody(['tags' => []])
(new ServerRequest())->withAttribute('shortCode', 'abc123')
->withParsedBody(['tags' => []])
);
$this->assertEquals(200, $response->getStatusCode());
}

View File

@ -8,7 +8,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Shlinkio\Shlink\Rest\Action\ShortUrl\ListShortUrlsAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;
@ -37,7 +37,7 @@ class ListShortUrlsActionTest extends TestCase
$this->service->listShortUrls($page, null, [], null)->willReturn(new Paginator(new ArrayAdapter()))
->shouldBeCalledOnce();
$response = $this->action->handle(ServerRequestFactory::fromGlobals()->withQueryParams([
$response = $this->action->handle((new ServerRequest())->withQueryParams([
'page' => $page,
]));
$this->assertEquals(200, $response->getStatusCode());
@ -52,7 +52,7 @@ class ListShortUrlsActionTest extends TestCase
$this->service->listShortUrls($page, null, [], null)->willThrow(Exception::class)
->shouldBeCalledOnce();
$response = $this->action->handle(ServerRequestFactory::fromGlobals()->withQueryParams([
$response = $this->action->handle((new ServerRequest())->withQueryParams([
'page' => $page,
]));
$this->assertEquals(500, $response->getStatusCode());

View File

@ -12,7 +12,7 @@ use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Rest\Action\ShortUrl\ResolveShortUrlAction;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use function strpos;
class ResolveShortUrlActionTest extends TestCase
@ -37,7 +37,7 @@ class ResolveShortUrlActionTest extends TestCase
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->handle($request);
$this->assertEquals(404, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_ARGUMENT_ERROR) > 0);
@ -53,7 +53,7 @@ class ResolveShortUrlActionTest extends TestCase
new ShortUrl('http://domain.com/foo/bar')
)->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), 'http://domain.com/foo/bar') > 0);
@ -68,7 +68,7 @@ class ResolveShortUrlActionTest extends TestCase
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->handle($request);
$this->assertEquals(400, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_SHORTCODE_ERROR) > 0);
@ -83,7 +83,7 @@ class ResolveShortUrlActionTest extends TestCase
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(Exception::class)
->shouldBeCalledOnce();
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
$request = (new ServerRequest())->withAttribute('shortCode', $shortCode);
$response = $this->action->handle($request);
$this->assertEquals(500, $response->getStatusCode());
$this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);

View File

@ -13,7 +13,7 @@ use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Rest\Action\ShortUrl\SingleStepCreateShortUrlAction;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class SingleStepCreateShortUrlActionTest extends TestCase
{
@ -44,7 +44,7 @@ class SingleStepCreateShortUrlActionTest extends TestCase
*/
public function errorResponseIsReturnedIfInvalidApiKeyIsProvided()
{
$request = ServerRequestFactory::fromGlobals()->withQueryParams(['apiKey' => 'abc123']);
$request = (new ServerRequest())->withQueryParams(['apiKey' => 'abc123']);
$findApiKey = $this->apiKeyService->check('abc123')->willReturn(false);
/** @var JsonResponse $resp */
@ -62,7 +62,7 @@ class SingleStepCreateShortUrlActionTest extends TestCase
*/
public function errorResponseIsReturnedIfNoUrlIsProvided()
{
$request = ServerRequestFactory::fromGlobals()->withQueryParams(['apiKey' => 'abc123']);
$request = (new ServerRequest())->withQueryParams(['apiKey' => 'abc123']);
$findApiKey = $this->apiKeyService->check('abc123')->willReturn(true);
/** @var JsonResponse $resp */
@ -80,7 +80,7 @@ class SingleStepCreateShortUrlActionTest extends TestCase
*/
public function properDataIsPassedWhenGeneratingShortCode()
{
$request = ServerRequestFactory::fromGlobals()->withQueryParams([
$request = (new ServerRequest())->withQueryParams([
'apiKey' => 'abc123',
'longUrl' => 'http://foobar.com',
]);

View File

@ -9,7 +9,7 @@ use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\CreateTagsAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class CreateTagsActionTest extends TestCase
{
@ -31,7 +31,7 @@ class CreateTagsActionTest extends TestCase
*/
public function processDelegatesIntoService($tags)
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody(['tags' => $tags]);
$request = (new ServerRequest())->withParsedBody(['tags' => $tags]);
/** @var MethodProphecy $deleteTags */
$deleteTags = $this->tagService->createTags($tags ?: [])->willReturn(new ArrayCollection());

View File

@ -8,7 +8,7 @@ use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\DeleteTagsAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class DeleteTagsActionTest extends TestCase
{
@ -30,7 +30,7 @@ class DeleteTagsActionTest extends TestCase
*/
public function processDelegatesIntoService($tags)
{
$request = ServerRequestFactory::fromGlobals()->withQueryParams(['tags' => $tags]);
$request = (new ServerRequest())->withQueryParams(['tags' => $tags]);
/** @var MethodProphecy $deleteTags */
$deleteTags = $this->tagService->deleteTags($tags ?: []);

View File

@ -9,7 +9,7 @@ use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\ListTagsAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use function Shlinkio\Shlink\Common\json_decode;
class ListTagsActionTest extends TestCase
@ -33,7 +33,7 @@ class ListTagsActionTest extends TestCase
/** @var MethodProphecy $listTags */
$listTags = $this->tagService->listTags()->willReturn([new Tag('foo'), new Tag('bar')]);
$resp = $this->action->handle(ServerRequestFactory::fromGlobals());
$resp = $this->action->handle(new ServerRequest());
$this->assertEquals([
'tags' => [

View File

@ -10,7 +10,7 @@ use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
use Shlinkio\Shlink\Rest\Action\Tag\UpdateTagAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class UpdateTagActionTest extends TestCase
{
@ -32,7 +32,7 @@ class UpdateTagActionTest extends TestCase
*/
public function whenInvalidParamsAreProvidedAnErrorIsReturned(array $bodyParams)
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody($bodyParams);
$request = (new ServerRequest())->withParsedBody($bodyParams);
$resp = $this->action->handle($request);
$this->assertEquals(400, $resp->getStatusCode());
@ -52,7 +52,7 @@ class UpdateTagActionTest extends TestCase
*/
public function requestingInvalidTagReturnsError()
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'oldName' => 'foo',
'newName' => 'bar',
]);
@ -70,7 +70,7 @@ class UpdateTagActionTest extends TestCase
*/
public function correctInvocationRenamesTag()
{
$request = ServerRequestFactory::fromGlobals()->withParsedBody([
$request = (new ServerRequest())->withParsedBody([
'oldName' => 'foo',
'newName' => 'bar',
]);

View File

@ -12,7 +12,7 @@ use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Service\VisitsTracker;
use Shlinkio\Shlink\Rest\Action\Visit\GetVisitsAction;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;
@ -39,7 +39,7 @@ class GetVisitsActionTest extends TestCase
new Paginator(new ArrayAdapter([]))
)->shouldBeCalledOnce();
$response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', $shortCode));
$this->assertEquals(200, $response->getStatusCode());
}
@ -53,7 +53,7 @@ class GetVisitsActionTest extends TestCase
InvalidArgumentException::class
)->shouldBeCalledOnce();
$response = $this->action->handle(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode));
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', $shortCode));
$this->assertEquals(404, $response->getStatusCode());
}
@ -72,12 +72,12 @@ class GetVisitsActionTest extends TestCase
->shouldBeCalledOnce();
$response = $this->action->handle(
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)
->withQueryParams([
'endDate' => '2016-01-01 00:00:00',
'page' => '3',
'itemsPerPage' => '10',
])
(new ServerRequest())->withAttribute('shortCode', $shortCode)
->withQueryParams([
'endDate' => '2016-01-01 00:00:00',
'page' => '3',
'itemsPerPage' => '10',
])
);
$this->assertEquals(200, $response->getStatusCode());
}

View File

@ -10,7 +10,7 @@ use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class ApiKeyHeaderPluginTest extends TestCase
{
@ -68,6 +68,6 @@ class ApiKeyHeaderPluginTest extends TestCase
private function createRequest(string $apiKey): ServerRequestInterface
{
return ServerRequestFactory::fromGlobals()->withHeader(ApiKeyHeaderPlugin::HEADER_NAME, $apiKey);
return (new ServerRequest())->withHeader(ApiKeyHeaderPlugin::HEADER_NAME, $apiKey);
}
}

View File

@ -9,7 +9,7 @@ use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin;
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use function sprintf;
class AuthorizationHeaderPluginTest extends TestCase
@ -31,7 +31,7 @@ class AuthorizationHeaderPluginTest extends TestCase
public function verifyAnAuthorizationWithoutBearerTypeThrowsException()
{
$authToken = 'ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withHeader(
$request = (new ServerRequest())->withHeader(
AuthorizationHeaderPlugin::HEADER_NAME,
$authToken
);
@ -51,7 +51,7 @@ class AuthorizationHeaderPluginTest extends TestCase
public function verifyAnAuthorizationWithWrongTypeThrowsException()
{
$authToken = 'Basic ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withHeader(
$request = (new ServerRequest())->withHeader(
AuthorizationHeaderPlugin::HEADER_NAME,
$authToken
);
@ -70,7 +70,7 @@ class AuthorizationHeaderPluginTest extends TestCase
public function verifyAnExpiredTokenThrowsException()
{
$authToken = 'Bearer ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withHeader(
$request = (new ServerRequest())->withHeader(
AuthorizationHeaderPlugin::HEADER_NAME,
$authToken
);
@ -94,7 +94,7 @@ class AuthorizationHeaderPluginTest extends TestCase
public function verifyValidTokenDoesNotThrowException()
{
$authToken = 'Bearer ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withHeader(
$request = (new ServerRequest())->withHeader(
AuthorizationHeaderPlugin::HEADER_NAME,
$authToken
);
@ -111,7 +111,7 @@ class AuthorizationHeaderPluginTest extends TestCase
public function updateReturnsAnUpdatedResponseWithNewJwt()
{
$authToken = 'Bearer ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withHeader(
$request = (new ServerRequest())->withHeader(
AuthorizationHeaderPlugin::HEADER_NAME,
$authToken
);

View File

@ -11,7 +11,7 @@ use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthenticationPluginInterface;
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin;
use Shlinkio\Shlink\Rest\Authentication\RequestToHttpAuthPlugin;
use Shlinkio\Shlink\Rest\Exception\NoAuthenticationException;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use function implode;
use function sprintf;
@ -33,7 +33,7 @@ class RequestToAuthPluginTest extends TestCase
*/
public function exceptionIsFoundWhenNoneOfTheSupportedMethodsIsFound()
{
$request = ServerRequestFactory::fromGlobals();
$request = new ServerRequest();
$this->expectException(NoAuthenticationException::class);
$this->expectExceptionMessage(sprintf(
@ -50,7 +50,7 @@ class RequestToAuthPluginTest extends TestCase
*/
public function properPluginIsFetchedWhenAnyAuthTypeIsFound(array $headers, string $expectedHeader)
{
$request = ServerRequestFactory::fromGlobals();
$request = new ServerRequest();
foreach ($headers as $header => $value) {
$request = $request->withHeader($header, $value);
}

View File

@ -6,7 +6,7 @@ namespace ShlinkioTest\Shlink\Rest\ErrorHandler;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorResponseGenerator;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class JsonErrorResponseGeneratorTest extends TestCase
{
@ -23,7 +23,7 @@ class JsonErrorResponseGeneratorTest extends TestCase
*/
public function noErrorStatusReturnsInternalServerError()
{
$response = $this->errorHandler->__invoke(null, ServerRequestFactory::fromGlobals(), new Response());
$response = $this->errorHandler->__invoke(null, new ServerRequest(), new Response());
$this->assertInstanceOf(Response\JsonResponse::class, $response);
$this->assertEquals(500, $response->getStatusCode());
}
@ -35,7 +35,7 @@ class JsonErrorResponseGeneratorTest extends TestCase
{
$response = $this->errorHandler->__invoke(
null,
ServerRequestFactory::fromGlobals(),
new ServerRequest(),
(new Response())->withStatus(405)
);
$this->assertInstanceOf(Response\JsonResponse::class, $response);

View File

@ -22,7 +22,7 @@ use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use function implode;
@ -68,18 +68,18 @@ class AuthenticationMiddlewareTest extends TestCase
$dummyMiddleware = $this->getDummyMiddleware();
return [
'with no route result' => [ServerRequestFactory::fromGlobals()],
'with failure route result' => [ServerRequestFactory::fromGlobals()->withAttribute(
'with no route result' => [new ServerRequest()],
'with failure route result' => [(new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRouteFailure([RequestMethodInterface::METHOD_GET])
)],
'with whitelisted route' => [ServerRequestFactory::fromGlobals()->withAttribute(
'with whitelisted route' => [(new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(
new Route('foo', $dummyMiddleware, Route::HTTP_METHOD_ANY, AuthenticateAction::class)
)
)],
'with OPTIONS method' => [ServerRequestFactory::fromGlobals()->withAttribute(
'with OPTIONS method' => [(new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $dummyMiddleware), [])
)->withMethod(RequestMethodInterface::METHOD_OPTIONS)],
@ -92,7 +92,7 @@ class AuthenticationMiddlewareTest extends TestCase
*/
public function errorIsReturnedWhenNoValidAuthIsProvided($e)
{
$request = ServerRequestFactory::fromGlobals()->withAttribute(
$request = (new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), [])
);
@ -124,7 +124,7 @@ class AuthenticationMiddlewareTest extends TestCase
*/
public function errorIsReturnedWhenVerificationFails()
{
$request = ServerRequestFactory::fromGlobals()->withAttribute(
$request = (new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), [])
);
@ -151,7 +151,7 @@ class AuthenticationMiddlewareTest extends TestCase
public function updatedResponseIsReturnedWhenVerificationPasses()
{
$newResponse = new Response();
$request = ServerRequestFactory::fromGlobals()->withAttribute(
$request = (new ServerRequest())->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', $this->getDummyMiddleware()), [])
);

View File

@ -10,7 +10,7 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Stream;
use function array_shift;
@ -29,7 +29,7 @@ class BodyParserMiddlewareTest extends TestCase
*/
public function requestsFromOtherMethodsJustFallbackToNextMiddleware()
{
$request = ServerRequestFactory::fromGlobals()->withMethod('GET');
$request = (new ServerRequest())->withMethod('GET');
$delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */
$process = $delegate->handle($request)->willReturn(new Response());
@ -47,9 +47,9 @@ class BodyParserMiddlewareTest extends TestCase
$test = $this;
$body = new Stream('php://temp', 'wr');
$body->write('{"foo": "bar", "bar": ["one", 5]}');
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body)
->withHeader('content-type', 'application/json');
$request = (new ServerRequest())->withMethod('PUT')
->withBody($body)
->withHeader('content-type', 'application/json');
$delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
@ -79,8 +79,8 @@ class BodyParserMiddlewareTest extends TestCase
$test = $this;
$body = new Stream('php://temp', 'wr');
$body->write('foo=bar&bar[]=one&bar[]=5');
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body);
$request = (new ServerRequest())->withMethod('PUT')
->withBody($body);
$delegate = $this->prophesize(RequestHandlerInterface::class);
/** @var MethodProphecy $process */
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(

View File

@ -9,7 +9,7 @@ use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class CrossDomainMiddlewareTest extends TestCase
{
@ -32,7 +32,7 @@ class CrossDomainMiddlewareTest extends TestCase
$originalResponse = new Response();
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->delegate->reveal());
$response = $this->middleware->process(new ServerRequest(), $this->delegate->reveal());
$this->assertSame($originalResponse, $response);
$headers = $response->getHeaders();
@ -49,7 +49,7 @@ class CrossDomainMiddlewareTest extends TestCase
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process(
ServerRequestFactory::fromGlobals()->withHeader('Origin', 'local'),
(new ServerRequest())->withHeader('Origin', 'local'),
$this->delegate->reveal()
);
$this->assertNotSame($originalResponse, $response);
@ -65,7 +65,7 @@ class CrossDomainMiddlewareTest extends TestCase
public function optionsRequestIncludesMoreHeaders()
{
$originalResponse = new Response();
$request = ServerRequestFactory::fromGlobals()->withMethod('OPTIONS')->withHeader('Origin', 'local');
$request = (new ServerRequest())->withMethod('OPTIONS')->withHeader('Origin', 'local');
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process($request, $this->delegate->reveal());

View File

@ -10,7 +10,7 @@ use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
use function array_shift;
@ -29,7 +29,7 @@ class PathVersionMiddlewareTest extends TestCase
*/
public function whenVersionIsProvidedRequestRemainsUnchanged()
{
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo'));
$request = (new ServerRequest())->withUri(new Uri('/v2/foo'));
$delegate = $this->prophesize(RequestHandlerInterface::class);
$process = $delegate->handle($request)->willReturn(new Response());
@ -44,7 +44,7 @@ class PathVersionMiddlewareTest extends TestCase
*/
public function versionOneIsPrependedWhenNoVersionIsDefined()
{
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz'));
$request = (new ServerRequest())->withUri(new Uri('/bar/baz'));
$delegate = $this->prophesize(RequestHandlerInterface::class);
$delegate->handle(Argument::type(Request::class))->will(function (array $args) use ($request) {

View File

@ -10,7 +10,7 @@ use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\ServerRequest;
class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
{
@ -33,7 +33,7 @@ class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
$expectedResp = new Response();
$this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn($expectedResp);
$resp = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->requestHandler->reveal());
$resp = $this->middleware->process(new ServerRequest(), $this->requestHandler->reveal());
$this->assertSame($expectedResp, $resp);
}
@ -45,7 +45,7 @@ class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
*/
public function properResponseIsReturned(?string $accept, array $query, string $expectedContentType)
{
$request = ServerRequestFactory::fromGlobals()->withQueryParams($query);
$request = (new ServerRequest())->withQueryParams($query);
if ($accept !== null) {
$request = $request->withHeader('Accept', $accept);
}
@ -81,7 +81,7 @@ class CreateShortUrlContentNegotiationMiddlewareTest extends TestCase
*/
public function properBodyIsReturnedInPlainTextResponses(array $json, string $expectedBody)
{
$request = ServerRequestFactory::fromGlobals()->withQueryParams(['format' => 'txt']);
$request = (new ServerRequest())->withQueryParams(['format' => 'txt']);
$handle = $this->requestHandler->handle(Argument::type(ServerRequestInterface::class))->willReturn(
new JsonResponse($json)