Completed
Pull Request — master (#3)
by Chad
01:45
created

FileTest::deletePathIsWhitespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 \InvalidArgumentException
70
     * @expectedExceptionMessage $directoryPath is not a string
71
     */
72
    public function deleteDirectoryContentsNonStringPath()
73
    {
74
        F::deleteDirectoryContents(1);
75
    }
76
77
    /**
78
     * @test
79
     * @covers ::deleteDirectoryContents
80
     * @expectedException \Exception
81
     * @expectedExceptionMessage cannot list directory '/some/where/that/doesnt/exist'
82
     */
83
    public function deleteDirectoryContentsNonExistentPath()
84
    {
85
        error_reporting(0);
86
        F::deleteDirectoryContents('/some/where/that/doesnt/exist');
87
    }
88
89
    /**
90
     * @test
91
     * @covers ::deleteDirectoryContents
92
     */
93
    public function deleteDirectoryContentsEmpty()
94
    {
95
        $this->assertTrue(mkdir($this->topLevelDirPath));
96
97
        F::deleteDirectoryContents($this->topLevelDirPath);
98
99
        $this->assertTrue(rmdir($this->topLevelDirPath));
100
    }
101
102
    /**
103
     * @test
104
     * @covers ::deleteDirectoryContents
105
     */
106
    public function deleteDirectoryContentsWithFiles()
107
    {
108
        $this->assertTrue(mkdir($this->subLevelDirPath, 0777, true));
109
110
        file_put_contents($this->topLevelFilePath, 'hello dolly !');
111
        file_put_contents($this->subLevelFilePath, 'hello dolly 2!');
112
113
        F::deleteDirectoryContents($this->topLevelDirPath);
114
115
        $this->assertTrue(rmdir($this->topLevelDirPath));
116
    }
117
118
    /**
119
     * @test
120
     * @covers ::deleteDirectoryContents
121
     * @expectedException \Exception
122
     * @expectedExceptionCode 2
123
     */
124 View Code Duplication
    public function deleteDirectoryContentsWithProtectedFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
125
    {
126
        $this->assertTrue(mkdir($this->topLevelDirPath));
127
128
        file_put_contents($this->topLevelFilePath, 'hello dolly !');
129
130
        $this->assertTrue(chmod($this->topLevelDirPath, 0555));
131
132
        error_reporting(0);
133
        F::deleteDirectoryContents($this->topLevelDirPath);
134
    }
135
136
    /**
137
     * @test
138
     * @covers ::deleteDirectoryContents
139
     * @expectedException \Exception
140
     * @expectedExceptionCode 1
141
     */
142
    public function deleteDirectoryContentsWithProtectedDirectory()
143
    {
144
        $this->assertTrue(mkdir($this->subLevelDirPath, 0777, true));
145
146
        $this->assertTrue(chmod($this->topLevelDirPath, 0555));
147
148
        error_reporting(0);
149
        F::deleteDirectoryContents($this->topLevelDirPath);
150
    }
151
152
    /**
153
     * @test
154
     * @covers ::delete
155
     */
156 View Code Duplication
    public function deleteBasic()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
157
    {
158
        $this->assertTrue(mkdir($this->topLevelDirPath));
159
        file_put_contents($this->topLevelFilePath, 'some text');
160
        F::delete($this->topLevelFilePath);
161
        $this->assertFalse(file_exists($this->topLevelFilePath));
162
    }
163
164
    /**
165
     * @test
166
     * @covers ::delete
167
     */
168
    public function deleteNonExistent()
169
    {
170
        $this->assertFalse(file_exists('/path/does/not/exist'));
171
        F::delete('/path/does/not/exist');
172
    }
173
174
    /**
175
     * @test
176
     * @covers ::delete
177
     * @expectedException \Exception
178
     */
179
    public function deleteDirectory()
180
    {
181
        $this->assertTrue(mkdir($this->topLevelDirPath));
182
        error_reporting(0);
183
        F::delete($this->topLevelDirPath);
184
    }
185
186
    /**
187
     * @test
188
     * @covers ::delete
189
     * @expectedException \InvalidArgumentException
190
     * @expectedExceptionMessage $path is not a string or is whitespace
191
     */
192
    public function deleteNonStringPath()
193
    {
194
        F::delete(1);
195
    }
196
197
    /**
198
     * @test
199
     * @covers ::delete
200
     * @expectedException \InvalidArgumentException
201
     * @expectedExceptionMessage $path is not a string or is whitespace
202
     */
203
    public function deletePathIsWhitespace()
204
    {
205
        F::delete('  ');
206
    }
207
208
    /**
209
     * Verify behavior of delete() with protected file.
210
     *
211
     * @test
212
     * @covers ::delete
213
     * @expectedException \Exception
214
     */
215 View Code Duplication
    public function deleteProtectedFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
216
    {
217
        $this->assertTrue(mkdir($this->topLevelDirPath));
218
219
        file_put_contents($this->topLevelFilePath, 'hello dolly !');
220
221
        $this->assertTrue(chmod($this->topLevelDirPath, 0555));
222
223
        error_reporting(0);
224
        F::delete($this->topLevelDirPath);
225
    }
226
227
    /**
228
     * verify basic behavior of deletePathIfEmpty().
229
     *
230
     * @test
231
     * @covers ::deletePathIfEmpty
232
     *
233
     * @return void
234
     */
235
    public function deletePathIfEmpty()
236
    {
237
        $path = "{$this->topLevelDirPath}/path/to/sub/folder";
238
        mkdir($path, 0755, true);
239
        $this->assertFileExists($path, "Unable to create '{$path}'.");
240
        F::deletePathIfEmpty($path, $this->topLevelDirPath);
241
        $this->assertFileNotExists($path, "Unable to delete '{$path}'.");
242
    }
243
244
    /**
245
     * verify behavior of deletePathIfEmpty() when $deletePath does not exist.
246
     *
247
     * @test
248
     * @covers ::deletePathIfEmpty
249
     *
250
     * @return void
251
     */
252
    public function deletePathIfEmptyNotExists()
253
    {
254
        $path = "{$this->topLevelDirPath}/path/to/sub/folder";
255
        $this->assertFileNotExists($path, "Unable to delete '{$path}'.");
256
        $this->assertNull(F::deletePathIfEmpty($path));
257
    }
258
259
    /**
260
     * verify behavior of deletePathIfEmpty() when a folder in $deletePath contains a file.
261
     *
262
     * @test
263
     * @covers ::deletePathIfEmpty
264
     *
265
     * @return void
266
     */
267
    public function deletePathIfEmptyNotEmpty()
268
    {
269
        $path = "{$this->topLevelDirPath}/path/to/sub/folder";
270
        $file = "{$this->topLevelDirPath}/path/to/file.txt";
271
        mkdir($path, 0777, true);
272
        touch($file);
273
        $this->assertFileExists($path, "Unable to create '{$path}'.");
274
        $this->assertFileExists($file, 'Unable to create text file');
275
        F::deletePathIfEmpty($path, $this->topLevelDirPath);
276
        $this->assertFileNotExists("{$this->topLevelDirPath}/path/to/sub/folder");
277
        $this->assertFileNotExists("{$this->topLevelDirPath}/path/to/sub");
278
        $this->assertFileExists($file, "{$file} was deleted");
279
        unlink($file);
280
        $this->assertFileNotExists($file, "{$file} was not deleted");
281
        F::deletePathIfEmpty("{$this->topLevelDirPath}/path/to", $this->topLevelDirPath);
282
        $this->assertFileNotExists("{$this->topLevelDirPath}/path/to");
283
    }
284
}
285