shlink/module/PreviewGenerator/test/Service/PreviewGeneratorTest.php

86 lines
3 KiB
PHP
Raw Normal View History

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