Completed
Pull Request — master (#252)
by Alejandro
03:36 queued 01:06
created

redirectUrlOptionIsNotAskedIfAnswerToPreviousQuestionIsNo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 25
rs 9.6333
c 0
b 0
f 0
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
            'ENABLE_NOT_FOUND_REDIRECTION' => true,
50
            'NOT_FOUND_REDIRECT_TO' => 'asked',
51
        ], $config->getUrlShortener());
52
        $ask->shouldHaveBeenCalledTimes(3);
53
        $choice->shouldHaveBeenCalledTimes(1);
54
        $confirm->shouldHaveBeenCalledTimes(2);
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function onlyMissingOptionsAreAsked()
61
    {
62
        $choice = $this->io->choice(Argument::cetera())->willReturn('chosen');
63
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
64
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(false);
65
        $config = new CustomizableAppConfig();
66
        $config->setUrlShortener([
67
            'SCHEMA' => 'foo',
68
            'HOSTNAME' => 'foo',
69
            'ENABLE_NOT_FOUND_REDIRECTION' => true,
70
            'NOT_FOUND_REDIRECT_TO' => 'foo',
71
        ]);
72
73
        $this->plugin->process($this->io->reveal(), $config);
74
75
        $this->assertEquals([
76
            'SCHEMA' => 'foo',
77
            'HOSTNAME' => 'foo',
78
            'CHARS' => 'asked',
79
            'VALIDATE_URL' => false,
80
            'ENABLE_NOT_FOUND_REDIRECTION' => true,
81
            'NOT_FOUND_REDIRECT_TO' => 'foo',
82
        ], $config->getUrlShortener());
83
        $choice->shouldNotHaveBeenCalled();
84
        $ask->shouldHaveBeenCalledTimes(1);
85
        $confirm->shouldHaveBeenCalledTimes(1);
86
    }
87
88
    /**
89
     * @test
90
     */
91
    public function noQuestionsAskedIfImportedConfigContainsEverything()
92
    {
93
        $choice = $this->io->choice(Argument::cetera())->willReturn('chosen');
94
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
95
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(false);
96
97
        $config = new CustomizableAppConfig();
98
        $config->setUrlShortener([
99
            'SCHEMA' => 'foo',
100
            'HOSTNAME' => 'foo',
101
            'CHARS' => 'foo',
102
            'VALIDATE_URL' => true,
103
            'ENABLE_NOT_FOUND_REDIRECTION' => true,
104
            'NOT_FOUND_REDIRECT_TO' => 'foo',
105
        ]);
106
107
        $this->plugin->process($this->io->reveal(), $config);
108
109
        $this->assertEquals([
110
            'SCHEMA' => 'foo',
111
            'HOSTNAME' => 'foo',
112
            'CHARS' => 'foo',
113
            'VALIDATE_URL' => true,
114
            'ENABLE_NOT_FOUND_REDIRECTION' => true,
115
            'NOT_FOUND_REDIRECT_TO' => 'foo',
116
        ], $config->getUrlShortener());
117
        $choice->shouldNotHaveBeenCalled();
118
        $ask->shouldNotHaveBeenCalled();
119
        $confirm->shouldNotHaveBeenCalled();
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function redirectUrlOptionIsNotAskedIfAnswerToPreviousQuestionIsNo()
126
    {
127
        $ask = $this->io->ask(Argument::cetera())->willReturn('asked');
128
        $confirm = $this->io->confirm(Argument::cetera())->willReturn(false);
129
130
        $config = new CustomizableAppConfig();
131
        $config->setUrlShortener([
132
            'SCHEMA' => 'foo',
133
            'HOSTNAME' => 'foo',
134
            'CHARS' => 'foo',
135
            'VALIDATE_URL' => true,
136
        ]);
137
138
        $this->plugin->process($this->io->reveal(), $config);
139
140
        $this->assertTrue($config->hasUrlShortener());
141
        $this->assertEquals([
142
            'SCHEMA' => 'foo',
143
            'HOSTNAME' => 'foo',
144
            'CHARS' => 'foo',
145
            'VALIDATE_URL' => true,
146
            'ENABLE_NOT_FOUND_REDIRECTION' => false,
147
        ], $config->getUrlShortener());
148
        $ask->shouldNotHaveBeenCalled();
149
        $confirm->shouldHaveBeenCalledTimes(1);
150
    }
151
}
152