Skip to content

Commit 274e255

Browse files
committed
IHF: relative_path helper added with tests.
1 parent ba17355 commit 274e255

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/filesystem.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Symfony\Component\Filesystem\Filesystem;
4+
5+
if (!function_exists('relative_path')) {
6+
function relative_path($to, $from)
7+
{
8+
return (new Filesystem)->makePathRelative(realpath($to), realpath($from));
9+
}
10+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Illuminated\Helpers\HelperFunctions\Tests\FileSystem;
4+
5+
use Illuminated\Helpers\HelperFunctions\Tests\TestCase;
6+
7+
class RelativePathTest extends TestCase
8+
{
9+
/** @test */
10+
public function it_returns_relative_path_for_two_folders()
11+
{
12+
$path1 = realpath(__DIR__ . '/../../../src/');
13+
$path2 = realpath(__DIR__ . '/../../../tests/HelperFunctions/filesystem');
14+
15+
$this->assertEquals('../../../src/', relative_path($path1, $path2));
16+
}
17+
18+
/** @test */
19+
public function it_correctly_works_with_params_which_are_relative_paths()
20+
{
21+
$path1 = __DIR__ . '/../../../src/';
22+
$path2 = __DIR__ . '/../../../tests/HelperFunctions/filesystem';
23+
24+
$this->assertEquals('../../../src/', relative_path($path1, $path2));
25+
}
26+
27+
/** @test */
28+
public function it_correctly_works_for_the_same_folder()
29+
{
30+
$path1 = __DIR__;
31+
$path2 = __DIR__;
32+
33+
$this->assertEquals('./', relative_path($path1, $path2));
34+
}
35+
36+
/** @test */
37+
public function even_if_the_paths_are_relative_and_different()
38+
{
39+
$path1 = __DIR__;
40+
$path2 = __DIR__ . '/../../../tests/HelperFunctions/filesystem';
41+
42+
$this->assertEquals('./', relative_path($path1, $path2));
43+
}
44+
}

0 commit comments

Comments
 (0)