Completed
Push — master ( b53e51...1009f9 )
by Alejandro
04:13 queued 38s
created

DatabaseConfigCustomizerPluginTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 137
Duplicated Lines 13.87 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 19
loc 137
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A configIsRequestedToTheUser() 19 19 1
B overwriteIsRequestedIfValueIsAlreadySet() 0 29 2
B existingValueIsKeptIfRequested() 0 27 1
A sqliteDatabaseIsImportedWhenRequested() 0 20 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\DatabaseConfigCustomizerPlugin;
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
use Symfony\Component\Filesystem\Filesystem;
15
16
class DatabaseConfigCustomizerPluginTest extends TestCase
17
{
18
    /**
19
     * @var DatabaseConfigCustomizerPlugin
20
     */
21
    private $plugin;
22
    /**
23
     * @var ObjectProphecy
24
     */
25
    private $questionHelper;
26
    /**
27
     * @var ObjectProphecy
28
     */
29
    private $filesystem;
30
31
    public function setUp()
32
    {
33
        $this->questionHelper = $this->prophesize(QuestionHelper::class);
34
        $this->filesystem = $this->prophesize(Filesystem::class);
35
36
        $this->plugin = new DatabaseConfigCustomizerPlugin(
37
            $this->questionHelper->reveal(),
38
            $this->filesystem->reveal()
39
        );
40
    }
41
42
    /**
43
     * @test
44
     */
45 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...
46
    {
47
        /** @var MethodProphecy $askSecret */
48
        $askSecret = $this->questionHelper->ask(Argument::cetera())->willReturn('MySQL');
49
        $config = new CustomizableAppConfig();
50
51
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
52
53
        $this->assertTrue($config->hasDatabase());
54
        $this->assertEquals([
55
            'DRIVER' => 'pdo_mysql',
56
            'NAME' => 'MySQL',
57
            'USER' => 'MySQL',
58
            'PASSWORD' => 'MySQL',
59
            'HOST' => 'MySQL',
60
            'PORT' => 'MySQL',
61
        ], $config->getDatabase());
62
        $askSecret->shouldHaveBeenCalledTimes(6);
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function overwriteIsRequestedIfValueIsAlreadySet()
69
    {
70
        /** @var MethodProphecy $ask */
71
        $ask = $this->questionHelper->ask(Argument::cetera())->will(function (array $args) {
72
            $last = array_pop($args);
73
            return $last instanceof ConfirmationQuestion ? false : 'MySQL';
74
        });
75
        $config = new CustomizableAppConfig();
76
        $config->setDatabase([
77
            'DRIVER' => 'pdo_pgsql',
78
            'NAME' => 'MySQL',
79
            'USER' => 'MySQL',
80
            'PASSWORD' => 'MySQL',
81
            'HOST' => 'MySQL',
82
            'PORT' => 'MySQL',
83
        ]);
84
85
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
86
87
        $this->assertEquals([
88
            'DRIVER' => 'pdo_mysql',
89
            'NAME' => 'MySQL',
90
            'USER' => 'MySQL',
91
            'PASSWORD' => 'MySQL',
92
            'HOST' => 'MySQL',
93
            'PORT' => 'MySQL',
94
        ], $config->getDatabase());
95
        $ask->shouldHaveBeenCalledTimes(7);
96
    }
97
98
    /**
99
     * @test
100
     */
101
    public function existingValueIsKeptIfRequested()
102
    {
103
        /** @var MethodProphecy $ask */
104
        $ask = $this->questionHelper->ask(Argument::cetera())->willReturn(true);
105
106
        $config = new CustomizableAppConfig();
107
        $config->setDatabase([
108
            'DRIVER' => 'pdo_pgsql',
109
            'NAME' => 'MySQL',
110
            'USER' => 'MySQL',
111
            'PASSWORD' => 'MySQL',
112
            'HOST' => 'MySQL',
113
            'PORT' => 'MySQL',
114
        ]);
115
116
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
117
118
        $this->assertEquals([
119
            'DRIVER' => 'pdo_pgsql',
120
            'NAME' => 'MySQL',
121
            'USER' => 'MySQL',
122
            'PASSWORD' => 'MySQL',
123
            'HOST' => 'MySQL',
124
            'PORT' => 'MySQL',
125
        ], $config->getDatabase());
126
        $ask->shouldHaveBeenCalledTimes(1);
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function sqliteDatabaseIsImportedWhenRequested()
133
    {
134
        /** @var MethodProphecy $ask */
135
        $ask = $this->questionHelper->ask(Argument::cetera())->willReturn(true);
136
        /** @var MethodProphecy $copy */
137
        $copy = $this->filesystem->copy(Argument::cetera())->willReturn(null);
138
139
        $config = new CustomizableAppConfig();
140
        $config->setDatabase([
141
            'DRIVER' => 'pdo_sqlite',
142
        ]);
143
144
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
145
146
        $this->assertEquals([
147
            'DRIVER' => 'pdo_sqlite',
148
        ], $config->getDatabase());
149
        $ask->shouldHaveBeenCalledTimes(1);
150
        $copy->shouldHaveBeenCalledTimes(1);
151
    }
152
}
153