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

add more tests for new methods and bits of clean up

This commit is contained in:
James Barnard
2019-01-19 23:31:57 +00:00
committed by Matt Stauffer
parent 25c4fab2b4
commit 6158e5129b
5 changed files with 121 additions and 27 deletions

View File

@@ -2,7 +2,6 @@
namespace Valet; namespace Valet;
use Exception;
use DomainException; use DomainException;
class Brew class Brew
@@ -292,33 +291,32 @@ function ($exitCode, $errorOutput) use ($formula) {
* Search for a formula and return found, optional grep to filter results * Search for a formula and return found, optional grep to filter results
* *
* @param $formula * @param $formula
* @param null $grep
* *
* @return string * @return \Illuminate\Support\Collection
*/ */
function search($formula, $grep = null) { function search($formula) {
return str_replace(PHP_EOL, '', $this->cli->runAsUser( return collect(explode(PHP_EOL, $this->cli->runAsUser(
sprintf('brew search %s%s', $formula, $grep ? ' | grep ' . $grep : ''), sprintf('brew search %s', $formula),
function ($exitCode, $errorOutput) use ($formula) { function ($exitCode, $errorOutput) use ($formula) {
output($errorOutput); output($errorOutput);
throw new DomainException('Brew was unable to find [' . $formula . '].'); throw new DomainException('Brew was unable to find [' . $formula . '].');
} }
)); )))->filter(function ($formulaFound) {
// Filter out empty and search category headers
return $formulaFound && !in_array($formulaFound, ['==> Formulae', '==> Casks']);
});
} }
/** /**
* Get the currently running brew services * Get the currently running brew services
* *
* @param null $grep
* @return \Illuminate\Support\Collection * @return \Illuminate\Support\Collection
*/ */
function getRunningServices($grep = null) function getRunningServices()
{ {
$grep = 'started' . ($grep ? '.*' . $grep : '');
return collect(array_filter(explode(PHP_EOL, $this->cli->runAsUser( return collect(array_filter(explode(PHP_EOL, $this->cli->runAsUser(
sprintf('brew services list | grep %s | awk \'{ print $1; }\'', $grep), 'brew services list | grep started | awk \'{ print $1; }\'',
function ($exitCode, $errorOutput) { function ($exitCode, $errorOutput) {
output($errorOutput); output($errorOutput);

View File

@@ -2,9 +2,7 @@
namespace Valet; namespace Valet;
use Exception;
use DomainException; use DomainException;
use Symfony\Component\Process\Process;
class PhpFpm class PhpFpm
{ {
@@ -127,19 +125,32 @@ function fpmConfigPath()
function stopRunning() function stopRunning()
{ {
$this->brew->stopService( $this->brew->stopService(
$this->brew->getRunningServices('php')->all() $this->brew->getRunningServices()
->filter(function ($service) {
return substr($service, 0, 3) === 'php';
})
->all()
); );
} }
/**
* Use a specific version of php
*
* @param $version
*/
function useVersion($version) function useVersion($version)
{ {
// Ensure we have php{version} // Ensure we have php prefixed
if (substr($version, 0, 3) !== 'php') { if (substr($version, 0, 3) !== 'php') {
$version = 'php' . $version; $version = 'php' . $version;
} }
info(sprintf('Finding brew formula for: %s', $version)); info(sprintf('Finding brew formula for: %s', $version));
$foundVersion = $this->brew->search($version, 'php'); $foundVersion = $this->brew->search($version)
->filter(function ($service) {
return $this->brew->supportedPhpVersions()->contains($service);
})
->first();
info(sprintf('Found brew formula: %s', $foundVersion)); info(sprintf('Found brew formula: %s', $foundVersion));
if (!$this->brew->supportedPhpVersions()->contains($foundVersion)) { if (!$this->brew->supportedPhpVersions()->contains($foundVersion)) {

View File

@@ -277,9 +277,9 @@
})->descriptions('Add sudoers files for Brew and Valet to make Valet commands run without passwords'); })->descriptions('Add sudoers files for Brew and Valet to make Valet commands run without passwords');
/** /**
* Allow the user to use another version of php * Allow the user to change the version of php valet uses
*/ */
$app->command('use phpVersion', function ($phpVersion = null) { $app->command('use phpVersion', function ($phpVersion) {
PhpFpm::stopRunning(); PhpFpm::stopRunning();
PhpFpm::useVersion($phpVersion); PhpFpm::useVersion($phpVersion);
@@ -287,7 +287,7 @@
Nginx::restart(); Nginx::restart();
info(sprintf('Valet is now using %s.', $phpVersion)); info(sprintf('Valet is now using %s.', $phpVersion));
})->descriptions('Change the version of php used by valet', [ })->descriptions('Change the version of php used by valet', [
'phpVersion' => 'The PHP version you want to use, e.g php72', 'phpVersion' => 'The PHP version you want to use, e.g php@7.2',
]); ]);
} }

View File

@@ -6,6 +6,7 @@
use function Valet\user; use function Valet\user;
use function Valet\resolve; use function Valet\resolve;
use function Valet\swap; use function Valet\swap;
use Illuminate\Support\Collection;
use Illuminate\Container\Container; use Illuminate\Container\Container;
class BrewTest extends PHPUnit_Framework_TestCase class BrewTest extends PHPUnit_Framework_TestCase
@@ -337,9 +338,70 @@ public function test_unlink_will_pass_formula_to_run_as_user()
$this->assertSame('Some output', resolve(Brew::class)->unlink('aformula')); $this->assertSame('Some output', resolve(Brew::class)->unlink('aformula'));
} }
// TODO: search will pass to brew search public function test_search_will_pass_to_brew_search_and_return_array()
// TODO: search will pass into grep if passed {
// TODO: search will throw if fails $cli = Mockery::mock(CommandLine::class);
// TODO: getRunningServices will return array of services currently started $cli->shouldReceive('runAsUser')->once()->withArgs([
// TODO: getRunningServices can pass grep to filter result 'brew search term',
Mockery::type('callable')
])->andReturn('==> Formulae' . PHP_EOL . 'found' . PHP_EOL . '==> Casks' . PHP_EOL . 'another found' . PHP_EOL);
swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->search('term');
$this->assertInstanceOf(Collection::class, $result);
$this->assertSame([
'found',
'another found',
], array_values($result->all()));
}
/**
* @expectedException DomainException
*/
public function test_search_will_throw_exception_on_failure()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew search term',
Mockery::type('callable'),
])->andReturnUsing(function ($command, $onError) {
$onError(1, 'test error output');
});
swap(CommandLine::class, $cli);
resolve(Brew::class)->search('term');
}
/**
* @expectedException DomainException
*/
public function test_getRunningServices_will_throw_exception_on_failure()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew services list | grep started | awk \'{ print $1; }\'',
Mockery::type('callable'),
])->andReturnUsing(function ($command, $onError) {
$onError(1, 'test error output');
});
swap(CommandLine::class, $cli);
resolve(Brew::class)->getRunningServices();
}
public function test_getRunningServices_will_pass_to_brew_services_list_and_return_array()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew services list | grep started | awk \'{ print $1; }\'',
Mockery::type('callable')
])->andReturn('service1' . PHP_EOL . 'service2' . PHP_EOL . PHP_EOL . 'service3' . PHP_EOL);
swap(CommandLine::class, $cli);
$result = resolve(Brew::class)->getRunningServices('term');
$this->assertInstanceOf(Collection::class, $result);
$this->assertSame([
'service1',
'service2',
'service3',
], array_values($result->all()));
}
} }

View File

@@ -1,7 +1,9 @@
<?php <?php
use Valet\Brew;
use Valet\PhpFpm; use Valet\PhpFpm;
use function Valet\user; use function Valet\user;
use function Valet\swap;
use function Valet\resolve; use function Valet\resolve;
use Illuminate\Container\Container; use Illuminate\Container\Container;
@@ -36,6 +38,29 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port()
$this->assertContains("\nlisten = ".VALET_HOME_PATH."/valet.sock", $contents); $this->assertContains("\nlisten = ".VALET_HOME_PATH."/valet.sock", $contents);
} }
public function test_stopRunning_will_pass_filtered_result_of_getRunningServices_to_stopService()
{
$brewMock = Mockery::mock(Brew::class);
$brewMock->shouldReceive('getRunningServices')->once()
->andReturn(collect([
'php7.2',
'php@7.3',
'php56',
'php',
'nginx',
'somethingelse',
]));
$brewMock->shouldReceive('stopService')->once()->with([
'php7.2',
'php@7.3',
'php56',
'php',
]);
swap(Brew::class, $brewMock);
resolve(PhpFpm::class)->stopRunning();
}
// TODO: useVersion if no php at start it will prefix // TODO: useVersion if no php at start it will prefix
// TODO: useVersion will pass version to Brew::search and then check if it's supported // TODO: useVersion will pass version to Brew::search and then check if it's supported
// TODO: - if not supported will through // TODO: - if not supported will through
@@ -43,8 +68,6 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port()
// TODO: useVersion will ensure new version is installed // TODO: useVersion will ensure new version is installed
// TODO: useVersion will link found version (force) // TODO: useVersion will link found version (force)
// TODO: useVersion will call install at end // TODO: useVersion will call install at end
// TODO: stopRunning will get the running php services and stop them
} }