Issues (99)

test/FileSystem/ShellTest.php (3 issues)

1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace DroneTest\FileSystem;
12
13
use Drone\FileSystem\Shell;
14
use PHPUnit\Framework\TestCase;
15
16
class ShellTest extends TestCase
17
{
18
    /**
19
     * Tests home path position
20
     *
21
     * @return null
22
     */
23
    public function testHomePath()
24
    {
25
        mkdir('foo');
26
27
        $shell = new Shell('foo');
28
        $this->assertSame('foo', $shell->getHome());
29
        $this->assertSame('foo', basename($shell->pwd()));
0 ignored issues
show
It seems like $shell->pwd() can also be of type false; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $this->assertSame('foo', basename(/** @scrutinizer ignore-type */ $shell->pwd()));
Loading history...
30
        $this->assertSame('foo', basename(getcwd()));
31
    }
32
33
    /**
34
     * Tests file creation
35
     *
36
     * @return null
37
     */
38
    public function testFileCreation()
39
    {
40
        $shell = new Shell('foo');
41
        $cmd = $shell->touch('new.txt');
42
43
        $this->assertTrue($cmd);
44
        $this->assertTrue(file_exists('new.txt'));
45
    }
46
47
    /**
48
     * Tests changing path
49
     *
50
     * @return null
51
     */
52
    public function testChangePath()
53
    {
54
        $shell = new Shell('foo');
55
        $shell->cd('..');
56
57
        $this->assertTrue(file_exists('foo'));
58
        $this->assertTrue(file_exists('foo/new.txt'));
59
60
        # back to home path
61
        $shell->cd();
62
        $this->assertSame('foo', basename($shell->pwd()));
0 ignored issues
show
It seems like $shell->pwd() can also be of type false; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $this->assertSame('foo', basename(/** @scrutinizer ignore-type */ $shell->pwd()));
Loading history...
63
        $this->assertSame('foo', basename(getcwd()));
64
    }
65
66
    /**
67
     * Tests simple copy
68
     *
69
     * @return null
70
     */
71
    public function testFileCopy()
72
    {
73
        $shell = new Shell('foo');
74
        $shell->cp('new.txt', 'new2.txt');
75
76
        $this->assertTrue(file_exists('new2.txt'));
77
78
        mkdir('bar');
79
80
        $shell->cp('new.txt', 'bar');
81
        $this->assertTrue(file_exists('bar/new.txt'));
82
    }
83
84
    /**
85
     * Tests directory creation
86
     *
87
     * @return null
88
     */
89
    public function testMakeDirectory()
90
    {
91
        $shell = new Shell('foo');
92
        $cmd = $shell->mkdir('foo2');
93
94
        $this->assertTrue(file_exists('foo2'));
95
        $this->assertTrue(is_dir('foo2'));
96
        $this->assertTrue($cmd);
97
    }
98
99
    /**
100
     * Tests the list of files retrived by ls command
101
     *
102
     * @return null
103
     */
104
    public function testListingFiles()
105
    {
106
        $shell = new Shell('foo');
107
        $files = $shell->ls();
108
        sort($files);
109
110
        $expected = ['bar', 'foo2', 'new.txt', 'new2.txt'];
111
        sort($expected);
112
113
        $this->assertSame($expected, $files);
114
    }
115
116
    /**
117
     * Tests the list of files retrived by ls command
118
     *
119
     * @return null
120
     */
121
    public function testListingFilesRecursively()
122
    {
123
        $shell = new Shell('foo');
124
        $files = $shell->ls('.', true);
125
126
        /**
127
         * Actually the $files variable would be the following array
128
         * [['bar' => ['new.txt']], ['foo2' => []], 'new.txt', 'new2.txt'];
129
         */
130
131
        # check directories
132
        $this->assertTrue(array_key_exists('bar', $files));
133
        $this->assertTrue(is_array($files["bar"]));
134
        $this->assertTrue(array_key_exists('foo2', $files));
135
        $this->assertTrue(is_array($files["foo2"]));
136
137
        # check files
138
        $this->assertSame(['new.txt'], $files["bar"]);
139
        $this->assertTrue(in_array('new.txt', $files));
140
        $this->assertTrue(in_array('new2.txt', $files));
141
    }
142
143
    /**
144
     * Tests copying a directory with its contents
145
     *
146
     * @return null
147
     */
148
    public function testDirectoryCopy()
149
    {
150
        $shell = new Shell('foo');
151
152
        $shell->touch('foo2/new3.txt');
153
        $shell->touch('foo2/new4.txt');
154
155
        $errorObject = null;
156
157
        try {
158
            $shell->cp('foo2', 'foo3');
159
        } catch (\Exception $e) {
160
            # omitting directory
161
            $errorObject = ($e instanceof \RuntimeException);
162
        } finally {
163
            $this->assertTrue($errorObject, $e->getMessage());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
164
        }
165
166
        mkdir('foo3');
167
168
        # must be recursive
169
        $shell->cp(
170
            'foo2', // directory
171
            'foo3', // directory
172
        true
173
        );
174
175
        $this->assertTrue(file_exists('foo3'));
176
        $this->assertTrue(is_dir('foo3'));
177
        $this->assertTrue(file_exists('foo3/foo2'));
178
        $this->assertTrue(is_dir('foo3/foo2'));
179
        $this->assertTrue(file_exists('foo3/foo2/new3.txt'));
180
        $this->assertTrue(file_exists('foo3/foo2/new4.txt'));
181
182
        $shell->cp(
183
            'foo3', // directory
184
            'foo4', // not a directory or file
185
        true
186
        );
187
188
        $this->assertTrue(file_exists('foo4'));
189
        $this->assertTrue(is_dir('foo4'));
190
        $this->assertTrue(file_exists('foo4/foo2'));
191
        $this->assertTrue(is_dir('foo4/foo2'));
192
        $this->assertTrue(file_exists('foo4/foo2/new3.txt'));
193
        $this->assertTrue(file_exists('foo4/foo2/new4.txt'));
194
    }
195
196
    /**
197
     * Tests removing files
198
     *
199
     * @return null
200
     */
201
    public function testRemovingFiles()
202
    {
203
        $shell = new Shell('foo');
204
        $cmd = $shell->rm('new2.txt');
205
206
        $this->assertTrue($cmd);
207
        $this->assertNotTrue(file_exists('new2.txt'));
208
    }
209
210
    /**
211
     * Tests removing not empty directories
212
     *
213
     * @return null
214
     */
215
    public function testRemovingNotEmptyDirectories()
216
    {
217
        $shell = new Shell('foo');
218
        $cmd = $shell->rm('foo4', true);
219
220
        $this->assertTrue($cmd);
221
        $this->assertNotTrue(file_exists('foo4'));
222
    }
223
224
    /**
225
     * Tests renaming files
226
     *
227
     * @return null
228
     */
229
    public function testRenamingFiles()
230
    {
231
        $shell = new Shell('foo');
232
        $cmd = $shell->mv('new.txt', 'renamed.txt');
233
234
        $this->assertTrue($cmd);
235
        $this->assertTrue(file_exists('renamed.txt'));
236
    }
237
238
    /**
239
     * Tests moving files
240
     *
241
     * @return null
242
     */
243
    public function testMovingFiles()
244
    {
245
        $shell = new Shell('foo');
246
        $cmd = $shell->mv('renamed.txt', 'bar');
247
248
        $this->assertTrue($cmd);
249
        $this->assertTrue(file_exists('bar/renamed.txt'));
250
    }
251
252
    /**
253
     * Tests removing empty directories
254
     *
255
     * @return null
256
     */
257
    public function testRemovingDirectories()
258
    {
259
        $shell = new Shell('foo');
260
        mkdir('temp');
261
        $cmd = $shell->rmdir('temp');
262
263
        $this->assertTrue($cmd);
264
        $this->assertNotTrue(file_exists('temp'));
265
266
        # remove all work
267
        $shell->cd('..');
268
        $shell->rm('foo', true);
269
    }
270
}
271