Skip to content

Commit 583b6f8

Browse files
committed
Path utils
1 parent d6374ab commit 583b6f8

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/Utils/Path.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Utils;
4+
5+
class Path
6+
{
7+
public static function join(string $base, string ...$parts): string
8+
{
9+
return implode(
10+
'/',
11+
array_merge(
12+
[rtrim($base, '/')],
13+
array_map(function (string $part) {
14+
return trim($part, '/');
15+
}, $parts)
16+
)
17+
);
18+
}
19+
}

test/Utils/PathTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Util\s;
4+
5+
use PhpSchool\PhpWorkshop\Utils\Path;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class PathTest extends TestCase
9+
{
10+
public function testJoin(): void
11+
{
12+
$this->assertEquals(
13+
'/some/path/some-folder/file.txt',
14+
Path::join('/some/path', 'some-folder/file.txt')
15+
);
16+
17+
$this->assertEquals(
18+
'/some/path/some-folder/file.txt',
19+
Path::join('/some/path/', 'some-folder/file.txt')
20+
);
21+
22+
$this->assertEquals(
23+
'/some/path/some-folder/file.txt',
24+
Path::join('/some/path', '/some-folder/file.txt')
25+
);
26+
27+
$this->assertEquals(
28+
'/some/path/some-folder/file.txt',
29+
Path::join('/some/path/', '/some-folder/file.txt')
30+
);
31+
32+
$this->assertEquals(
33+
'/some/path/some-folder/file.txt',
34+
Path::join('/some/path//', '//some-folder/file.txt')
35+
);
36+
37+
$this->assertEquals(
38+
'/some/path/some-folder/file.txt',
39+
Path::join('/some/path/', 'some-folder', 'file.txt')
40+
);
41+
42+
$this->assertEquals(
43+
'/some/path/some-folder/file.txt',
44+
Path::join('/some/path/', '/some-folder/', '/file.txt')
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)