Passed
Push — master ( 0d49ff...ad352f )
by Darío
01:47
created

ShellTest::testMakeDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
Bug introduced by
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
Bug introduced by
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
        {
159
            $shell->cp('foo2', 'foo3');
160
        }
161
        catch (\Exception $e)
162
        {
163
            # omitting directory
164
            $errorObject = ($e instanceof \RuntimeException);
165
        }
166
        finally
167
        {
168
            $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...
169
        }
170
171
        mkdir('foo3');
172
173
        # must be recursive
174
        $shell->cp(
175
            'foo2', // directory
176
            'foo3', // directory
177
        true);
178
179
        $this->assertTrue(file_exists('foo3'));
180
        $this->assertTrue(is_dir('foo3'));
181
        $this->assertTrue(file_exists('foo3/foo2'));
182
        $this->assertTrue(is_dir('foo3/foo2'));
183
        $this->assertTrue(file_exists('foo3/foo2/new3.txt'));
184
        $this->assertTrue(file_exists('foo3/foo2/new4.txt'));
185
186
        $shell->cp(
187
            'foo3', // directory
188
            'foo4', // not a directory or file
189
        true);
190
191
        $this->assertTrue(file_exists('foo4'));
192
        $this->assertTrue(is_dir('foo4'));
193
        $this->assertTrue(file_exists('foo4/foo2'));
194
        $this->assertTrue(is_dir('foo4/foo2'));
195
        $this->assertTrue(file_exists('foo4/foo2/new3.txt'));
196
        $this->assertTrue(file_exists('foo4/foo2/new4.txt'));
197
   }
198
199
    /**
200
     * Tests removing files
201
     *
202
     * @return null
203
     */
204
    public function testRemovingFiles()
205
    {
206
        $shell = new Shell('foo');
207
        $cmd = $shell->rm('new2.txt');
208
209
        $this->assertTrue($cmd);
210
        $this->assertNotTrue(file_exists('new2.txt'));
211
    }
212
213
    /**
214
     * Tests removing not empty directories
215
     *
216
     * @return null
217
     */
218
    public function testRemovingNotEmptyDirectories()
219
    {
220
        $shell = new Shell('foo');
221
        $cmd = $shell->rm('foo4', true);
222
223
        $this->assertTrue($cmd);
224
        $this->assertNotTrue(file_exists('foo4'));
225
    }
226
227
    /**
228
     * Tests renaming files
229
     *
230
     * @return null
231
     */
232
    public function testRenamingFiles()
233
    {
234
        $shell = new Shell('foo');
235
        $cmd = $shell->mv('new.txt', 'renamed.txt');
236
237
        $this->assertTrue($cmd);
238
        $this->assertTrue(file_exists('renamed.txt'));
239
    }
240
241
    /**
242
     * Tests moving files
243
     *
244
     * @return null
245
     */
246
    public function testMovingFiles()
247
    {
248
        $shell = new Shell('foo');
249
        $cmd = $shell->mv('renamed.txt', 'bar');
250
251
        $this->assertTrue($cmd);
252
        $this->assertTrue(file_exists('bar/renamed.txt'));
253
    }
254
255
    /**
256
     * Tests removing empty directories
257
     *
258
     * @return null
259
     */
260
    public function testRemovingDirectories()
261
    {
262
        $shell = new Shell('foo');
263
        mkdir('temp');
264
        $cmd = $shell->rmdir('temp');
265
266
        $this->assertTrue($cmd);
267
        $this->assertNotTrue(file_exists('temp'));
268
    }
269
}