2016-08-09 15:18:20 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-09 15:18:20 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Action;
|
|
|
|
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequest;
|
|
|
|
use Mezzio\Router\RouterInterface;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-09 15:18:20 +03:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-03-26 20:02:41 +03:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-09 15:18:20 +03:00
|
|
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
|
|
|
use Shlinkio\Shlink\Core\Action\QrCodeAction;
|
2018-08-11 11:40:44 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2019-11-25 01:11:25 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2020-02-01 13:46:54 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2020-01-26 21:21:51 +03:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
2016-08-09 15:18:20 +03:00
|
|
|
|
|
|
|
class QrCodeActionTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:27:00 +03:00
|
|
|
private QrCodeAction $action;
|
2020-01-26 21:21:51 +03:00
|
|
|
private ObjectProphecy $urlResolver;
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
|
|
|
$router = $this->prophesize(RouterInterface::class);
|
|
|
|
$router->generateUri(Argument::cetera())->willReturn('/foo/bar');
|
|
|
|
|
2020-01-26 21:21:51 +03:00
|
|
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2020-04-09 13:31:03 +03:00
|
|
|
$this->action = new QrCodeAction($this->urlResolver->reveal(), ['domain' => 'doma.in']);
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-22 20:52:28 +03:00
|
|
|
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware(): void
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2020-02-01 13:46:54 +03:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
|
|
|
|
->willThrow(ShortUrlNotFoundException::class)
|
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2018-12-26 01:01:30 +03:00
|
|
|
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
|
2017-03-25 01:19:42 +03:00
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-22 20:52:28 +03:00
|
|
|
public function anInvalidShortCodeWillReturnNotFoundResponse(): void
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2020-02-01 13:46:54 +03:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
|
|
|
|
->willThrow(ShortUrlNotFoundException::class)
|
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2018-12-26 01:01:30 +03:00
|
|
|
$this->action->process((new ServerRequest())->withAttribute('shortCode', $shortCode), $delegate->reveal());
|
2017-03-25 01:19:42 +03:00
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-22 20:52:28 +03:00
|
|
|
public function aCorrectRequestReturnsTheQrCodeResponse(): void
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2020-02-01 13:46:54 +03:00
|
|
|
$this->urlResolver->resolveEnabledShortUrl(new ShortUrlIdentifier($shortCode, ''))
|
|
|
|
->willReturn(new ShortUrl(''))
|
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2017-03-25 01:19:42 +03:00
|
|
|
$resp = $this->action->process(
|
2018-12-26 01:01:30 +03:00
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode),
|
2020-01-01 22:48:31 +03:00
|
|
|
$delegate->reveal(),
|
2016-08-09 15:18:20 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(QrCodeResponse::class, $resp);
|
|
|
|
$this->assertEquals(200, $resp->getStatusCode());
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate->handle(Argument::any())->shouldHaveBeenCalledTimes(0);
|
2016-08-09 15:18:20 +03:00
|
|
|
}
|
|
|
|
}
|