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;
|
|
|
|
|
2017-03-25 11:22:00 +03:00
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 00:07:28 +03:00
|
|
|
use Prophecy\Argument;
|
2016-08-18 18:23:40 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
|
|
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
|
|
|
use Shlinkio\Shlink\Core\Action\PreviewAction;
|
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;
|
2016-08-18 18:23:40 +03:00
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
|
|
|
|
class PreviewActionTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var PreviewAction
|
|
|
|
*/
|
|
|
|
protected $action;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
private $previewGenerator;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
->shouldBeCalledTimes(1);
|
2017-03-25 11:22:00 +03:00
|
|
|
$delegate = $this->prophesize(DelegateInterface::class);
|
|
|
|
$delegate->process(Argument::cetera())->shouldBeCalledTimes(1);
|
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';
|
|
|
|
$path = __FILE__;
|
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($url)->shouldBeCalledTimes(1);
|
|
|
|
$this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledTimes(1);
|
|
|
|
|
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),
|
2017-03-25 00:07:28 +03:00
|
|
|
TestUtils::createDelegateMock()->reveal()
|
2016-08-18 18:23:40 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
|
|
|
|
$this->assertEquals((new \finfo(FILEINFO_MIME))->file($path), $resp->getHeaderLine('Content-type'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)
|
|
|
|
->shouldBeCalledTimes(1);
|
2017-03-25 11:22:00 +03:00
|
|
|
$delegate = $this->prophesize(DelegateInterface::class);
|
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
|
|
|
);
|
|
|
|
|
2017-03-25 00:07:28 +03:00
|
|
|
$delegate->process(Argument::any())->shouldHaveBeenCalledTimes(1);
|
2016-08-18 18:23:40 +03:00
|
|
|
}
|
|
|
|
}
|