Completed
Pull Request — master (#209)
by Alejandro
04:24
created

GeneratePreviewCommandTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 82
Duplicated Lines 18.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 15
loc 82
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 15 15 1
A previewsForEveryUrlAreGenerated() 0 17 1
A exceptionWillOutputError() 0 18 1
A createPaginator() 0 7 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
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\CLI\Command\ShortUrl\GeneratePreviewCommand;
10
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
11
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
12
use Shlinkio\Shlink\Core\Entity\ShortUrl;
13
use Shlinkio\Shlink\Core\Service\ShortUrlService;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Tester\CommandTester;
16
use Zend\I18n\Translator\Translator;
17
use Zend\Paginator\Adapter\ArrayAdapter;
18
use Zend\Paginator\Paginator;
19
20
class GeneratePreviewCommandTest extends TestCase
21
{
22
    /**
23
     * @var CommandTester
24
     */
25
    protected $commandTester;
26
    /**
27
     * @var ObjectProphecy
28
     */
29
    private $previewGenerator;
30
    /**
31
     * @var ObjectProphecy
32
     */
33
    private $shortUrlService;
34
35 View Code Duplication
    public function setUp()
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...
36
    {
37
        $this->previewGenerator = $this->prophesize(PreviewGenerator::class);
38
        $this->shortUrlService = $this->prophesize(ShortUrlService::class);
39
40
        $command = new GeneratePreviewCommand(
41
            $this->shortUrlService->reveal(),
42
            $this->previewGenerator->reveal(),
43
            Translator::factory([])
44
        );
45
        $app = new Application();
46
        $app->add($command);
47
48
        $this->commandTester = new CommandTester($command);
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function previewsForEveryUrlAreGenerated()
55
    {
56
        $paginator = $this->createPaginator([
57
            (new ShortUrl())->setOriginalUrl('http://foo.com'),
0 ignored issues
show
Deprecated Code introduced by
The method Shlinkio\Shlink\Core\Ent...rtUrl::setOriginalUrl() has been deprecated with message: Use setLongUrl() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
            (new ShortUrl())->setOriginalUrl('https://bar.com'),
0 ignored issues
show
Deprecated Code introduced by
The method Shlinkio\Shlink\Core\Ent...rtUrl::setOriginalUrl() has been deprecated with message: Use setLongUrl() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
            (new ShortUrl())->setOriginalUrl('http://baz.com/something'),
0 ignored issues
show
Deprecated Code introduced by
The method Shlinkio\Shlink\Core\Ent...rtUrl::setOriginalUrl() has been deprecated with message: Use setLongUrl() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60
        ]);
61
        $this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledTimes(1);
62
63
        $this->previewGenerator->generatePreview('http://foo.com')->shouldBeCalledTimes(1);
64
        $this->previewGenerator->generatePreview('https://bar.com')->shouldBeCalledTimes(1);
65
        $this->previewGenerator->generatePreview('http://baz.com/something')->shouldBeCalledTimes(1);
66
67
        $this->commandTester->execute([
68
            'command' => 'shortcode:process-previews',
69
        ]);
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function exceptionWillOutputError()
76
    {
77
        $items = [
78
            (new ShortUrl())->setOriginalUrl('http://foo.com'),
0 ignored issues
show
Deprecated Code introduced by
The method Shlinkio\Shlink\Core\Ent...rtUrl::setOriginalUrl() has been deprecated with message: Use setLongUrl() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
79
            (new ShortUrl())->setOriginalUrl('https://bar.com'),
0 ignored issues
show
Deprecated Code introduced by
The method Shlinkio\Shlink\Core\Ent...rtUrl::setOriginalUrl() has been deprecated with message: Use setLongUrl() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
            (new ShortUrl())->setOriginalUrl('http://baz.com/something'),
0 ignored issues
show
Deprecated Code introduced by
The method Shlinkio\Shlink\Core\Ent...rtUrl::setOriginalUrl() has been deprecated with message: Use setLongUrl() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
81
        ];
82
        $paginator = $this->createPaginator($items);
83
        $this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledTimes(1);
84
        $this->previewGenerator->generatePreview(Argument::any())->willThrow(PreviewGenerationException::class)
85
                                                                 ->shouldBeCalledTimes(count($items));
86
87
        $this->commandTester->execute([
88
            'command' => 'shortcode:process-previews',
89
        ]);
90
        $output = $this->commandTester->getDisplay();
91
        $this->assertEquals(count($items), substr_count($output, 'Error'));
92
    }
93
94
    protected function createPaginator(array $items)
95
    {
96
        $paginator = new Paginator(new ArrayAdapter($items));
97
        $paginator->setItemCountPerPage(count($items));
98
99
        return $paginator;
100
    }
101
}
102