Skip to content

Commit

Permalink
Merge pull request #1212 from laravel/mes/pass-site-to-isolate-commands
Browse files Browse the repository at this point in the history
Add --site to isolate and unisolate commands
  • Loading branch information
mattstauffer authored Mar 21, 2022
2 parents c8e7039 + 7532ace commit 0aeebe8
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 59 deletions.
16 changes: 8 additions & 8 deletions cli/Valet/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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"

Expand Down Expand Up @@ -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), '@')) {
Expand All @@ -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');
}
}

Expand Down
4 changes: 2 additions & 2 deletions cli/Valet/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
25 changes: 18 additions & 7 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand Down Expand Up @@ -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.
Expand Down
101 changes: 60 additions & 41 deletions tests/PhpFpmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]',
Expand Down Expand Up @@ -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 = [
[
Expand All @@ -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',
Expand Down Expand Up @@ -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]');
}
}

Expand Down
25 changes: 24 additions & 1 deletion tests/SiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0aeebe8

Please sign in to comment.