1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HexMakina\LocalFS\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
|
|
|
6
|
|
|
use HexMakina\LocalFS\FileSystem; |
7
|
|
|
|
8
|
|
|
class FileSystemTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
private $testRootPath; |
11
|
|
|
private $fileSystem; |
12
|
|
|
|
13
|
|
|
protected function setUp(): void |
14
|
|
|
{ |
15
|
|
|
$this->testRootPath = sys_get_temp_dir() . '/hexmakina_localfs_test'; |
16
|
|
|
if (!file_exists($this->testRootPath)) { |
17
|
|
|
mkdir($this->testRootPath); |
18
|
|
|
} |
19
|
|
|
$this->fileSystem = new FileSystem($this->testRootPath); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function tearDown(): void |
23
|
|
|
{ |
24
|
|
|
$this->removeDirectory($this->testRootPath); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function removeDirectory($dir) |
28
|
|
|
{ |
29
|
|
|
if (is_dir($dir)) { |
30
|
|
|
$objects = scandir($dir); |
31
|
|
|
foreach ($objects as $object) { |
32
|
|
|
if ($object != "." && $object != "..") { |
33
|
|
|
if (is_dir($dir . "/" . $object)) { |
34
|
|
|
$this->removeDirectory($dir . "/" . $object); |
35
|
|
|
} else { |
36
|
|
|
unlink($dir . "/" . $object); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
rmdir($dir); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testConstructWithInvalidRootPath() |
45
|
|
|
{ |
46
|
|
|
$this->expectException(\InvalidArgumentException::class); |
47
|
|
|
$this->expectExceptionMessage('INVALID_ROOT_PATH'); |
48
|
|
|
|
49
|
|
|
new FileSystem('/non/existent/path'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testAbsolutePathForReturnsCorrectPath() |
53
|
|
|
{ |
54
|
|
|
$testRootPath = '/tmp/test_root'; |
55
|
|
|
mkdir($testRootPath); |
56
|
|
|
$fileSystem = new FileSystem($testRootPath); |
57
|
|
|
|
58
|
|
|
$relativePath = 'subdir/file.txt'; |
59
|
|
|
$expectedPath = $testRootPath . '/' . $relativePath; |
60
|
|
|
|
61
|
|
|
$result = $fileSystem->absolutePathFor($relativePath); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($expectedPath, $result); |
64
|
|
|
|
65
|
|
|
rmdir($testRootPath); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testListNonExistentDirectory() |
69
|
|
|
{ |
70
|
|
|
$nonExistentPath = 'non/existent/directory'; |
71
|
|
|
|
72
|
|
|
$this->expectException(\InvalidArgumentException::class); |
73
|
|
|
$this->expectExceptionMessage('RELATIVE_PATH_NOT_A_DIRECTORY'); |
74
|
|
|
|
75
|
|
|
$this->fileSystem->list($nonExistentPath); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testCopyNonExistentSourceFile() |
79
|
|
|
{ |
80
|
|
|
$nonExistentSourcePath = $this->testRootPath . '/non_existent_file.txt'; |
81
|
|
|
$destinationPath = $this->testRootPath . '/destination.txt'; |
82
|
|
|
|
83
|
|
|
$result = FileSystem::copy($nonExistentSourcePath, $destinationPath); |
84
|
|
|
|
85
|
|
|
$this->assertFalse($result); |
86
|
|
|
$this->assertFileDoesNotExist($destinationPath); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testResolveSymlink() |
90
|
|
|
{ |
91
|
|
|
$tempDir = sys_get_temp_dir() . '/symlink_test'; |
92
|
|
|
mkdir($tempDir); |
93
|
|
|
$targetFile = $tempDir . '/target.txt'; |
94
|
|
|
$symlinkFile = $tempDir . '/symlink.txt'; |
95
|
|
|
|
96
|
|
|
file_put_contents($targetFile, 'Test content'); |
97
|
|
|
symlink($targetFile, $symlinkFile); |
98
|
|
|
|
99
|
|
|
$resolvedPath = FileSystem::resolve_symlink($symlinkFile); |
100
|
|
|
|
101
|
|
|
$this->assertEquals($targetFile, $resolvedPath); |
102
|
|
|
|
103
|
|
|
unlink($symlinkFile); |
104
|
|
|
unlink($targetFile); |
105
|
|
|
rmdir($tempDir); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testEnsureWritablePathCreatesDirectories() |
109
|
|
|
{ |
110
|
|
|
$nonExistentPath = $this->testRootPath . '/new/nested/directory'; |
111
|
|
|
$this->assertFalse(is_dir($nonExistentPath)); |
112
|
|
|
|
113
|
|
|
$result = $this->fileSystem->ensureWritablePath($nonExistentPath); |
114
|
|
|
|
115
|
|
|
$this->assertTrue($result); |
116
|
|
|
$this->assertTrue(is_dir($nonExistentPath)); |
117
|
|
|
$this->assertTrue(is_writable($nonExistentPath)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testEnsureWritablePathOutsideRootPath() |
121
|
|
|
{ |
122
|
|
|
$outsidePath = '/tmp/outside_root_path'; |
123
|
|
|
|
124
|
|
|
$this->expectException(\InvalidArgumentException::class); |
125
|
|
|
$this->expectExceptionMessage('PATH_NOT_INSIDE_ROOT_PATH'); |
126
|
|
|
|
127
|
|
|
$this->fileSystem->ensureWritablePath($outsidePath); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testDirectoriesListsOnlyDirectories() |
131
|
|
|
{ |
132
|
|
|
$testDir = $this->testRootPath . '/test_directories'; |
133
|
|
|
mkdir($testDir); |
134
|
|
|
mkdir($testDir . '/dir1'); |
135
|
|
|
mkdir($testDir . '/dir2'); |
136
|
|
|
touch($testDir . '/file1.txt'); |
137
|
|
|
touch($testDir . '/file2.txt'); |
138
|
|
|
|
139
|
|
|
$result = $this->fileSystem->directories('test_directories'); |
140
|
|
|
|
141
|
|
|
$this->assertCount(2, $result); |
142
|
|
|
$this->assertContains('dir1', $result); |
143
|
|
|
$this->assertContains('dir2', $result); |
144
|
|
|
$this->assertNotContains('file1.txt', $result); |
145
|
|
|
$this->assertNotContains('file2.txt', $result); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testFilesListsOnlyFilesInSpecifiedDirectory() |
149
|
|
|
{ |
150
|
|
|
$testDir = $this->testRootPath . '/test_files'; |
151
|
|
|
mkdir($testDir); |
152
|
|
|
|
153
|
|
|
// Create test files and directories |
154
|
|
|
touch($testDir . '/file1.txt'); |
155
|
|
|
touch($testDir . '/file2.txt'); |
156
|
|
|
mkdir($testDir . '/subdir'); |
157
|
|
|
|
158
|
|
|
$fileSystem = new FileSystem($this->testRootPath); |
159
|
|
|
$files = $fileSystem->files('test_files'); |
160
|
|
|
|
161
|
|
|
$this->assertCount(2, $files); |
162
|
|
|
$this->assertContains('file1.txt', $files); |
163
|
|
|
$this->assertContains('file2.txt', $files); |
164
|
|
|
$this->assertNotContains('subdir', $files); |
165
|
|
|
|
166
|
|
|
// Clean up |
167
|
|
|
unlink($testDir . '/file1.txt'); |
168
|
|
|
unlink($testDir . '/file2.txt'); |
169
|
|
|
rmdir($testDir . '/subdir'); |
170
|
|
|
rmdir($testDir); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths