1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Defines the \TraderInteractive\Util\FileTest class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace TraderInteractive\Util; |
7
|
|
|
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use TraderInteractive\Util\File as F; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @coversDefaultClass \TraderInteractive\Util\File |
13
|
|
|
*/ |
14
|
|
|
final class FileTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private $topLevelDirPath; |
17
|
|
|
private $topLevelFilePath; |
18
|
|
|
private $subLevelDirPath; |
19
|
|
|
private $subLevelFilePath; |
20
|
|
|
|
21
|
|
|
private $oldErrorReporting; |
22
|
|
|
|
23
|
|
|
public function setup() |
24
|
|
|
{ |
25
|
|
|
parent::setup(); |
26
|
|
|
|
27
|
|
|
$this->oldErrorReporting = error_reporting(); |
28
|
|
|
|
29
|
|
|
$this->topLevelDirPath = sys_get_temp_dir() . '/topLevelTempDir'; |
30
|
|
|
$this->topLevelFilePath = "{$this->topLevelDirPath}/topLevelTempFile"; |
31
|
|
|
$this->subLevelDirPath = "{$this->topLevelDirPath}/subLevelTempDir"; |
32
|
|
|
$this->subLevelFilePath = "{$this->subLevelDirPath}/subLevelTempFile"; |
33
|
|
|
|
34
|
|
|
$this->deleteTestFiles(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
//this is just for convenience, DO NOT RELY ON IT |
38
|
|
|
public function tearDown() |
39
|
|
|
{ |
40
|
|
|
error_reporting($this->oldErrorReporting); |
41
|
|
|
|
42
|
|
|
$this->deleteTestFiles(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function deleteTestFiles() |
46
|
|
|
{ |
47
|
|
|
if (is_dir($this->topLevelDirPath)) { |
48
|
|
|
chmod($this->topLevelDirPath, 0777); |
49
|
|
|
|
50
|
|
|
if (is_file($this->topLevelFilePath)) { |
51
|
|
|
unlink($this->topLevelFilePath); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (is_dir($this->subLevelDirPath)) { |
55
|
|
|
if (is_file($this->subLevelFilePath)) { |
56
|
|
|
unlink($this->subLevelFilePath); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
rmdir($this->subLevelDirPath); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
rmdir($this->topLevelDirPath); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
* @covers ::deleteDirectoryContents |
69
|
|
|
* @expectedException \Exception |
70
|
|
|
* @expectedExceptionMessage cannot list directory '/some/where/that/doesnt/exist' |
71
|
|
|
*/ |
72
|
|
|
public function deleteDirectoryContentsNonExistentPath() |
73
|
|
|
{ |
74
|
|
|
error_reporting(0); |
75
|
|
|
F::deleteDirectoryContents('/some/where/that/doesnt/exist'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
* @covers ::deleteDirectoryContents |
81
|
|
|
*/ |
82
|
|
|
public function deleteDirectoryContentsEmpty() |
83
|
|
|
{ |
84
|
|
|
$this->assertTrue(mkdir($this->topLevelDirPath)); |
85
|
|
|
|
86
|
|
|
F::deleteDirectoryContents($this->topLevelDirPath); |
87
|
|
|
|
88
|
|
|
$this->assertTrue(rmdir($this->topLevelDirPath)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
* @covers ::deleteDirectoryContents |
94
|
|
|
*/ |
95
|
|
|
public function deleteDirectoryContentsWithFiles() |
96
|
|
|
{ |
97
|
|
|
$this->assertTrue(mkdir($this->subLevelDirPath, 0777, true)); |
98
|
|
|
|
99
|
|
|
file_put_contents($this->topLevelFilePath, 'hello dolly !'); |
100
|
|
|
file_put_contents($this->subLevelFilePath, 'hello dolly 2!'); |
101
|
|
|
|
102
|
|
|
F::deleteDirectoryContents($this->topLevelDirPath); |
103
|
|
|
|
104
|
|
|
$this->assertTrue(rmdir($this->topLevelDirPath)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
* @covers ::deleteDirectoryContents |
110
|
|
|
* @expectedException \Exception |
111
|
|
|
* @expectedExceptionCode 2 |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function deleteDirectoryContentsWithProtectedFile() |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->assertTrue(mkdir($this->topLevelDirPath)); |
116
|
|
|
|
117
|
|
|
file_put_contents($this->topLevelFilePath, 'hello dolly !'); |
118
|
|
|
|
119
|
|
|
$this->assertTrue(chmod($this->topLevelDirPath, 0555)); |
120
|
|
|
|
121
|
|
|
error_reporting(0); |
122
|
|
|
F::deleteDirectoryContents($this->topLevelDirPath); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @test |
127
|
|
|
* @covers ::deleteDirectoryContents |
128
|
|
|
* @expectedException \Exception |
129
|
|
|
* @expectedExceptionCode 1 |
130
|
|
|
*/ |
131
|
|
|
public function deleteDirectoryContentsWithProtectedDirectory() |
132
|
|
|
{ |
133
|
|
|
$this->assertTrue(mkdir($this->subLevelDirPath, 0777, true)); |
134
|
|
|
|
135
|
|
|
$this->assertTrue(chmod($this->topLevelDirPath, 0555)); |
136
|
|
|
|
137
|
|
|
error_reporting(0); |
138
|
|
|
F::deleteDirectoryContents($this->topLevelDirPath); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
* @covers ::delete |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function deleteBasic() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$this->assertTrue(mkdir($this->topLevelDirPath)); |
148
|
|
|
file_put_contents($this->topLevelFilePath, 'some text'); |
149
|
|
|
F::delete($this->topLevelFilePath); |
150
|
|
|
$this->assertFalse(file_exists($this->topLevelFilePath)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @test |
155
|
|
|
* @covers ::delete |
156
|
|
|
*/ |
157
|
|
|
public function deleteNonExistent() |
158
|
|
|
{ |
159
|
|
|
$this->assertFalse(file_exists('/path/does/not/exist')); |
160
|
|
|
F::delete('/path/does/not/exist'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @test |
165
|
|
|
* @covers ::delete |
166
|
|
|
* @expectedException \Exception |
167
|
|
|
*/ |
168
|
|
|
public function deleteDirectory() |
169
|
|
|
{ |
170
|
|
|
$this->assertTrue(mkdir($this->topLevelDirPath)); |
171
|
|
|
error_reporting(0); |
172
|
|
|
F::delete($this->topLevelDirPath); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @test |
177
|
|
|
* @covers ::delete |
178
|
|
|
* @expectedException \InvalidArgumentException |
179
|
|
|
* @expectedExceptionMessage $path is not a string or is whitespace |
180
|
|
|
*/ |
181
|
|
|
public function deletePathIsWhitespace() |
182
|
|
|
{ |
183
|
|
|
F::delete(' '); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Verify behavior of delete() with protected file. |
188
|
|
|
* |
189
|
|
|
* @test |
190
|
|
|
* @covers ::delete |
191
|
|
|
* @expectedException \Exception |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function deleteProtectedFile() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$this->assertTrue(mkdir($this->topLevelDirPath)); |
196
|
|
|
|
197
|
|
|
file_put_contents($this->topLevelFilePath, 'hello dolly !'); |
198
|
|
|
|
199
|
|
|
$this->assertTrue(chmod($this->topLevelDirPath, 0555)); |
200
|
|
|
|
201
|
|
|
error_reporting(0); |
202
|
|
|
F::delete($this->topLevelDirPath); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* verify basic behavior of deletePathIfEmpty(). |
207
|
|
|
* |
208
|
|
|
* @test |
209
|
|
|
* @covers ::deletePathIfEmpty |
210
|
|
|
* |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
public function deletePathIfEmpty() |
214
|
|
|
{ |
215
|
|
|
$path = "{$this->topLevelDirPath}/path/to/sub/folder"; |
216
|
|
|
mkdir($path, 0755, true); |
217
|
|
|
$this->assertFileExists($path, "Unable to create '{$path}'."); |
218
|
|
|
F::deletePathIfEmpty($path, $this->topLevelDirPath); |
219
|
|
|
$this->assertFileNotExists($path, "Unable to delete '{$path}'."); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* verify behavior of deletePathIfEmpty() when $deletePath does not exist. |
224
|
|
|
* |
225
|
|
|
* @test |
226
|
|
|
* @covers ::deletePathIfEmpty |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
public function deletePathIfEmptyNotExists() |
231
|
|
|
{ |
232
|
|
|
$path = "{$this->topLevelDirPath}/path/to/sub/folder"; |
233
|
|
|
$this->assertFileNotExists($path, "Unable to delete '{$path}'."); |
234
|
|
|
$this->assertNull(F::deletePathIfEmpty($path)); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* verify behavior of deletePathIfEmpty() when a folder in $deletePath contains a file. |
239
|
|
|
* |
240
|
|
|
* @test |
241
|
|
|
* @covers ::deletePathIfEmpty |
242
|
|
|
* |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
public function deletePathIfEmptyNotEmpty() |
246
|
|
|
{ |
247
|
|
|
$path = "{$this->topLevelDirPath}/path/to/sub/folder"; |
248
|
|
|
$file = "{$this->topLevelDirPath}/path/to/file.txt"; |
249
|
|
|
mkdir($path, 0777, true); |
250
|
|
|
touch($file); |
251
|
|
|
$this->assertFileExists($path, "Unable to create '{$path}'."); |
252
|
|
|
$this->assertFileExists($file, 'Unable to create text file'); |
253
|
|
|
F::deletePathIfEmpty($path, $this->topLevelDirPath); |
254
|
|
|
$this->assertFileNotExists("{$this->topLevelDirPath}/path/to/sub/folder"); |
255
|
|
|
$this->assertFileNotExists("{$this->topLevelDirPath}/path/to/sub"); |
256
|
|
|
$this->assertFileExists($file, "{$file} was deleted"); |
257
|
|
|
unlink($file); |
258
|
|
|
$this->assertFileNotExists($file, "{$file} was not deleted"); |
259
|
|
|
F::deletePathIfEmpty("{$this->topLevelDirPath}/path/to", $this->topLevelDirPath); |
260
|
|
|
$this->assertFileNotExists("{$this->topLevelDirPath}/path/to"); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.