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
134 lines (110 loc) · 4.12 KB
/
Copy pathFileTest.php
File metadata and controls
134 lines (110 loc) · 4.12 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
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* 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 ZipArchive;
/**
* @internal
*/
final class FileTest extends CIUnitTestCase
{
public function testNewGoodChecked()
{
$path = SYSTEMPATH . 'Common.php';
$file = new File($path, true);
$this->assertSame($path, $file->getRealPath());
}
public function testNewGoodUnchecked()
{
$path = SYSTEMPATH . 'Common.php';
$file = new File($path, false);
$this->assertSame($path, $file->getRealPath());
}
public function testNewBadUnchecked()
{
$path = SYSTEMPATH . 'bogus';
$file = new File($path, false);
$this->assertFalse($file->getRealPath());
}
public function testGuessExtension()
{
$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()
{
$file = new File(SYSTEMPATH . 'Common.php');
$result1 = $file->getRandomName();
$this->assertNotSame($result1, $file->getRandomName());
}
public function testCanAccessSplFileInfoMethods()
{
$file = new File(SYSTEMPATH . 'Common.php');
$this->assertSame('file', $file->getType());
}
public function testGetSizeReturnsKB()
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024, 3);
$this->assertSame($size, $file->getSizeByUnit('kb'));
}
public function testGetSizeReturnsMB()
{
$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()
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = filesize(SYSTEMPATH . 'Common.php');
$this->assertSame($size, $file->getSizeByUnit('b'));
}
public function testThrowsExceptionIfNotAFile()
{
$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');
}
}