forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTest.php
More file actions
192 lines (162 loc) · 6.02 KB
/
FileTest.php
File metadata and controls
192 lines (162 loc) · 6.02 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Files;
use CodeIgniter\Files\Exceptions\FileNotFoundException;
use CodeIgniter\Test\CIUnitTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use ZipArchive;
/**
* @internal
*/
#[Group('Others')]
final class FileTest extends CIUnitTestCase
{
public function testNewGoodChecked(): void
{
$path = SYSTEMPATH . 'Common.php';
$file = new File($path, true);
$this->assertSame($path, $file->getRealPath());
}
public function testNewGoodUnchecked(): void
{
$path = SYSTEMPATH . 'Common.php';
$file = new File($path, false);
$this->assertSame($path, $file->getRealPath());
}
public function testNewBadUnchecked(): void
{
$path = SYSTEMPATH . 'bogus';
$file = new File($path, false);
$this->assertFalse($file->getRealPath());
}
public function testGuessExtension(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$this->assertSame('php', $file->guessExtension());
$file = new File(SYSTEMPATH . 'index.html');
$this->assertSame('html', $file->guessExtension());
$file = new File(ROOTPATH . 'phpunit.xml.dist');
$this->assertSame('xml', $file->guessExtension());
$tmp = tempnam(SUPPORTPATH, 'foo');
$file = new File($tmp, true);
$this->assertNull($file->guessExtension());
unlink($tmp);
}
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6046
*/
public function testGuessExtensionOnZip(): void
{
$tmp = SUPPORTPATH . 'foobar.zip';
$zip = new ZipArchive();
$zip->open($tmp, ZipArchive::CREATE | ZipArchive::CHECKCONS | ZipArchive::EXCL);
$zip->addFile(SYSTEMPATH . 'Common.php');
$zip->close();
$file = new File($tmp, true);
$this->assertSame('zip', $file->guessExtension());
unlink($tmp);
}
public function testRandomName(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$result1 = $file->getRandomName();
$this->assertNotSame($result1, $file->getRandomName());
}
public function testCanAccessSplFileInfoMethods(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$this->assertSame('file', $file->getType());
}
public function testGetSizeReturnsKB(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024, 3);
$this->assertSame($size, $file->getSizeByUnit('kb'));
}
public function testGetSizeReturnsMB(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024 / 1024, 3);
$this->assertSame($size, $file->getSizeByUnit('mb'));
}
public function testGetSizeReturnsBytes(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = filesize(SYSTEMPATH . 'Common.php');
$this->assertSame($size, $file->getSizeByUnit('b'));
}
#[DataProvider('provideGetSizeData')]
public function testGetSizeBinary(FileSizeUnit $unit): void
{
$divider = 1024 ** $unit->value;
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / $divider, 3);
$this->assertSame($size, $file->getSizeByBinaryUnit($unit));
}
public function testGetSizeBinaryBytes(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = filesize(SYSTEMPATH . 'Common.php');
$this->assertSame($size, $file->getSizeByBinaryUnit(FileSizeUnit::B));
}
#[DataProvider('provideGetSizeData')]
public function testGetSizeMetric(FileSizeUnit $unit): void
{
$divider = 1000 ** $unit->value;
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / $divider, 3);
$this->assertSame($size, $file->getSizeByMetricUnit($unit));
}
public function testGetSizeMetricBytes(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = filesize(SYSTEMPATH . 'Common.php');
$this->assertSame($size, $file->getSizeByMetricUnit(FileSizeUnit::B));
}
public function testThrowsExceptionIfNotAFile(): void
{
$this->expectException(FileNotFoundException::class);
new File(SYSTEMPATH . 'Commoner.php', true);
}
public function testGetDestination(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
copy(SYSTEMPATH . 'Common.php', SYSTEMPATH . 'Common_Copy.php');
$this->assertSame(SYSTEMPATH . 'Common_Copy_1.php', $file->getDestination(SYSTEMPATH . 'Common_Copy.php', ''));
$this->assertSame(SYSTEMPATH . 'Common_1.php', $file->getDestination(SYSTEMPATH . 'Common.php'));
$this->assertSame(SYSTEMPATH . 'Common_Copy_5.php', $file->getDestination(SYSTEMPATH . 'Common_Copy_5.php'));
copy(SYSTEMPATH . 'Common_Copy.php', SYSTEMPATH . 'Common_Copy_5.php');
$this->assertSame(SYSTEMPATH . 'Common_Copy_6.php', $file->getDestination(SYSTEMPATH . 'Common_Copy_5.php'));
unlink(SYSTEMPATH . 'Common_Copy.php');
unlink(SYSTEMPATH . 'Common_Copy_5.php');
}
/**
* @return array<string, array<int, FileSizeUnit>>
*/
public static function provideGetSizeData(): array
{
return [
'returns KB binary' => [
FileSizeUnit::KB,
],
'returns MB binary' => [
FileSizeUnit::MB,
],
'returns GB binary' => [
FileSizeUnit::GB,
],
'returns TB binary' => [
FileSizeUnit::TB,
],
];
}
}