2018-09-15 05:56:17 -05:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Action\ShortUrl;
|
2018-09-15 05:56:17 -05: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 12:55:24 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Action\ShortUrl\DeleteShortUrlAction;
|
2018-09-15 05:56:17 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
2018-10-28 02:24:06 -05:00
|
|
|
use Throwable;
|
2018-09-15 05:56:17 -05:00
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
2018-12-25 16:01:30 -06:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2018-09-15 05:56:17 -05:00
|
|
|
|
2018-09-20 12:55:24 -05:00
|
|
|
class DeleteShortUrlActionTest extends TestCase
|
2018-09-15 05:56:17 -05:00
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var DeleteShortUrlAction */
|
2018-09-15 05:56:17 -05:00
|
|
|
private $action;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-09-15 05:56:17 -05:00
|
|
|
private $service;
|
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2018-09-15 05:56:17 -05:00
|
|
|
{
|
|
|
|
$this->service = $this->prophesize(DeleteShortUrlServiceInterface::class);
|
2018-11-18 09:28:04 -06:00
|
|
|
$this->action = new DeleteShortUrlAction($this->service->reveal());
|
2018-09-15 05:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
|
|
|
public function emptyResponseIsReturnedIfProperlyDeleted(): void
|
2018-09-15 05:56:17 -05:00
|
|
|
{
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->will(function () {
|
|
|
|
});
|
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$resp = $this->action->handle(new ServerRequest());
|
2018-09-15 05:56:17 -05:00
|
|
|
|
|
|
|
$this->assertEquals(204, $resp->getStatusCode());
|
2018-11-11 06:18:21 -06:00
|
|
|
$deleteByShortCode->shouldHaveBeenCalledOnce();
|
2018-09-15 05:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideExceptions
|
|
|
|
*/
|
2019-02-17 13:28:34 -06:00
|
|
|
public function returnsErrorResponseInCaseOfException(Throwable $e, string $error, int $statusCode): void
|
2018-09-15 05:56:17 -05:00
|
|
|
{
|
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode(Argument::any())->willThrow($e);
|
|
|
|
|
|
|
|
/** @var JsonResponse $resp */
|
2018-12-25 16:01:30 -06:00
|
|
|
$resp = $this->action->handle(new ServerRequest());
|
2018-09-15 05:56:17 -05:00
|
|
|
$payload = $resp->getPayload();
|
|
|
|
|
|
|
|
$this->assertEquals($statusCode, $resp->getStatusCode());
|
|
|
|
$this->assertEquals($error, $payload['error']);
|
2018-11-11 06:18:21 -06:00
|
|
|
$deleteByShortCode->shouldHaveBeenCalledOnce();
|
2018-09-15 05:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
public function provideExceptions(): iterable
|
2018-09-15 05:56:17 -05:00
|
|
|
{
|
2019-02-17 13:28:34 -06:00
|
|
|
yield 'not found' => [new Exception\InvalidShortCodeException(), RestUtils::INVALID_SHORTCODE_ERROR, 404];
|
|
|
|
yield 'bad request' => [
|
|
|
|
new Exception\DeleteShortUrlException(5),
|
|
|
|
RestUtils::INVALID_SHORTCODE_DELETION_ERROR,
|
|
|
|
400,
|
2018-09-15 05:56:17 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|