1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* this file is part of magerun |
4
|
|
|
* |
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace N98\Util; |
9
|
|
|
|
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class FilesystemTest |
14
|
|
|
* @package N98\Util |
15
|
|
|
* @author Aydin Hassan <[email protected]> |
16
|
|
|
* @covers N98\Util\Filesystem |
17
|
|
|
*/ |
18
|
|
|
class FilesystemTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Filesystem |
22
|
|
|
*/ |
23
|
|
|
protected $fileSystem; |
24
|
|
|
|
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->fileSystem = new Filesystem(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @expectedException RuntimeException |
32
|
|
|
*/ |
33
|
|
|
public function testRecursiveCopy() |
34
|
|
|
{ |
35
|
|
|
$tmp = sys_get_temp_dir(); |
36
|
|
|
$basePath = $tmp . "/n98_testdir"; |
37
|
|
|
$folder1 = $basePath . "/folder1"; |
38
|
|
|
$folder2 = $basePath . "/folder2"; |
39
|
|
|
$file1 = $folder1 . "/file1.txt"; |
40
|
|
|
$file2 = $folder2 . "/file2.txt"; |
41
|
|
|
$dest = sys_get_temp_dir() . "/n98_copy_dest"; |
42
|
|
|
|
43
|
|
|
@mkdir($folder1, 0777, true); |
|
|
|
|
44
|
|
|
@mkdir($folder2, 0777, true); |
|
|
|
|
45
|
|
|
touch($file1); |
46
|
|
|
touch($file2); |
47
|
|
|
|
48
|
|
|
$this->fileSystem->recursiveCopy($basePath, $dest); |
49
|
|
|
$this->assertFileExists($dest . "/folder1/file1.txt"); |
50
|
|
|
$this->assertFileExists($dest . "/folder2/file2.txt"); |
51
|
|
|
|
52
|
|
|
//cleanup |
53
|
|
|
unlink($file1); |
54
|
|
|
unlink($file2); |
55
|
|
|
rmdir($folder1); |
56
|
|
|
rmdir($folder2); |
57
|
|
|
rmdir($basePath); |
58
|
|
|
|
59
|
|
|
unlink($dest . "/folder1/file1.txt"); |
60
|
|
|
unlink($dest . "/folder2/file2.txt"); |
61
|
|
|
rmdir($dest . "/folder1"); |
62
|
|
|
rmdir($dest . "/folder2"); |
63
|
|
|
rmdir($dest); |
64
|
|
|
|
65
|
|
|
$this->assertFileNotExists($dest . "/folder1/file1.txt"); |
66
|
|
|
$this->assertFileNotExists($dest); |
67
|
|
|
|
68
|
|
|
is_dir($tmp . '/a') || mkdir($tmp . '/a'); |
69
|
|
|
touch($tmp . '/file1.txt'); |
70
|
|
|
$this->fileSystem->recursiveCopy($tmp . '/a', $tmp . '/file1.txt'); |
71
|
|
|
unlink($tmp . '/file1.txt'); |
72
|
|
|
rmdir($tmp . '/a'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testRecursiveCopyWithBlacklist() |
76
|
|
|
{ |
77
|
|
|
$tmp = sys_get_temp_dir(); |
78
|
|
|
$basePath = $tmp . "/n98_testdir"; |
79
|
|
|
$folder1 = $basePath . "/folder1"; |
80
|
|
|
$folder2 = $basePath . "/folder2"; |
81
|
|
|
$file1 = $folder1 . "/file1.txt"; |
82
|
|
|
$ignoreMe = $folder1 . "/ignore.me"; |
83
|
|
|
$file2 = $folder2 . "/file2.txt"; |
84
|
|
|
$dest = sys_get_temp_dir() . "/n98_copy_dest"; |
85
|
|
|
$this->fileSystem->recursiveRemoveDirectory($dest, true); |
86
|
|
|
|
87
|
|
|
@mkdir($folder1, 0777, true); |
|
|
|
|
88
|
|
|
@mkdir($folder2, 0777, true); |
|
|
|
|
89
|
|
|
touch($file1); |
90
|
|
|
touch($ignoreMe); |
91
|
|
|
touch($file2); |
92
|
|
|
|
93
|
|
|
$this->fileSystem->recursiveCopy($basePath, $dest, array('ignore.me')); |
94
|
|
|
$this->assertFileExists($dest . "/folder1/file1.txt"); |
95
|
|
|
$this->assertFileNotExists($dest . "/folder1/ignore.me"); |
96
|
|
|
$this->assertFileExists($dest . "/folder2/file2.txt"); |
97
|
|
|
|
98
|
|
|
//cleanup |
99
|
|
|
unlink($file1); |
100
|
|
|
unlink($ignoreMe); |
101
|
|
|
unlink($file2); |
102
|
|
|
rmdir($folder1); |
103
|
|
|
rmdir($folder2); |
104
|
|
|
rmdir($basePath); |
105
|
|
|
|
106
|
|
|
unlink($dest . "/folder1/file1.txt"); |
107
|
|
|
unlink($dest . "/folder2/file2.txt"); |
108
|
|
|
rmdir($dest . "/folder1"); |
109
|
|
|
rmdir($dest . "/folder2"); |
110
|
|
|
rmdir($dest); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @requires function symlink |
115
|
|
|
*/ |
116
|
|
|
public function testRecursiveDirectoryRemoveUnLinksSymLinks() |
117
|
|
|
{ |
118
|
|
|
$tmp = sys_get_temp_dir(); |
119
|
|
|
$basePath = $tmp . "/n98_testdir"; |
120
|
|
|
$symLinked = $tmp . "/n98_linked"; |
121
|
|
|
$symLinkedFile = $symLinked . "/symlinkme.txt"; |
122
|
|
|
|
123
|
|
|
@mkdir($basePath, 0777, true); |
|
|
|
|
124
|
|
|
@mkdir($symLinked, 0777, true); |
|
|
|
|
125
|
|
|
|
126
|
|
|
touch($symLinkedFile); |
127
|
|
|
|
128
|
|
|
$result = @symlink($symLinked, $basePath . "/symlink"); |
129
|
|
|
$this->assertTrue($result); |
130
|
|
|
|
131
|
|
|
$this->fileSystem->recursiveRemoveDirectory($basePath); |
132
|
|
|
|
133
|
|
|
$this->assertFileExists($symLinkedFile); |
134
|
|
|
$this->assertFileNotExists($basePath); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
View Code Duplication |
public function testRecursiveRemove() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$tmp = sys_get_temp_dir(); |
140
|
|
|
$basePath = $tmp . "/n98_testdir"; |
141
|
|
|
$folder1 = $basePath . "/folder1"; |
142
|
|
|
$folder2 = $basePath . "/folder2"; |
143
|
|
|
$file1 = $folder1 . "/file1.txt"; |
144
|
|
|
$file2 = $folder2 . "/file2.txt"; |
145
|
|
|
|
146
|
|
|
@mkdir($folder1, 0777, true); |
|
|
|
|
147
|
|
|
@mkdir($folder2, 0777, true); |
|
|
|
|
148
|
|
|
touch($file1); |
149
|
|
|
touch($file2); |
150
|
|
|
|
151
|
|
|
$this->fileSystem->recursiveRemoveDirectory($basePath); |
152
|
|
|
$this->assertFileNotExists($basePath); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function testRecursiveRemoveWithTrailingSlash() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$tmp = sys_get_temp_dir(); |
158
|
|
|
$basePath = $tmp . "/n98_testdir"; |
159
|
|
|
$folder1 = $basePath . "/folder1"; |
160
|
|
|
$folder2 = $basePath . "/folder2"; |
161
|
|
|
$file1 = $folder1 . "/file1.txt"; |
162
|
|
|
$file2 = $folder2 . "/file2.txt"; |
163
|
|
|
|
164
|
|
|
@mkdir($folder1, 0777, true); |
|
|
|
|
165
|
|
|
@mkdir($folder2, 0777, true); |
|
|
|
|
166
|
|
|
touch($file1); |
167
|
|
|
touch($file2); |
168
|
|
|
|
169
|
|
|
$this->fileSystem->recursiveRemoveDirectory($basePath . "/"); |
170
|
|
|
$this->assertFileNotExists($basePath); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testFalseIsReturnedIfDirectoryNotExist() |
174
|
|
|
{ |
175
|
|
|
$this->assertFalse($this->fileSystem->recursiveRemoveDirectory("not-a-folder")); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testFalseIsReturnedIfDirectoryNotReadable() |
179
|
|
|
{ |
180
|
|
|
$tmp = sys_get_temp_dir(); |
181
|
|
|
$basePath = $tmp . "/n98_testdir-never-existed"; |
182
|
|
|
|
183
|
|
|
$this->assertFalse($this->fileSystem->recursiveRemoveDirectory($basePath)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testParentIsNotRemovedIfEmptyIsTrue() |
187
|
|
|
{ |
188
|
|
|
$tmp = sys_get_temp_dir(); |
189
|
|
|
$basePath = $tmp . "/n98_testdir"; |
190
|
|
|
$folder1 = $basePath . "/folder1"; |
191
|
|
|
$folder2 = $basePath . "/folder2"; |
192
|
|
|
$file1 = $folder1 . "/file1.txt"; |
193
|
|
|
$file2 = $folder2 . "/file2.txt"; |
194
|
|
|
|
195
|
|
|
@mkdir($folder1, 0777, true); |
|
|
|
|
196
|
|
|
@mkdir($folder2, 0777, true); |
|
|
|
|
197
|
|
|
touch($file1); |
198
|
|
|
touch($file2); |
199
|
|
|
|
200
|
|
|
$this->fileSystem->recursiveRemoveDirectory($basePath, true); |
201
|
|
|
$this->assertFileExists($basePath); |
202
|
|
|
$this->assertFileNotExists($folder1); |
203
|
|
|
$this->assertFileNotExists($folder2); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param int $bytes |
208
|
|
|
* @param int $decimalPlaces |
209
|
|
|
* @param string $expected |
210
|
|
|
* @dataProvider convertedBytesProvider |
211
|
|
|
*/ |
212
|
|
|
public function testConvertBytesToHumanReadable($bytes, $decimalPlaces, $expected) |
213
|
|
|
{ |
214
|
|
|
$res = Filesystem::humanFileSize($bytes, $decimalPlaces); |
215
|
|
|
$this->assertSame($expected, $res); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public static function convertedBytesProvider() |
222
|
|
|
{ |
223
|
|
|
return array( |
224
|
|
|
array(20000000, 2, '19.07M'), |
225
|
|
|
array(20000000, 3, '19.073M'), |
226
|
|
|
array(2000000000, 2, '1.86G'), |
227
|
|
|
array(2, 2, '2.00B'), |
228
|
|
|
array(2048, 2, '2.00K'), |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: