FilesystemTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
        @mkdir($folder2, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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";
0 ignored issues
show
Unused Code introduced by
$ignoreMe is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
        $file2      = $folder2 . "/file2.txt";
84
        $dest       = sys_get_temp_dir() . "/n98_copy_dest";
85
86
        @mkdir($folder1, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
        @mkdir($folder2, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
88
        touch($file1);
89
        touch($file2);
90
91
        $this->fileSystem->recursiveCopy($basePath, $dest, array('ignore.me'));
92
        $this->assertFileExists($dest . "/folder1/file1.txt");
93
        $this->assertFileExists($dest . "/folder2/file2.txt");
94
        $this->assertFileNotExists($dest . "/folder1/ignore.me");
95
96
        //cleanup
97
        unlink($file1);
98
        unlink($file2);
99
        rmdir($folder1);
100
        rmdir($folder2);
101
        rmdir($basePath);
102
103
        unlink($dest . "/folder1/file1.txt");
104
        unlink($dest . "/folder2/file2.txt");
105
        rmdir($dest . "/folder1");
106
        rmdir($dest . "/folder2");
107
        rmdir($dest);
108
    }
109
110
    /**
111
     * @requires function symlink
112
     */
113
    public function testRecursiveDirectoryRemoveUnLinksSymLinks()
114
    {
115
        $tmp            = sys_get_temp_dir();
116
        $basePath       = $tmp . "/n98_testdir";
117
        $symLinked      = $tmp . "/n98_linked";
118
        $symLinkedFile = $symLinked . "/symlinkme.txt";
119
120
        @mkdir($basePath, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
121
        @mkdir($symLinked, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
122
123
        touch($symLinkedFile);
124
125
        $result = @symlink($symLinked, $basePath . "/symlink");
126
        $this->assertTrue($result);
127
128
        $this->fileSystem->recursiveRemoveDirectory($basePath);
129
130
        $this->assertFileExists($symLinkedFile);
131
        $this->assertFileNotExists($basePath);
132
    }
133
134 View Code Duplication
    public function testRecursiveRemove()
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...
135
    {
136
        $tmp        = sys_get_temp_dir();
137
        $basePath   = $tmp . "/n98_testdir";
138
        $folder1    = $basePath . "/folder1";
139
        $folder2    = $basePath . "/folder2";
140
        $file1      = $folder1 . "/file1.txt";
141
        $file2      = $folder2 . "/file2.txt";
142
143
        @mkdir($folder1, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
144
        @mkdir($folder2, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
145
        touch($file1);
146
        touch($file2);
147
148
        $this->fileSystem->recursiveRemoveDirectory($basePath);
149
        $this->assertFileNotExists($basePath);
150
    }
151
152 View Code Duplication
    public function testRecursiveRemoveWithTrailingSlash()
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...
153
    {
154
        $tmp        = sys_get_temp_dir();
155
        $basePath   = $tmp . "/n98_testdir";
156
        $folder1    = $basePath . "/folder1";
157
        $folder2    = $basePath . "/folder2";
158
        $file1      = $folder1 . "/file1.txt";
159
        $file2      = $folder2 . "/file2.txt";
160
161
        @mkdir($folder1, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
162
        @mkdir($folder2, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
163
        touch($file1);
164
        touch($file2);
165
166
        $this->fileSystem->recursiveRemoveDirectory($basePath . "/");
167
        $this->assertFileNotExists($basePath);
168
    }
169
170
    public function testFalseIsReturnedIfDirectoryNotExist()
171
    {
172
        $this->assertFalse($this->fileSystem->recursiveRemoveDirectory("not-a-folder"));
173
    }
174
175
    public function testFalseIsReturnedIfDirectoryNotReadable()
176
    {
177
        $tmp        = sys_get_temp_dir();
178
        $basePath   = $tmp . "/n98_testdir-never-existed";
179
180
        $this->assertFalse($this->fileSystem->recursiveRemoveDirectory($basePath));
181
    }
182
183
    public function testParentIsNotRemovedIfEmptyIsTrue()
184
    {
185
        $tmp        = sys_get_temp_dir();
186
        $basePath   = $tmp . "/n98_testdir";
187
        $folder1    = $basePath . "/folder1";
188
        $folder2    = $basePath . "/folder2";
189
        $file1      = $folder1 . "/file1.txt";
190
        $file2      = $folder2 . "/file2.txt";
191
192
        @mkdir($folder1, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
193
        @mkdir($folder2, 0777, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
194
        touch($file1);
195
        touch($file2);
196
197
        $this->fileSystem->recursiveRemoveDirectory($basePath, true);
198
        $this->assertFileExists($basePath);
199
        $this->assertFileNotExists($folder1);
200
        $this->assertFileNotExists($folder2);
201
    }
202
203
    /**
204
     * @param int $bytes
205
     * @param int $decimalPlaces
206
     * @param string $expected
207
     * @dataProvider convertedBytesProvider
208
     */
209
    public function testConvertBytesToHumanReadable($bytes, $decimalPlaces, $expected)
210
    {
211
        $res = Filesystem::humanFileSize($bytes, $decimalPlaces);
212
        $this->assertSame($expected, $res);
213
214
    }
215
216
    /**
217
     * @return array
218
     */
219
    public static function convertedBytesProvider()
220
    {
221
        return array(
222
            array(20000000, 2, '19.07M'),
223
            array(20000000, 3, '19.073M'),
224
            array(2000000000, 2, '1.86G'),
225
            array(2, 2, '2.00B'),
226
            array(2048, 2, '2.00K'),
227
        );
228
    }
229
}
230