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