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

Merging master.

This commit is contained in:
Mikaël Popowicz
2020-11-30 09:45:11 +01:00
17 changed files with 245 additions and 306 deletions

View File

@@ -29,11 +29,6 @@ jobs:
- name: Install dependencies
run: composer install --no-interaction --prefer-dist
if: matrix.php != 8.0
- name: Install dependencies
run: composer install --no-interaction --prefer-dist --ignore-platform-req=php
if: matrix.php == 8.0
- name: Execute tests
env:

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ vendor/
composer.lock
error.log
.idea
.phpunit.result.cache

View File

@@ -23,7 +23,7 @@ class Brew
'php56'
];
const LATEST_PHP_VERSION = 'php@7.4';
const LATEST_PHP_VERSION = 'php@8.0';
var $cli, $files;
@@ -41,14 +41,23 @@ function __construct(CommandLine $cli, Filesystem $files)
}
/**
* Determine if the given formula is installed.
* Ensure the formula exists in the current Homebrew configuration
*
* @param string $formula
* @return bool
*/
function installed($formula)
{
return in_array($formula, explode(PHP_EOL, $this->cli->runAsUser('brew list --formula | grep '.$formula)));
$result = $this->cli->runAsUser("brew info $formula --json");
// should be a json response, but if not installed then "Error: No available formula ..."
if (starts_with($result, 'Error: No')) {
return false;
}
$details = json_decode($result);
return !empty($details[0]->installed);
}
/**
@@ -58,9 +67,11 @@ function installed($formula)
*/
function hasInstalledPhp()
{
return $this->supportedPhpVersions()->contains(function ($version) {
return $this->installed($version);
$installed = $this->installedPhpFormulae()->first(function ($formula) {
return $this->supportedPhpVersions()->contains($formula);
});
return !empty($installed);
}
/**
@@ -73,13 +84,25 @@ function supportedPhpVersions()
return collect(static::SUPPORTED_PHP_VERSIONS);
}
function installedPhpFormulae()
{
return collect(
explode(PHP_EOL, $this->cli->runAsUser('brew list --formula | grep php'))
);
}
/**
* Get the aliased formula version from Homebrew
*/
function determineAliasedVersion($formula)
{
$details = json_decode($this->cli->runAsUser("brew info $formula --json"));
return $details[0]->aliases[0] ?: 'ERROR - NO BREW ALIAS FOUND';
if (!empty($details[0]->aliases[0])) {
return $details[0]->aliases[0];
}
return 'ERROR - NO BREW ALIAS FOUND';
}
/**
@@ -135,6 +158,9 @@ function installOrFail($formula, $options = [], $taps = [])
}
output('<info>['.$formula.'] is not installed, installing it now via Brew...</info> 🍻');
if ($formula !== 'php' && starts_with($formula, 'php') && preg_replace('/[^\d]/', '', $formula) < '73') {
warning('Note: older PHP versions may take 10+ minutes to compile from source. Please wait ...');
}
$this->cli->runAsUser(trim('brew install '.$formula.' '.implode(' ', $options)), function ($exitCode, $errorOutput) use ($formula) {
output($errorOutput);
@@ -213,16 +239,16 @@ function hasLinkedPhp()
function getParsedLinkedPhp()
{
if (! $this->hasLinkedPhp()) {
throw new DomainException("Homebrew PHP appears not to be linked.");
throw new DomainException("Homebrew PHP appears not to be linked. Please run [valet use php@X.Y]");
}
$resolvedPath = $this->files->readLink(BREW_PREFIX.'/bin/php');
/**
* Typical homebrew path resolutions are like:
* "../Cellar/php@7.2/7.2.13/bin/php"
* "../Cellar/php@7.4/7.4.13/bin/php"
* or older styles:
* "../Cellar/php/7.2.9_2/bin/php
* "../Cellar/php/7.4.9_2/bin/php
* "../Cellar/php55/bin/php
*/
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
@@ -231,12 +257,11 @@ function getParsedLinkedPhp()
}
/**
* Gets the currently linked formula
* E.g if under php, will be php, if under php@7.3 will be that
* Different to ->linkedPhp() in that this will just get the linked directory name (whether that is php, php55 or
* php@7.2)
* Gets the currently linked formula by identifying the symlink in the hombrew bin directory.
* Different to ->linkedPhp() in that this will just get the linked directory name,
* whether that is php, php74 or php@7.4
*
* @return mixed
* @return string
*/
function getLinkedPhpFormula()
{

View File

@@ -20,6 +20,7 @@ class Diagnose
'brew services list',
'brew list --formula --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"',
'brew outdated',
'brew tap',
'php -v',
'which -a php',
'php --ini',
@@ -40,6 +41,8 @@ class Diagnose
'ls -al ~/Library/LaunchAgents | grep homebrew',
'ls -al /Library/LaunchAgents | grep homebrew',
'ls -al /Library/LaunchDaemons | grep homebrew',
'ls -aln /etc/resolv.conf',
'cat /etc/resolv.conf',
];
var $cli, $files, $print, $progressBar;

View File

@@ -9,7 +9,8 @@ class PhpFpm
var $brew, $cli, $files;
var $taps = [
'homebrew/homebrew-core'
'homebrew/homebrew-core',
'shivammathur/php',
];
/**
@@ -142,11 +143,8 @@ function fpmConfigPath()
{
$version = $this->brew->linkedPhp();
$versionNormalized = preg_replace(
'/php@?(\d)\.?(\d)/',
'$1.$2',
$version === 'php' ? Brew::LATEST_PHP_VERSION : $version
);
$versionNormalized = $this->normalizePhpVersion($version === 'php' ? Brew::LATEST_PHP_VERSION : $version);
$versionNormalized = preg_replace('~[^\d\.]~', '', $versionNormalized);
return $versionNormalized === '5.6'
? BREW_PREFIX.'/etc/php/5.6/php-fpm.conf'
@@ -171,14 +169,26 @@ function stopRunning()
* Use a specific version of php
*
* @param $version
* @param $force
* @return string
*/
function useVersion($version)
function useVersion($version, $force = false)
{
$version = $this->validateRequestedVersion($version);
// Install the relevant formula if not already installed
$this->brew->ensureInstalled($version);
try {
if ($this->brew->linkedPhp() === $version && !$force) {
output(sprintf('<info>Valet is already using version: <comment>%s</comment>.</info> To re-link and re-configure use the --force parameter.' . PHP_EOL,
$version));
exit();
}
} catch (DomainException $e)
{ /* ignore thrown exception when no linked php is found */ }
if (!$this->brew->installed($version)) {
// Install the relevant formula if not already installed
$this->brew->ensureInstalled($version, [], $this->taps);
}
// Unlink the current php if there is one
if ($this->brew->hasLinkedPhp()) {
@@ -190,11 +200,23 @@ function useVersion($version)
info(sprintf('Linking new version: %s', $version));
$this->brew->link($version, true);
$this->stopRunning();
// ensure configuration is correct and start the linked version
$this->install();
return $version === 'php' ? $this->brew->determineAliasedVersion($version) : $version;
}
/**
* If passed php7.4 or php74 formats, normalize to php@7.4 format.
*/
function normalizePhpVersion($version)
{
return preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version);
}
/**
* Validate the requested version to be sure we can support it.
*
@@ -203,18 +225,7 @@ function useVersion($version)
*/
function validateRequestedVersion($version)
{
// If passed php7.2 or php72 formats, normalize to php@7.2 format:
$version = preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version);
if ($version === 'php') {
if (strpos($this->brew->determineAliasedVersion($version), '@')) {
return $version;
}
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');
}
}
$version = $this->normalizePhpVersion($version);
if (!$this->brew->supportedPhpVersions()->contains($version)) {
throw new DomainException(
@@ -225,6 +236,20 @@ function validateRequestedVersion($version)
);
}
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
return $aliasedVersion;
}
if ($version === 'php') {
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
return $aliasedVersion;
}
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');
}
}
return $version;
}
}

