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

85 lines
3 KiB
PHP
Raw Normal View History

<?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;
use mikehaertl\wkhtmlto\Image;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
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;
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 12:53:45 +03: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 11:13:20 +03:00
],
]), $this->filesystem->reveal(), 'dir');
}
2019-02-17 22:28:34 +03:00
/** @test */
public function alreadyProcessedElementsAreNotProcessed()
{
$url = 'http://foo.com';
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(true)
2018-11-11 15:18:21 +03: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 22:28:34 +03: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 15:18:21 +03:00
->shouldBeCalledOnce();
2018-11-11 15:18:21 +03:00
$this->image->saveAs($expectedPath)->shouldBeCalledOnce();
$this->image->getError()->willReturn('')->shouldBeCalledOnce();
$this->assertEquals($expectedPath, $this->generator->generatePreview($url));
}
2019-02-16 12:53:45 +03: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 15:18:21 +03:00
->shouldBeCalledOnce();
2018-11-11 15:18:21 +03:00
$this->image->saveAs($expectedPath)->shouldBeCalledOnce();
$this->image->getError()->willReturn('Error!!')->shouldBeCalledOnce();
2019-02-16 12:53:45 +03:00
$this->expectException(PreviewGenerationException::class);
$this->generator->generatePreview($url);
}
}