RoutesTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Config;
6
7
use AbterPhp\Framework\Exception\Config;
8
use PHPUnit\Framework\TestCase;
9
10
class RoutesTest extends TestCase
11
{
12
    /** @var Routes - System Under Test */
13
    protected Routes $sut;
14
15
    /** @var string|bool|null */
16
    protected $origValue;
17
18
    protected ?string $origName;
19
20
    public function setUp(): void
21
    {
22
        $this->origName  = null;
23
        $this->origValue = null;
24
25
        $this->sut = new Routes();
26
    }
27
28
    public function tearDown(): void
29
    {
30
        parent::tearDown();
31
32
        if (!$this->origName) {
33
            return;
34
        }
35
36
        if ($this->origValue === null) {
37
            putenv($this->origName);
38
            unset($_ENV[$this->origName]);
39
            unset($_SERVER[$this->origName]);
40
41
            return;
42
        }
43
44
        putenv(sprintf("%s=%s", $this->origName, $this->origValue));
45
        $_ENV[$this->origName]    = $this->origValue;
46
        $_SERVER[$this->origName] = $this->origValue;
47
    }
48
49
    protected function newEnvironmentVariable(string $name, ?string $value = null)
50
    {
51
        $this->origValue = getenv($name);
52
53
        if ($value === null) {
54
            putenv($name);
55
            unset($_ENV[$name]);
56
            unset($_SERVER[$name]);
57
        } else {
58
            putenv("$name=$value");
59
            $_ENV[$name]    = $value;
60
            $_SERVER[$name] = $value;
61
        }
62
    }
63
64
    public function testGetLoginPathThrowsExceptionIfRelatedEnvironmentVariableDoesNotExist()
65
    {
66
        $this->newEnvironmentVariable('ADMIN_LOGIN_PATH');
67
68
        $this->expectException(Config::class);
69
70
        $this->sut->getLoginPath();
71
    }
72
73
    public function testGetLoginPathWillReturnEnvironmentVariableValueByDefault()
74
    {
75
        $envValue = 'foo';
76
77
        $this->newEnvironmentVariable('ADMIN_LOGIN_PATH', $envValue);
78
79
        $actualResult = $this->sut->getLoginPath();
80
81
        $this->assertSame($envValue, $actualResult);
82
    }
83
84
    public function testGetLoginPathCanReturnEarlyIfAlreadyRun()
85
    {
86
        $envValue = 'foo';
87
88
        $this->newEnvironmentVariable('ADMIN_LOGIN_PATH', $envValue);
89
90
        $this->sut->getLoginPath();
91
92
        // We're removing any environment variable already set
93
        putenv('ADMIN_LOGIN_PATH');
94
        unset($_ENV['ADMIN_LOGIN_PATH']);
95
        unset($_SERVER['ADMIN_LOGIN_PATH']);
96
97
        $actualResult = $this->sut->getLoginPath();
98
99
        $this->assertSame($envValue, $actualResult);
100
    }
101
102
    public function testGetLoginPathCanBePreset()
103
    {
104
        $envValue    = 'foo';
105
        $presetValue = 'bar';
106
107
        $this->sut->setLoginPath($presetValue);
108
109
        $this->newEnvironmentVariable('ADMIN_LOGIN_PATH', $envValue);
110
111
        $this->sut->getLoginPath();
112
113
        $actualResult = $this->sut->getLoginPath();
114
115
        $this->assertSame($presetValue, $actualResult);
116
    }
117
118
    public function testGetLogoutPathThrowsExceptionIfRelatedEnvironmentVariableDoesNotExist()
119
    {
120
        $this->newEnvironmentVariable('ADMIN_LOGOUT_PATH');
121
122
        $this->expectException(Config::class);
123
124
        $this->sut->getLogoutPath();
125
    }
126
127
    public function testGetLogoutPathWillReturnEnvironmentVariableValueByDefault()
128
    {
129
        $envValue = 'foo';
130
131
        $this->newEnvironmentVariable('ADMIN_LOGOUT_PATH', $envValue);
132
133
        $actualResult = $this->sut->getLogoutPath();
134
135
        $this->assertSame($envValue, $actualResult);
136
    }
137
138
    public function testGetLogoutPathCanReturnEarlyIfAlreadyRun()
139
    {
140
        $envValue = 'foo';
141
142
        $this->newEnvironmentVariable('ADMIN_LOGOUT_PATH', $envValue);
143
144
        $this->sut->getLogoutPath();
145
146
        // We're removing any environment variable already set
147
        putenv('ADMIN_LOGOUT_PATH');
148
        unset($_ENV['ADMIN_LOGOUT_PATH']);
149
        unset($_SERVER['ADMIN_LOGOUT_PATH']);
150
151
        $actualResult = $this->sut->getLogoutPath();
152
153
        $this->assertSame($envValue, $actualResult);
154
    }
155
156
    public function testGetLogoutPathCanBePreset()
157
    {
158
        $envValue    = 'foo';
159
        $presetValue = 'bar';
160
161
        $this->sut->setLogoutPath($presetValue);
162
163
        $this->newEnvironmentVariable('ADMIN_LOGOUT_PATH', $envValue);
164
165
        $this->sut->getLogoutPath();
166
167
        $actualResult = $this->sut->getLogoutPath();
168
169
        $this->assertSame($presetValue, $actualResult);
170
    }
171
172
    public function testGetAdminBasePathThrowExceptionIfRelatedEnvironmentVariableDoesNotExist()
173
    {
174
        $this->newEnvironmentVariable('ADMIN_BASE_PATH');
175
176
        $this->expectException(Config::class);
177
178
        $this->sut->getAdminBasePath();
179
    }
180
181
    public function testGetAdminBasePathWillReturnEnvironmentVariableValueByDefault()
182
    {
183
        $envValue = 'foo';
184
185
        $this->newEnvironmentVariable('ADMIN_BASE_PATH', $envValue);
186
187
        $actualResult = $this->sut->getAdminBasePath();
188
189
        $this->assertSame($envValue, $actualResult);
190
    }
191
192
    public function testGetAdminBasePathCanReturnEarlyIfAlreadyRun()
193
    {
194
        $envValue = 'foo';
195
196
        $this->newEnvironmentVariable('ADMIN_BASE_PATH', $envValue);
197
198
        $this->sut->getAdminBasePath();
199
200
        // We're removing any environment variable already set
201
        putenv('ADMIN_BASE_PATH');
202
        unset($_ENV['ADMIN_BASE_PATH']);
203
        unset($_SERVER['ADMIN_BASE_PATH']);
204
205
        $actualResult = $this->sut->getAdminBasePath();
206
207
        $this->assertSame($envValue, $actualResult);
208
    }
209
210
    public function testGetAdminBasenPathCanBePreset()
211
    {
212
        $envValue    = 'foo';
213
        $presetValue = 'bar';
214
215
        $this->sut->setAdminBasePath($presetValue);
216
217
        $this->newEnvironmentVariable('ADMIN_BASE_PATH', $envValue);
218
219
        $actualResult = $this->sut->getAdminBasePath();
220
221
        $this->assertSame($presetValue, $actualResult);
222
    }
223
224
    public function testGetApiBasePathThrowExceptionIfRelatedEnvironmentVariableDoesNotExist()
225
    {
226
        $this->newEnvironmentVariable('API_BASE_PATH');
227
228
        $this->expectException(Config::class);
229
230
        $this->sut->getApiBasePath();
231
    }
232
233
    public function testGetApiBasePathWillReturnEnvironmentVariableValueByDefault()
234
    {
235
        $envValue = 'foo';
236
237
        $this->newEnvironmentVariable('API_BASE_PATH', $envValue);
238
239
        $actualResult = $this->sut->getApiBasePath();
240
241
        $this->assertSame($envValue, $actualResult);
242
    }
243
244
    public function testGetApiBasePathCanReturnEarlyIfAlreadyRun()
245
    {
246
        $envValue = 'foo';
247
248
        $this->newEnvironmentVariable('API_BASE_PATH', $envValue);
249
250
        $this->sut->getApiBasePath();
251
252
        // We're removing any environment variable already set
253
        putenv('API_BASE_PATH');
254
        unset($_ENV['API_BASE_PATH']);
255
        unset($_SERVER['API_BASE_PATH']);
256
257
        $actualResult = $this->sut->getApiBasePath();
258
259
        $this->assertSame($envValue, $actualResult);
260
    }
261
262
    public function testGetApiBasenPathCanBePreset()
263
    {
264
        $envValue    = 'foo';
265
        $presetValue = 'bar';
266
267
        $this->sut->setApiBasePath($presetValue);
268
269
        $this->newEnvironmentVariable('API_BASE_PATH', $envValue);
270
271
        $actualResult = $this->sut->getApiBasePath();
272
273
        $this->assertSame($presetValue, $actualResult);
274
    }
275
276
    public function testGetLoginFailurePathContainsAdminPath()
277
    {
278
        $adminBasePath = $this->sut->getAdminBasePath();
279
280
        $actualResult = $this->sut->getLoginFailurePath();
281
282
        $this->assertNotEmpty($adminBasePath);
283
        $this->assertStringContainsString($adminBasePath, $actualResult);
284
    }
285
286
    public function testGetLoginSuccessPathContainsAdminPath()
287
    {
288
        $adminBasePath = $this->sut->getAdminBasePath();
289
290
        $actualResult = $this->sut->getLoginSuccessPath();
291
292
        $this->assertNotEmpty($adminBasePath);
293
        $this->assertStringContainsString($adminBasePath, $actualResult);
294
    }
295
296
    public function testGetProfilePathContainsAdminPath()
297
    {
298
        $adminBasePath = $this->sut->getAdminBasePath();
299
300
        $actualResult = $this->sut->getProfilePath();
301
302
        $this->assertNotEmpty($adminBasePath);
303
        $this->assertStringContainsString($adminBasePath, $actualResult);
304
    }
305
306
    public function testGetUploadUrl()
307
    {
308
        $uploadUrl = 'foo';
309
310
        $this->sut->setUploadUrl($uploadUrl);
311
312
        $actualResult = $this->sut->getUploadUrl();
313
314
        $this->assertSame($uploadUrl, $actualResult);
315
    }
316
}
317