mirror of
https://github.com/laravel/valet.git
synced 2026-02-05 00:20:08 +01:00
Tests for Parallel PHP Version Support
This commit is contained in:
committed by
GitHub
parent
cc41e3c19f
commit
9a5d30bb69
@@ -223,8 +223,12 @@ public function useVersion($version, $force = false, $directory = null)
|
||||
$site = $this->site->getSiteUrl($directory);
|
||||
|
||||
if (! $site) {
|
||||
warning(sprintf("The [%s] site could not be found in Valet's site list.", $directory));
|
||||
return;
|
||||
throw new DomainException(
|
||||
sprintf(
|
||||
"The [%s] site could not be found in Valet's site list.",
|
||||
$directory
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($version == 'default') { // Remove isolation for this site
|
||||
@@ -233,6 +237,7 @@ public function useVersion($version, $force = false, $directory = null)
|
||||
$this->stopIfUnused($oldCustomPhpVersion);
|
||||
$this->nginx->restart();
|
||||
info(sprintf('The site [%s] is now using the default PHP version.', $site));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -263,6 +268,7 @@ public function useVersion($version, $force = false, $directory = null)
|
||||
$this->restart($version);
|
||||
$this->nginx->restart();
|
||||
info(sprintf('The site [%s] is now using %s.', $site, $version));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -424,6 +430,6 @@ public function utilizedPhpVersions()
|
||||
return $this->normalizePhpVersion($phpVersion); // Example output php@7.4
|
||||
}
|
||||
}
|
||||
})->merge([$this->brew->getLinkedPhpFormula()])->filter()->unique()->toArray();
|
||||
})->merge([$this->brew->getLinkedPhpFormula()])->filter()->unique()->values()->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,186 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port()
|
||||
$this->assertStringContainsString(sprintf("\nuser = %s", user()), $contents);
|
||||
$this->assertStringContainsString("\ngroup = staff", $contents);
|
||||
$this->assertStringContainsString("\nlisten = ".VALET_HOME_PATH.'/valet.sock', $contents);
|
||||
|
||||
// Passing speicifc version will change the .sock file
|
||||
resolve(StubForUpdatingFpmConfigFiles::class)->updateConfiguration('php@7.2');
|
||||
$contents = file_get_contents(__DIR__.'/output/fpm.conf');
|
||||
$this->assertStringContainsString(sprintf("\nuser = %s", user()), $contents);
|
||||
$this->assertStringContainsString("\ngroup = staff", $contents);
|
||||
$this->assertStringContainsString("\nlisten = ".VALET_HOME_PATH.'/valet72.sock', $contents);
|
||||
}
|
||||
|
||||
public function test_it_can_generate_sock_file_name_from_php_version()
|
||||
{
|
||||
$this->assertEquals('valet72.sock', resolve(PhpFpm::class)->fpmSockName('php@7.2'));
|
||||
$this->assertEquals('valet72.sock', resolve(PhpFpm::class)->fpmSockName('php@72'));
|
||||
$this->assertEquals('valet72.sock', resolve(PhpFpm::class)->fpmSockName('php72'));
|
||||
$this->assertEquals('valet72.sock', resolve(PhpFpm::class)->fpmSockName('72'));
|
||||
}
|
||||
|
||||
public function test_utilized_php_versions()
|
||||
{
|
||||
$fileSystemMock = Mockery::mock(Filesystem::class);
|
||||
$brewMock = Mockery::mock(Brew::class);
|
||||
|
||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
||||
$brewMock,
|
||||
Mockery::mock(CommandLine::class),
|
||||
$fileSystemMock,
|
||||
resolve(Configuration::class),
|
||||
Mockery::mock(Site::class),
|
||||
Mockery::mock(Nginx::class),
|
||||
])->makePartial();
|
||||
|
||||
swap(PhpFpm::class, $phpFpmMock);
|
||||
|
||||
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
|
||||
'php@7.1',
|
||||
'php@7.2',
|
||||
'php@7.3',
|
||||
'php@7.4',
|
||||
]));
|
||||
|
||||
$brewMock->shouldReceive('getLinkedPhpFormula')->andReturn('php@7.3');
|
||||
|
||||
$fileSystemMock->shouldReceive('scandir')
|
||||
->once()
|
||||
->with(VALET_HOME_PATH.'/Nginx')
|
||||
->andReturn(['.gitkeep', 'isolated-site-71.test', 'isolated-site-72.test', 'isolated-site-73.test']);
|
||||
|
||||
$fileSystemMock->shouldNotReceive('get')->with(VALET_HOME_PATH.'/Nginx/.gitkeep');
|
||||
|
||||
$sites = [
|
||||
[
|
||||
'site' => 'isolated-site-71.test',
|
||||
'conf' => '# Valet isolated PHP version : 71'.PHP_EOL.'valet71.sock',
|
||||
],
|
||||
[
|
||||
'site' => 'isolated-site-72.test',
|
||||
'conf' => '# Valet isolated PHP version : php@7.2'.PHP_EOL.'valet72.sock',
|
||||
],
|
||||
[
|
||||
'site' => 'isolated-site-73.test',
|
||||
'conf' => '# Valet isolated PHP version : 73'.PHP_EOL.'valet.sock',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($sites as $site) {
|
||||
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH.'/Nginx/'.$site['site'])->andReturn($site['conf']);
|
||||
}
|
||||
|
||||
$this->assertEquals(['php@7.1', 'php@7.2', 'php@7.3'], resolve(PhpFpm::class)->utilizedPhpVersions());
|
||||
}
|
||||
|
||||
public function test_global_php_version_update_will_swap_socks()
|
||||
{
|
||||
$fileSystemMock = Mockery::mock(Filesystem::class);
|
||||
|
||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
||||
Mockery::mock(Brew::class),
|
||||
Mockery::mock(CommandLine::class),
|
||||
$fileSystemMock,
|
||||
resolve(Configuration::class),
|
||||
Mockery::mock(Site::class),
|
||||
Mockery::mock(Nginx::class),
|
||||
])->makePartial();
|
||||
|
||||
swap(PhpFpm::class, $phpFpmMock);
|
||||
|
||||
$fileSystemMock->shouldReceive('scandir')
|
||||
->once()
|
||||
->with(VALET_HOME_PATH.'/Nginx')
|
||||
->andReturn([
|
||||
'.gitkeep',
|
||||
'isolated-site-71.test',
|
||||
'isolated-site-72.test',
|
||||
'isolated-site-73.test',
|
||||
'non-isolated-site.test',
|
||||
]);
|
||||
|
||||
// Skip dotfiles
|
||||
$fileSystemMock->shouldNotReceive('get')->with(VALET_HOME_PATH.'/Nginx/.gitkeep');
|
||||
|
||||
// Any isolated site running on php72 would be replaced with default valet.sock,
|
||||
// as 72 will be the default version after the global PHP version switch
|
||||
$fileSystemMock->shouldReceive('get')
|
||||
->once()
|
||||
->with(VALET_HOME_PATH.'/Nginx/isolated-site-72.test')
|
||||
->andReturn('# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet72.sock }');
|
||||
|
||||
$fileSystemMock->shouldReceive('put')->once()->withArgs([
|
||||
VALET_HOME_PATH.'/Nginx/isolated-site-72.test',
|
||||
'# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet.sock }',
|
||||
]);
|
||||
|
||||
// Any isolated site running on current PHP version (with valet.sock),
|
||||
// should be still be running on the same version after the global version update
|
||||
$fileSystemMock->shouldReceive('get')
|
||||
->once()
|
||||
->with(VALET_HOME_PATH.'/Nginx/isolated-site-71.test')
|
||||
->andReturn('# Valet isolated PHP version : 71'.PHP_EOL.'server { fastcgi_pass: valet.sock }');
|
||||
|
||||
$fileSystemMock->shouldReceive('put')->once()->withArgs([
|
||||
VALET_HOME_PATH.'/Nginx/isolated-site-71.test',
|
||||
'# Valet isolated PHP version : 71'.PHP_EOL.'server { fastcgi_pass: valet71.sock }',
|
||||
]);
|
||||
|
||||
// PHP 7.3 sites won't be affected here
|
||||
$fileSystemMock->shouldReceive('get')
|
||||
->once()
|
||||
->with(VALET_HOME_PATH.'/Nginx/isolated-site-73.test')
|
||||
->andReturn('# Valet isolated PHP version : 73'.PHP_EOL.'server { fastcgi_pass: valet73.sock }');
|
||||
|
||||
$fileSystemMock->shouldNotReceive('put')->withArgs([
|
||||
VALET_HOME_PATH.'/Nginx/isolated-site-73.test',
|
||||
Mockery::any(),
|
||||
]);
|
||||
|
||||
// Nginx config that doesn't have the isolation header, It would not swap .sock files
|
||||
$fileSystemMock->shouldReceive('get')->once()->with(VALET_HOME_PATH.'/Nginx/non-isolated-site.test')->andReturn('valet.sock');
|
||||
$fileSystemMock->shouldNotReceive('put')->withArgs([
|
||||
VALET_HOME_PATH.'/Nginx/non-isolated-site.test',
|
||||
'valet71.sock',
|
||||
]);
|
||||
|
||||
// Switching from php7.1 to php7.2
|
||||
resolve(PhpFpm::class)->updateConfigurationForGlobalUpdate('php@7.2', 'php@7.1');
|
||||
}
|
||||
|
||||
public function test_stop_unused_php_versions()
|
||||
{
|
||||
$brewMock = Mockery::mock(Brew::class);
|
||||
|
||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
||||
$brewMock,
|
||||
Mockery::mock(CommandLine::class),
|
||||
Mockery::mock(Filesystem::class),
|
||||
resolve(Configuration::class),
|
||||
Mockery::mock(Site::class),
|
||||
Mockery::mock(Nginx::class),
|
||||
])->makePartial();
|
||||
|
||||
swap(PhpFpm::class, $phpFpmMock);
|
||||
|
||||
$phpFpmMock->shouldReceive('utilizedPhpVersions')->andReturn([
|
||||
'php@7.1',
|
||||
'php@7.2',
|
||||
]);
|
||||
|
||||
// Would do nothing
|
||||
resolve(PhpFpm::class)->stopIfUnused(null);
|
||||
|
||||
// Currently, not utilizeing this PHP version, should be stopped
|
||||
$brewMock->shouldReceive('stopService')->times(3)->with('php@7.3');
|
||||
resolve(PhpFpm::class)->stopIfUnused('73');
|
||||
resolve(PhpFpm::class)->stopIfUnused('php73');
|
||||
resolve(PhpFpm::class)->stopIfUnused('php@7.3');
|
||||
|
||||
// Utilizeing PHP Versions, should not receive stop command
|
||||
$brewMock->shouldNotReceive('stopService')->with('php@7.1');
|
||||
$brewMock->shouldNotReceive('stopService')->with('php@7.2');
|
||||
resolve(PhpFpm::class)->stopIfUnused('php@7.1');
|
||||
resolve(PhpFpm::class)->stopIfUnused('php@7.2');
|
||||
}
|
||||
|
||||
public function test_stopRunning_will_pass_filtered_result_of_getRunningServices_to_stopService()
|
||||
@@ -122,24 +302,32 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
|
||||
$brewMock = Mockery::mock(Brew::class);
|
||||
$nginxMock = Mockery::mock(Nginx::class);
|
||||
$siteMock = Mockery::mock(Site::class);
|
||||
$cliMock = Mockery::mock(CommandLine::class);
|
||||
$fileSystemMock = Mockery::mock(Filesystem::class);
|
||||
|
||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
||||
$brewMock,
|
||||
resolve(CommandLine::class),
|
||||
resolve(Filesystem::class),
|
||||
$cliMock,
|
||||
$fileSystemMock,
|
||||
resolve(Configuration::class),
|
||||
$siteMock,
|
||||
$nginxMock,
|
||||
])->makePartial();
|
||||
|
||||
$phpFpmMock->shouldReceive('install');
|
||||
$phpFpmMock->shouldReceive('updateConfigurationForGlobalUpdate');
|
||||
$cliMock->shouldReceive('quietly')->with('sudo rm '.VALET_HOME_PATH.'/valet*.sock')->once();
|
||||
$fileSystemMock->shouldReceive('unlink')->with(VALET_HOME_PATH.'/valet.sock')->once();
|
||||
|
||||
$phpFpmMock->shouldReceive('updateConfiguration')->with('php@7.1')->once();
|
||||
$phpFpmMock->shouldReceive('updateConfigurationForGlobalUpdate')->withArgs(['php@7.2', 'php@7.1'])->once();
|
||||
|
||||
$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('getLinkedPhpFormula')->andReturn('php@7.1');
|
||||
$brewMock->shouldReceive('unlink')->with('php@7.1');
|
||||
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2', [], $phpFpmMock->taps);
|
||||
@@ -152,9 +340,98 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
|
||||
|
||||
$nginxMock->shouldReceive('restart');
|
||||
|
||||
// Test both non prefixed and prefixed
|
||||
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php@7.2'));
|
||||
}
|
||||
|
||||
public function test_use_version_with_site_paramter_will_isolate_a_site()
|
||||
{
|
||||
$brewMock = Mockery::mock(Brew::class);
|
||||
$nginxMock = Mockery::mock(Nginx::class);
|
||||
$siteMock = Mockery::mock(Site::class);
|
||||
|
||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
||||
$brewMock,
|
||||
resolve(CommandLine::class),
|
||||
resolve(Filesystem::class),
|
||||
resolve(Configuration::class),
|
||||
$siteMock,
|
||||
$nginxMock,
|
||||
])->makePartial();
|
||||
|
||||
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
|
||||
'php@7.2',
|
||||
'php@5.6',
|
||||
]));
|
||||
|
||||
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2', [], $phpFpmMock->taps);
|
||||
$brewMock->shouldReceive('installed')->with('php@7.2');
|
||||
$brewMock->shouldReceive('determineAliasedVersion')->with('php@7.2')->andReturn('php@7.2');
|
||||
$brewMock->shouldReceive('linkedPhp')->once();
|
||||
|
||||
$siteMock->shouldReceive('getSiteUrl')->with('test')->andReturn('test.test');
|
||||
$siteMock->shouldReceive('installSiteConfig')->withArgs(['test.test', 'valet72.sock', 'php@7.2']);
|
||||
$siteMock->shouldReceive('customPhpVersion')->with('test.test')->andReturn('72');
|
||||
|
||||
$phpFpmMock->shouldReceive('stopIfUnused')->with('72')->once();
|
||||
$phpFpmMock->shouldReceive('updateConfiguration')->with('php@7.2')->once();
|
||||
$phpFpmMock->shouldReceive('restart')->with('php@7.2')->once();
|
||||
|
||||
$nginxMock->shouldReceive('restart');
|
||||
|
||||
// These should only run when doing global PHP switches
|
||||
$brewMock->shouldNotReceive('stopService');
|
||||
$brewMock->shouldNotReceive('link');
|
||||
$brewMock->shouldNotReceive('unlink');
|
||||
$phpFpmMock->shouldNotReceive('stopRunning');
|
||||
$phpFpmMock->shouldNotReceive('install');
|
||||
$phpFpmMock->shouldNotReceive('updateConfigurationForGlobalUpdate');
|
||||
|
||||
$this->assertSame(null, $phpFpmMock->useVersion('php@7.2', false, 'test'));
|
||||
}
|
||||
|
||||
public function test_use_version_can_remove_isolation_for_a_site()
|
||||
{
|
||||
$nginxMock = Mockery::mock(Nginx::class);
|
||||
$siteMock = Mockery::mock(Site::class);
|
||||
|
||||
$phpFpmMock = Mockery::mock(PhpFpm::class, [
|
||||
Mockery::mock(Brew::class),
|
||||
resolve(CommandLine::class),
|
||||
resolve(Filesystem::class),
|
||||
resolve(Configuration::class),
|
||||
$siteMock,
|
||||
$nginxMock,
|
||||
])->makePartial();
|
||||
|
||||
$siteMock->shouldReceive('getSiteUrl')->with('test')->andReturn('test.test');
|
||||
$siteMock->shouldReceive('customPhpVersion')->with('test.test')->andReturn('74');
|
||||
$siteMock->shouldReceive('removeIsolation')->with('test.test')->once();
|
||||
$phpFpmMock->shouldReceive('stopIfUnused')->with('74');
|
||||
$nginxMock->shouldReceive('restart');
|
||||
|
||||
$this->assertSame(null, $phpFpmMock->useVersion('default', false, 'test'));
|
||||
}
|
||||
|
||||
public function test_use_version_will_throw_if_site_is_not_parked_or_linked()
|
||||
{
|
||||
$siteMock = Mockery::mock(Site::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();
|
||||
|
||||
$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->useVersion('default', false, 'test'));
|
||||
}
|
||||
}
|
||||
|
||||
class StubForUpdatingFpmConfigFiles extends PhpFpm
|
||||
|
||||
@@ -526,6 +526,261 @@ public function test_remove_proxy()
|
||||
|
||||
$this->assertEquals([], $site->proxies()->all());
|
||||
}
|
||||
|
||||
public function test_get_site_url_from_directory()
|
||||
{
|
||||
$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([
|
||||
'site1' => [
|
||||
'site' => 'site1',
|
||||
'secured' => '',
|
||||
'url' => 'http://site1.test',
|
||||
'path' => '/Users/name/code/site1',
|
||||
],
|
||||
]));
|
||||
|
||||
$siteMock->shouldReceive('links')->andReturn(collect([
|
||||
'site2' => [
|
||||
'site' => 'site2',
|
||||
'secured' => 'X',
|
||||
'url' => 'http://site2.test',
|
||||
'path' => '/Users/name/code/site2',
|
||||
],
|
||||
]));
|
||||
|
||||
$siteMock->shouldReceive('host')->andReturn('site1');
|
||||
|
||||
$site = resolve(Site::class);
|
||||
|
||||
$this->assertEquals('site1.test', $site->getSiteUrl('.'));
|
||||
$this->assertEquals('site1.test', $site->getSiteUrl('./'));
|
||||
|
||||
$this->assertEquals('site1.test', $site->getSiteUrl('site1'));
|
||||
$this->assertEquals('site1.test', $site->getSiteUrl('site1.test'));
|
||||
|
||||
$this->assertEquals('site2.test', $site->getSiteUrl('site2'));
|
||||
$this->assertEquals('site2.test', $site->getSiteUrl('site2.test'));
|
||||
|
||||
$this->assertEquals(false, $site->getSiteUrl('site3'));
|
||||
$this->assertEquals(false, $site->getSiteUrl('site3.test'));
|
||||
}
|
||||
|
||||
public function test_adding_ssl_certificate_would_preserve_isolation()
|
||||
{
|
||||
$files = Mockery::mock(Filesystem::class);
|
||||
$config = Mockery::mock(Configuration::class);
|
||||
|
||||
$siteMock = Mockery::mock(Site::class, [
|
||||
$config,
|
||||
Mockery::mock(CommandLine::class),
|
||||
$files,
|
||||
])->makePartial();
|
||||
|
||||
swap(Site::class, $siteMock);
|
||||
|
||||
$siteMock->shouldReceive('unsecure');
|
||||
$files->shouldReceive('ensureDirExists');
|
||||
$files->shouldReceive('putAsUser');
|
||||
$siteMock->shouldReceive('createCa');
|
||||
$siteMock->shouldReceive('createCertificate');
|
||||
$siteMock->shouldReceive('buildSecureNginxServer');
|
||||
|
||||
// If site has an isolated PHP version for the site, it would replace .sock file
|
||||
$siteMock->shouldReceive('customPhpVersion')->with('site1.test')->andReturn('73')->once();
|
||||
$siteMock->shouldReceive('replaceSockFile')->withArgs([Mockery::any(), 'valet73.sock', '73'])->once();
|
||||
resolve(Site::class)->secure('site1.test');
|
||||
|
||||
// Sites without isolated PHP version, should not replace anything
|
||||
$siteMock->shouldReceive('customPhpVersion')->with('site2.test')->andReturn(null)->once();
|
||||
$siteMock->shouldNotReceive('replaceSockFile');
|
||||
resolve(Site::class)->secure('site2.test');
|
||||
}
|
||||
|
||||
public function test_removing_ssl_certificate_would_preserve_isolation()
|
||||
{
|
||||
$files = Mockery::mock(Filesystem::class);
|
||||
$config = Mockery::mock(Configuration::class);
|
||||
$cli = Mockery::mock(CommandLine::class);
|
||||
|
||||
$siteMock = Mockery::mock(Site::class, [
|
||||
$config,
|
||||
$cli,
|
||||
$files,
|
||||
])->makePartial();
|
||||
|
||||
swap(Site::class, $siteMock);
|
||||
|
||||
$cli->shouldReceive('run');
|
||||
$files->shouldReceive('exists')->andReturn(false);
|
||||
|
||||
// If site has an isolated PHP version, it would install nginx site config
|
||||
$siteMock->shouldReceive('customPhpVersion')->with('site1.test')->andReturn('73')->once();
|
||||
$siteMock->shouldReceive('installSiteConfig')->withArgs(['site1.test', 'valet73.sock', '73'])->once();
|
||||
resolve(Site::class)->unsecure('site1.test');
|
||||
|
||||
// Site without a custom PHP version, should not install site config
|
||||
$siteMock->shouldReceive('customPhpVersion')->with('site2.test')->andReturn(null)->once();
|
||||
$siteMock->shouldNotReceive('installSiteConfig');
|
||||
resolve(Site::class)->unsecure('site2.test');
|
||||
}
|
||||
|
||||
public function test_can_install_nginx_site_config_for_specific_php_version()
|
||||
{
|
||||
$files = Mockery::mock(Filesystem::class);
|
||||
$config = Mockery::mock(Configuration::class);
|
||||
|
||||
$siteMock = Mockery::mock(Site::class, [
|
||||
$config,
|
||||
resolve(CommandLine::class),
|
||||
$files,
|
||||
])->makePartial();
|
||||
|
||||
$config->shouldReceive('read')
|
||||
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK]);
|
||||
|
||||
// If Nginx config exists for the site, modify exising config
|
||||
$files->shouldReceive('exists')->once()->with($siteMock->nginxPath('site1.test'))->andReturn(true);
|
||||
|
||||
$files->shouldReceive('get')
|
||||
->once()
|
||||
->with($siteMock->nginxPath('site1.test'))
|
||||
->andReturn('# Valet isolated PHP version : php@7.4'.PHP_EOL.'server { fastcgi_pass: valet74.sock }');
|
||||
|
||||
$files->shouldReceive('putAsUser')
|
||||
->once()
|
||||
->withArgs([
|
||||
$siteMock->nginxPath('site1.test'),
|
||||
'# Valet isolated PHP version : php@8.0'.PHP_EOL.'server { fastcgi_pass: valet80.sock }',
|
||||
]);
|
||||
|
||||
$siteMock->installSiteConfig('site1.test', 'valet80.sock', 'php@8.0');
|
||||
|
||||
// When there's no Nginx file exists, create new config from the template
|
||||
$files->shouldReceive('exists')->once()->with($siteMock->nginxPath('site2.test'))->andReturn(false);
|
||||
$files->shouldReceive('get')
|
||||
->once()
|
||||
->with(dirname(__DIR__).'/cli/Valet/../stubs/site.valet.conf')
|
||||
->andReturn(file_get_contents(__DIR__.'/../cli/stubs/site.valet.conf'));
|
||||
|
||||
$files->shouldReceive('putAsUser')
|
||||
->once()
|
||||
->withArgs([
|
||||
$siteMock->nginxPath('site2.test'),
|
||||
Mockery::on(function ($argument) {
|
||||
return preg_match('/^# Valet isolated PHP version : php@8.0/', $argument)
|
||||
&& preg_match('#fastcgi_pass "unix:.*/valet80.sock#', $argument)
|
||||
&& strpos($argument, 'server_name site2.test www.site2.test *.site2.test;') !== false;
|
||||
}),
|
||||
]);
|
||||
|
||||
$siteMock->installSiteConfig('site2.test', 'valet80.sock', 'php@8.0');
|
||||
}
|
||||
|
||||
public function test_removeing_isolation()
|
||||
{
|
||||
$files = Mockery::mock(Filesystem::class);
|
||||
|
||||
$siteMock = Mockery::mock(Site::class, [
|
||||
resolve(Configuration::class),
|
||||
resolve(CommandLine::class),
|
||||
$files,
|
||||
])->makePartial();
|
||||
|
||||
swap(Site::class, $siteMock);
|
||||
|
||||
// SSL Site
|
||||
$files->shouldReceive('exists')->once()->with($siteMock->certificatesPath('site1.test', 'crt'))->andReturn(true);
|
||||
$files->shouldReceive('putAsUser')->withArgs([$siteMock->nginxPath('site1.test'), Mockery::any()])->once();
|
||||
$siteMock->shouldReceive('buildSecureNginxServer')->once()->with('site1.test');
|
||||
resolve(Site::class)->removeIsolation('site1.test');
|
||||
|
||||
// Non-SSL Site
|
||||
$files->shouldReceive('exists')->once()->with($siteMock->certificatesPath('site2.test', 'crt'))->andReturn(false);
|
||||
$files->shouldReceive('unlink')->with($siteMock->nginxPath('site2.test'))->once();
|
||||
$siteMock->shouldNotReceive('buildSecureNginxServer')->with('site2.test');
|
||||
resolve(Site::class)->removeIsolation('site2.test');
|
||||
}
|
||||
|
||||
public function test_retrive_custom_php_version_from_nginx_config()
|
||||
{
|
||||
$files = Mockery::mock(Filesystem::class);
|
||||
|
||||
$siteMock = Mockery::mock(Site::class, [
|
||||
resolve(Configuration::class),
|
||||
resolve(CommandLine::class),
|
||||
$files,
|
||||
])->makePartial();
|
||||
|
||||
swap(Site::class, $siteMock);
|
||||
|
||||
// Site with isolated PHP version
|
||||
$files->shouldReceive('exists')->once()->with($siteMock->nginxPath('site1.test'))->andReturn(true);
|
||||
$files->shouldReceive('get')
|
||||
->once()
|
||||
->with($siteMock->nginxPath('site1.test'))
|
||||
->andReturn('# Valet isolated PHP version : php@7.4');
|
||||
$this->assertEquals('74', resolve(Site::class)->customPhpVersion('site1.test'));
|
||||
|
||||
// Site without any custom nginx config
|
||||
$files->shouldReceive('exists')->once()->with($siteMock->nginxPath('site2.test'))->andReturn(false);
|
||||
$files->shouldNotReceive('get')->with($siteMock->nginxPath('site2.test'));
|
||||
$this->assertEquals(null, resolve(Site::class)->customPhpVersion('site2.test'));
|
||||
|
||||
// Site with a custom nginx config, but doesn't have the header
|
||||
$files->shouldReceive('exists')->once()->with($siteMock->nginxPath('site3.test'))->andReturn(true);
|
||||
$files->shouldReceive('get')
|
||||
->once()
|
||||
->with($siteMock->nginxPath('site3.test'))
|
||||
->andReturn('server { }');
|
||||
$this->assertEquals(null, resolve(Site::class)->customPhpVersion('site3.test'));
|
||||
}
|
||||
|
||||
public function test_replace_sock_file_in_nginx_config()
|
||||
{
|
||||
$site = resolve(Site::class);
|
||||
|
||||
// Switiching to php71, valet71.sock should be replaced with valet.sock
|
||||
// It would prepend isolation header
|
||||
$this->assertEquals(
|
||||
'# Valet isolated PHP version : 71'.PHP_EOL.'server { fastcgi_pass: valet.sock }',
|
||||
$site->replaceSockFile('server { fastcgi_pass: valet71.sock }', 'valet.sock', '71')
|
||||
);
|
||||
|
||||
// Switiching to php72, valet.sock should be replaced with valet72.sock
|
||||
$this->assertEquals(
|
||||
'# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet72.sock }',
|
||||
$site->replaceSockFile('server { fastcgi_pass: valet.sock }', 'valet72.sock', '72')
|
||||
);
|
||||
|
||||
// Switiching to php73 from php72, valet72.sock should be replaced with valet73.sock
|
||||
// Isolation header should be updated to 73
|
||||
$this->assertEquals(
|
||||
'# Valet isolated PHP version : 73'.PHP_EOL.'server { fastcgi_pass: valet73.sock }',
|
||||
$site->replaceSockFile('# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet72.sock }', 'valet73.sock', '73')
|
||||
);
|
||||
|
||||
// Switiching to php72 from php74, valet72.sock should be replaced with valet74.sock
|
||||
// Isolation header should be updated to php@7.4
|
||||
$this->assertEquals(
|
||||
'# Valet isolated PHP version : php@7.4'.PHP_EOL.'server { fastcgi_pass: valet74.sock }',
|
||||
$site->replaceSockFile('# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet.sock }', 'valet74.sock', 'php@7.4')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CommandLineFake extends CommandLine
|
||||
|
||||
Reference in New Issue
Block a user