2018-09-15 13:56:17 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
2018-09-15 13:56:17 +03:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Core\Exception;
|
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface;
|
2018-09-20 20:55:24 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\DeleteShortUrlAction;
|
2018-09-15 13:56:17 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
2018-10-28 10:24:06 +03:00
|
|
|
use Throwable;
|
2018-09-15 13:56:17 +03:00
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
use Zend\I18n\Translator\Translator;
|
|
|
|
|
2018-09-20 20:55:24 +03:00
|
|
|
class DeleteShortUrlActionTest extends TestCase
|
2018-09-15 13:56:17 +03:00
|
|
|
{
|
|
|
|
/**
|
2018-09-20 20:55:24 +03:00
|
|
|
* @var DeleteShortUrlAction
|
2018-09-15 13:56:17 +03:00
|
|
|
*/
|
|
|
|
private $action;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
private $service;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->service = $this->prophesize(DeleteShortUrlServiceInterface::class);
|
2018-09-20 20:55:24 +03:00
|
|
|
$this->action = new DeleteShortUrlAction($this->service->reveal(), Translator::factory([]));
|
2018-09-15 13:56:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function emptyResponseIsReturnedIfProperlyDeleted()
|
|
|
|
{
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->will(function () {
|
|
|
|
});
|
|
|
|
|
|
|
|
$resp = $this->action->handle(ServerRequestFactory::fromGlobals());
|
|
|
|
|
|
|
|
$this->assertEquals(204, $resp->getStatusCode());
|
|
|
|
$deleteByShortCode->shouldHaveBeenCalledTimes(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideExceptions
|
|
|
|
*/
|
2018-10-28 10:24:06 +03:00
|
|
|
public function returnsErrorResponseInCaseOfException(Throwable $e, string $error, int $statusCode)
|
2018-09-15 13:56:17 +03:00
|
|
|
{
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->willThrow($e);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
|
|
|
$resp = $this->action->handle(ServerRequestFactory::fromGlobals());
|
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
|
|
|
$this->assertEquals($statusCode, $resp->getStatusCode());
|
|
|
|
$this->assertEquals($error, $payload['error']);
|
|
|
|
$deleteByShortCode->shouldHaveBeenCalledTimes(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExceptions(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[new Exception\InvalidShortCodeException(), RestUtils::INVALID_SHORTCODE_ERROR, 404],
|
|
|
|
[new Exception\DeleteShortUrlException(5), RestUtils::INVALID_SHORTCODE_DELETION_ERROR, 400],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|