2016-08-09 15:18:20 +03:00
|
|
|
<?php
|
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;
|
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-09 15:18:20 +03:00
|
|
|
use Prophecy\Argument;
|
2017-10-13 13:22:19 +03:00
|
|
|
use Prophecy\Prophecy\MethodProphecy;
|
2016-08-09 15:18:20 +03:00
|
|
|
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;
|
2017-10-12 12:28:45 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
2016-08-09 15:18:20 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2017-10-13 13:22:19 +03:00
|
|
|
use Zend\Diactoros\Response;
|
2016-08-09 15:18:20 +03:00
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
use Zend\Expressive\Router\RouterInterface;
|
|
|
|
|
|
|
|
class QrCodeActionTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var QrCodeAction */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $action;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $urlShortener;
|
2016-08-09 15:18:20 +03:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2017-03-25 01:19:42 +03:00
|
|
|
public function aNotFoundShortCodeWillDelegateIntoNextMiddleware()
|
2016-08-09 15:18:20 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2017-10-12 12:28:45 +03:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
|
2018-11-11 15:18:21 +03:00
|
|
|
->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
|
|
|
|
2017-03-25 01:19:42 +03:00
|
|
|
$this->action->process(
|
2016-08-09 15:18:20 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 01:19:42 +03:00
|
|
|
$delegate->reveal()
|
2016-08-09 15:18:20 +03:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function anInvalidShortCodeWillReturnNotFoundResponse()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-10-13 13:22:19 +03:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 20:02:41 +03:00
|
|
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
2016-08-09 15:18:20 +03:00
|
|
|
|
2017-03-25 01:19:42 +03:00
|
|
|
$this->action->process(
|
2016-08-09 15:18:20 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 01:19:42 +03:00
|
|
|
$delegate->reveal()
|
2016-08-09 15:18:20 +03:00
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function aCorrectRequestReturnsTheQrCodeResponse()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2018-10-28 18:00:54 +03:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(new ShortUrl(''))
|
2018-11-11 15:18:21 +03:00
|
|
|
->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(
|
2016-08-09 15:18:20 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 01:19:42 +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
|
|
|
}
|
|
|
|
}
|