-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTest.php
More file actions
303 lines (260 loc) · 8.2 KB
/
Copy pathFileTest.php
File metadata and controls
303 lines (260 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Filesystem Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <[email protected]>
* @link : https://floatphp.com
* @license : MIT
*
* This file if a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Filesystem;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Filesystem\File;
/**
* File class tests.
*/
final class FileTest extends TestCase
{
private static string $testDir;
private static string $testFile;
public static function setUpBeforeClass(): void
{
self::$testDir = sys_get_temp_dir() . '/floatphp_test_' . uniqid();
self::$testFile = self::$testDir . '/test.txt';
mkdir(self::$testDir, 0777, true);
file_put_contents(self::$testFile, "Line 1\nLine 2\nLine 3");
}
public static function tearDownAfterClass(): void
{
if (file_exists(self::$testFile)) {
unlink(self::$testFile);
}
if (is_dir(self::$testDir)) {
rmdir(self::$testDir);
}
}
/**
* Test file analysis.
*/
public function testAnalyse(): void
{
$analysis = File::analyse(self::$testFile);
$this->assertIsArray($analysis);
$this->assertArrayHasKey('parent', $analysis);
$this->assertArrayHasKey('name', $analysis);
$this->assertArrayHasKey('filename', $analysis);
$this->assertArrayHasKey('extension', $analysis);
$this->assertArrayHasKey('size', $analysis);
$this->assertArrayHasKey('type', $analysis);
}
/**
* Test getting parent directory.
*/
public function testGetParentDir(): void
{
$parent = File::getParentDir(self::$testFile);
// Normalize path for comparison
$expectedParent = str_replace('\\', '/', self::$testDir);
$this->assertEquals($expectedParent, $parent);
}
/**
* Test getting file extension.
*/
public function testGetExtension(): void
{
$ext = File::getExtension(self::$testFile);
$this->assertEquals('txt', $ext);
$extUnformatted = File::getExtension(self::$testFile, false);
$this->assertEquals('txt', $extUnformatted);
}
/**
* Test getting file name without extension.
*/
public function testGetName(): void
{
$name = File::getName(self::$testFile);
$this->assertEquals('test', $name);
}
/**
* Test getting full filename.
*/
public function testGetFileName(): void
{
$filename = File::getFileName(self::$testFile);
$this->assertEquals('test.txt', $filename);
}
/**
* Test getting file size.
*/
public function testGetSize(): void
{
$size = File::getSize(self::$testFile);
$this->assertIsString($size);
$this->assertStringContainsString('B', $size);
$sizeWithDecimals = File::getSize(self::$testFile, 3);
$this->assertIsString($sizeWithDecimals);
}
/**
* Test getting file permissions.
*/
public function testGetPermissions(): void
{
$permissions = File::getPermissions(self::$testFile);
$this->assertIsString($permissions);
$this->assertEquals(4, strlen($permissions));
$permissionsInt = File::getPermissions(self::$testFile, true);
$this->assertIsInt($permissionsInt);
}
/**
* Test getting file lines.
*/
public function testGetLines(): void
{
$lines = File::getLines(self::$testFile);
$this->assertIsArray($lines);
$this->assertCount(3, $lines);
$this->assertStringContainsString('Line 1', $lines[0]);
}
/**
* Test checking if path is file.
*/
public function testIsFile(): void
{
$this->assertTrue(File::isFile(self::$testFile));
$this->assertFalse(File::isFile(self::$testDir));
$this->assertFalse(File::isFile('/nonexistent/file.txt'));
}
/**
* Test checking if path is directory.
*/
public function testIsDir(): void
{
$this->assertTrue(File::isDir(self::$testDir));
$this->assertFalse(File::isDir(self::$testFile));
$this->assertFalse(File::isDir('/nonexistent/directory'));
}
/**
* Test file existence.
*/
public function testExists(): void
{
$this->assertTrue(File::exists(self::$testFile));
$this->assertTrue(File::exists(self::$testDir));
$this->assertFalse(File::exists('/nonexistent/path'));
}
/**
* Test checking if file is empty.
*/
public function testIsEmpty(): void
{
$this->assertFalse(File::isEmpty(self::$testFile));
// Create empty file
$emptyFile = self::$testDir . '/empty.txt';
touch($emptyFile);
$this->assertTrue(File::isEmpty($emptyFile));
unlink($emptyFile);
}
/**
* Test file reading.
*/
public function testRead(): void
{
$content = File::read(self::$testFile);
$this->assertIsString($content);
$this->assertStringContainsString('Line 1', $content);
$this->assertStringContainsString('Line 2', $content);
}
/**
* Test file writing.
*/
public function testWrite(): void
{
$writeFile = self::$testDir . '/write_test.txt';
$content = 'Test write content';
$result = File::w($writeFile, $content);
$this->assertTrue($result);
$this->assertTrue(file_exists($writeFile));
$this->assertEquals($content, file_get_contents($writeFile));
unlink($writeFile);
}
/**
* Test file copying.
*/
public function testCopy(): void
{
$copyFile = self::$testDir . '/copy_test.txt';
$result = File::copy(self::$testFile, $copyFile);
$this->assertTrue($result);
$this->assertTrue(file_exists($copyFile));
$this->assertEquals(file_get_contents(self::$testFile), file_get_contents($copyFile));
unlink($copyFile);
}
/**
* Test file moving.
*/
public function testMove(): void
{
$tempFile = self::$testDir . '/temp_move.txt';
file_put_contents($tempFile, 'temp content');
$moveFile = self::$testDir . '/moved_test.txt';
$result = File::move($tempFile, $moveFile);
$this->assertTrue($result);
$this->assertFalse(file_exists($tempFile));
$this->assertTrue(file_exists($moveFile));
unlink($moveFile);
}
/**
* Test file deletion.
*/
public function testDelete(): void
{
$deleteFile = self::$testDir . '/delete_test.txt';
file_put_contents($deleteFile, 'delete me');
$this->assertTrue(file_exists($deleteFile));
$result = File::remove($deleteFile);
$this->assertTrue($result);
$this->assertFalse(file_exists($deleteFile));
}
/**
* Test getting MIME type.
*/
public function testGetMimeType(): void
{
$mimeType = File::getMimeType(self::$testFile);
$this->assertIsString($mimeType);
// MIME type detection may vary, just ensure it returns a string
}
/**
* Test file search functionality.
*/
public function testSearch(): void
{
$results = File::scanDir(self::$testDir);
$this->assertIsArray($results);
$this->assertContains('test.txt', $results);
}
/**
* Test edge cases with non-existent files.
*/
public function testEdgeCases(): void
{
$nonExistent = '/nonexistent/file.txt';
$this->assertFalse(File::isFile($nonExistent));
$this->assertFalse(File::exists($nonExistent));
$this->assertEquals('', File::read($nonExistent));
$this->assertFalse(File::copy($nonExistent, '/tmp/test.txt'));
}
/**
* Test path formatting.
*/
public function testPathFormatting(): void
{
$windowsPath = 'C:\\test\\file.txt';
$unixPath = \FloatPHP\Classes\Filesystem\Stringify::formatPath($windowsPath);
$this->assertStringNotContainsString('\\', $unixPath);
}
}