2016-08-09 07:18:20 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-09 07:18:20 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Action;
|
|
|
|
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-09 07:18:20 -05:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-03-26 12:02:41 -05:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-09 07:18:20 -05:00
|
|
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
|
|
|
use Shlinkio\Shlink\Core\Action\QrCodeAction;
|
2018-08-11 03:40:44 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2017-10-12 04:28:45 -05:00
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
2019-11-24 16:11:25 -06:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2016-08-09 07:18:20 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2017-10-13 05:22:19 -05:00
|
|
|
use Zend\Diactoros\Response;
|
2018-12-25 16:01:30 -06:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2016-08-09 07:18:20 -05:00
|
|
|
use Zend\Expressive\Router\RouterInterface;
|
|
|
|
|
|
|
|
class QrCodeActionTest extends TestCase
|
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var QrCodeAction */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $action;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $urlShortener;
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
|
|
|
$router = $this->prophesize(RouterInterface::class);
|
|
|
|
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
|
|
|
|
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
|
|
|
|
|
|
|
$this->action = new QrCodeAction($router->reveal(), $this->urlShortener->reveal());
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-10-22 12:52:28 -05:00
|
|
|
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2019-10-22 12:52:28 -05:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(EntityDoesNotExistException::class)
|
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
|
2017-03-24 17:19:42 -05:00
|
|
|
|
2018-11-11 06:18:21 -06:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-10-22 12:52:28 -05:00
|
|
|
public function anInvalidShortCodeWillReturnNotFoundResponse(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2019-11-24 16:11:25 -06:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode, '')->willThrow(ShortUrlNotFoundException::class)
|
2019-10-22 12:52:28 -05:00
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
|
2017-03-24 17:19:42 -05:00
|
|
|
|
2018-11-11 06:18:21 -06:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-10-22 12:52:28 -05:00
|
|
|
public function aCorrectRequestReturnsTheQrCodeResponse(): void
|
2016-08-09 07:18:20 -05:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2019-10-22 12:52:28 -05:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode, '')->willReturn(new ShortUrl(''))
|
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2016-08-09 07:18:20 -05:00
|
|
|
|
2017-03-24 17:19:42 -05:00
|
|
|
$resp = $this->action->process(
|
2018-12-25 16:01:30 -06:00
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode),
|
2017-03-24 17:19:42 -05:00
|
|
|
$delegate->reveal()
|
2016-08-09 07:18:20 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(QrCodeResponse::class, $resp);
|
|
|
|
$this->assertEquals(200, $resp->getStatusCode());
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
|
2016-08-09 07:18:20 -05:00
|
|
|
}
|
|
|
|
}
|