From cf4a2147277358ba1b410be44397206d1908d0a1 Mon Sep 17 00:00:00 2001 From: Pushpak Chhajed Date: Tue, 12 Aug 2025 11:23:40 +0530 Subject: [PATCH 1/2] test: add unit tests for Herd class --- tests/Unit/Install/HerdTest.php | 177 ++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 tests/Unit/Install/HerdTest.php diff --git a/tests/Unit/Install/HerdTest.php b/tests/Unit/Install/HerdTest.php new file mode 100644 index 0000000..170c289 --- /dev/null +++ b/tests/Unit/Install/HerdTest.php @@ -0,0 +1,177 @@ + $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(); From efd4da8accf8307e02676fa0f2fd4d11deaa92fa Mon Sep 17 00:00:00 2001 From: Pushpak Chhajed Date: Tue, 12 Aug 2025 11:26:21 +0530 Subject: [PATCH 2/2] refactor: extract isWindowsPlatform method in Herd class and update related logic --- src/Install/Herd.php | 17 ++++++++++------- tests/Unit/Install/HerdTest.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Install/Herd.php b/src/Install/Herd.php index f88448a..3501fa9 100644 --- a/src/Install/Herd.php +++ b/src/Install/Herd.php @@ -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'); } @@ -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']; } @@ -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; + } } diff --git a/tests/Unit/Install/HerdTest.php b/tests/Unit/Install/HerdTest.php index 170c289..8cb4b04 100644 --- a/tests/Unit/Install/HerdTest.php +++ b/tests/Unit/Install/HerdTest.php @@ -175,3 +175,15 @@ function getHerdTestTempDir(): string 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();