|
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}"); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->oldWorkingDirectory = getcwd(); |
|
42
|
|
|
|
|
43
|
|
|
$this->platform = new WindowsPlatform(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function tearDown() { |
|
47
|
|
|
putenv("PATH={$this->oldPath}"); |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
|
|
public function testGetDirectorySeparator() { |
|
74
|
|
|
$this->assertEquals('\\', $this->platform->getDirectorySeparator()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @covers ::getExecutablePath |
|
79
|
|
|
* @covers \ComponentManager\Platform\AbstractPlatform::getDirectorySeparator |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.