1
|
|
|
<?php |
2
|
|
|
namespace ShlinkioTest\Shlink\Common\Service; |
3
|
|
|
|
4
|
|
|
use mikehaertl\wkhtmlto\Image; |
5
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
6
|
|
|
use Prophecy\Argument; |
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
8
|
|
|
use Shlinkio\Shlink\Common\Image\ImageBuilder; |
9
|
|
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Zend\ServiceManager\ServiceManager; |
12
|
|
|
|
13
|
|
|
class PreviewGeneratorTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var PreviewGenerator |
17
|
|
|
*/ |
18
|
|
|
protected $generator; |
19
|
|
|
/** |
20
|
|
|
* @var ObjectProphecy |
21
|
|
|
*/ |
22
|
|
|
protected $image; |
23
|
|
|
/** |
24
|
|
|
* @var ObjectProphecy |
25
|
|
|
*/ |
26
|
|
|
protected $filesystem; |
27
|
|
|
|
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
$this->image = $this->prophesize(Image::class); |
31
|
|
|
$this->filesystem = $this->prophesize(Filesystem::class); |
32
|
|
|
|
33
|
|
|
$this->generator = new PreviewGenerator(new ImageBuilder(new ServiceManager(), [ |
34
|
|
|
'factories' => [ |
35
|
|
|
Image::class => function () { |
36
|
|
|
return $this->image->reveal(); |
37
|
|
|
}, |
38
|
|
|
] |
39
|
|
|
]), $this->filesystem->reveal(), 'dir'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @test |
44
|
|
|
*/ |
45
|
|
|
public function alreadyProcessedElementsAreNotProcessed() |
46
|
|
|
{ |
47
|
|
|
$url = 'http://foo.com'; |
48
|
|
|
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(true) |
49
|
|
|
->shouldBeCalledTimes(1); |
50
|
|
|
$this->image->saveAs(Argument::cetera())->shouldBeCalledTimes(0); |
51
|
|
|
$this->assertEquals(sprintf('dir/preview_%s.png', urlencode($url)), $this->generator->generatePreview($url)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function nonProcessedElementsAreProcessed() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$url = 'http://foo.com'; |
60
|
|
|
$cacheId = sprintf('preview_%s.png', urlencode($url)); |
61
|
|
|
$expectedPath = 'dir/' . $cacheId; |
62
|
|
|
|
63
|
|
|
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(false) |
64
|
|
|
->shouldBeCalledTimes(1); |
65
|
|
|
|
66
|
|
|
$this->image->saveAs($expectedPath)->shouldBeCalledTimes(1); |
67
|
|
|
$this->image->getError()->willReturn('')->shouldBeCalledTimes(1); |
68
|
|
|
$this->assertEquals($expectedPath, $this->generator->generatePreview($url)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
* @expectedException \Shlinkio\Shlink\Common\Exception\PreviewGenerationException |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function errorWhileGeneratingPreviewThrowsException() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$url = 'http://foo.com'; |
78
|
|
|
$cacheId = sprintf('preview_%s.png', urlencode($url)); |
79
|
|
|
$expectedPath = 'dir/' . $cacheId; |
80
|
|
|
|
81
|
|
|
$this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(false) |
82
|
|
|
->shouldBeCalledTimes(1); |
83
|
|
|
|
84
|
|
|
$this->image->saveAs($expectedPath)->shouldBeCalledTimes(1); |
85
|
|
|
$this->image->getError()->willReturn('Error!!')->shouldBeCalledTimes(1); |
86
|
|
|
|
87
|
|
|
$this->generator->generatePreview($url); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.