|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\PieUnitTest\SelfManage\Update; |
| 6 | + |
| 7 | +use Composer\Util\Filesystem; |
| 8 | +use Php\Pie\SelfManage\Update\IsBrewInstallation; |
| 9 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +use function Safe\mkdir; |
| 15 | +use function Safe\realpath; |
| 16 | +use function Safe\symlink; |
| 17 | +use function Safe\touch; |
| 18 | +use function sys_get_temp_dir; |
| 19 | +use function uniqid; |
| 20 | + |
| 21 | +use const DIRECTORY_SEPARATOR; |
| 22 | + |
| 23 | +#[CoversClass(IsBrewInstallation::class)] |
| 24 | +final class IsBrewInstallationTest extends TestCase |
| 25 | +{ |
| 26 | + /** @return array<non-empty-string, array{0: string, 1: string, 2: bool}> */ |
| 27 | + public static function pathProvider(): array |
| 28 | + { |
| 29 | + return [ |
| 30 | + 'regular-path' => ['/home/user/.local/bin/pie.phar', '/home/user/.local/bin/pie.phar', false], |
| 31 | + 'both-regular-usr-local' => ['/usr/local/bin/pie', '/usr/local/bin/pie', false], |
| 32 | + 'intel-mac-cellar-direct' => ['/usr/local/Cellar/pie/1.0/bin/pie', '/usr/local/Cellar/pie/1.0/bin/pie', true], |
| 33 | + 'apple-silicon-cellar-direct' => ['/opt/homebrew/Cellar/pie/1.0/bin/pie', '/opt/homebrew/Cellar/pie/1.0/bin/pie', true], |
| 34 | + 'resolved-is-cellar' => ['/opt/homebrew/Cellar/pie/1.0/bin/pie', '/usr/local/bin/pie', true], |
| 35 | + 'original-is-cellar' => ['/usr/local/bin/pie', '/opt/homebrew/Cellar/pie/1.0/bin/pie', true], |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + #[DataProvider('pathProvider')] |
| 40 | + public function testIsBrewInstallationWithPaths( |
| 41 | + string $resolvedPath, |
| 42 | + string $originalPath, |
| 43 | + bool $expected, |
| 44 | + ): void { |
| 45 | + self::assertSame($expected, (new IsBrewInstallation())($resolvedPath, $originalPath)); |
| 46 | + } |
| 47 | + |
| 48 | + #[RequiresOperatingSystemFamily('Linux')] |
| 49 | + public function testSymlinkAtRegularPathPointingIntoBrewCellarIsDetected(): void |
| 50 | + { |
| 51 | + $tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_brew_test_', true); |
| 52 | + $cellarFile = $tmpDir . '/opt/homebrew/Cellar/pie/1.0/pie.phar'; |
| 53 | + $symlinkPath = $tmpDir . '/pie'; |
| 54 | + |
| 55 | + mkdir($tmpDir . '/opt/homebrew/Cellar/pie/1.0', 0777, true); |
| 56 | + touch($cellarFile); |
| 57 | + symlink($cellarFile, $symlinkPath); |
| 58 | + |
| 59 | + try { |
| 60 | + $resolvedPath = realpath($symlinkPath); |
| 61 | + self::assertTrue((new IsBrewInstallation())($resolvedPath, $symlinkPath)); |
| 62 | + } finally { |
| 63 | + (new Filesystem())->remove($tmpDir); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #[RequiresOperatingSystemFamily('Linux')] |
| 68 | + public function testSymlinkAtRegularPathPointingToNonBrewPathIsNotDetected(): void |
| 69 | + { |
| 70 | + $tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_brew_test_', true); |
| 71 | + $regularFile = $tmpDir . '/regular/pie.phar'; |
| 72 | + $symlinkPath = $tmpDir . '/pie'; |
| 73 | + |
| 74 | + mkdir($tmpDir . '/regular', 0777, true); |
| 75 | + touch($regularFile); |
| 76 | + symlink($regularFile, $symlinkPath); |
| 77 | + |
| 78 | + try { |
| 79 | + $resolvedPath = realpath($symlinkPath); |
| 80 | + self::assertFalse((new IsBrewInstallation())($resolvedPath, $symlinkPath)); |
| 81 | + } finally { |
| 82 | + (new Filesystem())->remove($tmpDir); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + #[RequiresOperatingSystemFamily('Linux')] |
| 87 | + public function testSymlinkInCellarPointingToRegularPathIsDetectedViaOriginalPath(): void |
| 88 | + { |
| 89 | + $tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_brew_test_', true); |
| 90 | + $regularFile = $tmpDir . '/regular/pie.phar'; |
| 91 | + $symlinkPath = $tmpDir . '/opt/homebrew/Cellar/pie/1.0/pie'; |
| 92 | + |
| 93 | + mkdir($tmpDir . '/regular', 0777, true); |
| 94 | + mkdir($tmpDir . '/opt/homebrew/Cellar/pie/1.0', 0777, true); |
| 95 | + touch($regularFile); |
| 96 | + symlink($regularFile, $symlinkPath); |
| 97 | + |
| 98 | + try { |
| 99 | + $resolvedPath = realpath($symlinkPath); |
| 100 | + self::assertTrue((new IsBrewInstallation())($resolvedPath, $symlinkPath)); |
| 101 | + } finally { |
| 102 | + (new Filesystem())->remove($tmpDir); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments