1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-04 16:10:08 +01:00

Add --site to isolate and unisolate commands

- Add --site to isolate
- Add --site to unisolate
- Refactor some tests
- Update Site@getSiteUrl to throw an exception instead of returning false
- Fix a few minor typos/grammatical issues
This commit is contained in:
Matt Stauffer
2022-03-21 01:25:23 -04:00
parent c8e70396e4
commit 29b2f45719
5 changed files with 111 additions and 60 deletions

View File

@@ -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 'php@8.1')");
}
$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');
}
}

View File

@@ -194,7 +194,7 @@ public function proxies()
* Get the site URL from a directory if it's a valid Valet site.
*
* @param string $directory
* @return string|false
* @return string
*/
public function getSiteUrl($directory)
{
@@ -207,7 +207,7 @@ public function getSiteUrl($directory)
$directory = str_replace('.'.$tld, '', $directory); // Remove .tld from sitename if it was provided
if (! $this->parked()->merge($this->links())->where('site', $directory)->count() > 0) {
return false; // Invalid directory provided
throw new DomainException("The [{$directory}] site could not be found in Valet's site list.");
}
return $directory.'.'.$tld;

View File

@@ -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 php@7.3',
'phpVersion' => 'The PHP version you want to use; e.g php@8.1',
'--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.

View File

@@ -65,22 +65,49 @@ public function test_it_normalizes_php_versions()
$this->assertEquals('php@8.1', resolve(PhpFpm::class)->normalizePhpVersion('81'));
}
public function test_it_validates_php_versions_when_installed()
{
$brewMock = Mockery::mock(Brew::class);
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect(['php@7.4']));
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('7.4');
swap(Brew::class, $brewMock);
$this->assertEquals('php@7.4', 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(['php@7.4']));
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('ERROR - NO BREW ALIAS FOUND');
swap(Brew::class, $brewMock);
$this->assertEquals('php@7.4', 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(['php@7.4',]));
$brewMock->shouldReceive('determineAliasedVersion')->andReturn('ERROR - NO BREW ALIAS FOUND');
swap(Brew::class, $brewMock);
$this->assertEquals('php@7.4', resolve(PhpFpm::class)->validateRequestedVersion('9.1'));
}
public function test_utilized_php_versions()
{
$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();
swap(PhpFpm::class, $phpFpmMock);
$fileSystemMock = Mockery::mock(Filesystem::class);
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
'php@7.1',
@@ -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(['php@7.1', 'php@7.2', 'php@7.3'], 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('php@7.1');
$site->shouldReceive('normalizePhpVersion')->with('72')->andReturn('php@7.2');
$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('php@7.1');
$siteMock->shouldReceive('normalizePhpVersion')->with('72')->andReturn('php@7.2');
$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,24 +424,17 @@ 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);
$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));
$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', 'php@8.1'));
resolve(PhpFpm::class)->isolateDirectory('test', 'php@8.1');
}
}
class StubForUpdatingFpmConfigFiles extends PhpFpm

View File

@@ -575,9 +575,32 @@ public function test_gets_site_url_from_directory()
$this->assertEquals('site2.test', $site->getSiteUrl('site2'));
$this->assertEquals('site2.test', $site->getSiteUrl('site2.test'));
}
public function test_it_throws_getting_nonexistent_site()
{
$this->expectException(DomainException::class);
$config = Mockery::mock(Configuration::class);
swap(Configuration::class, $config);
$siteMock = Mockery::mock(Site::class, [
resolve(Configuration::class),
resolve(CommandLine::class),
resolve(Filesystem::class),
])->makePartial();
swap(Site::class, $siteMock);
$config->shouldReceive('read')
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK, 'paths' => []]);
$siteMock->shouldReceive('parked')->andReturn(collect());
$siteMock->shouldReceive('links')->andReturn(collect([]));
$siteMock->shouldReceive('host')->andReturn('site1');
$site = resolve(Site::class);
$this->assertEquals(false, $site->getSiteUrl('site3'));
$this->assertEquals(false, $site->getSiteUrl('site3.test'));
}
public function test_isolation_will_persist_when_adding_ssl_certificate()