Completed
Push — master ( 065cdd...96faaf )
by Alejandro
09:28
created

PreviewGeneratorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 35.06 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 27
loc 77
rs 10
wmc 4
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A alreadyProcessedElementsAreNotProcessed() 0 8 1
A nonProcessedElementsAreProcessed() 13 13 1
A errorWhileGeneratingPreviewThrowsException() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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