Completed
Pull Request — master (#98)
by Alejandro
06:29
created

ApplicationConfigCustomizerPluginTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 39.24 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 31
loc 79
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A configIsRequestedToTheUser() 14 14 1
A overwriteIsRequestedIfValueIsAlreadySet() 0 19 2
A existingValueIsKeptIfRequested() 17 17 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\CLI\Install\Plugin;
3
4
use PHPUnit\Framework\TestCase;
5
use Prophecy\Argument;
6
use Prophecy\Prophecy\MethodProphecy;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Shlinkio\Shlink\CLI\Install\Plugin\ApplicationConfigCustomizerPlugin;
9
use Shlinkio\Shlink\CLI\Model\CustomizableAppConfig;
10
use Symfony\Component\Console\Helper\QuestionHelper;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Output\NullOutput;
13
use Symfony\Component\Console\Question\ConfirmationQuestion;
14
15
class ApplicationConfigCustomizerPluginTest extends TestCase
16
{
17
    /**
18
     * @var ApplicationConfigCustomizerPlugin
19
     */
20
    private $plugin;
21
    /**
22
     * @var ObjectProphecy
23
     */
24
    private $questionHelper;
25
26
    public function setUp()
27
    {
28
        $this->questionHelper = $this->prophesize(QuestionHelper::class);
29
        $this->plugin = new ApplicationConfigCustomizerPlugin($this->questionHelper->reveal());
30
    }
31
32
    /**
33
     * @test
34
     */
35 View Code Duplication
    public function configIsRequestedToTheUser()
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
        /** @var MethodProphecy $askSecret */
38
        $askSecret = $this->questionHelper->ask(Argument::cetera())->willReturn('the_secret');
39
        $config = new CustomizableAppConfig();
40
41
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
42
43
        $this->assertTrue($config->hasApp());
44
        $this->assertEquals([
45
            'SECRET' => 'the_secret',
46
        ], $config->getApp());
47
        $askSecret->shouldHaveBeenCalledTimes(1);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function overwriteIsRequestedIfValueIsAlreadySet()
54
    {
55
        /** @var MethodProphecy $ask */
56
        $ask = $this->questionHelper->ask(Argument::cetera())->will(function (array $args) {
57
            $last = array_pop($args);
58
            return $last instanceof ConfirmationQuestion ? false : 'the_new_secret';
59
        });
60
        $config = new CustomizableAppConfig();
61
        $config->setApp([
62
            'SECRET' => 'foo',
63
        ]);
64
65
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
66
67
        $this->assertEquals([
68
            'SECRET' => 'the_new_secret',
69
        ], $config->getApp());
70
        $ask->shouldHaveBeenCalledTimes(2);
71
    }
72
73
    /**
74
     * @test
75
     */
76 View Code Duplication
    public function existingValueIsKeptIfRequested()
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...
77
    {
78
        /** @var MethodProphecy $ask */
79
        $ask = $this->questionHelper->ask(Argument::cetera())->willReturn(true);
80
81
        $config = new CustomizableAppConfig();
82
        $config->setApp([
83
            'SECRET' => 'foo',
84
        ]);
85
86
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
87
88
        $this->assertEquals([
89
            'SECRET' => 'foo',
90
        ], $config->getApp());
91
        $ask->shouldHaveBeenCalledTimes(1);
92
    }
93
}
94