Created API tests for errors when getting short URL visits

This commit is contained in:
Alejandro Celaya 2019-11-20 20:58:16 +01:00
parent d044e1a5b7
commit 34e60ec5b8
5 changed files with 34 additions and 15 deletions

View file

@ -9,15 +9,13 @@ use Psr\EventDispatcher\EventDispatcherInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Paginator\Adapter\VisitsPaginatorAdapter;
use Shlinkio\Shlink\Core\Repository\VisitRepository;
use Zend\Paginator\Paginator;
use function sprintf;
class VisitsTracker implements VisitsTrackerInterface
{
/** @var ORM\EntityManagerInterface */
@ -53,14 +51,14 @@ class VisitsTracker implements VisitsTrackerInterface
* Returns the visits on certain short code
*
* @return Visit[]|Paginator
* @throws InvalidArgumentException
* @throws InvalidShortCodeException
*/
public function info(string $shortCode, VisitsParams $params): Paginator
{
/** @var ORM\EntityRepository $repo */
$repo = $this->em->getRepository(ShortUrl::class);
if ($repo->count(['shortCode' => $shortCode]) < 1) {
throw new InvalidArgumentException(sprintf('Short code "%s" not found', $shortCode));
throw InvalidShortCodeException::fromNotFoundShortCode($shortCode);
}
/** @var VisitRepository $repo */

View file

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Service;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Zend\Paginator\Paginator;
@ -21,7 +21,7 @@ interface VisitsTrackerInterface
* Returns the visits on certain short code
*
* @return Visit[]|Paginator
* @throws InvalidArgumentException
* @throws InvalidShortCodeException
*/
public function info(string $shortCode, VisitsParams $params): Paginator;
}

View file

@ -7,8 +7,8 @@ namespace Shlinkio\Shlink\Rest\Action\Visit;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
@ -48,10 +48,10 @@ class GetVisitsAction extends AbstractRestAction
return new JsonResponse([
'visits' => $this->serializePaginator($visits),
]);
} catch (InvalidArgumentException $e) {
} catch (InvalidShortCodeException $e) {
$this->logger->warning('Provided nonexistent short code {e}', ['e' => $e]);
return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e),
'error' => RestUtils::INVALID_ARGUMENT_ERROR, // FIXME Wrong code. Use correct one in "type"
'message' => sprintf('Provided short code %s does not exist', $shortCode),
], self::STATUS_NOT_FOUND);
}

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace ShlinkioApiTest\Shlink\Rest\Action;
use Shlinkio\Shlink\Rest\Util\RestUtils;
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
class GetVisitsActionTest extends ApiTestCase
{
/** @test */
public function tryingToGetVisitsForInvalidUrlReturnsNotFoundError(): void
{
$resp = $this->callApiWithKey(self::METHOD_GET, '/short-urls/invalid/visits');
['error' => $error] = $this->getJsonResponsePayload($resp);
$this->assertEquals(self::STATUS_NOT_FOUND, $resp->getStatusCode());
$this->assertEquals(RestUtils::INVALID_ARGUMENT_ERROR, $error);
}
}

View file

@ -8,8 +8,8 @@ use Cake\Chronos\Chronos;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Service\VisitsTracker;
use Shlinkio\Shlink\Rest\Action\Visit\GetVisitsAction;
@ -31,7 +31,7 @@ class GetVisitsActionTest extends TestCase
}
/** @test */
public function providingCorrectShortCodeReturnsVisits()
public function providingCorrectShortCodeReturnsVisits(): void
{
$shortCode = 'abc123';
$this->visitsTracker->info($shortCode, Argument::type(VisitsParams::class))->willReturn(
@ -43,11 +43,11 @@ class GetVisitsActionTest extends TestCase
}
/** @test */
public function providingInvalidShortCodeReturnsError()
public function providingInvalidShortCodeReturnsError(): void
{
$shortCode = 'abc123';
$this->visitsTracker->info($shortCode, Argument::type(VisitsParams::class))->willThrow(
InvalidArgumentException::class
InvalidShortCodeException::class
)->shouldBeCalledOnce();
$response = $this->action->handle((new ServerRequest())->withAttribute('shortCode', $shortCode));
@ -55,7 +55,7 @@ class GetVisitsActionTest extends TestCase
}
/** @test */
public function paramsAreReadFromQuery()
public function paramsAreReadFromQuery(): void
{
$shortCode = 'abc123';
$this->visitsTracker->info($shortCode, new VisitsParams(