Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Dec 12, 2024
1 parent be721de commit f86e132
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,20 @@ function refresh_router_lookups(Router $router): void
function transform_realpath_to_relative(string $path, ?string $workingPath = null, string $prefix = ''): string
{
if (! \is_null($workingPath)) {
return str_replace(rtrim($workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, $prefix, $path);
return str_replace(rtrim($workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, "{$prefix}/", $path);
}

$laravelPath = base_path();
$packagePath = package_path();
$workbenchPath = workbench_path();
$packagePath = package_path();

$locate = static fn ($path) => match (true) {
return match (true) {
str_starts_with($path, $laravelPath) => str_replace("{$laravelPath}/", '@laravel/', $path),
str_starts_with($path, $workbenchPath) => str_replace("{$workbenchPath}/", '@workbench/', $path),
str_starts_with($path, $packagePath) => str_replace("{$packagePath}/", './', $path),
default => $prefix.$path,
! empty($prefix) => implode('/', [$prefix, ltrim($path, '/')]),
default => $path,
};

return $locate($path);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Helpers/TransformRealpathToRelativeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Orchestra\Testbench\Tests\Helpers;

use Orchestra\Testbench\TestCase;

use function Orchestra\Testbench\default_skeleton_path;
use function Orchestra\Testbench\package_path;
use function Orchestra\Testbench\transform_realpath_to_relative;

class TransformRealpathToRelativeTest extends TestCase
{
/** @test */
public function it_can_use_transform_realpath_to_relative()
{
$this->assertSame('Testbench.php', transform_realpath_to_relative('Testbench.php'));

$this->assertSame(
'./src/TestCase.php',
transform_realpath_to_relative(package_path('src', 'TestCase.php'))
);

$this->assertSame(
'@laravel/composer.json',
transform_realpath_to_relative(default_skeleton_path('composer.json'))
);

$this->assertSame(
'@workbench/app/Providers/WorkbenchServiceProvider.php',
transform_realpath_to_relative(package_path('workbench', 'app', 'Providers', 'WorkbenchServiceProvider.php'))
);
}

/** @test */
public function it_can_use_transform_realpath_to_relative_using_custom_working_path()
{
$this->assertSame(
'@tests/Helpers/TransformRealpathToRelativeTest.php',
transform_realpath_to_relative(__FILE__, package_path('tests'), '@tests')
);
}
}
5 changes: 1 addition & 4 deletions tests/Helpers/TransformRelativePathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class TransformRelativePathTest extends TestCase
/** @test */
public function it_can_use_transform_relative_path()
{
$this->assertSame(
realpath(__DIR__.DIRECTORY_SEPARATOR.'TransformRelativePathTest.php'),
transform_relative_path('./TransformRelativePathTest.php', realpath(__DIR__))
);
$this->assertSame(__FILE__, transform_relative_path('./TransformRelativePathTest.php', __DIR__));
}
}

0 comments on commit f86e132

Please sign in to comment.