1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-07 01:00:09 +01:00

Stop root homebrew services on php version switch

Before the change, when running `valet use` the code intended to stop currently running PHP services. But the `getRunningServices` method only returned non-root running services. As PHP services started by Valet are run using `sudo` (so running as root), they were not returned and subsequently not stopped.

This change is intended to fix the above and stop PHP services that are started by Valet on a PHP version switch.
This commit is contained in:
Cristian Calara
2022-01-08 10:30:13 +02:00
parent f938a854e5
commit 9e3a89c742
4 changed files with 90 additions and 15 deletions

View File

@@ -307,7 +307,7 @@ public function test_getRunningServices_will_throw_exception_on_failure()
$onError(1, 'test error output');
});
swap(CommandLine::class, $cli);
resolve(Brew::class)->getRunningServices();
resolve(Brew::class)->getRunningServices(true);
}
public function test_getRunningServices_will_pass_to_brew_services_list_and_return_array()
@@ -319,7 +319,7 @@ public function test_getRunningServices_will_pass_to_brew_services_list_and_retu
])->andReturn('service1'.PHP_EOL.'service2'.PHP_EOL.PHP_EOL.'service3'.PHP_EOL);
swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->getRunningServices('term');
$result = resolve(Brew::class)->getRunningServices(true);
$this->assertInstanceOf(Collection::class, $result);
$this->assertSame([
'service1',
@@ -328,6 +328,43 @@ public function test_getRunningServices_will_pass_to_brew_services_list_and_retu
], array_values($result->all()));
}
public function test_getAllRunningServices_will_return_both_root_and_user_services()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->once()->withArgs([
'sudo brew services list | grep started | awk \'{ print $1; }\'',
Mockery::type('callable'),
])->andReturn('sudo_ran_service');
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew services list | grep started | awk \'{ print $1; }\'',
Mockery::type('callable'),
])->andReturn('user_ran_service');
swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->getAllRunningServices();
$this->assertSame([
'sudo_ran_service',
'user_ran_service',
], array_values($result->all()));
}
public function test_getAllRunningServices_will_return_unique_services()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->once()->andReturn('service1'.PHP_EOL.'service2'.PHP_EOL.'service1'.PHP_EOL);
$cli->shouldReceive('runAsUser')->once()->andReturn('service3'.PHP_EOL.'service4'.PHP_EOL.'service2'.PHP_EOL);
swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->getAllRunningServices();
$this->assertSame([
'service1',
'service2',
'service3',
'service4'
], array_values($result->all()));
}
/**
* @dataProvider supportedPhpLinkPathProvider
*

View File

@@ -42,7 +42,7 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port()
public function test_stopRunning_will_pass_filtered_result_of_getRunningServices_to_stopService()
{
$brewMock = Mockery::mock(Brew::class);
$brewMock->shouldReceive('getRunningServices')->once()
$brewMock->shouldReceive('getAllRunningServices')->once()
->andReturn(collect([
'php7.2',
'php@7.3',
@@ -83,7 +83,7 @@ public function test_use_version_will_convert_passed_php_version()
$brewMock->shouldReceive('link')->withArgs(['php@7.2', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('getAllRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');
// Test both non prefixed and prefixed
@@ -128,7 +128,7 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
$brewMock->shouldReceive('link')->withArgs(['php@7.2', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('getAllRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');
// Test both non prefixed and prefixed