Skip to content

Refactor Herd class and add unit tests #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Install/Herd.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Laravel\Boost\Install;

use Laravel\Boost\Install\Enums\Platform;

class Herd
{
public function isInstalled(): bool
{
$isWindows = PHP_OS_FAMILY === 'Windows';

if (! $isWindows) {
if ($this->isWindowsPlatform()) {
return file_exists('/Applications/Herd.app/Contents/MacOS/Herd');
}

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

public function getHomePath(): string
{
if (PHP_OS_FAMILY === 'Windows') {
if ($this->isWindowsPlatform()) {
if (! isset($_SERVER['HOME'])) {
$_SERVER['HOME'] = $_SERVER['USERPROFILE'];
}
Expand All @@ -37,12 +37,15 @@ public function getHomePath(): string

public function mcpPath(): string
{
$isWindows = PHP_OS_FAMILY === 'Windows';

if ($isWindows) {
if ($this->isWindowsPlatform()) {
return $this->getHomePath().'/.config/herd/bin/herd-mcp.phar';
}

return $this->getHomePath().'/Library/Application Support/Herd/bin/herd-mcp.phar';
}

public function isWindowsPlatform(): bool
{
return Platform::current() === Platform::Windows;
}
}
189 changes: 189 additions & 0 deletions tests/Unit/Install/HerdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

declare(strict_types=1);

use Laravel\Boost\Install\Herd;

$herdTestCleanupData = [];

beforeEach(function () {
global $herdTestCleanupData;

$herdTestCleanupData = initializeHerdTestEnvironment();

mkdir($herdTestCleanupData['tempDir'], 0755, true);
});

afterEach(function () {
global $herdTestCleanupData;

foreach ($herdTestCleanupData['originalEnv'] as $key => $value) {
if ($value === null) {
unset($_SERVER[$key]);
} else {
$_SERVER[$key] = $value;
}
}

// Clean up temp directory
if (is_dir($herdTestCleanupData['tempDir'])) {
removeHerdTestDirectory($herdTestCleanupData['tempDir']);
}

$herdTestCleanupData = [];
});

function removeHerdTestDirectory(string $dir): void
{
if (! is_dir($dir)) {
return;
}

$files = array_diff(scandir($dir), ['.', '..']);

foreach ($files as $file) {
$path = $dir.DIRECTORY_SEPARATOR.$file;
is_dir($path) ? removeHerdTestDirectory($path) : unlink($path);
}

rmdir($dir);
}

function initializeHerdTestEnvironment(): array
{
return [
'originalEnv' => [
'HOME' => $_SERVER['HOME'] ?? null,
'USERPROFILE' => $_SERVER['USERPROFILE'] ?? null,
],
'tempDir' => sys_get_temp_dir().'/herd_test_'.uniqid().'_'.getmypid(),
];
}

function getHerdTestTempDir(): string
{
global $herdTestCleanupData;

return $herdTestCleanupData['tempDir'];
}

test('mcpPath builds correct path from HOME on non-Windows', function () {
$testHome = getHerdTestTempDir().'/home';
mkdir($testHome, 0755, true);
$_SERVER['HOME'] = $testHome;

$herd = new Herd();
$expected = $testHome.'/Library/Application Support/Herd/bin/herd-mcp.phar';

expect($herd->mcpPath())->toBe($expected);
})->skipOnWindows();

test('mcpPath builds correct Windows path from USERPROFILE when HOME missing', function () {
unset($_SERVER['HOME']);
$_SERVER['USERPROFILE'] = 'C:\\Users\\TestUser';

$herd = new Herd();
$expected = 'C:/Users/TestUser/.config/herd/bin/herd-mcp.phar';

expect($herd->mcpPath())->toBe($expected);
})->onlyOnWindows();

test('isMcpAvailable returns false when MCP file is missing', function () {
$testHome = getHerdTestTempDir().'/home';
mkdir($testHome, 0755, true);
$_SERVER['HOME'] = $testHome;

$herd = new Herd();

expect($herd->isMcpAvailable())->toBeFalse();
});

test('isMcpAvailable returns true when MCP file exists', function () {
$testHome = getHerdTestTempDir().'/home';
mkdir($testHome, 0755, true);
$_SERVER['HOME'] = $testHome;

$herd = new Herd();
$mcpPath = $herd->mcpPath();

$mcpDir = dirname($mcpPath);
mkdir($mcpDir, 0755, true);

file_put_contents($mcpPath, 'test phar content');

expect($herd->isMcpAvailable())->toBeTrue();
});

test('isMcpAvailable returns false after MCP file is removed', function () {
$testHome = getHerdTestTempDir().'/home';
mkdir($testHome, 0755, true);
$_SERVER['HOME'] = $testHome;

$herd = new Herd();
$mcpPath = $herd->mcpPath();

$mcpDir = dirname($mcpPath);
mkdir($mcpDir, 0755, true);
file_put_contents($mcpPath, 'test phar content');

expect($herd->isMcpAvailable())->toBeTrue();

// Remove file
unlink($mcpPath);

expect($herd->isMcpAvailable())->toBeFalse();
});

test('getHomePath returns HOME on non-Windows', function () {
$testHome = getHerdTestTempDir().'/home';
mkdir($testHome, 0755, true);
$_SERVER['HOME'] = $testHome;

$herd = new Herd();

expect($herd->getHomePath())->toBe($testHome);
})->skipOnWindows();

test('getHomePath uses USERPROFILE on Windows when HOME is not set and normalizes slashes', function () {
unset($_SERVER['HOME']);
$_SERVER['USERPROFILE'] = 'C:\\Users\\TestUser';

$herd = new Herd();

expect($herd->getHomePath())->toBe('C:/Users/TestUser');
})->onlyOnWindows();

test('isInstalled returns true when herd config directory exists on Windows', function () {
$testHome = getHerdTestTempDir().'/home';
mkdir($testHome, 0755, true);
$_SERVER['HOME'] = $testHome;

$configDir = $testHome.'/.config/herd';
mkdir($configDir, 0755, true);

$herd = new Herd();

expect($herd->isInstalled())->toBeTrue();
})->onlyOnWindows();

test('isInstalled returns false when herd config directory is missing on Windows', function () {
$testHome = getHerdTestTempDir().'/home';
mkdir($testHome, 0755, true);
$_SERVER['HOME'] = $testHome;

$herd = new Herd();

expect($herd->isInstalled())->toBeFalse();
})->onlyOnWindows();

test('isWindowsPlatform returns true on Windows', function () {
$herd = new Herd();

expect($herd->isWindowsPlatform())->toBeTrue();
})->onlyOnWindows();

test('isWindowsPlatform returns false on non-Windows platforms', function () {
$herd = new Herd();

expect($herd->isWindowsPlatform())->toBeFalse();
})->skipOnWindows();
Loading