|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of fast-forward/dev-tools. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | + * @license https://opensource.org/licenses/MIT MIT License |
| 13 | + * |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward |
| 16 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | + */ |
| 18 | + |
| 19 | +namespace FastForward\DevTools\Tests\Filesystem; |
| 20 | + |
| 21 | +use FastForward\DevTools\Filesystem\Filesystem; |
| 22 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 23 | +use PHPUnit\Framework\Attributes\Test; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Symfony\Component\Filesystem\Path; |
| 26 | +use function Safe\getcwd; |
| 27 | + |
| 28 | +#[CoversClass(Filesystem::class)] |
| 29 | +final class FilesystemTest extends TestCase |
| 30 | +{ |
| 31 | + private Filesystem $filesystem; |
| 32 | + |
| 33 | + private string $tempDir; |
| 34 | + |
| 35 | + protected function setUp(): void |
| 36 | + { |
| 37 | + $this->filesystem = new Filesystem(); |
| 38 | + $this->tempDir = sys_get_temp_dir() . '/' . uniqid('ff_dev_tools_', true); |
| 39 | + $this->filesystem->mkdir($this->tempDir); |
| 40 | + } |
| 41 | + |
| 42 | + protected function tearDown(): void |
| 43 | + { |
| 44 | + $this->filesystem->remove($this->tempDir); |
| 45 | + } |
| 46 | + |
| 47 | + #[Test] |
| 48 | + public function getAbsolutePathWillReturnAbsoluteForRelativePath(): void |
| 49 | + { |
| 50 | + $expected = Path::makeAbsolute('test/file.php', getcwd()); |
| 51 | + |
| 52 | + self::assertSame($expected, $this->filesystem->getAbsolutePath('test/file.php')); |
| 53 | + } |
| 54 | + |
| 55 | + #[Test] |
| 56 | + public function getAbsolutePathWillReturnAbsoluteForMultipleRelativePaths(): void |
| 57 | + { |
| 58 | + $expected = [ |
| 59 | + Path::makeAbsolute('test1.php', getcwd()), |
| 60 | + Path::makeAbsolute('test2.php', getcwd()), |
| 61 | + ]; |
| 62 | + |
| 63 | + // Ensure returning array has matching elements |
| 64 | + $result = $this->filesystem->getAbsolutePath(['test1.php', 'test2.php']); |
| 65 | + |
| 66 | + self::assertEquals($expected, $result); |
| 67 | + } |
| 68 | + |
| 69 | + #[Test] |
| 70 | + public function getAbsolutePathWillUseProvidedBasePath(): void |
| 71 | + { |
| 72 | + $basePath = '/var/www'; |
| 73 | + $expected = Path::makeAbsolute('test.php', $basePath); |
| 74 | + |
| 75 | + self::assertSame($expected, $this->filesystem->getAbsolutePath('test.php', $basePath)); |
| 76 | + } |
| 77 | + |
| 78 | + #[Test] |
| 79 | + public function basenameWillReturnCorrectBasename(): void |
| 80 | + { |
| 81 | + self::assertSame('file', $this->filesystem->basename('/path/to/file.txt', '.txt')); |
| 82 | + self::assertSame('file.txt', $this->filesystem->basename('/path/to/file.txt')); |
| 83 | + } |
| 84 | + |
| 85 | + #[Test] |
| 86 | + public function dirnameWillReturnCorrectDirname(): void |
| 87 | + { |
| 88 | + self::assertSame('/path/to', $this->filesystem->dirname('/path/to/file.txt')); |
| 89 | + self::assertSame('/path', $this->filesystem->dirname('/path/to/file.txt', 2)); |
| 90 | + } |
| 91 | + |
| 92 | + #[Test] |
| 93 | + public function makePathRelativeWillReturnRelativePathAgainstBase(): void |
| 94 | + { |
| 95 | + $path = '/var/www/project/src/file.php'; |
| 96 | + $basePath = '/var/www/project'; |
| 97 | + |
| 98 | + $relative = $this->filesystem->makePathRelative($path, $basePath); |
| 99 | + |
| 100 | + // Symfony makePathRelative usually returns trailing slash for directories, but not required for files |
| 101 | + self::assertStringStartsWith('src/file.php', $relative); |
| 102 | + } |
| 103 | + |
| 104 | + #[Test] |
| 105 | + public function dumpFileAndReadFileWillWorkWithRelativePaths(): void |
| 106 | + { |
| 107 | + $filename = 'test_file.txt'; |
| 108 | + $content = 'hello world'; |
| 109 | + |
| 110 | + $this->filesystem->dumpFile($filename, $content, $this->tempDir); |
| 111 | + |
| 112 | + self::assertTrue($this->filesystem->exists($filename, $this->tempDir)); |
| 113 | + self::assertSame($content, $this->filesystem->readFile($filename, $this->tempDir)); |
| 114 | + } |
| 115 | + |
| 116 | + #[Test] |
| 117 | + public function mkdirWillCreateDirectoryWithRelativePath(): void |
| 118 | + { |
| 119 | + $dirName = 'nested/dir'; |
| 120 | + |
| 121 | + $this->filesystem->mkdir($dirName, 0777, $this->tempDir); |
| 122 | + |
| 123 | + self::assertTrue($this->filesystem->exists($dirName, $this->tempDir)); |
| 124 | + self::assertTrue(is_dir($this->tempDir . '/' . $dirName)); |
| 125 | + } |
| 126 | +} |
0 commit comments