Passed
Push — master ( 95d224...68f44f )
by Luke
02:40
created

testGetExecutablePathThrows()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
use ComponentManager\Platform\WindowsPlatform;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * @coversDefaultClass \ComponentManager\Platform\WindowsPlatform
16
 * @group platform
17
 * @group platform-windows
18
 */
19
class PlatformWindowsPlatformTest extends TestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
20
    /**
21
     * @var string
22
     */
23
    protected $oldPath;
24
25
    /**
26
     * @var string
27
     */
28
    protected $oldWorkingDirectory;
29
30
    /**
31
     * @var \ComponentManager\Platform\WindowsPlatform
32
     */
33
    protected $platform;
34
35
    public function setUp() {
36
        $this->oldPath = getenv('PATH');
37
        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...
38
39
        $this->oldWorkingDirectory = getcwd();
40
41
        $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...
42
    }
43
44
    public function tearDown() {
45
        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...
46
47
        chdir($this->oldWorkingDirectory);
48
    }
49
50
    /**
51
     * @covers \ComponentManager\Platform\AbstractPlatform::createTempDirectory
52
     */
53
    public function testCreateTempDirectory() {
54
        $temp = $this->platform->createTempDirectory();
55
56
        $this->assertFileExists($temp);
57
58
        rmdir($temp);
59
    }
60
61
    /**
62
     * @covers ::expandPath
63
     */
64
    public function testExpandPath() {
65
        $this->assertEquals('~\blah', $this->platform->expandPath('~\blah'));
66
    }
67
68
    /**
69
     * @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...
70
     */
71
    public function testGetDirectorySeparator() {
72
        $this->assertEquals('\\', $this->platform->getDirectorySeparator());
73
    }
74
75
    /**
76
     * @covers ::getExecutablePath
77
     * @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...
78
     * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths
79
     */
80
    public function testGetExecutablePath() {
81
        $this->assertEquals(
82
                'c:\\windows\\system32\\cmd.exe',
83
                strtolower($this->platform->getExecutablePath('cmd')));
84
    }
85
86
    /**
87
     * @covers ::getExecutablePath
88
     * @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...
89
     * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths
90
     *
91
     * @expectedException \ComponentManager\Exception\PlatformException
92
     * @expectedExceptionCode 2
93
     */
94
    public function testGetExecutablePathThrows() {
95
        $this->platform->getExecutablePath('likelynotathing');
96
    }
97
98
    /**
99
     * @covers ::getHomeDirectory
100
     */
101
    public function testGetHomeDirectory() {
102
        $home = $this->platform->getHomeDirectory();
103
        $user = getenv('USERNAME');
104
105
        $this->assertStringEndsWith($user, $home);
106
        $this->assertFileExists($home);
107
    }
108
109
    /**
110
     * @covers ::getLocalSharedDirectory
111
     */
112
    public function testGetLocalSharedDirectory() {
113
        $this->assertFileExists($this->platform->getLocalSharedDirectory());
114
    }
115
116
    /**
117
     * @covers \ComponentManager\Platform\AbstractPlatform::getPhpExecutable
118
     */
119
    public function testGetPhpExecutable() {
120
        $this->assertFileExists($this->platform->getPhpExecutable());
121
    }
122
123
    /**
124
     * @covers \ComponentManager\Platform\AbstractPlatform::getPhpScript
125
     */
126
    public function testGetPhpScript() {
127
        $this->assertFileExists($this->platform->getPhpScript());
128
    }
129
130
    /**
131
     * @covers \ComponentManager\Platform\AbstractPlatform::getWorkingDirectory
132
     */
133
    public function testGetWorkingDirectory() {
134
        $target = 'c:\windows';
135
136
        $this->assertEquals(getcwd(), $this->platform->getWorkingDirectory());
137
138
        chdir($target);
139
        $this->assertEquals($target, $this->platform->getWorkingDirectory());
140
    }
141
142
    /**
143
     * @covers \ComponentManager\Platform\AbstractPlatform::joinPaths
144
     * @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...
145
     */
146
    public function testJoinPaths() {
147
        $this->assertEquals(
148
                'c:\\windows', $this->platform->joinPaths(['c:', 'windows']));
149
150
        $this->assertEquals(
151
                'c:\\windows\\system32',
152
                $this->platform->joinPaths(['c:', 'windows\\system32']));
153
154
        $this->assertEquals(
155
                'c:\\windows\\system32',
156
                $this->platform->joinPaths(['c:\\windows', 'system32']));
157
    }
158
}
159