Completed
Pull Request — master (#224)
by Alejandro
03:14
created

UrlShortenerConfigCustomizerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 99
Duplicated Lines 49.49 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A configIsRequestedToTheUser() 0 20 1
A onlyMissingOptionsAreAsked() 23 23 1
A noQuestionsAskedIfImportedConfigContainsEverything() 26 26 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\Installer\Config\Plugin;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\Installer\Config\Plugin\UrlShortenerConfigCustomizer;
10
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class UrlShortenerConfigCustomizerTest extends TestCase
14
{
15
    /**
16
     * @var UrlShortenerConfigCustomizer
17
     */
18
    private $plugin;
19
    /**
20
     * @var ObjectProphecy
21
     */
22
    private $io;
23
24
    public function setUp()
25
    {
26
        $this->io = $this->prophesize(SymfonyStyle::class);
27
        $this->io->title(Argument::any())->willReturn(null);
28
        $this->plugin = new UrlShortenerConfigCustomizer();
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function configIsRequestedToTheUser()
35
    {
36
        $choice = $this->io->choice(Argument::cetera())->willReturn('chosen');
37
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
38
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(true);
39
        $config = new CustomizableAppConfig();
40
41
        $this->plugin->process($this->io->reveal(), $config);
42
43
        $this->assertTrue($config->hasUrlShortener());
44
        $this->assertEquals([
45
            'SCHEMA' => 'chosen',
46
            'HOSTNAME' => 'asked',
47
            'CHARS' => 'asked',
48
            'VALIDATE_URL' => true,
49
        ], $config->getUrlShortener());
50
        $ask->shouldHaveBeenCalledTimes(2);
51
        $choice->shouldHaveBeenCalledTimes(1);
52
        $confirm->shouldHaveBeenCalledTimes(1);
53
    }
54
55
    /**
56
     * @test
57
     */
58 View Code Duplication
    public function onlyMissingOptionsAreAsked()
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...
59
    {
60
        $choice = $this->io->choice(Argument::cetera())->willReturn('chosen');
61
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
62
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(false);
63
        $config = new CustomizableAppConfig();
64
        $config->setUrlShortener([
65
            'SCHEMA' => 'foo',
66
            'HOSTNAME' => 'foo',
67
        ]);
68
69
        $this->plugin->process($this->io->reveal(), $config);
70
71
        $this->assertEquals([
72
            'SCHEMA' => 'foo',
73
            'HOSTNAME' => 'foo',
74
            'CHARS' => 'asked',
75
            'VALIDATE_URL' => false,
76
        ], $config->getUrlShortener());
77
        $choice->shouldNotHaveBeenCalled();
78
        $ask->shouldHaveBeenCalledTimes(1);
79
        $confirm->shouldHaveBeenCalledTimes(1);
80
    }
81
82
    /**
83
     * @test
84
     */
85 View Code Duplication
    public function noQuestionsAskedIfImportedConfigContainsEverything()
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...
86
    {
87
        $choice = $this->io->choice(Argument::cetera())->willReturn('chosen');
88
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
89
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(false);
90
91
        $config = new CustomizableAppConfig();
92
        $config->setUrlShortener([
93
            'SCHEMA' => 'foo',
94
            'HOSTNAME' => 'foo',
95
            'CHARS' => 'foo',
96
            'VALIDATE_URL' => true,
97
        ]);
98
99
        $this->plugin->process($this->io->reveal(), $config);
100
101
        $this->assertEquals([
102
            'SCHEMA' => 'foo',
103
            'HOSTNAME' => 'foo',
104
            'CHARS' => 'foo',
105
            'VALIDATE_URL' => true,
106
        ], $config->getUrlShortener());
107
        $choice->shouldNotHaveBeenCalled();
108
        $ask->shouldNotHaveBeenCalled();
109
        $confirm->shouldNotHaveBeenCalled();
110
    }
111
}
112