FileTest   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 90
c 2
b 0
f 0
dl 0
loc 247
rs 10
wmc 20

16 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteTestFiles() 0 18 5
A deleteBasic() 0 6 1
A tearDown() 0 5 1
A deleteDirectoryContentsWithFiles() 0 10 1
A deletePathIfEmpty() 0 7 1
A deleteDirectoryContentsEmpty() 0 7 1
A deletePathIfEmptyNotExists() 0 5 1
A deleteNonExistent() 0 4 1
A setUp() 0 12 1
A deleteProtectedFile() 0 11 1
A deleteDirectoryContentsNonExistentPath() 0 6 1
A deleteDirectoryContentsWithProtectedFile() 0 12 1
A deletePathIsWhitespace() 0 5 1
A deletePathIfEmptyNotEmpty() 0 16 1
A deleteDirectory() 0 6 1
A deleteDirectoryContentsWithProtectedDirectory() 0 10 1
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(): void
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(): void
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
     */
70
    public function deleteDirectoryContentsNonExistentPath()
71
    {
72
        $this->expectException(\Exception::class);
73
        $this->expectExceptionMessage('cannot list directory \'/some/where/that/doesnt/exist\'');
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
     */
111
    public function deleteDirectoryContentsWithProtectedFile()
112
    {
113
        $this->expectException(\Exception::class);
114
        $this->expectExceptionCode('2');
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
     */
129
    public function deleteDirectoryContentsWithProtectedDirectory()
130
    {
131
        $this->expectException(\Exception::class);
132
        $this->expectExceptionCode('1');
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
    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
     */
167
    public function deleteDirectory()
168
    {
169
        $this->expectException(\Exception::class);
170
        $this->assertTrue(mkdir($this->topLevelDirPath));
171
        error_reporting(0);
172
        F::delete($this->topLevelDirPath);
173
    }
174
175
    /**
176
     * @test
177
     * @covers ::delete
178
     */
179
    public function deletePathIsWhitespace()
180
    {
181
        $this->expectException(\InvalidArgumentException::class);
182
        $this->expectExceptionMessage('$path is not a string or is whitespace');
183
        F::delete('  ');
184
    }
185
186
    /**
187
     * Verify behavior of delete() with protected file.
188
     *
189
     * @test
190
     * @covers ::delete
191
     */
192
    public function deleteProtectedFile()
193
    {
194
        $this->expectException(\Exception::class);
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->assertFileDoesNotExist($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->assertFileDoesNotExist($path, "Unable to delete '{$path}'.");
234
        $this->assertNull(F::deletePathIfEmpty($path));
0 ignored issues
show
Bug introduced by
Are you sure the usage of TraderInteractive\Util\F...eletePathIfEmpty($path) targeting TraderInteractive\Util\File::deletePathIfEmpty() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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->assertFileDoesNotExist("{$this->topLevelDirPath}/path/to/sub/folder");
255
        $this->assertFileDoesNotExist("{$this->topLevelDirPath}/path/to/sub");
256
        $this->assertFileExists($file, "{$file} was deleted");
257
        unlink($file);
258
        $this->assertFileDoesNotExist($file, "{$file} was not deleted");
259
        F::deletePathIfEmpty("{$this->topLevelDirPath}/path/to", $this->topLevelDirPath);
260
        $this->assertFileDoesNotExist("{$this->topLevelDirPath}/path/to");
261
    }
262
}
263