Passed
Push — master ( 864cd3...c7da61 )
by Darío
02:21
created

ShellTest::testFileCreation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
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('..');
0 ignored issues
show
Bug introduced by
'..' of type string is incompatible with the type boolean|null expected by parameter $path of Drone\FileSystem\Shell::cd(). ( Ignorable by Annotation )

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

55
        $shell->cd(/** @scrutinizer ignore-type */ '..');
Loading history...
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
        $shell->mkdir('foo2');
93
94
        $this->assertTrue(file_exists('foo2'));
95
        $this->assertTrue(is_dir('foo2'));
96
    }
97
98
    /**
99
     * Tests the list of files retrived by ls command
100
     *
101
     * @return null
102
     */
103
    public function testListingFiles()
104
    {
105
        $shell = new Shell('foo');
106
        $files = $shell->ls();
107
108
        $this->assertSame(['bar', 'foo2', 'new.txt', 'new2.txt'], $files);
109
    }
110
111
    /**
112
     * Tests the list of files retrived by ls command
113
     *
114
     * @return null
115
     */
116
    public function testListingFilesRecursively()
117
    {
118
        $shell = new Shell('foo');
119
        $files = $shell->ls('.', true);
120
121
        $this->assertSame(
122
            ['./bar', './foo2', './bar/new.txt', './new.txt', './new2.txt'],
123
            $files
124
        );
125
    }
126
127
    /**
128
     * Tests copying a directory with its contents
129
     *
130
     * @return null
131
     */
132
    public function testDirectoryCopy()
133
    {
134
        $shell = new Shell('foo');
135
136
        $shell->touch('foo2/new3.txt');
137
        $shell->touch('foo2/new4.txt');
138
139
        $errorObject = null;
140
141
        try
142
        {
143
            $shell->cp('foo2', 'foo3');
144
        }
145
        catch (\Exception $e)
146
        {
147
            # omitting directory
148
            $errorObject = ($e instanceof \RuntimeException);
149
        }
150
        finally
151
        {
152
            $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...
153
        }
154
155
        mkdir('foo3');
156
157
        # must be recursive
158
        $shell->cp(
159
            'foo2', // directory
160
            'foo3', // directory
161
        true);
162
163
        $this->assertTrue(file_exists('foo3'));
164
        $this->assertTrue(is_dir('foo3'));
165
        $this->assertTrue(file_exists('foo3/foo2'));
166
        $this->assertTrue(is_dir('foo3/foo2'));
167
        $this->assertTrue(file_exists('foo3/foo2/new3.txt'));
168
        $this->assertTrue(file_exists('foo3/foo2/new4.txt'));
169
170
        $shell->cp(
171
            'foo2', // directory
172
            'foo4', // not a directory or file
173
        true);
174
   }
175
}