Skip to content

Commit 2d8adc3

Browse files
f3l1xclaude
authored andcommitted
Cleanup File class and rewrite tests to phpt format
- Remove deprecated exist() method - Remove useless docblock comments - Rewrite tests to use Toolkit::test() format
1 parent 8e75ac9 commit 2d8adc3

File tree

6 files changed

+233
-307
lines changed

6 files changed

+233
-307
lines changed

src/File.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ public function __construct(string $path, string|null $title = null)
2020
$this->title = $title;
2121
}
2222

23-
/**
24-
* Get the file name (basename)
25-
*/
2623
public function getName(): string
2724
{
2825
return basename($this->path);
2926
}
3027

3128
/**
32-
* Get the file size in bytes
33-
*
3429
* @throws FileNotFoundException
3530
*/
3631
public function getSize(): int
@@ -42,41 +37,22 @@ public function getSize(): int
4237
return $size !== false ? $size : 0;
4338
}
4439

45-
/**
46-
* Get the file title
47-
*/
4840
public function getTitle(): string|null
4941
{
5042
return $this->title;
5143
}
5244

53-
/**
54-
* Get the file path
55-
*/
5645
public function getPath(): string
5746
{
5847
return $this->path;
5948
}
6049

61-
/**
62-
* Check if file exists
63-
*/
6450
public function exists(): bool
6551
{
6652
return file_exists($this->path) && is_file($this->path);
6753
}
6854

6955
/**
70-
* @deprecated Use exists() instead
71-
*/
72-
public function exist(): bool
73-
{
74-
return $this->exists();
75-
}
76-
77-
/**
78-
* Move the file to a new location
79-
*
8056
* @throws FileNotFoundException
8157
*/
8258
public function move(string $destination, bool $overwrite = true): static
@@ -91,8 +67,6 @@ public function move(string $destination, bool $overwrite = true): static
9167
}
9268

9369
/**
94-
* Convert file to Nette Image
95-
*
9670
* @throws FileNotFoundException
9771
*/
9872
public function toImage(): Image
@@ -103,8 +77,6 @@ public function toImage(): Image
10377
}
10478

10579
/**
106-
* Convert file to FileResponse for download
107-
*
10880
* @throws FileNotFoundException
10981
*/
11082
public function toResponse(string|null $name = null, string|null $contentType = null, bool $forceDownload = true): FileResponse
@@ -120,8 +92,6 @@ public function toResponse(string|null $name = null, string|null $contentType =
12092
}
12193

12294
/**
123-
* Assert that the file exists
124-
*
12595
* @throws FileNotFoundException
12696
*/
12797
private function assertExists(): void

src/Http/FileResponse.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ public function isForceDownload(): bool
6161
return $this->forceDownload;
6262
}
6363

