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 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());
}