Passed
Pull Request — master (#110)
by Dave
02:26
created

PathTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 674
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 424
dl 0
loc 674
rs 10
c 1
b 0
f 0
wmc 27

26 Methods

Rating   Name   Duplication   Size   Complexity  
B provideIsBasePathTests() 0 80 1
A setUp() 0 9 1
A provideAbsolutePathsWithDifferentRoots() 0 25 1
A testJoin() 0 3 1
A testMakeRelative() 0 3 1
A testIsAbsolute() 0 3 1
A testMakeRelativeFailsIfAbsolutePathAndBasePathNotAbsolute() 0 5 1
B provideCanonicalizationTests() 0 122 1
A providePathTests() 0 18 1
A testCanonicalize() 0 3 1
A tearDown() 0 5 1
B provideMakeRelativeTests() 0 92 1
A testGetHomeDirectoryFailsIfNotSupportedOperationSystem() 0 8 1
A testMakeAbsolute() 0 3 1
A getEnv() 0 8 2
A testIsBasePath() 0 3 1
A testGetHomeDirectoryForUnix() 0 3 1
A testGetHomeDirectoryForWindows() 0 7 1
A provideIsAbsolutePathTests() 0 26 1
A provideIsLocalTests() 0 8 1
A provideMakeAbsoluteTests() 0 64 1
A testMakeAbsoluteDoesNotFailIfDifferentRoot() 0 5 1
A provideJoinTests() 0 59 1
A testJoinVarArgs() 0 6 1
A testMakeRelativeFailsIfAbsolutePathAndBasePathEmpty() 0 5 1
A testMakeRelativeFailsIfDifferentRoot() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the webmozart/path-util package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Tests\Unit\Core\Utils;
13
14
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\InvalidPathException;
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Utils\Path;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * @author Bernhard Schussek <[email protected]>
20
 * @author Thomas Schulz <[email protected]>
21
 */
22
class PathTest extends TestCase
23
{
24
    /** @var array<string,string> */
25
    protected $storedEnv = [];
26
27
    protected function setUp(): void
28
    {
29
        $this->storedEnv['HOME'] = $this->getEnv('HOME');
30
        $this->storedEnv['HOMEDRIVE'] = $this->getEnv('HOMEDRIVE');
31
        $this->storedEnv['HOMEPATH'] = $this->getEnv('HOMEPATH');
32
33
        putenv('HOME=/home/webmozart');
34
        putenv('HOMEDRIVE=');
35
        putenv('HOMEPATH=');
36
    }
37
38
    private function getEnv(string $key): string
39
    {
40
        $value = getenv($key);
41
        if (false === $value) {
42
            return '';
43
        }
44
45
        return $value;
46
    }
47
48
    protected function tearDown(): void
49
    {
50
        putenv('HOME='.$this->storedEnv['HOME']);
51
        putenv('HOMEDRIVE='.$this->storedEnv['HOMEDRIVE']);
52
        putenv('HOMEPATH='.$this->storedEnv['HOMEPATH']);
53
    }
54
55
    /** @return array<int,array{string,string}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
56
    public function provideCanonicalizationTests(): array
57
    {
58
        return [
59
            // relative paths (forward slash)
60
            ['css/./style.css', 'css/style.css'],
61
            ['css/../style.css', 'style.css'],
62
            ['css/./../style.css', 'style.css'],
63
            ['css/.././style.css', 'style.css'],
64
            ['css/../../style.css', '../style.css'],
65
            ['./css/style.css', 'css/style.css'],
66
            ['../css/style.css', '../css/style.css'],
67
            ['./../css/style.css', '../css/style.css'],
68
            ['.././css/style.css', '../css/style.css'],
69
            ['../../css/style.css', '../../css/style.css'],
70
            ['', ''],
71
            ['.', ''],
72
            ['..', '..'],
73
            ['./..', '..'],
74
            ['../.', '..'],
75
            ['../..', '../..'],
76
77
            // relative paths (backslash)
78
            ['css\\.\\style.css', 'css/style.css'],
79
            ['css\\..\\style.css', 'style.css'],
80
            ['css\\.\\..\\style.css', 'style.css'],
81
            ['css\\..\\.\\style.css', 'style.css'],
82
            ['css\\..\\..\\style.css', '../style.css'],
83
            ['.\\css\\style.css', 'css/style.css'],
84
            ['..\\css\\style.css', '../css/style.css'],
85
            ['.\\..\\css\\style.css', '../css/style.css'],
86
            ['..\\.\\css\\style.css', '../css/style.css'],
87
            ['..\\..\\css\\style.css', '../../css/style.css'],
88
89
            // absolute paths (forward slash, UNIX)
90
            ['/css/style.css', '/css/style.css'],
91
            ['/css/./style.css', '/css/style.css'],
92
            ['/css/../style.css', '/style.css'],
93
            ['/css/./../style.css', '/style.css'],
94
            ['/css/.././style.css', '/style.css'],
95
            ['/./css/style.css', '/css/style.css'],
96
            ['/../css/style.css', '/css/style.css'],
97
            ['/./../css/style.css', '/css/style.css'],
98
            ['/.././css/style.css', '/css/style.css'],
99
            ['/../../css/style.css', '/css/style.css'],
100
101
            // absolute paths (backslash, UNIX)
102
            ['\\css\\style.css', '/css/style.css'],
103
            ['\\css\\.\\style.css', '/css/style.css'],
104
            ['\\css\\..\\style.css', '/style.css'],
105
            ['\\css\\.\\..\\style.css', '/style.css'],
106
            ['\\css\\..\\.\\style.css', '/style.css'],
107
            ['\\.\\css\\style.css', '/css/style.css'],
108
            ['\\..\\css\\style.css', '/css/style.css'],
109
            ['\\.\\..\\css\\style.css', '/css/style.css'],
110
            ['\\..\\.\\css\\style.css', '/css/style.css'],
111
            ['\\..\\..\\css\\style.css', '/css/style.css'],
112
113
            // absolute paths (forward slash, Windows)
114
            ['C:/css/style.css', 'C:/css/style.css'],
115
            ['C:/css/./style.css', 'C:/css/style.css'],
116
            ['C:/css/../style.css', 'C:/style.css'],
117
            ['C:/css/./../style.css', 'C:/style.css'],
118
            ['C:/css/.././style.css', 'C:/style.css'],
119
            ['C:/./css/style.css', 'C:/css/style.css'],
120
            ['C:/../css/style.css', 'C:/css/style.css'],
121
            ['C:/./../css/style.css', 'C:/css/style.css'],
122
            ['C:/.././css/style.css', 'C:/css/style.css'],
123
            ['C:/../../css/style.css', 'C:/css/style.css'],
124
125
            // absolute paths (backslash, Windows)
126
            ['C:\\css\\style.css', 'C:/css/style.css'],
127
            ['C:\\css\\.\\style.css', 'C:/css/style.css'],
128
            ['C:\\css\\..\\style.css', 'C:/style.css'],
129
            ['C:\\css\\.\\..\\style.css', 'C:/style.css'],
130
            ['C:\\css\\..\\.\\style.css', 'C:/style.css'],
131
            ['C:\\.\\css\\style.css', 'C:/css/style.css'],
132
            ['C:\\..\\css\\style.css', 'C:/css/style.css'],
133
            ['C:\\.\\..\\css\\style.css', 'C:/css/style.css'],
134
            ['C:\\..\\.\\css\\style.css', 'C:/css/style.css'],
135
            ['C:\\..\\..\\css\\style.css', 'C:/css/style.css'],
136
137
            // Windows special case
138
            ['C:', 'C:/'],
139
140
            // Don't change malformed path
141
            ['C:css/style.css', 'C:css/style.css'],
142
143
            // absolute paths (stream, UNIX)
144
            ['phar:///css/style.css', 'phar:///css/style.css'],
145
            ['phar:///css/./style.css', 'phar:///css/style.css'],
146
            ['phar:///css/../style.css', 'phar:///style.css'],
147
            ['phar:///css/./../style.css', 'phar:///style.css'],
148
            ['phar:///css/.././style.css', 'phar:///style.css'],
149
            ['phar:///./css/style.css', 'phar:///css/style.css'],
150
            ['phar:///../css/style.css', 'phar:///css/style.css'],
151
            ['phar:///./../css/style.css', 'phar:///css/style.css'],
152
            ['phar:///.././css/style.css', 'phar:///css/style.css'],
153
            ['phar:///../../css/style.css', 'phar:///css/style.css'],
154
155
            // absolute paths (stream, Windows)
156
            ['phar://C:/css/style.css', 'phar://C:/css/style.css'],
157
            ['phar://C:/css/./style.css', 'phar://C:/css/style.css'],
158
            ['phar://C:/css/../style.css', 'phar://C:/style.css'],
159
            ['phar://C:/css/./../style.css', 'phar://C:/style.css'],
160
            ['phar://C:/css/.././style.css', 'phar://C:/style.css'],
161
            ['phar://C:/./css/style.css', 'phar://C:/css/style.css'],
162
            ['phar://C:/../css/style.css', 'phar://C:/css/style.css'],
163
            ['phar://C:/./../css/style.css', 'phar://C:/css/style.css'],
164
            ['phar://C:/.././css/style.css', 'phar://C:/css/style.css'],
165
            ['phar://C:/../../css/style.css', 'phar://C:/css/style.css'],
166
167
            // paths with "~" UNIX
168
            ['~/css/style.css', '/home/webmozart/css/style.css'],
169
            ['~/css/./style.css', '/home/webmozart/css/style.css'],
170
            ['~/css/../style.css', '/home/webmozart/style.css'],
171
            ['~/css/./../style.css', '/home/webmozart/style.css'],
172
            ['~/css/.././style.css', '/home/webmozart/style.css'],
173
            ['~/./css/style.css', '/home/webmozart/css/style.css'],
174
            ['~/../css/style.css', '/home/css/style.css'],
175
            ['~/./../css/style.css', '/home/css/style.css'],
176
            ['~/.././css/style.css', '/home/css/style.css'],
177
            ['~/../../css/style.css', '/css/style.css'],
178
        ];
179
    }
180
181
    /**
182
     * @dataProvider provideCanonicalizationTests
183
     */
184
    public function testCanonicalize(string $path, string $canonicalized): void
185
    {
186
        $this->assertSame($canonicalized, Path::canonicalize($path));
187
    }
188
189
    /** @return array<int,array{string,bool}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,bool}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
190
    public function provideIsAbsolutePathTests(): array
191
    {
192
        return [
193
            ['/css/style.css', true],
194
            ['/', true],
195
            ['css/style.css', false],
196
            ['', false],
197
198
            ['\\css\\style.css', true],
199
            ['\\', true],
200
            ['css\\style.css', false],
201
202
            ['C:/css/style.css', true],
203
            ['D:/', true],
204
205
            ['E:\\css\\style.css', true],
206
            ['F:\\', true],
207
208
            ['phar:///css/style.css', true],
209
            ['phar:///', true],
210
211
            // Windows special case
212
            ['C:', true],
213
214
            // Not considered absolute
215
            ['C:css/style.css', false],
216
        ];
217
    }
218
219
    /**
220
     * @dataProvider provideIsAbsolutePathTests
221
     */
222
    public function testIsAbsolute(string $path, bool $isAbsolute): void
223
    {
224
        $this->assertSame($isAbsolute, Path::isAbsolute($path));
225
    }
226
227
    /** @return array<int,array{string,string,string}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,string,string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
228
    public function providePathTests(): array
229
    {
230
        return [
231
            // relative to absolute path
232
            ['css/style.css', '/webmozart/puli', '/webmozart/puli/css/style.css'],
233
            ['../css/style.css', '/webmozart/puli', '/webmozart/css/style.css'],
234
            ['../../css/style.css', '/webmozart/puli', '/css/style.css'],
235
236
            // relative to root
237
            ['css/style.css', '/', '/css/style.css'],
238
            ['css/style.css', 'C:', 'C:/css/style.css'],
239
            ['css/style.css', 'C:/', 'C:/css/style.css'],
240
241
            // same sub directories in different base directories
242
            ['../../puli/css/style.css', '/webmozart/css', '/puli/css/style.css'],
243
244
            ['', '/webmozart/puli', '/webmozart/puli'],
245
            ['..', '/webmozart/puli', '/webmozart'],
246
        ];
247
    }
248
249
    /** @return array<int,array{string,string,string}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,string,string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
250
    public function provideMakeAbsoluteTests(): array
251
    {
252
        return array_merge($this->providePathTests(), [
253
            // collapse dots
254
            ['css/./style.css', '/webmozart/puli', '/webmozart/puli/css/style.css'],
255
            ['css/../style.css', '/webmozart/puli', '/webmozart/puli/style.css'],
256
            ['css/./../style.css', '/webmozart/puli', '/webmozart/puli/style.css'],
257
            ['css/.././style.css', '/webmozart/puli', '/webmozart/puli/style.css'],
258
            ['./css/style.css', '/webmozart/puli', '/webmozart/puli/css/style.css'],
259
260
            ['css\\.\\style.css', '\\webmozart\\puli', '/webmozart/puli/css/style.css'],
261
            ['css\\..\\style.css', '\\webmozart\\puli', '/webmozart/puli/style.css'],
262
            ['css\\.\\..\\style.css', '\\webmozart\\puli', '/webmozart/puli/style.css'],
263
            ['css\\..\\.\\style.css', '\\webmozart\\puli', '/webmozart/puli/style.css'],
264
            ['.\\css\\style.css', '\\webmozart\\puli', '/webmozart/puli/css/style.css'],
265
266
            // collapse dots on root
267
            ['./css/style.css', '/', '/css/style.css'],
268
            ['../css/style.css', '/', '/css/style.css'],
269
            ['../css/./style.css', '/', '/css/style.css'],
270
            ['../css/../style.css', '/', '/style.css'],
271
            ['../css/./../style.css', '/', '/style.css'],
272
            ['../css/.././style.css', '/', '/style.css'],
273
274
            ['.\\css\\style.css', '\\', '/css/style.css'],
275
            ['..\\css\\style.css', '\\', '/css/style.css'],
276
            ['..\\css\\.\\style.css', '\\', '/css/style.css'],
277
            ['..\\css\\..\\style.css', '\\', '/style.css'],
278
            ['..\\css\\.\\..\\style.css', '\\', '/style.css'],
279
            ['..\\css\\..\\.\\style.css', '\\', '/style.css'],
280
281
            ['./css/style.css', 'C:/', 'C:/css/style.css'],
282
            ['../css/style.css', 'C:/', 'C:/css/style.css'],
283
            ['../css/./style.css', 'C:/', 'C:/css/style.css'],
284
            ['../css/../style.css', 'C:/', 'C:/style.css'],
285
            ['../css/./../style.css', 'C:/', 'C:/style.css'],
286
            ['../css/.././style.css', 'C:/', 'C:/style.css'],
287
288
            ['.\\css\\style.css', 'C:\\', 'C:/css/style.css'],
289
            ['..\\css\\style.css', 'C:\\', 'C:/css/style.css'],
290
            ['..\\css\\.\\style.css', 'C:\\', 'C:/css/style.css'],
291
            ['..\\css\\..\\style.css', 'C:\\', 'C:/style.css'],
292
            ['..\\css\\.\\..\\style.css', 'C:\\', 'C:/style.css'],
293
            ['..\\css\\..\\.\\style.css', 'C:\\', 'C:/style.css'],
294
295
            ['./css/style.css', 'phar:///', 'phar:///css/style.css'],
296
            ['../css/style.css', 'phar:///', 'phar:///css/style.css'],
297
            ['../css/./style.css', 'phar:///', 'phar:///css/style.css'],
298
            ['../css/../style.css', 'phar:///', 'phar:///style.css'],
299
            ['../css/./../style.css', 'phar:///', 'phar:///style.css'],
300
            ['../css/.././style.css', 'phar:///', 'phar:///style.css'],
301
302
            ['./css/style.css', 'phar://C:/', 'phar://C:/css/style.css'],
303
            ['../css/style.css', 'phar://C:/', 'phar://C:/css/style.css'],
304
            ['../css/./style.css', 'phar://C:/', 'phar://C:/css/style.css'],
305
            ['../css/../style.css', 'phar://C:/', 'phar://C:/style.css'],
306
            ['../css/./../style.css', 'phar://C:/', 'phar://C:/style.css'],
307
            ['../css/.././style.css', 'phar://C:/', 'phar://C:/style.css'],
308
309
            // absolute paths
310
            ['/css/style.css', '/webmozart/puli', '/css/style.css'],
311
            ['\\css\\style.css', '/webmozart/puli', '/css/style.css'],
312
            ['C:/css/style.css', 'C:/webmozart/puli', 'C:/css/style.css'],
313
            ['D:\\css\\style.css', 'D:/webmozart/puli', 'D:/css/style.css'],
314
        ]);
315
    }
316
317
    /**
318
     * @dataProvider provideMakeAbsoluteTests
319
     */
320
    public function testMakeAbsolute(string $relativePath, string $basePath, string $absolutePath): void
321
    {
322
        $this->assertSame($absolutePath, Path::makeAbsolute($relativePath, $basePath));
323
    }
324
325
    /** @return array<int,array{string,string}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
326
    public function provideAbsolutePathsWithDifferentRoots(): array
327
    {
328
        return [
329
            ['C:/css/style.css', '/webmozart/puli'],
330
            ['C:/css/style.css', '\\webmozart\\puli'],
331
            ['C:\\css\\style.css', '/webmozart/puli'],
332
            ['C:\\css\\style.css', '\\webmozart\\puli'],
333
334
            ['/css/style.css', 'C:/webmozart/puli'],
335
            ['/css/style.css', 'C:\\webmozart\\puli'],
336
            ['\\css\\style.css', 'C:/webmozart/puli'],
337
            ['\\css\\style.css', 'C:\\webmozart\\puli'],
338
339
            ['D:/css/style.css', 'C:/webmozart/puli'],
340
            ['D:/css/style.css', 'C:\\webmozart\\puli'],
341
            ['D:\\css\\style.css', 'C:/webmozart/puli'],
342
            ['D:\\css\\style.css', 'C:\\webmozart\\puli'],
343
344
            ['phar:///css/style.css', '/webmozart/puli'],
345
            ['/css/style.css', 'phar:///webmozart/puli'],
346
347
            ['phar://C:/css/style.css', 'C:/webmozart/puli'],
348
            ['phar://C:/css/style.css', 'C:\\webmozart\\puli'],
349
            ['phar://C:\\css\\style.css', 'C:/webmozart/puli'],
350
            ['phar://C:\\css\\style.css', 'C:\\webmozart\\puli'],
351
        ];
352
    }
353
354
    /**
355
     * @dataProvider provideAbsolutePathsWithDifferentRoots
356
     */
357
    public function testMakeAbsoluteDoesNotFailIfDifferentRoot(string $basePath, string $absolutePath): void
358
    {
359
        // If a path in partition D: is passed, but $basePath is in partition
360
        // C:, the path should be returned unchanged
361
        $this->assertSame(Path::canonicalize($absolutePath), Path::makeAbsolute($absolutePath, $basePath));
362
    }
363
364
    /** @return array<int,array{string,string, string}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,string, string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
365
    public function provideMakeRelativeTests(): array
366
    {
367
        $paths = array_map(function (array $arguments) {
368
            return [$arguments[2], $arguments[1], $arguments[0]];
369
        }, $this->providePathTests());
370
371
        return array_merge($paths, [
372
            ['/webmozart/puli/./css/style.css', '/webmozart/puli', 'css/style.css'],
373
            ['/webmozart/puli/../css/style.css', '/webmozart/puli', '../css/style.css'],
374
            ['/webmozart/puli/.././css/style.css', '/webmozart/puli', '../css/style.css'],
375
            ['/webmozart/puli/./../css/style.css', '/webmozart/puli', '../css/style.css'],
376
            ['/webmozart/puli/../../css/style.css', '/webmozart/puli', '../../css/style.css'],
377
            ['/webmozart/puli/css/style.css', '/webmozart/./puli', 'css/style.css'],
378
            ['/webmozart/puli/css/style.css', '/webmozart/../puli', '../webmozart/puli/css/style.css'],
379
            ['/webmozart/puli/css/style.css', '/webmozart/./../puli', '../webmozart/puli/css/style.css'],
380
            ['/webmozart/puli/css/style.css', '/webmozart/.././puli', '../webmozart/puli/css/style.css'],
381
            ['/webmozart/puli/css/style.css', '/webmozart/../../puli', '../webmozart/puli/css/style.css'],
382
383
            // first argument shorter than second
384
            ['/css', '/webmozart/puli', '../../css'],
385
386
            // second argument shorter than first
387
            ['/webmozart/puli', '/css', '../webmozart/puli'],
388
389
            ['\\webmozart\\puli\\css\\style.css', '\\webmozart\\puli', 'css/style.css'],
390
            ['\\webmozart\\css\\style.css', '\\webmozart\\puli', '../css/style.css'],
391
            ['\\css\\style.css', '\\webmozart\\puli', '../../css/style.css'],
392
393
            ['C:/webmozart/puli/css/style.css', 'C:/webmozart/puli', 'css/style.css'],
394
            ['C:/webmozart/css/style.css', 'C:/webmozart/puli', '../css/style.css'],
395
            ['C:/css/style.css', 'C:/webmozart/puli', '../../css/style.css'],
396
397
            ['C:\\webmozart\\puli\\css\\style.css', 'C:\\webmozart\\puli', 'css/style.css'],
398
            ['C:\\webmozart\\css\\style.css', 'C:\\webmozart\\puli', '../css/style.css'],
399
            ['C:\\css\\style.css', 'C:\\webmozart\\puli', '../../css/style.css'],
400
401
            ['phar:///webmozart/puli/css/style.css', 'phar:///webmozart/puli', 'css/style.css'],
402
            ['phar:///webmozart/css/style.css', 'phar:///webmozart/puli', '../css/style.css'],
403
            ['phar:///css/style.css', 'phar:///webmozart/puli', '../../css/style.css'],
404
405
            ['phar://C:/webmozart/puli/css/style.css', 'phar://C:/webmozart/puli', 'css/style.css'],
406
            ['phar://C:/webmozart/css/style.css', 'phar://C:/webmozart/puli', '../css/style.css'],
407
            ['phar://C:/css/style.css', 'phar://C:/webmozart/puli', '../../css/style.css'],
408
409
            // already relative + already in root basepath
410
            ['../style.css', '/', 'style.css'],
411
            ['./style.css', '/', 'style.css'],
412
            ['../../style.css', '/', 'style.css'],
413
            ['..\\style.css', 'C:\\', 'style.css'],
414
            ['.\\style.css', 'C:\\', 'style.css'],
415
            ['..\\..\\style.css', 'C:\\', 'style.css'],
416
            ['../style.css', 'C:/', 'style.css'],
417
            ['./style.css', 'C:/', 'style.css'],
418
            ['../../style.css', 'C:/', 'style.css'],
419
            ['..\\style.css', '\\', 'style.css'],
420
            ['.\\style.css', '\\', 'style.css'],
421
            ['..\\..\\style.css', '\\', 'style.css'],
422
            ['../style.css', 'phar:///', 'style.css'],
423
            ['./style.css', 'phar:///', 'style.css'],
424
            ['../../style.css', 'phar:///', 'style.css'],
425
            ['..\\style.css', 'phar://C:\\', 'style.css'],
426
            ['.\\style.css', 'phar://C:\\', 'style.css'],
427
            ['..\\..\\style.css', 'phar://C:\\', 'style.css'],
428
429
            ['css/../style.css', '/', 'style.css'],
430
            ['css/./style.css', '/', 'css/style.css'],
431
            ['css\\..\\style.css', 'C:\\', 'style.css'],
432
            ['css\\.\\style.css', 'C:\\', 'css/style.css'],
433
            ['css/../style.css', 'C:/', 'style.css'],
434
            ['css/./style.css', 'C:/', 'css/style.css'],
435
            ['css\\..\\style.css', '\\', 'style.css'],
436
            ['css\\.\\style.css', '\\', 'css/style.css'],
437
            ['css/../style.css', 'phar:///', 'style.css'],
438
            ['css/./style.css', 'phar:///', 'css/style.css'],
439
            ['css\\..\\style.css', 'phar://C:\\', 'style.css'],
440
            ['css\\.\\style.css', 'phar://C:\\', 'css/style.css'],
441
442
            // already relative
443
            ['css/style.css', '/webmozart/puli', 'css/style.css'],
444
            ['css\\style.css', '\\webmozart\\puli', 'css/style.css'],
445
446
            // both relative
447
            ['css/style.css', 'webmozart/puli', '../../css/style.css'],
448
            ['css\\style.css', 'webmozart\\puli', '../../css/style.css'],
449
450
            // relative to empty
451
            ['css/style.css', '', 'css/style.css'],
452
            ['css\\style.css', '', 'css/style.css'],
453
454
            // different slashes in path and base path
455
            ['/webmozart/puli/css/style.css', '\\webmozart\\puli', 'css/style.css'],
456
            ['\\webmozart\\puli\\css\\style.css', '/webmozart/puli', 'css/style.css'],
457
        ]);
458
    }
459
460
    /**
461
     * @dataProvider provideMakeRelativeTests
462
     */
463
    public function testMakeRelative(string $absolutePath, string $basePath, string $relativePath): void
464
    {
465
        $this->assertSame($relativePath, Path::makeRelative($absolutePath, $basePath));
466
    }
467
468
    public function testMakeRelativeFailsIfAbsolutePathAndBasePathNotAbsolute(): void
469
    {
470
        $this->expectException(\InvalidArgumentException::class);
471
        $this->expectExceptionMessage('The absolute path "/webmozart/puli/css/style.css" cannot be made relative to the relative path "webmozart/puli". You should provide an absolute base path instead.');
472
        Path::makeRelative('/webmozart/puli/css/style.css', 'webmozart/puli');
473
    }
474
475
    public function testMakeRelativeFailsIfAbsolutePathAndBasePathEmpty(): void
476
    {
477
        $this->expectException(\InvalidArgumentException::class);
478
        $this->expectExceptionMessage('The absolute path "/webmozart/puli/css/style.css" cannot be made relative to the relative path "". You should provide an absolute base path instead.');
479
        Path::makeRelative('/webmozart/puli/css/style.css', '');
480
    }
481
482
    /**
483
     * @dataProvider provideAbsolutePathsWithDifferentRoots
484
     */
485
    public function testMakeRelativeFailsIfDifferentRoot(string $absolutePath, string $basePath): void
486
    {
487
        $this->expectException(\InvalidArgumentException::class);
488
        Path::makeRelative($absolutePath, $basePath);
489
    }
490
491
    /** @return array<int,array{string,bool}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,bool}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
492
    public function provideIsLocalTests(): array
493
    {
494
        return [
495
            ['/bg.png', true],
496
            ['bg.png', true],
497
            ['http://example.com/bg.png', false],
498
            ['http://example.com', false],
499
            ['', false],
500
        ];
501
    }
502
503
    /** @return array<int,array{string,string, bool}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string,string, bool}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
504
    public function provideIsBasePathTests(): array
505
    {
506
        return [
507
            // same paths
508
            ['/base/path', '/base/path', true],
509
            ['C:/base/path', 'C:/base/path', true],
510
            ['C:\\base\\path', 'C:\\base\\path', true],
511
            ['C:/base/path', 'C:\\base\\path', true],
512
            ['phar:///base/path', 'phar:///base/path', true],
513
            ['phar://C:/base/path', 'phar://C:/base/path', true],
514
515
            // trailing slash
516
            ['/base/path/', '/base/path', true],
517
            ['C:/base/path/', 'C:/base/path', true],
518
            ['C:\\base\\path\\', 'C:\\base\\path', true],
519
            ['C:/base/path/', 'C:\\base\\path', true],
520
            ['phar:///base/path/', 'phar:///base/path', true],
521
            ['phar://C:/base/path/', 'phar://C:/base/path', true],
522
523
            ['/base/path', '/base/path/', true],
524
            ['C:/base/path', 'C:/base/path/', true],
525
            ['C:\\base\\path', 'C:\\base\\path\\', true],
526
            ['C:/base/path', 'C:\\base\\path\\', true],
527
            ['phar:///base/path', 'phar:///base/path/', true],
528
            ['phar://C:/base/path', 'phar://C:/base/path/', true],
529
530
            // first in second
531
            ['/base/path/sub', '/base/path', false],
532
            ['C:/base/path/sub', 'C:/base/path', false],
533
            ['C:\\base\\path\\sub', 'C:\\base\\path', false],
534
            ['C:/base/path/sub', 'C:\\base\\path', false],
535
            ['phar:///base/path/sub', 'phar:///base/path', false],
536
            ['phar://C:/base/path/sub', 'phar://C:/base/path', false],
537
538
            // second in first
539
            ['/base/path', '/base/path/sub', true],
540
            ['C:/base/path', 'C:/base/path/sub', true],
541
            ['C:\\base\\path', 'C:\\base\\path\\sub', true],
542
            ['C:/base/path', 'C:\\base\\path\\sub', true],
543
            ['phar:///base/path', 'phar:///base/path/sub', true],
544
            ['phar://C:/base/path', 'phar://C:/base/path/sub', true],
545
546
            // first is prefix
547
            ['/base/path/di', '/base/path/dir', false],
548
            ['C:/base/path/di', 'C:/base/path/dir', false],
549
            ['C:\\base\\path\\di', 'C:\\base\\path\\dir', false],
550
            ['C:/base/path/di', 'C:\\base\\path\\dir', false],
551
            ['phar:///base/path/di', 'phar:///base/path/dir', false],
552
            ['phar://C:/base/path/di', 'phar://C:/base/path/dir', false],
553
554
            // second is prefix
555
            ['/base/path/dir', '/base/path/di', false],
556
            ['C:/base/path/dir', 'C:/base/path/di', false],
557
            ['C:\\base\\path\\dir', 'C:\\base\\path\\di', false],
558
            ['C:/base/path/dir', 'C:\\base\\path\\di', false],
559
            ['phar:///base/path/dir', 'phar:///base/path/di', false],
560
            ['phar://C:/base/path/dir', 'phar://C:/base/path/di', false],
561
562
            // root
563
            ['/', '/second', true],
564
            ['C:/', 'C:/second', true],
565
            ['C:', 'C:/second', true],
566
            ['C:\\', 'C:\\second', true],
567
            ['C:/', 'C:\\second', true],
568
            ['phar:///', 'phar:///second', true],
569
            ['phar://C:/', 'phar://C:/second', true],
570
571
            // windows vs unix
572
            ['/base/path', 'C:/base/path', false],
573
            ['C:/base/path', '/base/path', false],
574
            ['/base/path', 'C:\\base\\path', false],
575
            ['/base/path', 'phar:///base/path', false],
576
            ['phar:///base/path', 'phar://C:/base/path', false],
577
578
            // different partitions
579
            ['C:/base/path', 'D:/base/path', false],
580
            ['C:/base/path', 'D:\\base\\path', false],
581
            ['C:\\base\\path', 'D:\\base\\path', false],
582
            ['C:/base/path', 'phar://C:/base/path', false],
583
            ['phar://C:/base/path', 'phar://D:/base/path', false],
584
        ];
585
    }
586
587
    /**
588
     * @dataProvider provideIsBasePathTests
589
     */
590
    public function testIsBasePath(string $path, string $ofPath, bool $result): void
591
    {
592
        $this->assertSame($result, Path::isBasePath($path, $ofPath));
593
    }
594
595
    /** @return array<int,array{string, string, string}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int,array{string, string, string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
596
    public function provideJoinTests(): array
597
    {
598
        return [
599
            ['', '', ''],
600
            ['/path/to/test', '', '/path/to/test'],
601
            ['/path/to//test', '', '/path/to/test'],
602
            ['', '/path/to/test', '/path/to/test'],
603
            ['', '/path/to//test', '/path/to/test'],
604
605
            ['/path/to/test', 'subdir', '/path/to/test/subdir'],
606
            ['/path/to/test/', 'subdir', '/path/to/test/subdir'],
607
            ['/path/to/test', '/subdir', '/path/to/test/subdir'],
608
            ['/path/to/test/', '/subdir', '/path/to/test/subdir'],
609
            ['/path/to/test', './subdir', '/path/to/test/subdir'],
610
            ['/path/to/test/', './subdir', '/path/to/test/subdir'],
611
            ['/path/to/test/', '../parentdir', '/path/to/parentdir'],
612
            ['/path/to/test', '../parentdir', '/path/to/parentdir'],
613
            ['path/to/test/', '/subdir', 'path/to/test/subdir'],
614
            ['path/to/test', '/subdir', 'path/to/test/subdir'],
615
            ['../path/to/test', '/subdir', '../path/to/test/subdir'],
616
            ['path', '../../subdir', '../subdir'],
617
            ['/path', '../../subdir', '/subdir'],
618
            ['../path', '../../subdir', '../../subdir'],
619
620
            ['base/path', 'to/test', 'base/path/to/test'],
621
622
            ['C:\\path\\to\\test', 'subdir', 'C:/path/to/test/subdir'],
623
            ['C:\\path\\to\\test\\', 'subdir', 'C:/path/to/test/subdir'],
624
            ['C:\\path\\to\\test', '/subdir', 'C:/path/to/test/subdir'],
625
            ['C:\\path\\to\\test\\', '/subdir', 'C:/path/to/test/subdir'],
626
627
            ['/', 'subdir', '/subdir'],
628
            ['/', '/subdir', '/subdir'],
629
            ['C:/', 'subdir', 'C:/subdir'],
630
            ['C:/', '/subdir', 'C:/subdir'],
631
            ['C:\\', 'subdir', 'C:/subdir'],
632
            ['C:\\', '/subdir', 'C:/subdir'],
633
            ['C:', 'subdir', 'C:/subdir'],
634
            ['C:', '/subdir', 'C:/subdir'],
635
636
            ['phar://', '/path/to/test', 'phar:///path/to/test'],
637
            ['phar:///', '/path/to/test', 'phar:///path/to/test'],
638
            ['phar:///path/to/test', 'subdir', 'phar:///path/to/test/subdir'],
639
            ['phar:///path/to/test', 'subdir/', 'phar:///path/to/test/subdir'],
640
            ['phar:///path/to/test', '/subdir', 'phar:///path/to/test/subdir'],
641
            ['phar:///path/to/test/', 'subdir', 'phar:///path/to/test/subdir'],
642
            ['phar:///path/to/test/', '/subdir', 'phar:///path/to/test/subdir'],
643
644
            ['phar://', 'C:/path/to/test', 'phar://C:/path/to/test'],
645
            ['phar://', 'C:\\path\\to\\test', 'phar://C:/path/to/test'],
646
            ['phar://C:/path/to/test', 'subdir', 'phar://C:/path/to/test/subdir'],
647
            ['phar://C:/path/to/test', 'subdir/', 'phar://C:/path/to/test/subdir'],
648
            ['phar://C:/path/to/test', '/subdir', 'phar://C:/path/to/test/subdir'],
649
            ['phar://C:/path/to/test/', 'subdir', 'phar://C:/path/to/test/subdir'],
650
            ['phar://C:/path/to/test/', '/subdir', 'phar://C:/path/to/test/subdir'],
651
            ['phar://C:', 'path/to/test', 'phar://C:/path/to/test'],
652
            ['phar://C:', '/path/to/test', 'phar://C:/path/to/test'],
653
            ['phar://C:/', 'path/to/test', 'phar://C:/path/to/test'],
654
            ['phar://C:/', '/path/to/test', 'phar://C:/path/to/test'],
655
        ];
656
    }
657
658
    /**
659
     * @dataProvider provideJoinTests
660
     */
661
    public function testJoin(string $path1, string $path2, string $result): void
662
    {
663
        $this->assertSame($result, Path::join([$path1, $path2]));
664
    }
665
666
    public function testJoinVarArgs(): void
667
    {
668
        $this->assertSame('/path', Path::join(['/path']));
669
        $this->assertSame('/path/to', Path::join(['/path', 'to']));
670
        $this->assertSame('/path/to/test', Path::join(['/path', 'to', '/test']));
671
        $this->assertSame('/path/to/test/subdir', Path::join(['/path', 'to', '/test', 'subdir/']));
672
    }
673
674
    public function testGetHomeDirectoryFailsIfNotSupportedOperationSystem(): void
675
    {
676
        putenv('HOME=');
677
678
        $this->expectException(InvalidPathException::class);
679
        $this->expectExceptionMessage('Your environment or operating system is not supported');
680
681
        Path::getHomeDirectory();
682
    }
683
684
    public function testGetHomeDirectoryForUnix(): void
685
    {
686
        $this->assertEquals('/home/webmozart', Path::getHomeDirectory());
687
    }
688
689
    public function testGetHomeDirectoryForWindows(): void
690
    {
691
        putenv('HOME=');
692
        putenv('HOMEDRIVE=C:');
693
        putenv('HOMEPATH=/users/webmozart');
694
695
        $this->assertEquals('C:/users/webmozart', Path::getHomeDirectory());
696
    }
697
}
698