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

clean up to remove search, refactor to better handle versions using regex

This commit is contained in:
James Barnard
2019-01-20 17:01:39 +00:00
committed by Matt Stauffer
parent 6a5c06e934
commit ece3a1ff2f
4 changed files with 14 additions and 86 deletions

View File

@@ -287,27 +287,6 @@ function ($exitCode, $errorOutput) use ($formula) {
);
}
/**
* Search for a formula and return found, optional grep to filter results
*
* @param $formula
*
* @return \Illuminate\Support\Collection
*/
function search($formula) {
return collect(explode(PHP_EOL, $this->cli->runAsUser(
sprintf('brew search %s', $formula),
function ($exitCode, $errorOutput) use ($formula) {
output($errorOutput);
throw new DomainException('Brew was unable to find [' . $formula . '].');
}
)))->filter(function ($formulaFound) {
// Filter out empty and search category headers
return $formulaFound && !in_array($formulaFound, ['==> Formulae', '==> Casks']);
});
}
/**
* Get the currently running brew services
*

View File

@@ -141,38 +141,30 @@ function stopRunning()
*/
function useVersion($version)
{
// Ensure we have php prefixed
if (substr($version, 0, 3) !== 'php') {
$version = 'php' . $version;
}
// If passed php7.2 or php72 formats, convert to php@7.2 format:
$version = preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version);
info(sprintf('Finding brew formula for: %s', $version));
$foundVersion = $this->brew->search($version)
->first(function ($service) {
return $this->brew->supportedPhpVersions()->contains($service);
});
if (is_null($foundVersion)) {
if (!$this->brew->supportedPhpVersions()->contains($version)) {
throw new DomainException(
sprintf('Valet can\'t find a supported version of PHP for: %s', $version)
sprintf('Valet doesnn\'t support PHP version: %s', $version)
);
}
info(sprintf('Found brew formula: %s', $foundVersion));
// Install the relevant formula if not already installed
$this->brew->ensureInstalled($version);
// Unlink the current php if there is one
if ($this->brew->hasLinkedPhp()) {
$currentVersion = $this->brew->linkedPhp();
info(sprintf('Unlinking current version: %s', $currentVersion));
$this->brew->unlink($currentVersion);
}
$this->brew->ensureInstalled($foundVersion);
info(sprintf('Linking new version: %s', $foundVersion));
$this->brew->link($foundVersion, true);
info(sprintf('Linking new version: %s', $version));
$this->brew->link($version, true);
$this->install();
return $foundVersion;
return $version;
}
}

View File

@@ -338,39 +338,6 @@ public function test_unlink_will_pass_formula_to_run_as_user()
$this->assertSame('Some output', resolve(Brew::class)->unlink('aformula'));
}
public function test_search_will_pass_to_brew_search_and_return_array()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew search term',
Mockery::type('callable')
])->andReturn('==> Formulae' . PHP_EOL . 'found' . PHP_EOL . '==> Casks' . PHP_EOL . 'another found' . PHP_EOL);
swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->search('term');
$this->assertInstanceOf(Collection::class, $result);
$this->assertSame([
'found',
'another found',
], array_values($result->all()));
}
/**
* @expectedException DomainException
*/
public function test_search_will_throw_exception_on_failure()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew search term',
Mockery::type('callable'),
])->andReturnUsing(function ($command, $onError) {
$onError(1, 'test error output');
});
swap(CommandLine::class, $cli);
resolve(Brew::class)->search('term');
}
/**
* @expectedException DomainException
*/

View File

@@ -63,7 +63,7 @@ public function test_stopRunning_will_pass_filtered_result_of_getRunningServices
resolve(PhpFpm::class)->stopRunning();
}
public function test_use_version_if_no_php_at_start_will_prefix()
public function test_use_version_will_convert_passed_php_version()
{
$brewMock = Mockery::mock(Brew::class);
$phpFpmMock = Mockery::mock(PhpFpm::class, [
@@ -74,9 +74,6 @@ public function test_use_version_if_no_php_at_start_will_prefix()
$phpFpmMock->shouldReceive('install');
$brewMock->shouldReceive('search')->with('php7.2')->twice()->andReturn(collect([
'php@7.2'
]));
$brewMock->shouldReceive('supportedPhpVersions')->twice()->andReturn(collect([
'php@7.2',
'php@5.6',
@@ -86,28 +83,24 @@ public function test_use_version_if_no_php_at_start_will_prefix()
$brewMock->shouldReceive('link')->withArgs(['php@7.2', true]);
// Test both non prefixed and prefixed
$this->assertSame('php@7.2', $phpFpmMock->useVersion('7.2'));
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php7.2'));
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php72'));
}
/**
* @expectedException DomainException
* @expectedExceptionMessage Valet can't find a supported version of PHP for: php7.2
*/
public function test_use_version_will_throw_if_searched_version_is_not_supported()
public function test_use_version_will_throw_if_version_not_supported()
{
$brewMock = Mockery::mock(Brew::class);
swap(Brew::class, $brewMock);
$brewMock->shouldReceive('search')->with('php7.2')->andReturn(collect([
'php@7.2'
]));
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
'php@7.3',
'php@7.1',
]));
resolve(PhpFpm::class)->useVersion('7.2');
resolve(PhpFpm::class)->useVersion('php@7.2');
}
public function test_use_version_if_already_linked_php_will_unlink_before_installing()
@@ -120,9 +113,6 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
])->makePartial();
$phpFpmMock->shouldReceive('install');
$brewMock->shouldReceive('search')->with('php@7.2')->andReturn(collect([
'php@7.2'
]));
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
'php@7.2',
'php@5.6',