Skip to content

Commit a656347

Browse files
authored
Merge pull request #23 from laravel/cleanup_herd_class
Refactor Herd class and add unit tests
2 parents 1ae687d + efd4da8 commit a656347

File tree

2 files changed

+199
-7
lines changed

2 files changed

+199
-7
lines changed

src/Install/Herd.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Laravel\Boost\Install;
66

7+
use Laravel\Boost\Install\Enums\Platform;
8+
79
class Herd
810
{
911
public function isInstalled(): bool
1012
{
11-
$isWindows = PHP_OS_FAMILY === 'Windows';
12-
13-
if (! $isWindows) {
13+
if ($this->isWindowsPlatform()) {
1414
return file_exists('/Applications/Herd.app/Contents/MacOS/Herd');
1515
}
1616

@@ -24,7 +24,7 @@ public function isMcpAvailable(): bool
2424

2525
public function getHomePath(): string
2626
{
27-
if (PHP_OS_FAMILY === 'Windows') {
27+
if ($this->isWindowsPlatform()) {
2828
if (! isset($_SERVER['HOME'])) {
2929
$_SERVER['HOME'] = $_SERVER['USERPROFILE'];
3030
}
@@ -37,12 +37,15 @@ public function getHomePath(): string
3737

3838
public function mcpPath(): string
3939
{
40-
$isWindows = PHP_OS_FAMILY === 'Windows';
41-
42-
if ($isWindows) {
40+
if ($this->isWindowsPlatform()) {
4341
return $this->getHomePath().'/.config/herd/bin/herd-mcp.phar';
4442
}
4543

4644
return $this->getHomePath().'/Library/Application Support/Herd/bin/herd-mcp.phar';
4745
}
46+
47+
public function isWindowsPlatform(): bool
48+
{
49+
return Platform::current() === Platform::Windows;
50+
}
4851
}

tests/Unit/Install/HerdTest.php

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Laravel\Boost\Install\Herd;
6+
7+
$herdTestCleanupData = [];
8+
9+
beforeEach(function () {
10+
global $herdTestCleanupData;
11+
12+
$herdTestCleanupData = initializeHerdTestEnvironment();
13+
14+
mkdir($herdTestCleanupData['tempDir'], 0755, true);
15+
});
16+
17+
afterEach(function () {
18+
global $herdTestCleanupData;
19+
20+
foreach ($herdTestCleanupData['originalEnv'] as $key => $value) {
21+
if ($value === null) {
22+
unset($_SERVER[$key]);
23+
} else {
24+
$_SERVER[$key] = $value;
25+
}
26+
}
27+
28+
// Clean up temp directory
29+
if (is_dir($herdTestCleanupData['tempDir'])) {
30+
removeHerdTestDirectory($herdTestCleanupData['tempDir']);
31+
}
32+
33+
$herdTestCleanupData = [];
34+
});
35+
36+
function removeHerdTestDirectory(string $dir): void
37+
{
38+
if (! is_dir($dir)) {
39+
return;
40+
}
41+
42+
$files = array_diff(scandir($dir), ['.', '..']);
43+
44+
foreach ($files as $file) {
45+
$path = $dir.DIRECTORY_SEPARATOR.$file;
46+
is_dir($path) ? removeHerdTestDirectory($path) : unlink($path);
47+
}
48+
49+
rmdir($dir);
50+
}
51+
52+
function initializeHerdTestEnvironment(): array
53+
{
54+
return [
55+
'originalEnv' => [
56+
'HOME' => $_SERVER['HOME'] ?? null,
57+
'USERPROFILE' => $_SERVER['USERPROFILE'] ?? null,
58+
],
59+
'tempDir' => sys_get_temp_dir().'/herd_test_'.uniqid().'_'.getmypid(),
60+
];
61+
}
62+
63+
function getHerdTestTempDir(): string
64+
{
65+
global $herdTestCleanupData;
66+
67+
return $herdTestCleanupData['tempDir'];
68+
}
69+
70+
test('mcpPath builds correct path from HOME on non-Windows', function () {
71+
$testHome = getHerdTestTempDir().'/home';
72+
mkdir($testHome, 0755, true);
73+
$_SERVER['HOME'] = $testHome;
74+
75+
$herd = new Herd();
76+
$expected = $testHome.'/Library/Application Support/Herd/bin/herd-mcp.phar';
77+
78+
expect($herd->mcpPath())->toBe($expected);
79+
})->skipOnWindows();
80+
81+
test('mcpPath builds correct Windows path from USERPROFILE when HOME missing', function () {
82+
unset($_SERVER['HOME']);
83+
$_SERVER['USERPROFILE'] = 'C:\\Users\\TestUser';
84+
85+
$herd = new Herd();
86+
$expected = 'C:/Users/TestUser/.config/herd/bin/herd-mcp.phar';
87+
88+
expect($herd->mcpPath())->toBe($expected);
89+
})->onlyOnWindows();
90+
91+
test('isMcpAvailable returns false when MCP file is missing', function () {
92+
$testHome = getHerdTestTempDir().'/home';
93+
mkdir($testHome, 0755, true);
94+
$_SERVER['HOME'] = $testHome;
95+
96+
$herd = new Herd();
97+
98+
expect($herd->isMcpAvailable())->toBeFalse();
99+
});
100+
101+
test('isMcpAvailable returns true when MCP file exists', function () {
102+
$testHome = getHerdTestTempDir().'/home';
103+
mkdir($testHome, 0755, true);
104+
$_SERVER['HOME'] = $testHome;
105+
106+
$herd = new Herd();
107+
$mcpPath = $herd->mcpPath();
108+
109+
$mcpDir = dirname($mcpPath);
110+
mkdir($mcpDir, 0755, true);
111+
112+
file_put_contents($mcpPath, 'test phar content');
113+
114+
expect($herd->isMcpAvailable())->toBeTrue();
115+
});
116+
117+
test('isMcpAvailable returns false after MCP file is removed', function () {
118+
$testHome = getHerdTestTempDir().'/home';
119+
mkdir($testHome, 0755, true);
120+
$_SERVER['HOME'] = $testHome;
121+
122+
$herd = new Herd();
123+
$mcpPath = $herd->mcpPath();
124+
125+
$mcpDir = dirname($mcpPath);
126+
mkdir($mcpDir, 0755, true);
127+
file_put_contents($mcpPath, 'test phar content');
128+
129+
expect($herd->isMcpAvailable())->toBeTrue();
130+
131+
// Remove file
132+
unlink($mcpPath);
133+
134+
expect($herd->isMcpAvailable())->toBeFalse();
135+
});
136+
137+
test('getHomePath returns HOME on non-Windows', function () {
138+
$testHome = getHerdTestTempDir().'/home';
139+
mkdir($testHome, 0755, true);
140+
$_SERVER['HOME'] = $testHome;
141+
142+
$herd = new Herd();
143+
144+
expect($herd->getHomePath())->toBe($testHome);
145+
})->skipOnWindows();
146+
147+
test('getHomePath uses USERPROFILE on Windows when HOME is not set and normalizes slashes', function () {
148+
unset($_SERVER['HOME']);
149+
$_SERVER['USERPROFILE'] = 'C:\\Users\\TestUser';
150+
151+
$herd = new Herd();
152+
153+
expect($herd->getHomePath())->toBe('C:/Users/TestUser');
154+
})->onlyOnWindows();
155+
156+
test('isInstalled returns true when herd config directory exists on Windows', function () {
157+
$testHome = getHerdTestTempDir().'/home';
158+
mkdir($testHome, 0755, true);
159+
$_SERVER['HOME'] = $testHome;
160+
161+
$configDir = $testHome.'/.config/herd';
162+
mkdir($configDir, 0755, true);
163+
164+
$herd = new Herd();
165+
166+
expect($herd->isInstalled())->toBeTrue();
167+
})->onlyOnWindows();
168+
169+
test('isInstalled returns false when herd config directory is missing on Windows', function () {
170+
$testHome = getHerdTestTempDir().'/home';
171+
mkdir($testHome, 0755, true);
172+
$_SERVER['HOME'] = $testHome;
173+
174+
$herd = new Herd();
175+
176+
expect($herd->isInstalled())->toBeFalse();
177+
})->onlyOnWindows();
178+
179+
test('isWindowsPlatform returns true on Windows', function () {
180+
$herd = new Herd();
181+
182+
expect($herd->isWindowsPlatform())->toBeTrue();
183+
})->onlyOnWindows();
184+
185+
test('isWindowsPlatform returns false on non-Windows platforms', function () {
186+
$herd = new Herd();
187+
188+
expect($herd->isWindowsPlatform())->toBeFalse();
189+
})->skipOnWindows();

0 commit comments

Comments
 (0)