Passed
Push — master ( fc9abb...9945a9 )
by Luke
02:47
created

WindowsPlatformTest::testGetExecutablePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * Moodle component manager.
5
 *
6
 * @author Luke Carrier <[email protected]>
7
 * @copyright 2016 Luke Carrier
8
 * @license GPL-3.0+
9
 */
10
11
namespace ComponentManager\Test\Platform;
12
13
use ComponentManager\Platform\WindowsPlatform;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * @coversDefaultClass \ComponentManager\Platform\WindowsPlatform
18
 * @group platform
19
 * @group platform-windows
20
 */
21
class WindowsPlatformTest extends TestCase {
22
    /**
23
     * @var string
24
     */
25
    protected $oldPath;
26
27
    /**
28
     * @var string
29
     */
30
    protected $oldWorkingDirectory;
31
32
    /**
33
     * @var \ComponentManager\Platform\WindowsPlatform
34
     */
35
    protected $platform;
36
37
    public function setUp() {
38
        $this->oldPath = getenv('PATH');
39
        putenv("PATH=C:\\windows\\system32;{$this->oldPath}");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
40
41
        $this->oldWorkingDirectory = getcwd();
42
43
        $this->platform = new WindowsPlatform();
0 ignored issues
show
Bug introduced by
The call to WindowsPlatform::__construct() misses a required argument $filesystem.

This check looks for function calls that miss required arguments.

Loading history...
44
    }
45
46
    public function tearDown() {
47
        putenv("PATH={$this->oldPath}");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $this instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
48
49
        chdir($this->oldWorkingDirectory);
50
    }
51
52
    /**
53
     * @covers \ComponentManager\Platform\AbstractPlatform::createTempDirectory
54
     */
55
    public function testCreateTempDirectory() {
56
        $temp = $this->platform->createTempDirectory();
57
58
        $this->assertFileExists($temp);
59
60
        rmdir($temp);
61
    }
62
63
    /**
64
     * @covers ::expandPath
65
     */
66
    public function testExpandPath() {
67
        $this->assertEquals('~\blah', $this->platform->expandPath('~\blah'));
68
    }
69
70
    /**
71
     * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 81 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
72
     */
73
    public function testGetDirectorySeparator() {
74
        $this->assertEquals('\\', $this->platform->getDirectorySeparator());
75
    }
76
77
    /**
78
     * @covers ::getExecutablePath
79
     * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 81 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
80
     * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths
81
     */
82
    public function testGetExecutablePath() {
83
        $this->assertEquals(
84
                'c:\\windows\\system32\\cmd.exe',
85
                strtolower($this->platform->getExecutablePath('cmd')));
86
    }
87
88
    /**
89
     * @covers ::getExecutablePath
90
     * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 81 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
91
     * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths
92
     *
93
     * @expectedException \ComponentManager\Exception\PlatformException
94
     * @expectedExceptionCode 2
95
     */
96
    public function testGetExecutablePathThrows() {
97
        $this->platform->getExecutablePath('likelynotathing');
98
    }
99
100
    /**
101
     * @covers ::getHomeDirectory
102
     */
103
    public function testGetHomeDirectory() {
104
        $home = $this->platform->getHomeDirectory();
105
        $user = getenv('USERNAME');
106
107
        $this->assertStringEndsWith($user, $home);
108
        $this->assertFileExists($home);
109
    }
110
111
    /**
112
     * @covers ::getLocalSharedDirectory
113
     */
114
    public function testGetLocalSharedDirectory() {
115
        $this->assertFileExists($this->platform->getLocalSharedDirectory());
116
    }
117
118
    /**
119
     * @covers \ComponentManager\Platform\AbstractPlatform::getPhpExecutable
120
     */
121
    public function testGetPhpExecutable() {
122
        $this->assertFileExists($this->platform->getPhpExecutable());
123
    }
124
125
    /**
126
     * @covers \ComponentManager\Platform\AbstractPlatform::getPhpScript
127
     */
128
    public function testGetPhpScript() {
129
        $this->assertFileExists($this->platform->getPhpScript());
130
    }
131
132
    /**
133
     * @covers \ComponentManager\Platform\AbstractPlatform::getWorkingDirectory
134
     */
135
    public function testGetWorkingDirectory() {
136
        $target = 'c:\windows';
137
138
        $this->assertEquals(getcwd(), $this->platform->getWorkingDirectory());
139
140
        chdir($target);
141
        $this->assertEquals($target, $this->platform->getWorkingDirectory());
142
    }
143
144
    /**
145
     * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths
146
     * @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 81 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
147
     */
148
    public function testJoinPaths() {
149
        $this->assertEquals(
150
                'c:\\windows', $this->platform->joinPaths(['c:', 'windows']));
151
152
        $this->assertEquals(
153
                'c:\\windows\\system32',
154
                $this->platform->joinPaths(['c:', 'windows\\system32']));
155
156
        $this->assertEquals(
157
                'c:\\windows\\system32',
158
                $this->platform->joinPaths(['c:\\windows', 'system32']));
159
    }
160
}
161