1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 00:20:08 +01:00

Merge pull request #1008 from drbyte/valet-use-improvements

Improvements to "valet use" command
This commit is contained in:
Matt Stauffer
2020-11-29 22:49:25 -05:00
committed by GitHub
7 changed files with 173 additions and 177 deletions

View File

@@ -41,14 +41,23 @@ function __construct(CommandLine $cli, Filesystem $files)
} }
/** /**
* Determine if the given formula is installed. * Ensure the formula exists in the current Homebrew configuration
* *
* @param string $formula * @param string $formula
* @return bool * @return bool
*/ */
function installed($formula) function installed($formula)
{ {
return in_array($formula, explode(PHP_EOL, $this->cli->runAsUser('brew list --formula | grep '.$formula))); $result = $this->cli->runAsUser("brew info $formula --json");
// should be a json response, but if not installed then "Error: No available formula ..."
if (starts_with($result, 'Error: No')) {
return false;
}
$details = json_decode($result);
return !empty($details[0]->installed);
} }
/** /**
@@ -58,9 +67,11 @@ function installed($formula)
*/ */
function hasInstalledPhp() function hasInstalledPhp()
{ {
return $this->supportedPhpVersions()->contains(function ($version) { $installed = $this->installedPhpFormulae()->first(function ($formula) {
return $this->installed($version); return $this->supportedPhpVersions()->contains($formula);
}); });
return !empty($installed);
} }
/** /**
@@ -73,13 +84,25 @@ function supportedPhpVersions()
return collect(static::SUPPORTED_PHP_VERSIONS); return collect(static::SUPPORTED_PHP_VERSIONS);
} }
function installedPhpFormulae()
{
return collect(
explode(PHP_EOL, $this->cli->runAsUser('brew list --formula | grep php'))
);
}
/** /**
* Get the aliased formula version from Homebrew * Get the aliased formula version from Homebrew
*/ */
function determineAliasedVersion($formula) function determineAliasedVersion($formula)
{ {
$details = json_decode($this->cli->runAsUser("brew info $formula --json")); $details = json_decode($this->cli->runAsUser("brew info $formula --json"));
return $details[0]->aliases[0] ?: 'ERROR - NO BREW ALIAS FOUND';
if (!empty($details[0]->aliases[0])) {
return $details[0]->aliases[0];
}
return 'ERROR - NO BREW ALIAS FOUND';
} }
/** /**
@@ -135,6 +158,9 @@ function installOrFail($formula, $options = [], $taps = [])
} }
output('<info>['.$formula.'] is not installed, installing it now via Brew...</info> 🍻'); output('<info>['.$formula.'] is not installed, installing it now via Brew...</info> 🍻');
if ($formula !== 'php' && starts_with($formula, 'php') && preg_replace('/[^\d]/', '', $formula) < '73') {
warning('Note: older PHP versions may take 10+ minutes to compile from source. Please wait ...');
}
$this->cli->runAsUser(trim('brew install '.$formula.' '.implode(' ', $options)), function ($exitCode, $errorOutput) use ($formula) { $this->cli->runAsUser(trim('brew install '.$formula.' '.implode(' ', $options)), function ($exitCode, $errorOutput) use ($formula) {
output($errorOutput); output($errorOutput);
@@ -213,16 +239,16 @@ function hasLinkedPhp()
function getParsedLinkedPhp() function getParsedLinkedPhp()
{ {
if (! $this->hasLinkedPhp()) { if (! $this->hasLinkedPhp()) {
throw new DomainException("Homebrew PHP appears not to be linked."); throw new DomainException("Homebrew PHP appears not to be linked. Please run [valet use php@X.Y]");
} }
$resolvedPath = $this->files->readLink(BREW_PREFIX.'/bin/php'); $resolvedPath = $this->files->readLink(BREW_PREFIX.'/bin/php');
/** /**
* Typical homebrew path resolutions are like: * Typical homebrew path resolutions are like:
* "../Cellar/php@7.2/7.2.13/bin/php" * "../Cellar/php@7.4/7.4.13/bin/php"
* or older styles: * or older styles:
* "../Cellar/php/7.2.9_2/bin/php * "../Cellar/php/7.4.9_2/bin/php
* "../Cellar/php55/bin/php * "../Cellar/php55/bin/php
*/ */
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches); preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
@@ -231,12 +257,11 @@ function getParsedLinkedPhp()
} }
/** /**
* Gets the currently linked formula * Gets the currently linked formula by identifying the symlink in the hombrew bin directory.
* E.g if under php, will be php, if under php@7.3 will be that * Different to ->linkedPhp() in that this will just get the linked directory name,
* Different to ->linkedPhp() in that this will just get the linked directory name (whether that is php, php55 or * whether that is php, php74 or php@7.4
* php@7.2)
* *
* @return mixed * @return string
*/ */
function getLinkedPhpFormula() function getLinkedPhpFormula()
{ {

View File

@@ -20,6 +20,7 @@ class Diagnose
'brew services list', 'brew services list',
'brew list --formula --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"', 'brew list --formula --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"',
'brew outdated', 'brew outdated',
'brew tap',
'php -v', 'php -v',
'which -a php', 'which -a php',
'php --ini', 'php --ini',

View File

@@ -9,7 +9,8 @@ class PhpFpm
var $brew, $cli, $files; var $brew, $cli, $files;
var $taps = [ var $taps = [
'homebrew/homebrew-core' 'homebrew/homebrew-core',
'shivammathur/php',
]; ];
/** /**
@@ -142,11 +143,8 @@ function fpmConfigPath()
{ {
$version = $this->brew->linkedPhp(); $version = $this->brew->linkedPhp();
$versionNormalized = preg_replace( $versionNormalized = $this->normalizePhpVersion($version === 'php' ? Brew::LATEST_PHP_VERSION : $version);
'/php@?(\d)\.?(\d)/', $versionNormalized = preg_replace('~[^\d\.]~', '', $versionNormalized);
'$1.$2',
$version === 'php' ? Brew::LATEST_PHP_VERSION : $version
);
return $versionNormalized === '5.6' return $versionNormalized === '5.6'
? BREW_PREFIX.'/etc/php/5.6/php-fpm.conf' ? BREW_PREFIX.'/etc/php/5.6/php-fpm.conf'
@@ -171,14 +169,26 @@ function stopRunning()
* Use a specific version of php * Use a specific version of php
* *
* @param $version * @param $version
* @param $force
* @return string * @return string
*/ */
function useVersion($version) function useVersion($version, $force = false)
{ {
$version = $this->validateRequestedVersion($version); $version = $this->validateRequestedVersion($version);
// Install the relevant formula if not already installed try {
$this->brew->ensureInstalled($version); if ($this->brew->linkedPhp() === $version && !$force) {
output(sprintf('<info>Valet is already using version: <comment>%s</comment>.</info> To re-link and re-configure use the --force parameter.' . PHP_EOL,
$version));
exit();
}
} catch (DomainException $e)
{ /* ignore thrown exception when no linked php is found */ }
if (!$this->brew->installed($version)) {
// Install the relevant formula if not already installed
$this->brew->ensureInstalled($version, [], $this->taps);
}
// Unlink the current php if there is one // Unlink the current php if there is one
if ($this->brew->hasLinkedPhp()) { if ($this->brew->hasLinkedPhp()) {
@@ -190,11 +200,23 @@ function useVersion($version)
info(sprintf('Linking new version: %s', $version)); info(sprintf('Linking new version: %s', $version));
$this->brew->link($version, true); $this->brew->link($version, true);
$this->stopRunning();
// ensure configuration is correct and start the linked version
$this->install(); $this->install();
return $version === 'php' ? $this->brew->determineAliasedVersion($version) : $version; return $version === 'php' ? $this->brew->determineAliasedVersion($version) : $version;
} }
/**
* If passed php7.4 or php74 formats, normalize to php@7.4 format.
*/
function normalizePhpVersion($version)
{
return preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version);
}
/** /**
* Validate the requested version to be sure we can support it. * Validate the requested version to be sure we can support it.
* *
@@ -203,18 +225,7 @@ function useVersion($version)
*/ */
function validateRequestedVersion($version) function validateRequestedVersion($version)
{ {
// If passed php7.2 or php72 formats, normalize to php@7.2 format: $version = $this->normalizePhpVersion($version);
$version = preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version);
if ($version === 'php') {
if (strpos($this->brew->determineAliasedVersion($version), '@')) {
return $version;
}
if ($this->brew->hasInstalledPhp()) {
throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: php@7.3');
}
}
if (!$this->brew->supportedPhpVersions()->contains($version)) { if (!$this->brew->supportedPhpVersions()->contains($version)) {
throw new DomainException( throw new DomainException(
@@ -225,6 +236,20 @@ function validateRequestedVersion($version)
); );
} }
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
return $aliasedVersion;
}
if ($version === 'php') {
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
return $aliasedVersion;
}
if ($this->brew->hasInstalledPhp()) {
throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: php@7.3');
}
}
return $version; return $version;
} }
} }