View File

@@ -172,6 +172,26 @@ function ends_with($haystack, $needles) {
}
}
if (! function_exists('starts_with')) {
/**
* Determine if a given string starts with a given substring.
*
* @param string $haystack
* @param string|string[] $needles
* @return bool
*/
function starts_with($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
return true;
}
}
return false;
}
}
/**
* Get the user
*/

View File

@@ -1,4 +1,4 @@
# php-fpm error logging directives
; php-fpm error logging directives
error_log="VALET_HOME_PATH/Log/php-fpm.log"
log_errors=on

View File

@@ -32,7 +32,7 @@
*/
Container::setInstance(new Container);
$version = '2.13.4';
$version = '2.13.15';
$app = new Application('Laravel Valet', $version);
@@ -464,11 +464,14 @@
/**
* Allow the user to change the version of php valet uses
*/
$app->command('use phpVersion', function ($phpVersion) {
$app->command('use [phpVersion] [--force]', function ($phpVersion, $force) {
if (!$phpVersion) {
return info('Valet is using ' . Brew::linkedPhp());
}
PhpFpm::validateRequestedVersion($phpVersion);
PhpFpm::stopRunning();
$newVersion = PhpFpm::useVersion($phpVersion);
$newVersion = PhpFpm::useVersion($phpVersion, $force);
Nginx::restart();
info(sprintf('Valet is now using %s.', $newVersion) . PHP_EOL);

View File

@@ -33,7 +33,7 @@
},
"require-dev": {
"mockery/mockery": "^1.2.3",
"phpunit/phpunit": "~5.7"
"yoast/phpunit-polyfills": "^0.2.0"
},
"bin": [
"valet"

View File

@@ -7,7 +7,9 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="false"
beStrictAboutTestsThatDoNotTestAnything="false"
>
<testsuites>
<testsuite name="Valet Test Suite">
<directory suffix="Test.php">./tests</directory>

View File

@@ -9,193 +9,99 @@
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
class BrewTest extends PHPUnit_Framework_TestCase
class BrewTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function setUp()
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
}
public function tearDown()
public function tear_down()
{
Mockery::close();
}
public function test_brew_can_be_resolved_from_container()
{
$this->assertInstanceOf(Brew::class, resolve(Brew::class));
}
public function test_installed_returns_true_when_given_formula_is_installed()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn('php@7.4');
$cli->shouldReceive('runAsUser')->once()->with('brew info php@7.4 --json')
->andReturn('[{"name":"php@7.4","full_name":"php@7.4","aliases":[],"versioned_formulae":[],"versions":{"stable":"7.4.5"},"installed":[{"version":"7.4.5"}]}]');
swap(CommandLine::class, $cli);
$this->assertTrue(resolve(Brew::class)->installed('php@7.4'));
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php')->andReturn('php
php@7.4');
$cli->shouldReceive('runAsUser')->once()->with('brew info php --json')
->andReturn('[{"name":"php","full_name":"php","aliases":["php@8.0"],"versioned_formulae":[],"versions":{"stable":"8.0.0"},"installed":[{"version":"8.0.0"}]}]');
swap(CommandLine::class, $cli);
$this->assertTrue(resolve(Brew::class)->installed('php'));
}
public function test_installed_returns_false_when_given_formula_is_not_installed()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn('');
$cli->shouldReceive('runAsUser')->once()->with('brew info php@7.4 --json')->andReturn('');
swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php@7.4'));
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn('php');
swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php@7.4'));
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep php@7.4')->andReturn('php
something-else-php@7.4
php7');
$cli->shouldReceive('runAsUser')->once()->with('brew info php@7.4 --json')->andReturn('Error: No formula found');
swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php@7.4'));
}
public function test_has_installed_php_indicates_if_php_is_installed_via_brew()
{
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(true);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(true);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(true);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(true);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(true);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(true);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(true);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(true);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(true);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(true);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(true);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(true);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(true);
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installed]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installed')->with('php')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@8.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.4')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.3')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.2')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.1')->andReturn(false);
$brew->shouldReceive('installed')->with('php@7.0')->andReturn(false);
$brew->shouldReceive('installed')->with('php@5.6')->andReturn(false);
$brew->shouldReceive('installed')->with('php74')->andReturn(false);
$brew->shouldReceive('installed')->with('php73')->andReturn(false);
$brew->shouldReceive('installed')->with('php72')->andReturn(false);
$brew->shouldReceive('installed')->with('php71')->andReturn(false);
$brew->shouldReceive('installed')->with('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@5.5']));
$this->assertFalse($brew->hasInstalledPhp());
}
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@8.1']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@8.0']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.4']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.3']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php73']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.2', 'php72']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php71', 'php@7.1']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@7.0']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php@5.6']));
$this->assertTrue($brew->hasInstalledPhp());
$brew = Mockery::mock(Brew::class.'[installedPhpFormulae]', [new CommandLine, new Filesystem]);
$brew->shouldReceive('installedPhpFormulae')->andReturn(collect(['php56']));
$this->assertTrue($brew->hasInstalledPhp());
}
public function test_tap_taps_the_given_homebrew_repository()
{
@@ -207,28 +113,25 @@ public function test_tap_taps_the_given_homebrew_repository()
resolve(Brew::class)->tap('php@7.1', 'php@7.0', 'php@5.6');
}
public function test_restart_restarts_the_service_using_homebrew_services()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep dnsmasq')->andReturn('dnsmasq');
$cli->shouldReceive('runAsUser')->once()->with('brew info dnsmasq --json')->andReturn('[{"name":"dnsmasq","full_name":"dnsmasq","aliases":[],"versioned_formulae":[],"versions":{"stable":"1"},"installed":[{"version":"1"}]}]');
$cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq');
$cli->shouldReceive('quietly')->once()->with('sudo brew services start dnsmasq');
swap(CommandLine::class, $cli);
resolve(Brew::class)->restartService('dnsmasq');
}
public function test_stop_stops_the_service_using_homebrew_services()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew list --formula | grep dnsmasq')->andReturn('dnsmasq');
$cli->shouldReceive('runAsUser')->once()->with('brew info dnsmasq --json')->andReturn('[{"name":"dnsmasq","full_name":"dnsmasq","aliases":[],"versioned_formulae":[],"versions":{"stable":"1"},"installed":[{"version":"1"}]}]');
$cli->shouldReceive('quietly')->once()->with('sudo brew services stop dnsmasq');
swap(CommandLine::class, $cli);
resolve(Brew::class)->stopService('dnsmasq');
}
public function test_linked_php_returns_linked_php_formula_name()
{
$getBrewMock = function ($filesystem) {
@@ -262,18 +165,15 @@ public function test_linked_php_returns_linked_php_formula_name()
$this->assertSame('php@5.6', $getBrewMock($files)->linkedPhp());
}
/**
* @expectedException DomainException
*/
public function test_linked_php_throws_exception_if_no_php_link()
{
$this->expectException(DomainException::class);
$brewMock = Mockery::mock(Brew::class)->makePartial();
$brewMock->shouldReceive('hasLinkedPhp')->once()->andReturn(false);
$brewMock->linkedPhp();
}
public function test_has_linked_php_returns_true_if_php_link_exists()
{
$files = Mockery::mock(Filesystem::class);
@@ -285,12 +185,10 @@ public function test_has_linked_php_returns_true_if_php_link_exists()
$this->assertTrue($brew->hasLinkedPhp());
}
/**
* @expectedException DomainException
*/
public function test_linked_php_throws_exception_if_unsupported_php_version_is_linked()
{
$this->expectException(DomainException::class);
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->once()->with(BREW_PREFIX.'/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->once()->with(BREW_PREFIX.'/bin/php')->andReturn('/test/path/php/5.4.14/test');
@@ -298,8 +196,7 @@ public function test_linked_php_throws_exception_if_unsupported_php_version_is_l
resolve(Brew::class)->linkedPhp();
}
public function test_install_or_fail_will_install_brew_formulas()
public function test_install_or_fail_will_install_brew_formulae()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->with('brew install dnsmasq', Mockery::type('Closure'));
@@ -307,7 +204,6 @@ public function test_install_or_fail_will_install_brew_formulas()
resolve(Brew::class)->installOrFail('dnsmasq');
}
public function test_install_or_fail_can_install_taps()
{
$cli = Mockery::mock(CommandLine::class);
@@ -318,12 +214,10 @@ public function test_install_or_fail_can_install_taps()
$brew->installOrFail('dnsmasq', [], ['test/tap']);
}
/**
* @expectedException DomainException
*/
public function test_install_or_fail_throws_exception_on_failure()
{
$this->expectException(DomainException::class);
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->andReturnUsing(function ($command, $onError) {
$onError(1, 'test error ouput');
@@ -332,11 +226,10 @@ public function test_install_or_fail_throws_exception_on_failure()
resolve(Brew::class)->installOrFail('dnsmasq');
}
/**
* @expectedException DomainException
*/
public function test_link_will_throw_exception_on_failure()
{
$this->expectException(DomainException::class);
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew link aformula',
@@ -372,11 +265,10 @@ public function test_link_will_pass_formula_and_force_to_run_as_user_if_set()
$this->assertSame('Some output forced', resolve(Brew::class)->link('aformula', true));
}
/**
* @expectedException DomainException
*/
public function test_unlink_will_throw_exception_on_failure()
{
$this->expectException(DomainException::class);
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew unlink aformula',
@@ -400,11 +292,10 @@ public function test_unlink_will_pass_formula_to_run_as_user()
$this->assertSame('Some output', resolve(Brew::class)->unlink('aformula'));
}
/**
* @expectedException DomainException
*/
public function test_getRunningServices_will_throw_exception_on_failure()
{
$this->expectException(DomainException::class);
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('runAsUser')->once()->withArgs([
'brew services list | grep started | awk \'{ print $1; }\'',
@@ -485,48 +376,48 @@ public function supportedPhpLinkPathProvider()
{
return [
[
'/test/path/php/7.3.0/test', // linked path
'/test/path/php/7.4.0/test', // linked path
[ // matches
'path/php/7.3.0/test',
'path/php/7.4.0/test',
'php',
'',
'7.3',
'7.4',
'.0',
],
'php', // expected link formula
],
[
'/test/path/php@7.2/7.2.13/test',
'/test/path/php@7.4/7.4.13/test',
[
'path/php@7.2/7.2.13/test',
'path/php@7.4/7.4.13/test',
'php',
'@7.2',
'7.2',
'@7.4',
'7.4',
'.13',
],
'php@7.2'
'php@7.4'
],
[
'/test/path/php/7.2.9_2/test',
'/test/path/php/7.4.9_2/test',
[
'path/php/7.2.9_2/test',
'path/php/7.4.9_2/test',
'php',
'',
'7.2',
'7.4',
'.9_2',
],
'php',
],
[
'/test/path/php72/7.2.9_2/test',
'/test/path/php74/7.4.9_2/test',
[
'path/php72/7.2.9_2/test',
'path/php74/7.4.9_2/test',
'php',
'72',
'7.2',
'74',
'7.4',
'.9_2',
],
'php72',
'php74',
],
[
'/test/path/php56/test',

View File

@@ -9,22 +9,20 @@
use function Valet\swap;
use Illuminate\Container\Container;
class ConfigurationTest extends PHPUnit_Framework_TestCase
class ConfigurationTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function setUp()
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
}
public function tearDown()
public function tear_down()
{
Mockery::close();
}
public function test_configuration_directory_is_created_if_it_doesnt_exist()
{
$files = Mockery::mock(Filesystem::class.'[ensureDirExists,isDir]');
@@ -35,7 +33,6 @@ public function test_configuration_directory_is_created_if_it_doesnt_exist()
resolve(Configuration::class)->createConfigurationDirectory();
}
public function test_drivers_directory_is_created_with_sample_driver_if_it_doesnt_exist()
{
$files = Mockery::mock(Filesystem::class.'[isDir,mkdirAsUser,putAsUser]');
@@ -76,7 +73,6 @@ public function test_add_path_adds_a_path_to_the_paths_array_and_removes_duplica
$config->addPath('path-3');
}
public function test_paths_may_be_removed_from_the_configuration()
{
$config = Mockery::mock(Configuration::class.'[read,write]', [new Filesystem]);
@@ -89,7 +85,6 @@ public function test_paths_may_be_removed_from_the_configuration()
$config->removePath('path-2');
}
public function test_prune_removes_directories_from_paths_that_no_longer_exist()
{
$files = Mockery::mock(Filesystem::class.'[exists,isDir]');
@@ -107,7 +102,6 @@ public function test_prune_removes_directories_from_paths_that_no_longer_exist()
$config->prune();
}
public function test_prune_doesnt_execute_if_configuration_directory_doesnt_exist()
{
$files = Mockery::mock(Filesystem::class.'[exists]');
@@ -119,7 +113,6 @@ public function test_prune_doesnt_execute_if_configuration_directory_doesnt_exis
$config->prune();
}
public function test_update_key_updates_the_specified_configuration_key()
{
$config = Mockery::mock(Configuration::class.'[read,write]', [new Filesystem]);
@@ -128,7 +121,6 @@ public function test_update_key_updates_the_specified_configuration_key()
$config->updateKey('bar', 'baz');
}
public function test_trust_adds_the_sudoer_files()
{
$files = Mockery::mock(Filesystem::class.'[ensureDirExists,put]');

View File

@@ -10,17 +10,16 @@
use function Valet\swap;
use Illuminate\Container\Container;
class DnsMasqTest extends PHPUnit_Framework_TestCase
class DnsMasqTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function setUp()
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
}
public function tearDown()
public function tear_down()
{
exec('rm -rf '.__DIR__.'/output');
mkdir(__DIR__.'/output');
@@ -29,7 +28,6 @@ public function tearDown()
Mockery::close();
}
public function test_install_installs_and_places_configuration_files_in_proper_locations()
{
$brew = Mockery::mock(Brew::class);
@@ -56,7 +54,6 @@ public function test_install_installs_and_places_configuration_files_in_proper_l
);
}
public function test_update_tld_removes_old_resolver_and_reinstalls()
{
$cli = Mockery::mock(CommandLine::class);
@@ -68,7 +65,6 @@ public function test_update_tld_removes_old_resolver_and_reinstalls()
}
}
class StubForCreatingCustomDnsMasqConfigFiles extends DnsMasq
{
public function dnsmasqUserConfigDir()

View File

@@ -2,16 +2,15 @@
use Valet\Filesystem;
class FilesystemTest extends PHPUnit_Framework_TestCase
class FilesystemTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function tearDown()
public function tear_down()
{
exec('rm -rf '.__DIR__.'/output');
mkdir(__DIR__.'/output');
touch(__DIR__.'/output/.gitkeep');
}
public function test_remove_broken_links_removes_broken_symlinks()
{
$files = new Filesystem;
@@ -20,6 +19,6 @@ public function test_remove_broken_links_removes_broken_symlinks()
$this->assertFileExists(__DIR__.'/output/file.link');
unlink(__DIR__.'/output/file.out');
$files->removeBrokenLinksAt(__DIR__.'/output');
$this->assertFileNotExists(__DIR__.'/output/file.link');
$this->assertFileDoesNotExist(__DIR__.'/output/file.link');
}
}

View File

@@ -9,29 +9,27 @@
use function Valet\swap;
use Illuminate\Container\Container;
class NginxTest extends PHPUnit_Framework_TestCase
class NginxTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function setUp()
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
}
public function tearDown()
public function tear_down()
{
Mockery::close();
}
public function test_install_nginx_configuration_places_nginx_base_configuration_in_proper_location()
{
$files = Mockery::mock(Filesystem::class.'[putAsUser]');
$files->shouldReceive('putAsUser')->andReturnUsing(function ($path, $contents) {
$this->assertSame(BREW_PREFIX.'/etc/nginx/nginx.conf', $path);
$this->assertContains('include "'.VALET_HOME_PATH.'/Nginx/*"', $contents);
$this->assertStringContainsString('include "'.VALET_HOME_PATH.'/Nginx/*"', $contents);
})->once();
swap(Filesystem::class, $files);
@@ -40,7 +38,6 @@ public function test_install_nginx_configuration_places_nginx_base_configuration
$nginx->installConfiguration();
}
public function test_install_nginx_directories_creates_location_for_site_specific_configuration()
{
$files = Mockery::mock(Filesystem::class);
@@ -56,7 +53,6 @@ public function test_install_nginx_directories_creates_location_for_site_specifi
$nginx->installNginxDirectory();
}
public function test_nginx_directory_is_never_created_if_it_already_exists()
{
$files = Mockery::mock(Filesystem::class);
@@ -72,7 +68,6 @@ public function test_nginx_directory_is_never_created_if_it_already_exists()
$nginx->installNginxDirectory();
}
public function test_install_nginx_directories_rewrites_secure_nginx_files()
{
$files = Mockery::mock(Filesystem::class);
@@ -89,5 +84,4 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files()
$site->shouldHaveReceived('resecureForNewTld', ['test', 'test']);
}
}

View File

@@ -9,17 +9,16 @@
use function Valet\resolve;
use Illuminate\Container\Container;
class PhpFpmTest extends PHPUnit_Framework_TestCase
class PhpFpmTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function setUp()
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
}
public function tearDown()
public function tear_down()
{
exec('rm -rf '.__DIR__.'/output');
mkdir(__DIR__.'/output');
@@ -35,9 +34,9 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port()
copy(__DIR__.'/files/php-memory-limits.ini', __DIR__.'/output/conf.d/php-memory-limits.ini');
resolve(StubForUpdatingFpmConfigFiles::class)->updateConfiguration();
$contents = file_get_contents(__DIR__.'/output/fpm.conf');
$this->assertContains(sprintf("\nuser = %s", user()), $contents);
$this->assertContains("\ngroup = staff", $contents);
$this->assertContains("\nlisten = ".VALET_HOME_PATH."/valet.sock", $contents);
$this->assertStringContainsString(sprintf("\nuser = %s", user()), $contents);
$this->assertStringContainsString("\ngroup = staff", $contents);
$this->assertStringContainsString("\nlisten = ".VALET_HOME_PATH."/valet.sock", $contents);
}
public function test_stopRunning_will_pass_filtered_result_of_getRunningServices_to_stopService()
@@ -79,19 +78,23 @@ public function test_use_version_will_convert_passed_php_version()
'php@5.6',
]));
$brewMock->shouldReceive('hasLinkedPhp')->andReturn(false);
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2');
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2', [], $phpFpmMock->taps);
$brewMock->shouldReceive('determineAliasedVersion')->with('php@7.2')->andReturn('php@7.2');
$brewMock->shouldReceive('link')->withArgs(['php@7.2', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');
// Test both non prefixed and prefixed
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php7.2'));
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php72'));
}
/**
* @expectedException DomainException
*/
public function test_use_version_will_throw_if_version_not_supported()
{
$this->expectException(DomainException::class);
$brewMock = Mockery::mock(Brew::class);
swap(Brew::class, $brewMock);
@@ -120,8 +123,13 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
$brewMock->shouldReceive('hasLinkedPhp')->andReturn(true);
$brewMock->shouldReceive('getLinkedPhpFormula')->andReturn('php@7.1');
$brewMock->shouldReceive('unlink')->with('php@7.1');
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2');
$brewMock->shouldReceive('ensureInstalled')->with('php@7.2', [], $phpFpmMock->taps);
$brewMock->shouldReceive('determineAliasedVersion')->with('php@7.2')->andReturn('php@7.2');
$brewMock->shouldReceive('link')->withArgs(['php@7.2', true]);
$brewMock->shouldReceive('linkedPhp');
$brewMock->shouldReceive('installed');
$brewMock->shouldReceive('getRunningServices')->andReturn(collect());
$brewMock->shouldReceive('stopService');
// Test both non prefixed and prefixed
$this->assertSame('php@7.2', $phpFpmMock->useVersion('php@7.2'));

View File

@@ -9,17 +9,16 @@
use function Valet\swap;
use Illuminate\Container\Container;
class SiteTest extends PHPUnit_Framework_TestCase
class SiteTest extends Yoast\PHPUnitPolyfills\TestCases\TestCase
{
public function setUp()
public function set_up()
{
$_SERVER['SUDO_USER'] = user();
Container::setInstance(new Container);
}
public function tearDown()
public function tear_down()
{
exec('rm -rf '.__DIR__.'/output');
mkdir(__DIR__.'/output');
@@ -28,7 +27,6 @@ public function tearDown()
Mockery::close();
}
public function test_get_certificates_will_return_with_multi_segment_tld()
{
$files = Mockery::mock(Filesystem::class);
@@ -53,7 +51,6 @@ public function test_get_certificates_will_return_with_multi_segment_tld()
$this->assertSame(['helloworld' => 0], $certs->all());
}
public function test_get_sites_will_return_if_secured()
{
$files = Mockery::mock(Filesystem::class);
@@ -108,7 +105,6 @@ public function test_get_sites_will_return_if_secured()
], $sites->last());
}
public function test_get_sites_will_work_with_non_symlinked_path()
{
$files = Mockery::mock(Filesystem::class);
@@ -151,7 +147,6 @@ public function test_get_sites_will_work_with_non_symlinked_path()
], $sites->first());
}
public function test_get_sites_will_not_return_if_path_is_not_directory()
{
$files = Mockery::mock(Filesystem::class);
@@ -189,7 +184,6 @@ public function test_get_sites_will_not_return_if_path_is_not_directory()
], $sites->first());
}
public function test_get_sites_will_work_with_symlinked_path()
{
$files = Mockery::mock(Filesystem::class);
@@ -232,7 +226,6 @@ public function test_get_sites_will_work_with_symlinked_path()
], $sites->first());
}
public function test_symlink_creates_symlink_to_given_path()
{
$files = Mockery::mock(Filesystem::class);
@@ -248,21 +241,19 @@ public function test_symlink_creates_symlink_to_given_path()
$this->assertSame(VALET_HOME_PATH.'/Sites/link', $linkPath);
}
public function test_unlink_removes_existing_symlink()
{
file_put_contents(__DIR__.'/output/file.out', 'test');
symlink(__DIR__.'/output/file.out', __DIR__.'/output/link');
$site = resolve(StubForRemovingLinks::class);
$site->unlink('link');
$this->assertFileNotExists(__DIR__.'/output/link');
$this->assertFileDoesNotExist(__DIR__.'/output/link');
$site = resolve(StubForRemovingLinks::class);
$site->unlink('link');
$this->assertFileNotExists(__DIR__.'/output/link');
$this->assertFileDoesNotExist(__DIR__.'/output/link');
}
public function test_prune_links_removes_broken_symlinks_in_sites_path()
{
file_put_contents(__DIR__.'/output/file.out', 'test');
@@ -270,10 +261,9 @@ public function test_prune_links_removes_broken_symlinks_in_sites_path()
unlink(__DIR__.'/output/file.out');
$site = resolve(StubForRemovingLinks::class);
$site->pruneLinks();
$this->assertFileNotExists(__DIR__.'/output/link');
$this->assertFileDoesNotExist(__DIR__.'/output/link');
}
public function test_certificates_trim_tld_for_custom_tlds()
{
$files = Mockery::mock(Filesystem::class);
@@ -300,7 +290,6 @@ public function test_certificates_trim_tld_for_custom_tlds()
$this->assertEquals('fiveletters', $certs->last());
}
public function test_no_proxies()
{
$config = Mockery::mock(Configuration::class);
@@ -317,7 +306,6 @@ public function test_no_proxies()
$this->assertEquals([], $site->proxies()->all());
}
public function test_lists_proxies()
{
$config = Mockery::mock(Configuration::class);
@@ -347,7 +335,6 @@ public function test_lists_proxies()
], $site->proxies()->all());
}
public function test_add_proxy()
{
$config = Mockery::mock(Configuration::class);
@@ -460,7 +447,6 @@ public function test_add_proxy_clears_previous_proxy_certificate()
], $site->proxies()->all());
}
public function test_add_proxy_clears_previous_non_proxy_certificate()
{
$config = Mockery::mock(Configuration::class);
@@ -501,7 +487,6 @@ public function test_add_proxy_clears_previous_non_proxy_certificate()
], $site->proxies()->all());
}
public function test_remove_proxy()
{
$config = Mockery::mock(Configuration::class);
@@ -636,7 +621,7 @@ public function assertNginxExists($urlWithTld)
public function assertNginxNotExists($urlWithTld)
{
SiteTest::assertFileNotExists($this->nginxPath($urlWithTld));
SiteTest::assertFileDoesNotExist($this->nginxPath($urlWithTld));
}
public function assertCertificateExists($urlWithTld)
@@ -647,8 +632,8 @@ public function assertCertificateExists($urlWithTld)
public function assertCertificateNotExists($urlWithTld)
{
SiteTest::assertFileNotExists($this->certificatesPath($urlWithTld, 'crt'));
SiteTest::assertFileNotExists($this->certificatesPath($urlWithTld, 'key'));
SiteTest::assertFileDoesNotExist($this->certificatesPath($urlWithTld, 'crt'));
SiteTest::assertFileDoesNotExist($this->certificatesPath($urlWithTld, 'key'));
}
public function assertCertificateExistsWithCounterValue($urlWithTld, $counter)