|
| 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