Completed
Push — master ( 1fd677...d734d1 )
by Alejandro
27s queued 11s
created

alreadyProcessedElementsAreNotProcessed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\PreviewGenerator\Service;
5
6
use mikehaertl\wkhtmlto\Image;
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Argument;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Shlinkio\Shlink\PreviewGenerator\Exception\PreviewGenerationException;
11
use Shlinkio\Shlink\PreviewGenerator\Image\ImageBuilder;
12
use Shlinkio\Shlink\PreviewGenerator\Service\PreviewGenerator;
13
use Symfony\Component\Filesystem\Filesystem;
14
use Zend\ServiceManager\ServiceManager;
15
16
use function sprintf;
17
use function urlencode;
18
19
class PreviewGeneratorTest extends TestCase
20
{
21
    /** @var PreviewGenerator */
22
    private $generator;
23
    /** @var ObjectProphecy */
24
    private $image;
25
    /** @var ObjectProphecy */
26
    private $filesystem;
27
28
    public function setUp(): void
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(), [
0 ignored issues
show
Deprecated Code introduced by
The class Shlinkio\Shlink\PreviewG...ator\Image\ImageBuilder has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

33
        $this->generator = new PreviewGenerator(/** @scrutinizer ignore-deprecated */ new ImageBuilder(new ServiceManager(), [
Loading history...
Deprecated Code introduced by
The class Shlinkio\Shlink\PreviewG...ervice\PreviewGenerator has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

33
        $this->generator = /** @scrutinizer ignore-deprecated */ new PreviewGenerator(new ImageBuilder(new ServiceManager(), [
Loading history...
34
            'factories' => [
35
                Image::class => function () {
36
                    return $this->image->reveal();
37
                },
38
            ],
39
        ]), $this->filesystem->reveal(), 'dir');
40
    }
41
42
    /** @test */
43
    public function alreadyProcessedElementsAreNotProcessed()
44
    {
45
        $url = 'http://foo.com';
46
        $this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(true)
47
                                                                                 ->shouldBeCalledOnce();
48
        $this->image->saveAs(Argument::cetera())->shouldBeCalledTimes(0);
49
        $this->assertEquals(sprintf('dir/preview_%s.png', urlencode($url)), $this->generator->generatePreview($url));
50
    }
51
52
    /** @test */
53
    public function nonProcessedElementsAreProcessed()
54
    {
55
        $url = 'http://foo.com';
56
        $cacheId = sprintf('preview_%s.png', urlencode($url));
57
        $expectedPath = 'dir/' . $cacheId;
58
59
        $this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(false)
60
                                                                                 ->shouldBeCalledOnce();
61
62
        $this->image->saveAs($expectedPath)->shouldBeCalledOnce();
63
        $this->image->getError()->willReturn('')->shouldBeCalledOnce();
64
        $this->assertEquals($expectedPath, $this->generator->generatePreview($url));
65
    }
66
67
    /** @test */
68
    public function errorWhileGeneratingPreviewThrowsException()
69
    {
70
        $url = 'http://foo.com';
71
        $cacheId = sprintf('preview_%s.png', urlencode($url));
72
        $expectedPath = 'dir/' . $cacheId;
73
74
        $this->filesystem->exists(sprintf('dir/preview_%s.png', urlencode($url)))->willReturn(false)
75
                                                                                 ->shouldBeCalledOnce();
76
77
        $this->image->saveAs($expectedPath)->shouldBeCalledOnce();
78
        $this->image->getError()->willReturn('Error!!')->shouldBeCalledOnce();
79
80
        $this->expectException(PreviewGenerationException::class);
81
82
        $this->generator->generatePreview($url);
83
    }
84
}
85