2016-08-18 18:23:40 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-18 18:23:40 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Action;
|
|
|
|
|
2018-10-28 10:24:06 +03:00
|
|
|
use finfo;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 00:07:28 +03:00
|
|
|
use Prophecy\Argument;
|
2017-10-13 13:22:19 +03:00
|
|
|
use Prophecy\Prophecy\MethodProphecy;
|
2016-08-18 18:23:40 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-03-26 20:02:41 +03:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-18 18:23:40 +03:00
|
|
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
|
|
|
use Shlinkio\Shlink\Core\Action\PreviewAction;
|
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-18 18:23:40 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2017-03-25 00:07:28 +03:00
|
|
|
use ShlinkioTest\Shlink\Common\Util\TestUtils;
|
2017-10-13 13:22:19 +03:00
|
|
|
use Zend\Diactoros\Response;
|
2016-08-18 18:23:40 +03:00
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
2018-10-28 10:34:02 +03:00
|
|
|
use const FILEINFO_MIME;
|
|
|
|
use function filesize;
|
2016-08-18 18:23:40 +03:00
|
|
|
|
|
|
|
class PreviewActionTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var PreviewAction */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $action;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2016-08-18 18:23:40 +03:00
|
|
|
private $previewGenerator;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2016-08-18 18:23:40 +03:00
|
|
|
private $urlShortener;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->previewGenerator = $this->prophesize(PreviewGenerator::class);
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
|
|
|
$this->action = new PreviewAction($this->previewGenerator->reveal(), $this->urlShortener->reveal());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2017-03-25 00:07:28 +03:00
|
|
|
public function invalidShortCodeFallsBackToNextMiddleware()
|
2016-08-18 18:23:40 +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);
|
2018-11-11 15:18:21 +03:00
|
|
|
$delegate->handle(Argument::cetera())->shouldBeCalledOnce()
|
2017-10-13 13:22:19 +03:00
|
|
|
->willReturn(new Response());
|
2016-08-18 18:23:40 +03:00
|
|
|
|
2017-03-25 00:07:28 +03:00
|
|
|
$this->action->process(
|
2016-08-18 18:23:40 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 00:07:28 +03:00
|
|
|
$delegate->reveal()
|
2016-08-18 18:23:40 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function correctShortCodeReturnsImageResponse()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$url = 'foobar.com';
|
2018-10-28 18:00:54 +03:00
|
|
|
$shortUrl = new ShortUrl($url);
|
2016-08-18 18:23:40 +03:00
|
|
|
$path = __FILE__;
|
2018-11-11 15:18:21 +03:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($shortUrl)->shouldBeCalledOnce();
|
|
|
|
$this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledOnce();
|
2016-08-18 18:23:40 +03:00
|
|
|
|
2017-03-25 00:07:28 +03:00
|
|
|
$resp = $this->action->process(
|
2016-08-18 18:23:40 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2018-03-26 20:02:41 +03:00
|
|
|
TestUtils::createReqHandlerMock()->reveal()
|
2016-08-18 18:23:40 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
|
2018-10-28 10:24:06 +03:00
|
|
|
$this->assertEquals((new finfo(FILEINFO_MIME))->file($path), $resp->getHeaderLine('Content-type'));
|
2016-08-18 18:23:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2017-03-25 11:22:00 +03:00
|
|
|
public function invalidShortCodeExceptionFallsBackToNextMiddleware()
|
2016-08-18 18:23:40 +03:00
|
|
|
{
|
|
|
|
$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-18 18:23:40 +03:00
|
|
|
|
2017-03-25 00:07:28 +03:00
|
|
|
$this->action->process(
|
2016-08-18 18:23:40 +03:00
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
2017-03-25 00:07:28 +03:00
|
|
|
$delegate->reveal()
|
2016-08-18 18:23:40 +03:00
|
|
|
);
|
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-18 18:23:40 +03:00
|
|
|
}
|
|
|
|
}
|