View File

@@ -172,6 +172,26 @@ function ends_with($haystack, $needles) {
} }
} }
if (! function_exists('starts_with')) {
/**
* Determine if a given string starts with a given substring.
*
* @param string $haystack
* @param string|string[] $needles
* @return bool
*/
function starts_with($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
return true;
}
}
return false;
}
}
/** /**
* Get the user * Get the user
*/ */

View File

@@ -462,11 +462,14 @@
/** /**
* Allow the user to change the version of php valet uses * Allow the user to change the version of php valet uses
*/ */
$app->command('use phpVersion', function ($phpVersion) { $app->command('use [phpVersion] [--force]', function ($phpVersion, $force) {
if (!$phpVersion) {
return info('Valet is using ' . Brew::linkedPhp());
}
PhpFpm::validateRequestedVersion($phpVersion); PhpFpm::validateRequestedVersion($phpVersion);
PhpFpm::stopRunning(); $newVersion = PhpFpm::useVersion($phpVersion, $force);
$newVersion = PhpFpm::useVersion($phpVersion);
Nginx::restart(); Nginx::restart();
info(sprintf('Valet is now using %s.', $newVersion) . PHP_EOL); info(sprintf('Valet is now using %s.', $newVersion) . PHP_EOL);

View File

@@ -31,13 +31,14 @@ public function test_brew_can_be_resolved_from_container()
public function test_installed_returns_true_when_given_formula_is_installed() public function test_installed_returns_true_when_given_formula_is_installed()
{ {
$cli = Mockery::mock(CommandLine::class); $cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn('php@7.4'); $cli->shouldReceive('runAsUser')->once()->with('brew info php@7.4 --json')
->andReturn('[{"name":"php@7.4","full_name":"php@7.4","aliases":[],"versioned_formulae":[],"versions":{"stable":"7.4.5"},"installed":[{"version":"7.4.5"}]}]');
swap(CommandLine::class, $cli); swap(CommandLine::class, $cli);
$this->assertTrue(resolve(Brew::class)->installed('php@7.4')); $this->assertTrue(resolve(Brew::class)->installed('php@7.4'));
$cli = Mockery::mock(CommandLine::class); $cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php')->andReturn('php $cli->shouldReceive('runAsUser')->once()->with('brew info php --json')
php@7.4'); ->andReturn('[{"name":"php","full_name":"php","aliases":["php@8.0"],"versioned_formulae":[],"versions":{"stable":"8.0.0"},"installed":[{"version":"8.0.0"}]}]');
swap(CommandLine::class, $cli); swap(CommandLine::class, $cli);
$this->assertTrue(resolve(Brew::class)->installed('php')); $this->assertTrue(resolve(Brew::class)->installed('php'));
} }
@@ -45,150 +46,61 @@ public function test_installed_returns_true_when_given_formula_is_installed()
public function test_installed_returns_false_when_given_formula_is_not_installed() public function test_installed_returns_false_when_given_formula_is_not_installed()
{ {
$cli = Mockery::mock(CommandLine::class); $cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn(''); $cli->shouldReceive('runAsUser')->once()->with('brew info php@7.4 --json')->andReturn('');
swap(CommandLine::class, $cli); swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php@7.4')); $this->assertFalse(resolve(Brew::class)->installed('php@7.4'));
$cli = Mockery::mock(CommandLine::class); $cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn('php'); $cli->shouldReceive('runAsUser')->once()->with('brew info php@7.4 --json')->andReturn('Error: No formula found');
swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php@7.4'));
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn('php
something-else-php@7.4
php7');
swap(CommandLine::class, $cli); swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php@7.4')); $this->assertFalse(resolve(Brew::class)->installed('php@7.4'));
} }
public function test_has_installed_php_indicates_if_php_is_installed_via_brew() public function test_has_installed_php_indicates_if_php_is_installed_via_brew()
{ {
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]); $brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(true); $brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@5.5']));
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(true);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(true);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(true);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(true);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(true);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(true);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(true);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(true);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(true);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(true);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(true);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertFalse($brew->hasInstalledPhp()); $this->assertFalse($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@8.1']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@8.0']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.4']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.3']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php73']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.2', 'php72']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php71', 'php@7.1']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.0']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@5.6']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php56']));
$this->assertTrue($brew->hasInstalledPhp());
} }
public function test_tap_taps_the_given_homebrew_repository() public function test_tap_taps_the_given_homebrew_repository()
@@ -204,7 +116,7 @@ public function test_tap_taps_the_given_homebrew_repository()
public function test_restart_restarts_the_service_using_homebrew_services() public function test_restart_restarts_the_service_using_homebrew_services()
{ {
$cli = Mockery::mock(CommandLine::class); $cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep dnsmasq')->andReturn('dnsmasq'); $cli->shouldReceive('runAsUser')->once()->with('brew info dnsmasq --json')->andReturn('[{"name":"dnsmasq","full_name":"dnsmasq","aliases":[],"versioned_formulae":[],"versions":{"stable":"1"},"installed":[{"version":"1"}]}]');
$cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq'); $cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq');
$cli->shouldReceive('quietly')->once()->with('sudo brew services start dnsmasq'); $cli->shouldReceive('quietly')->once()->with('sudo brew services start dnsmasq');
swap(CommandLine::class, $cli); swap(CommandLine::class, $cli);
@@ -214,7 +126,7 @@ public function test_restart_restarts_the_service_using_homebrew_services()
public function test_stop_stops_the_service_using_homebrew_services() public function test_stop_stops_the_service_using_homebrew_services()
{ {
$cli = Mockery::mock(CommandLine::class); $cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep dnsmasq')->andReturn('dnsmasq'); $cli->shouldReceive('runAsUser')->once()->with('brew info dnsmasq --json')->andReturn('[{"name":"dnsmasq","full_name":"dnsmasq","aliases":[],"versioned_formulae":[],"versions":{"stable":"1"},"installed":[{"version":"1"}]}]');
$cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq'); $cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq');
swap(CommandLine::class, $cli); swap(CommandLine::class, $cli);
resolve(Brew::class)->stopService('dnsmasq'); resolve(Brew::class)->stopService('dnsmasq');

View File

@@ -78,8 +78,13 @@ public function test_use_version_will_convert_passed_php_version()
'php@5.6', 'php@5.6',
])); ]));
$brewMock->shouldReceive('hasLinkedPhp')->andReturn(false); $brewMock->shouldReceive('hasLinkedPhp')->andReturn(false);
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2'); $brewMock->shouldReceive('ensureInstalled')->with('php@7.2', [], $phpFpmMock->taps);
$brewMock->shouldReceive('determineAliasedVersion')->with('php@7.2')->andReturn('php@7.2');
$brewMock->shouldReceive('link')->withArgs(['php@7.2', true]); $brewMock->shouldReceive('link')->withArgs(['php@7.2', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');
// Test both non prefixed and prefixed // Test both non prefixed and prefixed
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php7.2')); $this->assertSame('php@7.2', $phpFpmMock->useVersion('php7.2'));
@@ -118,8 +123,13 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
$brewMock->shouldReceive('hasLinkedPhp')->andReturn(true); $brewMock->shouldReceive('hasLinkedPhp')->andReturn(true);
$brewMock->shouldReceive('getLinkedPhpFormula')->andReturn('php@7.1'); $brewMock->shouldReceive('getLinkedPhpFormula')->andReturn('php@7.1');
$brewMock->shouldReceive('unlink')->with('php@7.1'); $brewMock->shouldReceive('unlink')->with('php@7.1');
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2'); $brewMock->shouldReceive('ensureInstalled')->with('php@7.2', [], $phpFpmMock->taps);
$brewMock->shouldReceive('determineAliasedVersion')->with('php@7.2')->andReturn('php@7.2');
$brewMock->shouldReceive('link')->withArgs(['php@7.2', true]); $brewMock->shouldReceive('link')->withArgs(['php@7.2', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');
// Test both non prefixed and prefixed // Test both non prefixed and prefixed
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php@7.2')); $this->assertSame('php@7.2', $phpFpmMock->useVersion('php@7.2'));