From ece3a1ff2fd0e46d1f47d16f577f7b064341c1c6 Mon Sep 17 00:00:00 2001 From: James Barnard Date: Sun, 20 Jan 2019 17:01:39 +0000 Subject: [PATCH] clean up to remove search, refactor to better handle versions using regex --- cli/Valet/Brew.php | 21 --------------------- cli/Valet/PhpFpm.php | 28 ++++++++++------------------ tests/BrewTest.php | 33 --------------------------------- tests/PhpFpmTest.php | 18 ++++-------------- 4 files changed, 14 insertions(+), 86 deletions(-) diff --git a/cli/Valet/Brew.php b/cli/Valet/Brew.php index 44f0bea..0dbae8b 100644 --- a/cli/Valet/Brew.php +++ b/cli/Valet/Brew.php @@ -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 * diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index b065c92..9260c08 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -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; } } diff --git a/tests/BrewTest.php b/tests/BrewTest.php index 7f0a81d..c7e6207 100644 --- a/tests/BrewTest.php +++ b/tests/BrewTest.php @@ -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 */ diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php index c8d675a..7973a62 100644 --- a/tests/PhpFpmTest.php +++ b/tests/PhpFpmTest.php @@ -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',