1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Config; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Shlinkio\Shlink\Core\Config\SimplifiedConfigParser; |
9
|
|
|
|
10
|
|
|
use function array_merge; |
11
|
|
|
|
12
|
|
|
class SimplifiedConfigParserTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private $postProcessor; |
15
|
|
|
|
16
|
|
|
public function setUp(): void |
17
|
|
|
{ |
18
|
|
|
$this->postProcessor = new SimplifiedConfigParser(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** @test */ |
22
|
|
|
public function properlyMapsSimplifiedConfig(): void |
23
|
|
|
{ |
24
|
|
|
$config = [ |
25
|
|
|
'app_options' => [ |
26
|
|
|
'disable_track_param' => 'foo', |
27
|
|
|
], |
28
|
|
|
|
29
|
|
|
'entity_manager' => [ |
30
|
|
|
'connection' => [ |
31
|
|
|
'driver' => 'mysql', |
32
|
|
|
'host' => 'shlink_db', |
33
|
|
|
'port' => '3306', |
34
|
|
|
], |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
$simplified = [ |
38
|
|
|
'disable_track_param' => 'bar', |
39
|
|
|
'short_domain_schema' => 'https', |
40
|
|
|
'short_domain_host' => 'doma.in', |
41
|
|
|
'validate_url' => false, |
42
|
|
|
'delete_short_url_threshold' => 50, |
43
|
|
|
'not_found_redirect_to' => 'foobar.com', |
44
|
|
|
'redis_servers' => [ |
45
|
|
|
'tcp://1.1.1.1:1111', |
46
|
|
|
'tcp://1.2.2.2:2222', |
47
|
|
|
], |
48
|
|
|
'db_config' => [ |
49
|
|
|
'dbname' => 'shlink', |
50
|
|
|
'user' => 'foo', |
51
|
|
|
'password' => 'bar', |
52
|
|
|
'port' => '1234', |
53
|
|
|
], |
54
|
|
|
'base_path' => '/foo/bar', |
55
|
|
|
]; |
56
|
|
|
$expected = [ |
57
|
|
|
'app_options' => [ |
58
|
|
|
'disable_track_param' => 'bar', |
59
|
|
|
], |
60
|
|
|
|
61
|
|
|
'entity_manager' => [ |
62
|
|
|
'connection' => [ |
63
|
|
|
'driver' => 'mysql', |
64
|
|
|
'host' => 'shlink_db', |
65
|
|
|
'dbname' => 'shlink', |
66
|
|
|
'user' => 'foo', |
67
|
|
|
'password' => 'bar', |
68
|
|
|
'port' => '1234', |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
|
72
|
|
|
'url_shortener' => [ |
73
|
|
|
'domain' => [ |
74
|
|
|
'schema' => 'https', |
75
|
|
|
'hostname' => 'doma.in', |
76
|
|
|
], |
77
|
|
|
'validate_url' => false, |
78
|
|
|
], |
79
|
|
|
|
80
|
|
|
'delete_short_urls' => [ |
81
|
|
|
'visits_threshold' => 50, |
82
|
|
|
'check_visits_threshold' => true, |
83
|
|
|
], |
84
|
|
|
|
85
|
|
|
'dependencies' => [ |
86
|
|
|
'aliases' => [ |
87
|
|
|
'lock_store' => 'redis_lock_store', |
88
|
|
|
], |
89
|
|
|
], |
90
|
|
|
|
91
|
|
|
'redis' => [ |
92
|
|
|
'servers' => [ |
93
|
|
|
'tcp://1.1.1.1:1111', |
94
|
|
|
'tcp://1.2.2.2:2222', |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
|
98
|
|
|
'router' => [ |
99
|
|
|
'base_path' => '/foo/bar', |
100
|
|
|
], |
101
|
|
|
|
102
|
|
|
'not_found_redirects' => [ |
103
|
|
|
'invalid_short_url' => 'foobar.com', |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
$result = ($this->postProcessor)(array_merge($config, $simplified)); |
108
|
|
|
|
109
|
|
|
$this->assertEquals(array_merge($expected, $simplified), $result); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @test |
114
|
|
|
* @dataProvider provideConfigWithDeprecates |
115
|
|
|
*/ |
116
|
|
|
public function properlyMapsDeprecatedConfigs(array $config, string $expected): void |
117
|
|
|
{ |
118
|
|
|
$result = ($this->postProcessor)($config); |
119
|
|
|
$this->assertEquals($expected, $result['not_found_redirects']['invalid_short_url']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function provideConfigWithDeprecates(): iterable |
123
|
|
|
{ |
124
|
|
|
yield 'only deprecated config' => [['not_found_redirect_to' => 'old_value'], 'old_value']; |
125
|
|
|
yield 'only new config' => [['invalid_short_url_redirect_to' => 'new_value'], 'new_value']; |
126
|
|
|
yield 'both configs, new first' => [ |
127
|
|
|
['invalid_short_url_redirect_to' => 'new_value', 'not_found_redirect_to' => 'old_value'], |
128
|
|
|
'new_value', |
129
|
|
|
]; |
130
|
|
|
yield 'both configs, deprecated first' => [ |
131
|
|
|
['not_found_redirect_to' => 'old_value', 'invalid_short_url_redirect_to' => 'new_value'], |
132
|
|
|
'new_value', |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|