Skip to content

Commit

Permalink
fix(testing): \LastDragon_ru\LaraASP\Testing\Utils\TestData relativ…
Browse files Browse the repository at this point in the history
…e paths (`./`, `../`) handling.
  • Loading branch information
LastDragon-ru committed Nov 28, 2024
1 parent 8a017bf commit 0646f90
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/testing/src/Utils/TestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public function __construct(string $test) {
public function path(string $path): string {
$dir = dirname(str_replace('\\', '/', $this->path));
$name = basename($this->path, '.php');
$path = str_starts_with($path, '.') || str_starts_with($path, '~') ? $path : '/'.ltrim($path, '/');
$path = str_replace('\\', '/', $path);
$path = (str_starts_with($path, '.') && !str_starts_with($path, './') && !str_starts_with($path, '../')) || str_starts_with($path, '~')
? $path
: '/'.ltrim($path, '/');
$path = "{$dir}/{$name}{$path}";

return $path;
Expand Down
29 changes: 29 additions & 0 deletions packages/testing/src/Utils/TestDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Testing\Utils;

use LastDragon_ru\LaraASP\Core\Path\FilePath;
use LastDragon_ru\LaraASP\Testing\Testing\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @internal
*/
#[CoversClass(TestData::class)]
final class TestDataTest extends TestCase {
public function testPath(): void {
$data = new TestData($this::class);
$file = (new FilePath(__FILE__))->getDirectoryPath('TestDataTest');

self::assertEquals("{$file}/", $data->path(''));
self::assertEquals("{$file}.php", $data->path('.php'));
self::assertEquals("{$file}.file.php", $data->path('.file.php'));
self::assertEquals("{$file}~.php", $data->path('~.php'));
self::assertEquals("{$file}~file.php", $data->path('~file.php'));
self::assertEquals("{$file}/php", $data->path('php'));
self::assertEquals("{$file}/file.php", $data->path('file.php'));
self::assertEquals("{$file}/path/to/file.php", $data->path('path/to/file.php'));
self::assertEquals("{$file}/./path/to/file.php", $data->path('./path/to/file.php'));
self::assertEquals("{$file}/../path/to/file.php", $data->path('../path/to/file.php'));
}
}

0 comments on commit 0646f90

Please sign in to comment.