Used filesystem check instead of cache check for preview generation

This commit is contained in:
Alejandro Celaya
2016-08-18 13:20:57 +02:00
parent 2c91ded514
commit fac519699a
5 changed files with 47 additions and 39 deletions

View File

@@ -1,13 +1,13 @@
<?php
namespace ShlinkioTest\Shlink\Common\Service;
use Doctrine\Common\Cache\ArrayCache;
use mikehaertl\wkhtmlto\Image;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Image\ImageBuilder;
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
use Symfony\Component\Filesystem\Filesystem;
use Zend\ServiceManager\ServiceManager;
class PreviewGeneratorTest extends TestCase
@@ -21,49 +21,51 @@ class PreviewGeneratorTest extends TestCase
*/
protected $image;
/**
* @var ArrayCache
* @var ObjectProphecy
*/
protected $cache;
protected $filesystem;
public function setUp()
{
$this->image = $this->prophesize(Image::class);
$this->cache = new ArrayCache();
$this->filesystem = $this->prophesize(Filesystem::class);
$this->generator = new PreviewGenerator(new ImageBuilder(new ServiceManager(), [
'factories' => [
Image::class => function () {
return $this->image->reveal();
},
]
]), $this->cache, 'dir');
]), $this->filesystem->reveal(), 'dir');
}
/**
* @test
*/
public function alreadyCachedElementsAreNotProcessed()
public function alreadyProcessedElementsAreNotProcessed()
{
$url = 'http://foo.com';
$this->cache->save(sprintf('preview_%s.png', urlencode($url)), 'dir/foo.png');
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(true)
->shouldBeCalledTimes(1);
$this->image->saveAs(Argument::cetera())->shouldBeCalledTimes(0);
$this->assertEquals('dir/foo.png', $this->generator->generatePreview($url));
$this->assertEquals(sprintf('dir/preview_%s.png', urlencode($url)), $this->generator->generatePreview($url));
}
/**
* @test
*/
public function nonCachedElementsAreProcessedAndThenCached()
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)
->shouldBeCalledTimes(1);
$this->image->saveAs($expectedPath)->shouldBeCalledTimes(1);
$this->image->getError()->willReturn('')->shouldBeCalledTimes(1);
$this->assertFalse($this->cache->contains($cacheId));
$this->assertEquals($expectedPath, $this->generator->generatePreview($url));
$this->assertTrue($this->cache->contains($cacheId));
}
/**
@@ -76,6 +78,9 @@ class PreviewGeneratorTest extends TestCase
$cacheId = sprintf('preview_%s.png', urlencode($url));
$expectedPath = 'dir/' . $cacheId;
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(false)
->shouldBeCalledTimes(1);
$this->image->saveAs($expectedPath)->shouldBeCalledTimes(1);
$this->image->getError()->willReturn('Error!!')->shouldBeCalledTimes(1);