2016-08-18 11:19:33 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-11 20:38:46 +03:00
|
|
|
namespace ShlinkioTest\Shlink\PreviewGenerator\Service;
|
2016-08-18 11:19:33 +03:00
|
|
|
|
|
|
|
use mikehaertl\wkhtmlto\Image;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-18 11:19:33 +03:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-08-11 20:47:15 +03:00
|
|
|
use Shlinkio\Shlink\PreviewGenerator\Exception\PreviewGenerationException;
|
2019-08-11 20:38:46 +03:00
|
|
|
use Shlinkio\Shlink\PreviewGenerator\Image\ImageBuilder;
|
|
|
|
use Shlinkio\Shlink\PreviewGenerator\Service\PreviewGenerator;
|
2016-08-18 14:20:57 +03:00
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
2016-08-18 13:21:26 +03:00
|
|
|
use Zend\ServiceManager\ServiceManager;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:34:02 +03:00
|
|
|
use function sprintf;
|
|
|
|
use function urlencode;
|
2016-08-18 11:19:33 +03:00
|
|
|
|
|
|
|
class PreviewGeneratorTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var PreviewGenerator */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $generator;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $image;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $filesystem;
|
2016-08-18 11:19:33 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-08-18 11:19:33 +03:00
|
|
|
{
|
|
|
|
$this->image = $this->prophesize(Image::class);
|
2016-08-18 14:20:57 +03:00
|
|
|
$this->filesystem = $this->prophesize(Filesystem::class);
|
|
|
|
|
2016-08-18 13:21:26 +03:00
|
|
|
$this->generator = new PreviewGenerator(new ImageBuilder(new ServiceManager(), [
|
|
|
|
'factories' => [
|
|
|
|
Image::class => function () {
|
|
|
|
return $this->image->reveal();
|
|
|
|
},
|
2017-10-12 11:13:20 +03:00
|
|
|
],
|
2016-08-18 14:20:57 +03:00
|
|
|
]), $this->filesystem->reveal(), 'dir');
|
2016-08-18 11:19:33 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2016-08-18 14:20:57 +03:00
|
|
|
public function alreadyProcessedElementsAreNotProcessed()
|
2016-08-18 11:19:33 +03:00
|
|
|
{
|
|
|
|
$url = 'http://foo.com';
|
2016-08-18 14:20:57 +03:00
|
|
|
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(true)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-18 11:19:33 +03:00
|
|
|
$this->image->saveAs(Argument::cetera())->shouldBeCalledTimes(0);
|
2016-08-18 14:20:57 +03:00
|
|
|
$this->assertEquals(sprintf('dir/preview_%s.png', urlencode($url)), $this->generator->generatePreview($url));
|
2016-08-18 11:19:33 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2016-08-18 14:20:57 +03:00
|
|
|
public function nonProcessedElementsAreProcessed()
|
2016-08-18 11:19:33 +03:00
|
|
|
{
|
|
|
|
$url = 'http://foo.com';
|
|
|
|
$cacheId = sprintf('preview_%s.png', urlencode($url));
|
|
|
|
$expectedPath = 'dir/' . $cacheId;
|
|
|
|
|
2016-08-18 14:20:57 +03:00
|
|
|
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(false)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-18 14:20:57 +03:00
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$this->image->saveAs($expectedPath)->shouldBeCalledOnce();
|
|
|
|
$this->image->getError()->willReturn('')->shouldBeCalledOnce();
|
2016-08-18 11:19:33 +03:00
|
|
|
$this->assertEquals($expectedPath, $this->generator->generatePreview($url));
|
|
|
|
}
|
2016-08-18 12:16:56 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
/** @test */
|
2016-08-18 12:16:56 +03:00
|
|
|
public function errorWhileGeneratingPreviewThrowsException()
|
|
|
|
{
|
|
|
|
$url = 'http://foo.com';
|
|
|
|
$cacheId = sprintf('preview_%s.png', urlencode($url));
|
|
|
|
$expectedPath = 'dir/' . $cacheId;
|
|
|
|
|
2016-08-18 14:20:57 +03:00
|
|
|
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(false)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-08-18 14:20:57 +03:00
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$this->image->saveAs($expectedPath)->shouldBeCalledOnce();
|
|
|
|
$this->image->getError()->willReturn('Error!!')->shouldBeCalledOnce();
|
2016-08-18 12:16:56 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->expectException(PreviewGenerationException::class);
|
|
|
|
|
2016-08-18 12:16:56 +03:00
|
|
|
$this->generator->generatePreview($url);
|
|
|
|
}
|
2016-08-18 11:19:33 +03:00
|
|
|
}
|