|
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\ApplicationConfigCustomizer; |
|
10
|
|
|
use Shlinkio\Shlink\Installer\Exception\InvalidConfigOptionException; |
|
11
|
|
|
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig; |
|
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
13
|
|
|
use function array_shift; |
|
14
|
|
|
use function strpos; |
|
15
|
|
|
|
|
16
|
|
|
class ApplicationConfigCustomizerTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ApplicationConfigCustomizer |
|
20
|
|
|
*/ |
|
21
|
|
|
private $plugin; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ObjectProphecy |
|
24
|
|
|
*/ |
|
25
|
|
|
private $io; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->io = $this->prophesize(SymfonyStyle::class); |
|
30
|
|
|
$this->io->title(Argument::any())->willReturn(null); |
|
31
|
|
|
|
|
32
|
|
|
$this->plugin = new ApplicationConfigCustomizer(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @test |
|
37
|
|
|
*/ |
|
38
|
|
|
public function configIsRequestedToTheUser() |
|
39
|
|
|
{ |
|
40
|
|
|
$ask = $this->io->ask(Argument::cetera())->willReturn('asked'); |
|
41
|
|
|
$confirm = $this->io->confirm(Argument::cetera())->willReturn(false); |
|
42
|
|
|
|
|
43
|
|
|
$config = new CustomizableAppConfig(); |
|
44
|
|
|
|
|
45
|
|
|
$this->plugin->process($this->io->reveal(), $config); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertTrue($config->hasApp()); |
|
48
|
|
|
$this->assertEquals([ |
|
49
|
|
|
'SECRET' => 'asked', |
|
50
|
|
|
'DISABLE_TRACK_PARAM' => 'asked', |
|
51
|
|
|
'CHECK_VISITS_THRESHOLD' => false, |
|
52
|
|
|
], $config->getApp()); |
|
53
|
|
|
$ask->shouldHaveBeenCalledTimes(2); |
|
54
|
|
|
$confirm->shouldHaveBeenCalledTimes(1); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @test |
|
59
|
|
|
*/ |
|
60
|
|
|
public function visitsThresholdIsRequestedIfCheckIsEnabled() |
|
61
|
|
|
{ |
|
62
|
|
|
$ask = $this->io->ask(Argument::cetera())->will(function (array $args) { |
|
63
|
|
|
$message = array_shift($args); |
|
64
|
|
|
return strpos($message, 'What is the amount of visits') === 0 ? 20 : 'asked'; |
|
65
|
|
|
}); |
|
66
|
|
|
$confirm = $this->io->confirm(Argument::cetera())->willReturn(true); |
|
67
|
|
|
|
|
68
|
|
|
$config = new CustomizableAppConfig(); |
|
69
|
|
|
|
|
70
|
|
|
$this->plugin->process($this->io->reveal(), $config); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertTrue($config->hasApp()); |
|
73
|
|
|
$this->assertEquals([ |
|
74
|
|
|
'SECRET' => 'asked', |
|
75
|
|
|
'DISABLE_TRACK_PARAM' => 'asked', |
|
76
|
|
|
'CHECK_VISITS_THRESHOLD' => true, |
|
77
|
|
|
'VISITS_THRESHOLD' => 20, |
|
78
|
|
|
], $config->getApp()); |
|
79
|
|
|
$ask->shouldHaveBeenCalledTimes(3); |
|
80
|
|
|
$confirm->shouldHaveBeenCalledTimes(1); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @test |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function onlyMissingOptionsAreAsked() |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$ask = $this->io->ask(Argument::cetera())->willReturn('disable_param'); |
|
89
|
|
|
$config = new CustomizableAppConfig(); |
|
90
|
|
|
$config->setApp([ |
|
91
|
|
|
'SECRET' => 'foo', |
|
92
|
|
|
'CHECK_VISITS_THRESHOLD' => true, |
|
93
|
|
|
'VISITS_THRESHOLD' => 20, |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
$this->plugin->process($this->io->reveal(), $config); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals([ |
|
99
|
|
|
'SECRET' => 'foo', |
|
100
|
|
|
'DISABLE_TRACK_PARAM' => 'disable_param', |
|
101
|
|
|
'CHECK_VISITS_THRESHOLD' => true, |
|
102
|
|
|
'VISITS_THRESHOLD' => 20, |
|
103
|
|
|
], $config->getApp()); |
|
104
|
|
|
$ask->shouldHaveBeenCalledTimes(1); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @test |
|
109
|
|
|
*/ |
|
110
|
|
View Code Duplication |
public function noQuestionsAskedIfImportedConfigContainsEverything() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$ask = $this->io->ask(Argument::cetera())->willReturn('the_new_secret'); |
|
113
|
|
|
|
|
114
|
|
|
$config = new CustomizableAppConfig(); |
|
115
|
|
|
$config->setApp([ |
|
116
|
|
|
'SECRET' => 'foo', |
|
117
|
|
|
'DISABLE_TRACK_PARAM' => 'the_new_secret', |
|
118
|
|
|
'CHECK_VISITS_THRESHOLD' => true, |
|
119
|
|
|
'VISITS_THRESHOLD' => 20, |
|
120
|
|
|
]); |
|
121
|
|
|
|
|
122
|
|
|
$this->plugin->process($this->io->reveal(), $config); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals([ |
|
125
|
|
|
'SECRET' => 'foo', |
|
126
|
|
|
'DISABLE_TRACK_PARAM' => 'the_new_secret', |
|
127
|
|
|
'CHECK_VISITS_THRESHOLD' => true, |
|
128
|
|
|
'VISITS_THRESHOLD' => 20, |
|
129
|
|
|
], $config->getApp()); |
|
130
|
|
|
$ask->shouldNotHaveBeenCalled(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @test |
|
135
|
|
|
* @dataProvider provideInvalidValues |
|
136
|
|
|
* @param mixed $value |
|
137
|
|
|
*/ |
|
138
|
|
|
public function validateVisitsThresholdThrowsExceptionWhenProvidedValueIsInvalid($value) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->expectException(InvalidConfigOptionException::class); |
|
141
|
|
|
$this->plugin->validateVisitsThreshold($value); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function provideInvalidValues(): array |
|
145
|
|
|
{ |
|
146
|
|
|
return [ |
|
147
|
|
|
'string' => ['foo'], |
|
148
|
|
|
'empty string' => [''], |
|
149
|
|
|
'negative number' => [-5], |
|
150
|
|
|
'negative number as string' => ['-5'], |
|
151
|
|
|
'zero' => [0], |
|
152
|
|
|
'zero as string' => ['0'], |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @test |
|
158
|
|
|
* @dataProvider provideValidValues |
|
159
|
|
|
* @param mixed $value |
|
160
|
|
|
*/ |
|
161
|
|
|
public function validateVisitsThresholdCastsToIntWhenProvidedValueIsValid($value, int $expected) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->assertEquals($expected, $this->plugin->validateVisitsThreshold($value)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function provideValidValues(): array |
|
167
|
|
|
{ |
|
168
|
|
|
return [ |
|
169
|
|
|
'positive as string' => ['20', 20], |
|
170
|
|
|
'positive as integer' => [5, 5], |
|
171
|
|
|
'one as string' => ['1', 1], |
|
172
|
|
|
'one as integer' => [1, 1], |
|
173
|
|
|
]; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
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.