From 6158e5129b34a9c48e0d366ac1246d83a9a7717c Mon Sep 17 00:00:00 2001 From: James Barnard Date: Sat, 19 Jan 2019 23:31:57 +0000 Subject: [PATCH] add more tests for new methods and bits of clean up --- cli/Valet/Brew.php | 22 ++++++-------- cli/Valet/PhpFpm.php | 21 ++++++++++--- cli/valet.php | 6 ++-- tests/BrewTest.php | 72 +++++++++++++++++++++++++++++++++++++++++--- tests/PhpFpmTest.php | 27 +++++++++++++++-- 5 files changed, 121 insertions(+), 27 deletions(-) diff --git a/cli/Valet/Brew.php b/cli/Valet/Brew.php index 52be476..44f0bea 100644 --- a/cli/Valet/Brew.php +++ b/cli/Valet/Brew.php @@ -2,7 +2,6 @@ namespace Valet; -use Exception; use DomainException; class Brew @@ -292,33 +291,32 @@ function ($exitCode, $errorOutput) use ($formula) { * Search for a formula and return found, optional grep to filter results * * @param $formula - * @param null $grep * - * @return string + * @return \Illuminate\Support\Collection */ - function search($formula, $grep = null) { - return str_replace(PHP_EOL, '', $this->cli->runAsUser( - sprintf('brew search %s%s', $formula, $grep ? ' | grep ' . $grep : ''), + 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 * - * @param null $grep * @return \Illuminate\Support\Collection */ - function getRunningServices($grep = null) + function getRunningServices() { - $grep = 'started' . ($grep ? '.*' . $grep : ''); - return collect(array_filter(explode(PHP_EOL, $this->cli->runAsUser( - sprintf('brew services list | grep %s | awk \'{ print $1; }\'', $grep), + 'brew services list | grep started | awk \'{ print $1; }\'', function ($exitCode, $errorOutput) { output($errorOutput); diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index a0a7758..385b6ee 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -2,9 +2,7 @@ namespace Valet; -use Exception; use DomainException; -use Symfony\Component\Process\Process; class PhpFpm { @@ -127,19 +125,32 @@ function fpmConfigPath() function stopRunning() { $this->brew->stopService( - $this->brew->getRunningServices('php')->all() + $this->brew->getRunningServices() + ->filter(function ($service) { + return substr($service, 0, 3) === 'php'; + }) + ->all() ); } + /** + * Use a specific version of php + * + * @param $version + */ function useVersion($version) { - // Ensure we have php{version} + // Ensure we have php prefixed if (substr($version, 0, 3) !== 'php') { $version = 'php' . $version; } info(sprintf('Finding brew formula for: %s', $version)); - $foundVersion = $this->brew->search($version, 'php'); + $foundVersion = $this->brew->search($version) + ->filter(function ($service) { + return $this->brew->supportedPhpVersions()->contains($service); + }) + ->first(); info(sprintf('Found brew formula: %s', $foundVersion)); if (!$this->brew->supportedPhpVersions()->contains($foundVersion)) { diff --git a/cli/valet.php b/cli/valet.php index b5db934..b3c4455 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -277,9 +277,9 @@ })->descriptions('Add sudoers files for Brew and Valet to make Valet commands run without passwords'); /** - * Allow the user to use another version of php + * Allow the user to change the version of php valet uses */ - $app->command('use phpVersion', function ($phpVersion = null) { + $app->command('use phpVersion', function ($phpVersion) { PhpFpm::stopRunning(); PhpFpm::useVersion($phpVersion); @@ -287,7 +287,7 @@ Nginx::restart(); info(sprintf('Valet is now using %s.', $phpVersion)); })->descriptions('Change the version of php used by valet', [ - 'phpVersion' => 'The PHP version you want to use, e.g php72', + 'phpVersion' => 'The PHP version you want to use, e.g php@7.2', ]); } diff --git a/tests/BrewTest.php b/tests/BrewTest.php index 33447ef..7f0a81d 100644 --- a/tests/BrewTest.php +++ b/tests/BrewTest.php @@ -6,6 +6,7 @@ use function Valet\user; use function Valet\resolve; use function Valet\swap; +use Illuminate\Support\Collection; use Illuminate\Container\Container; class BrewTest extends PHPUnit_Framework_TestCase @@ -337,9 +338,70 @@ public function test_unlink_will_pass_formula_to_run_as_user() $this->assertSame('Some output', resolve(Brew::class)->unlink('aformula')); } - // TODO: search will pass to brew search - // TODO: search will pass into grep if passed - // TODO: search will throw if fails - // TODO: getRunningServices will return array of services currently started - // TODO: getRunningServices can pass grep to filter result + 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 + */ + public function test_getRunningServices_will_throw_exception_on_failure() + { + $cli = Mockery::mock(CommandLine::class); + $cli->shouldReceive('runAsUser')->once()->withArgs([ + 'brew services list | grep started | awk \'{ print $1; }\'', + Mockery::type('callable'), + ])->andReturnUsing(function ($command, $onError) { + $onError(1, 'test error output'); + }); + swap(CommandLine::class, $cli); + resolve(Brew::class)->getRunningServices(); + } + + public function test_getRunningServices_will_pass_to_brew_services_list_and_return_array() + { + $cli = Mockery::mock(CommandLine::class); + $cli->shouldReceive('runAsUser')->once()->withArgs([ + 'brew services list | grep started | awk \'{ print $1; }\'', + Mockery::type('callable') + ])->andReturn('service1' . PHP_EOL . 'service2' . PHP_EOL . PHP_EOL . 'service3' . PHP_EOL); + + swap(CommandLine::class, $cli); + $result = resolve(Brew::class)->getRunningServices('term'); + $this->assertInstanceOf(Collection::class, $result); + $this->assertSame([ + 'service1', + 'service2', + 'service3', + ], array_values($result->all())); + } } diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php index b0dee47..4169cfa 100644 --- a/tests/PhpFpmTest.php +++ b/tests/PhpFpmTest.php @@ -1,7 +1,9 @@ assertContains("\nlisten = ".VALET_HOME_PATH."/valet.sock", $contents); } + public function test_stopRunning_will_pass_filtered_result_of_getRunningServices_to_stopService() + { + $brewMock = Mockery::mock(Brew::class); + $brewMock->shouldReceive('getRunningServices')->once() + ->andReturn(collect([ + 'php7.2', + 'php@7.3', + 'php56', + 'php', + 'nginx', + 'somethingelse', + ])); + $brewMock->shouldReceive('stopService')->once()->with([ + 'php7.2', + 'php@7.3', + 'php56', + 'php', + ]); + + swap(Brew::class, $brewMock); + resolve(PhpFpm::class)->stopRunning(); + } + // TODO: useVersion if no php at start it will prefix // TODO: useVersion will pass version to Brew::search and then check if it's supported // TODO: - if not supported will through @@ -43,8 +68,6 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port() // TODO: useVersion will ensure new version is installed // TODO: useVersion will link found version (force) // TODO: useVersion will call install at end - - // TODO: stopRunning will get the running php services and stop them }