diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index 385b6ee..b065c92 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -137,6 +137,7 @@ function stopRunning() * Use a specific version of php * * @param $version + * @return string */ function useVersion($version) { @@ -147,18 +148,18 @@ function useVersion($version) info(sprintf('Finding brew formula for: %s', $version)); $foundVersion = $this->brew->search($version) - ->filter(function ($service) { + ->first(function ($service) { return $this->brew->supportedPhpVersions()->contains($service); - }) - ->first(); - info(sprintf('Found brew formula: %s', $foundVersion)); + }); - if (!$this->brew->supportedPhpVersions()->contains($foundVersion)) { + if (is_null($foundVersion)) { throw new DomainException( - sprintf('Valet doesn\'t support PHP version: %s', $foundVersion) + sprintf('Valet can\'t find a supported version of PHP for: %s', $version) ); } + info(sprintf('Found brew formula: %s', $foundVersion)); + if ($this->brew->hasLinkedPhp()) { $currentVersion = $this->brew->linkedPhp(); info(sprintf('Unlinking current version: %s', $currentVersion)); @@ -171,5 +172,7 @@ function useVersion($version) $this->brew->link($foundVersion, true); $this->install(); + + return $foundVersion; } } diff --git a/cli/valet.php b/cli/valet.php index b3c4455..2f04166 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -282,10 +282,10 @@ $app->command('use phpVersion', function ($phpVersion) { PhpFpm::stopRunning(); - PhpFpm::useVersion($phpVersion); + $newVersion = PhpFpm::useVersion($phpVersion); Nginx::restart(); - info(sprintf('Valet is now using %s.', $phpVersion)); + info(sprintf('Valet is now using %s.', $newVersion)); })->descriptions('Change the version of php used by valet', [ 'phpVersion' => 'The PHP version you want to use, e.g php@7.2', ]); diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php index 4169cfa..c8d675a 100644 --- a/tests/PhpFpmTest.php +++ b/tests/PhpFpmTest.php @@ -2,6 +2,8 @@ use Valet\Brew; use Valet\PhpFpm; +use Valet\Filesystem; +use Valet\CommandLine; use function Valet\user; use function Valet\swap; use function Valet\resolve; @@ -61,13 +63,79 @@ public function test_stopRunning_will_pass_filtered_result_of_getRunningServices 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 - // TODO: useVersion if already linked php will unlink it - // TODO: useVersion will ensure new version is installed - // TODO: useVersion will link found version (force) - // TODO: useVersion will call install at end + public function test_use_version_if_no_php_at_start_will_prefix() + { + $brewMock = Mockery::mock(Brew::class); + $phpFpmMock = Mockery::mock(PhpFpm::class, [ + $brewMock, + resolve(CommandLine::class), + resolve(Filesystem::class), + ])->makePartial(); + + $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', + ])); + $brewMock->shouldReceive('hasLinkedPhp')->andReturn(False); + $brewMock->shouldReceive('ensureInstalled')->with('php@7.2'); + $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')); + } + + /** + * @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() + { + $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'); + } + + public function test_use_version_if_already_linked_php_will_unlink_before_installing() + { + $brewMock = Mockery::mock(Brew::class); + $phpFpmMock = Mockery::mock(PhpFpm::class, [ + $brewMock, + resolve(CommandLine::class), + resolve(Filesystem::class), + ])->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', + ])); + $brewMock->shouldReceive('hasLinkedPhp')->andReturn(true); + $brewMock->shouldReceive('linkedPhp')->andReturn('php@7.1'); + $brewMock->shouldReceive('unlink')->with('php@7.1'); + $brewMock->shouldReceive('ensureInstalled')->with('php@7.2'); + $brewMock->shouldReceive('link')->withArgs(['php@7.2', true]); + + // Test both non prefixed and prefixed + $this->assertSame('php@7.2', $phpFpmMock->useVersion('php@7.2')); + } }