-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1212 from laravel/mes/pass-site-to-isolate-commands
Add --site to isolate and unisolate commands
- Loading branch information
Showing
5 changed files
with
112 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,9 +211,7 @@ public function stopIfUnused($phpVersion = null) | |
*/ | ||
public function isolateDirectory($directory, $version) | ||
{ | ||
if (! $site = $this->site->getSiteUrl($directory)) { | ||
throw new DomainException("The [{$directory}] site could not be found in Valet's site list."); | ||
} | ||
$site = $this->site->getSiteUrl($directory); | ||
|
||
$version = $this->validateRequestedVersion($version); | ||
|
||
|
@@ -239,9 +237,7 @@ public function isolateDirectory($directory, $version) | |
*/ | ||
public function unIsolateDirectory($directory) | ||
{ | ||
if (! $site = $this->site->getSiteUrl($directory)) { | ||
throw new DomainException("The [{$directory}] site could not be found in Valet's site list."); | ||
} | ||
$site = $this->site->getSiteUrl($directory); | ||
|
||
$oldCustomPhpVersion = $this->site->customPhpVersion($site); // Example output: "74" | ||
|
||
|
@@ -339,10 +335,14 @@ public function normalizePhpVersion($version) | |
*/ | ||
public function validateRequestedVersion($version) | ||
{ | ||
if (is_null($version)) { | ||
throw new DomainException("Please specify a PHP version (try something like '[email protected]')"); | ||
} | ||
|
||
$version = $this->normalizePhpVersion($version); | ||
|
||
if (! $this->brew->supportedPhpVersions()->contains($version)) { | ||
throw new DomainException("Valet doesn't support PHP version: {$version} (try something like 'php@7.3' instead)"); | ||
throw new DomainException("Valet doesn't support PHP version: {$version} (try something like 'php@8.1' instead)"); | ||
} | ||
|
||
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) { | ||
|
@@ -351,7 +351,7 @@ public function validateRequestedVersion($version) | |
|
||
if ($version === 'php') { | ||
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'); | ||
throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: php@8.1'); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,7 +288,7 @@ | |
* Generate a publicly accessible URL for your project. | ||
*/ | ||
$app->command('share', function () { | ||
warning('It looks like you are running `cli/valet.php` directly, please use the `valet` script in the project root instead.'); | ||
warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.'); | ||
})->descriptions('Generate a publicly accessible URL for your project'); | ||
|
||
/** | ||
|
@@ -528,18 +528,29 @@ | |
/** | ||
* Allow the user to change the version of PHP Valet uses to serve the current site. | ||
*/ | ||
$app->command('isolate [phpVersion] ', function ($phpVersion) { | ||
PhpFpm::isolateDirectory(basename(getcwd()), $phpVersion); | ||
$app->command('isolate [phpVersion] [--site=]', function ($phpVersion, $site = null) { | ||
if (! $site) { | ||
$site = basename(getcwd()); | ||
} | ||
|
||
PhpFpm::isolateDirectory($site, $phpVersion); | ||
})->descriptions('Change the version of PHP used by Valet to serve the current working directory', [ | ||
'phpVersion' => 'The PHP version you want to use, e.g [email protected]', | ||
'phpVersion' => 'The PHP version you want to use; e.g [email protected]', | ||
'--site' => 'Specify the site to isolate (e.g. if the site isn\'t linked as its directory name)', | ||
]); | ||
|
||
/** | ||
* Allow the user to un-do specifying the version of PHP Valet uses to serve the current site. | ||
*/ | ||
$app->command('unisolate', function () { | ||
PhpFpm::unIsolateDirectory(basename(getcwd())); | ||
})->descriptions('Stop customizing the version of PHP used by Valet to serve the current working directory'); | ||
$app->command('unisolate [--site=]', function ($site = null) { | ||
if (! $site) { | ||
$site = basename(getcwd()); | ||
} | ||
|
||
PhpFpm::unIsolateDirectory($site); | ||
})->descriptions('Stop customizing the version of PHP used by Valet to serve the current working directory', [ | ||
'--site' => 'Specify the site to un-isolate (e.g. if the site isn\'t linked as its directory name)', | ||
]); | ||
|
||
/** | ||
* List isolated sites. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,22 +65,49 @@ public function test_it_normalizes_php_versions() | |
$this->assertEquals('[email protected]', resolve(PhpFpm::class)->normalizePhpVersion('81')); | ||
} | ||
|
||
public function test_utilized_php_versions() | ||
public function test_it_validates_php_versions_when_installed() | ||
{ | ||
$fileSystemMock = Mockery::mock(Filesystem::class); | ||
$brewMock = Mockery::mock(Brew::class); | ||
$nginxMock = Mockery::mock(Nginx::class); | ||
|
||
$phpFpmMock = Mockery::mock(PhpFpm::class, [ | ||
$brewMock, | ||
Mockery::mock(CommandLine::class), | ||
$fileSystemMock, | ||
resolve(Configuration::class), | ||
Mockery::mock(Site::class), | ||
$nginxMock, | ||
])->makePartial(); | ||
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect(['[email protected]'])); | ||
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('7.4'); | ||
|
||
swap(PhpFpm::class, $phpFpmMock); | ||
swap(Brew::class, $brewMock); | ||
|
||
$this->assertEquals('[email protected]', resolve(PhpFpm::class)->validateRequestedVersion('7.4')); | ||
} | ||
|
||
public function test_it_validates_php_versions_when_uninstalled() | ||
{ | ||
$brewMock = Mockery::mock(Brew::class); | ||
|
||
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect(['[email protected]'])); | ||
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('ERROR - NO BREW ALIAS FOUND'); | ||
|
||
swap(Brew::class, $brewMock); | ||
|
||
$this->assertEquals('[email protected]', resolve(PhpFpm::class)->validateRequestedVersion('7.4')); | ||
} | ||
|
||
public function test_it_throws_when_validating_invalid_php() | ||
{ | ||
$this->expectException(DomainException::class); | ||
|
||
$brewMock = Mockery::mock(Brew::class); | ||
|
||
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect(['[email protected]'])); | ||
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('ERROR - NO BREW ALIAS FOUND'); | ||
|
||
swap(Brew::class, $brewMock); | ||
|
||
$this->assertEquals('[email protected]', resolve(PhpFpm::class)->validateRequestedVersion('9.1')); | ||
} | ||
|
||
public function test_utilized_php_versions() | ||
{ | ||
$brewMock = Mockery::mock(Brew::class); | ||
$nginxMock = Mockery::mock(Nginx::class); | ||
$fileSystemMock = Mockery::mock(Filesystem::class); | ||
|
||
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([ | ||
'[email protected]', | ||
|
@@ -114,34 +141,27 @@ public function test_utilized_php_versions() | |
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH.'/Nginx/'.$site['site'])->andReturn($site['conf']); | ||
} | ||
|
||
swap(Filesystem::class, $fileSystemMock); | ||
swap(Brew::class, $brewMock); | ||
swap(Nginx::class, $nginxMock); | ||
|
||
$this->assertEquals(['[email protected]', '[email protected]', '[email protected]'], resolve(PhpFpm::class)->utilizedPhpVersions()); | ||
} | ||
|
||
public function test_it_lists_isolated_directories() | ||
{ | ||
$fileSystemMock = Mockery::mock(Filesystem::class); | ||
$nginxMock = Mockery::mock(Nginx::class); | ||
$site = Mockery::mock(Site::class); | ||
|
||
$phpFpmMock = Mockery::mock(PhpFpm::class, [ | ||
Mockery::mock(Brew::class), | ||
Mockery::mock(CommandLine::class), | ||
$fileSystemMock, | ||
resolve(Configuration::class), | ||
$site, | ||
$nginxMock, | ||
])->makePartial(); | ||
|
||
swap(PhpFpm::class, $phpFpmMock); | ||
$siteMock = Mockery::mock(Site::class); | ||
$fileSystemMock = Mockery::mock(Filesystem::class); | ||
|
||
$nginxMock->shouldReceive('configuredSites') | ||
->once() | ||
->andReturn(collect(['isolated-site-71.test', 'isolated-site-72.test', 'not-isolated-site.test'])); | ||
|
||
$site->shouldReceive('customPhpVersion')->with('isolated-site-71.test')->andReturn('71'); | ||
$site->shouldReceive('customPhpVersion')->with('isolated-site-72.test')->andReturn('72'); | ||
$site->shouldReceive('normalizePhpVersion')->with('71')->andReturn('[email protected]'); | ||
$site->shouldReceive('normalizePhpVersion')->with('72')->andReturn('[email protected]'); | ||
$siteMock->shouldReceive('customPhpVersion')->with('isolated-site-71.test')->andReturn('71'); | ||
$siteMock->shouldReceive('customPhpVersion')->with('isolated-site-72.test')->andReturn('72'); | ||
$siteMock->shouldReceive('normalizePhpVersion')->with('71')->andReturn('[email protected]'); | ||
$siteMock->shouldReceive('normalizePhpVersion')->with('72')->andReturn('[email protected]'); | ||
|
||
$sites = [ | ||
[ | ||
|
@@ -162,6 +182,10 @@ public function test_it_lists_isolated_directories() | |
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH.'/Nginx/'.$site['site'])->andReturn($site['conf']); | ||
} | ||
|
||
swap(Nginx::class, $nginxMock); | ||
swap(Site::class, $siteMock); | ||
swap(Filesystem::class, $fileSystemMock); | ||
|
||
$this->assertEquals([ | ||
[ | ||
'url' => 'isolated-site-71.test', | ||
|
@@ -400,23 +424,18 @@ public function test_un_isolate_will_remove_isolation_for_a_site() | |
|
||
public function test_isolate_will_throw_if_site_is_not_parked_or_linked() | ||
{ | ||
$siteMock = Mockery::mock(Site::class); | ||
$brewMock = Mockery::mock(Brew::class); | ||
$configMock = Mockery::mock(Configuration::class); | ||
$configMock->shouldReceive('read')->andReturn(['tld' => 'jamble', 'paths' => []]); | ||
|
||
$phpFpmMock = Mockery::mock(PhpFpm::class, [ | ||
Mockery::mock(Brew::class), | ||
resolve(CommandLine::class), | ||
resolve(Filesystem::class), | ||
resolve(Configuration::class), | ||
$siteMock, | ||
Mockery::mock(Nginx::class), | ||
])->makePartial(); | ||
swap(Brew::class, $brewMock); | ||
swap(Nginx::class, Mockery::mock(Nginx::class)); | ||
swap(Configuration::class, $configMock); | ||
|
||
$this->expectException(DomainException::class); | ||
$this->expectExceptionMessage("The [test] site could not be found in Valet's site list."); | ||
|
||
$siteMock->shouldReceive('getSiteUrl'); | ||
|
||
$this->assertSame(null, $phpFpmMock->isolateDirectory('test', '[email protected]')); | ||
resolve(PhpFpm::class)->isolateDirectory('test', '[email protected]'); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters