2016-08-18 17:23:40 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-18 17:23:40 +02:00
|
|
|
namespace ShlinkioTest\Shlink\Core\Action;
|
|
|
|
|
2018-10-28 08:24:06 +01:00
|
|
|
use finfo;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-24 22:07:28 +01:00
|
|
|
use Prophecy\Argument;
|
2016-08-18 17:23:40 +02:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-03-26 19:02:41 +02:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-18 17:23:40 +02:00
|
|
|
use Shlinkio\Shlink\Core\Action\PreviewAction;
|
2018-08-11 10:40:44 +02:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2019-11-24 23:11:25 +01:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2016-08-18 17:23:40 +02:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
2019-08-11 19:38:46 +02:00
|
|
|
use Shlinkio\Shlink\PreviewGenerator\Service\PreviewGenerator;
|
2017-10-13 12:22:19 +02:00
|
|
|
use Zend\Diactoros\Response;
|
2018-12-25 23:01:30 +01:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2019-02-26 22:56:43 +01:00
|
|
|
|
2018-10-28 08:34:02 +01:00
|
|
|
use function filesize;
|
2016-08-18 17:23:40 +02:00
|
|
|
|
2019-08-01 19:49:54 +02:00
|
|
|
use const FILEINFO_MIME;
|
|
|
|
|
2016-08-18 17:23:40 +02:00
|
|
|
class PreviewActionTest extends TestCase
|
|
|
|
{
|
2018-11-20 19:30:27 +01:00
|
|
|
/** @var PreviewAction */
|
2018-11-20 19:37:22 +01:00
|
|
|
private $action;
|
2018-11-20 19:30:27 +01:00
|
|
|
/** @var ObjectProphecy */
|
2016-08-18 17:23:40 +02:00
|
|
|
private $previewGenerator;
|
2018-11-20 19:30:27 +01:00
|
|
|
/** @var ObjectProphecy */
|
2016-08-18 17:23:40 +02:00
|
|
|
private $urlShortener;
|
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
public function setUp(): void
|
2016-08-18 17:23:40 +02:00
|
|
|
{
|
|
|
|
$this->previewGenerator = $this->prophesize(PreviewGenerator::class);
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
|
|
|
$this->action = new PreviewAction($this->previewGenerator->reveal(), $this->urlShortener->reveal());
|
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2019-08-12 18:34:52 +02:00
|
|
|
public function correctShortCodeReturnsImageResponse(): void
|
2016-08-18 17:23:40 +02:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$url = 'foobar.com';
|
2018-10-28 16:00:54 +01:00
|
|
|
$shortUrl = new ShortUrl($url);
|
2016-08-18 17:23:40 +02:00
|
|
|
$path = __FILE__;
|
2018-11-11 13:18:21 +01:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($shortUrl)->shouldBeCalledOnce();
|
|
|
|
$this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledOnce();
|
2016-08-18 17:23:40 +02:00
|
|
|
|
2017-03-24 22:07:28 +01:00
|
|
|
$resp = $this->action->process(
|
2018-12-25 23:01:30 +01:00
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode),
|
2019-08-12 18:34:52 +02:00
|
|
|
$this->prophesize(RequestHandlerInterface::class)->reveal()
|
2016-08-18 17:23:40 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
|
2018-10-28 08:24:06 +01:00
|
|
|
$this->assertEquals((new finfo(FILEINFO_MIME))->file($path), $resp->getHeaderLine('Content-type'));
|
2016-08-18 17:23:40 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2019-08-12 18:34:52 +02:00
|
|
|
public function invalidShortCodeExceptionFallsBackToNextMiddleware(): void
|
2016-08-18 17:23:40 +02:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2019-11-24 23:11:25 +01:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(ShortUrlNotFoundException::class)
|
2018-11-11 13:18:21 +01:00
|
|
|
->shouldBeCalledOnce();
|
2018-03-26 19:02:41 +02:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::any())->willReturn(new Response());
|
2016-08-18 17:23:40 +02:00
|
|
|
|
2017-03-24 22:07:28 +01:00
|
|
|
$this->action->process(
|
2018-12-25 23:01:30 +01:00
|
|
|
(new ServerRequest())->withAttribute('shortCode', $shortCode),
|
2017-03-24 22:07:28 +01:00
|
|
|
$delegate->reveal()
|
2016-08-18 17:23:40 +02:00
|
|
|
);
|
|
|
|
|
2018-11-11 13:18:21 +01:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-18 17:23:40 +02:00
|
|
|
}
|
|
|
|
}
|