Completed
Pull Request — master (#105)
by Mikołaj
05:54
created

overwriteIsRequestedIfValueIsAlreadySet()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 25
rs 8.8571
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\UrlShortenerConfigCustomizerPlugin;
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 UrlShortenerConfigCustomizerPluginTest extends TestCase
16
{
17
    /**
18
     * @var UrlShortenerConfigCustomizerPlugin
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 UrlShortenerConfigCustomizerPlugin($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('something');
39
        $config = new CustomizableAppConfig();
40
41
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
42
43
        $this->assertTrue($config->hasUrlShortener());
44
        $this->assertEquals([
45
            'SCHEMA' => 'something',
46
            'HOSTNAME' => 'something',
47
            'CHARS' => 'something',
48
            'VALIDATE_URL' => 'something',
49
        ], $config->getUrlShortener());
50
        $askSecret->shouldHaveBeenCalledTimes(4);
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function overwriteIsRequestedIfValueIsAlreadySet()
57
    {
58
        /** @var MethodProphecy $ask */
59
        $ask = $this->questionHelper->ask(Argument::cetera())->will(function (array $args) {
60
            $last = array_pop($args);
61
            return $last instanceof ConfirmationQuestion ? false : 'foo';
62
        });
63
        $config = new CustomizableAppConfig();
64
        $config->setUrlShortener([
65
            'SCHEMA' => 'bar',
66
            'HOSTNAME' => 'bar',
67
            'CHARS' => 'bar',
68
            'VALIDATE_URL' => 'bar',
69
        ]);
70
71
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
72
73
        $this->assertEquals([
74
            'SCHEMA' => 'foo',
75
            'HOSTNAME' => 'foo',
76
            'CHARS' => 'foo',
77
            'VALIDATE_URL' => false,
78
        ], $config->getUrlShortener());
79
        $ask->shouldHaveBeenCalledTimes(5);
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function existingValueIsKeptIfRequested()
86
    {
87
        /** @var MethodProphecy $ask */
88
        $ask = $this->questionHelper->ask(Argument::cetera())->willReturn(true);
89
90
        $config = new CustomizableAppConfig();
91
        $config->setUrlShortener([
92
            'SCHEMA' => 'foo',
93
            'HOSTNAME' => 'foo',
94
            'CHARS' => 'foo',
95
            'VALIDATE_URL' => 'foo',
96
        ]);
97
98
        $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
99
100
        $this->assertEquals([
101
            'SCHEMA' => 'foo',
102
            'HOSTNAME' => 'foo',
103
            'CHARS' => 'foo',
104
            'VALIDATE_URL' => 'foo',
105
        ], $config->getUrlShortener());
106
        $ask->shouldHaveBeenCalledTimes(1);
107
    }
108
}
109