64-
/**
65-
* Send file to output
66-
*/
6764
public function send(): void
6865
{
6966
$name = $this->name;

tests/Cases/File.phpt

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Contributte\Tester\Toolkit;
4+
use Contributte\Utils\Exception\Runtime\FileNotFoundException;
5+
use Contributte\Utils\File;
6+
use Contributte\Utils\Http\FileResponse;
7+
use Nette\Utils\FileSystem;
8+
use Nette\Utils\Image;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/../bootstrap.php';
12+
13+
$tempDir = __DIR__ . '/../temp/file-test';
14+
FileSystem::createDir($tempDir);
15+
16+
// File::getName
17+
Toolkit::test(function (): void {
18+
$file = new File('/path/to/test.txt');
19+
Assert::same('test.txt', $file->getName());
20+
21+
$file2 = new File('/another/path/document.pdf');
22+
Assert::same('document.pdf', $file2->getName());
23+
});
24+
25+
// File::getTitle
26+
Toolkit::test(function (): void {
27+
$file = new File('/path/to/test.txt', 'My Document');
28+
Assert::same('My Document', $file->getTitle());
29+
30+
$fileWithoutTitle = new File('/path/to/test.txt');
31+
Assert::null($fileWithoutTitle->getTitle());
32+
});
33+
34+
// File::getPath
35+
Toolkit::test(function (): void {
36+
$path = '/path/to/test.txt';
37+
$file = new File($path);
38+
Assert::same(str_replace('/', DIRECTORY_SEPARATOR, $path), $file->getPath());
39+
});
40+
41+
// File::exists
42+
Toolkit::test(function () use ($tempDir): void {
43+
$filePath = $tempDir . '/existing.txt';
44+
FileSystem::write($filePath, 'test content');
45+
46+
$file = new File($filePath);
47+
Assert::true($file->exists());
48+
49+
$nonExisting = new File($tempDir . '/non-existing.txt');
50+
Assert::false($nonExisting->exists());
51+
});
52+
53+
// File::getSize
54+
Toolkit::test(function () use ($tempDir): void {
55+
$content = 'Hello, World!';
56+
$filePath = $tempDir . '/size-test.txt';
57+
FileSystem::write($filePath, $content);
58+
59+
$file = new File($filePath);
60+
Assert::same(strlen($content), $file->getSize());
61+
});
62+
63+
// File::getSize throws on non-existing file
64+
Toolkit::test(function () use ($tempDir): void {
65+
$file = new File($tempDir . '/non-existing.txt');
66+
67+
Assert::exception(function () use ($file): void {
68+
$file->getSize();
69+
}, FileNotFoundException::class);
70+
});
71+
72+
// File::move
73+
Toolkit::test(function () use ($tempDir): void {
74+
$originalPath = $tempDir . '/original.txt';
75+
$newPath = $tempDir . '/moved.txt';
76+
FileSystem::write($originalPath, 'test content');
77+
78+
$file = new File($originalPath);
79+
$result = $file->move($newPath);
80+
81+
Assert::same($file, $result);
82+
Assert::same(str_replace('/', DIRECTORY_SEPARATOR, $newPath), $file->getPath());
83+
Assert::true($file->exists());
84+
Assert::false(file_exists($originalPath));
85+
});
86+
87+
// File::move throws on non-existing file
88+
Toolkit::test(function () use ($tempDir): void {
89+
$file = new File($tempDir . '/non-existing.txt');
90+
91+
Assert::exception(function () use ($file, $tempDir): void {
92+
$file->move($tempDir . '/destination.txt');
93+
}, FileNotFoundException::class);
94+
});
95+
96+
// File::toImage
97+
Toolkit::test(function () use ($tempDir): void {
98+
$imagePath = $tempDir . '/test.png';
99+
$image = Image::fromBlank(100, 100, Image::rgb(255, 0, 0));
100+
$image->save($imagePath);
101+
102+
$file = new File($imagePath);
103+
$loadedImage = $file->toImage();
104+
105+
Assert::type(Image::class, $loadedImage);
106+
Assert::same(100, $loadedImage->getWidth());
107+
Assert::same(100, $loadedImage->getHeight());
108+
});
109+
110+
// File::toImage throws on non-existing file
111+
Toolkit::test(function () use ($tempDir): void {
112+
$file = new File($tempDir . '/non-existing.png');
113+
114+
Assert::exception(function () use ($file): void {
115+
$file->toImage();
116+
}, FileNotFoundException::class);
117+
});
118+
119+
// File::toResponse
120+
Toolkit::test(function () use ($tempDir): void {
121+
$filePath = $tempDir . '/download.txt';
122+
FileSystem::write($filePath, 'downloadable content');
123+
124+
$file = new File($filePath, 'My Download');
125+
$response = $file->toResponse();
126+
127+
Assert::type(FileResponse::class, $response);
128+
});
129+
130+
// File::toResponse with custom name
131+
Toolkit::test(function () use ($tempDir): void {
132+
$filePath = $tempDir . '/download2.txt';
133+
FileSystem::write($filePath, 'downloadable content');
134+
135+
$file = new File($filePath);
136+
$response = $file->toResponse('custom-name.txt');
137+
138+
Assert::type(FileResponse::class, $response);
139+
});
140+
141+
// File::toResponse throws on non-existing file
142+
Toolkit::test(function () use ($tempDir): void {
143+
$file = new File($tempDir . '/non-existing.txt');
144+
145+
Assert::exception(function () use ($file): void {
146+
$file->toResponse();
147+
}, FileNotFoundException::class);
148+
});
149+
150+
// File path normalization
151+
Toolkit::test(function (): void {
152+
$file = new File('/path/to\\mixed/separators.txt');
153+
$path = $file->getPath();
154+
155+
if (DIRECTORY_SEPARATOR === '\\') {
156+
Assert::same('\\path\\to\\mixed\\separators.txt', $path);
157+
} else {
158+
Assert::same('/path/to/mixed/separators.txt', $path);
159+
}
160+
});
161+
162+
FileSystem::delete($tempDir);

0 commit comments

Comments
 (0)