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

progress refactoring

This commit is contained in:
Taylor Otwell
2016-05-07 22:10:32 -05:00
parent 9f4896399e
commit 354e1d4111
12 changed files with 901 additions and 159 deletions

152
tests/BrewTest.php Normal file
View File

@@ -0,0 +1,152 @@
<?php
use Valet\Brew;
use Valet\Filesystem;
use Valet\CommandLine;
use Illuminate\Container\Container;
class BrewTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$_SERVER['SUDO_USER'] = 'Taylor';
Container::setInstance(new Container);
}
public function tearDown()
{
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('run')->with('brew list | grep php70')->andReturn('php70');
swap(CommandLine::class, $cli);
$this->assertTrue(resolve(Brew::class)->installed('php70'));
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('php70-mcrypt
php70');
swap(CommandLine::class, $cli);
$this->assertTrue(resolve(Brew::class)->installed('php70'));
}
public function test_installed_returns_false_when_given_formula_is_not_installed()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('');
swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php70'));
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('php70-mcrypt');
swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php70'));
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('run')->with('brew list | grep php70')->andReturn('php70-mcrypt
php70-something-else
php7');
swap(CommandLine::class, $cli);
$this->assertFalse(resolve(Brew::class)->installed('php70'));
}
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('php70')->andReturn(true);
$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('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('php70')->andReturn(false);
$brew->shouldReceive('installed')->with('php56')->andReturn(false);
$this->assertFalse($brew->hasInstalledPhp());
}
public function test_tap_taps_the_given_homebrew_repository()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('passthru')->with('sudo -u Taylor brew tap php70');
$cli->shouldReceive('passthru')->with('sudo -u Taylor brew tap php56');
swap(CommandLine::class, $cli);
resolve(Brew::class)->tap('php70', 'php56');
}
public function test_restart_restarts_the_service_using_homebrew_services()
{
$cli = Mockery::mock(CommandLine::class);
$cli->shouldReceive('quietly')->with('sudo brew services restart 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('quietly')->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()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->with('/usr/local/bin/php')->andReturn('/test/path/php70/test');
swap(Filesystem::class, $files);
$this->assertEquals('php70', resolve(Brew::class)->linkedPhp());
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->with('/usr/local/bin/php')->andReturn('/test/path/php56/test');
swap(Filesystem::class, $files);
$this->assertEquals('php56', resolve(Brew::class)->linkedPhp());
}
/**
* @expectedException DomainException
*/
public function test_linked_php_throws_exception_if_no_php_link()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(false);
swap(Filesystem::class, $files);
resolve(Brew::class)->linkedPhp();
}
/**
* @expectedException DomainException
*/
public function test_linked_php_throws_exception_if_unsupported_php_version_is_linked()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->with('/usr/local/bin/php')->andReturn('/test/path/php42/test');
swap(Filesystem::class, $files);
resolve(Brew::class)->linkedPhp();
}
}

81
tests/CaddyTest.php Normal file
View File

@@ -0,0 +1,81 @@
<?php
use Valet\Caddy;
use Valet\Filesystem;
use Valet\CommandLine;
use Illuminate\Container\Container;
class CaddyTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$_SERVER['SUDO_USER'] = 'Taylor';
Container::setInstance(new Container);
}
public function tearDown()
{
Mockery::close();
}
public function test_install_caddy_file_places_stub_in_valet_home_directory()
{
$files = Mockery::mock(Filesystem::class.'[putAsUser]');
$files->shouldReceive('putAsUser')->andReturnUsing(function ($path, $contents) {
$this->assertEquals(VALET_HOME_PATH.'/Caddyfile', $path);
$this->assertTrue(strpos($contents, 'import /Users/'.user().'/.valet/Caddy/*') !== false);
});
swap(Filesystem::class, $files);
$caddy = resolve(Caddy::class);
$caddy->installCaddyFile();
}
public function test_install_caddy_directories_creates_location_for_site_specific_configuration()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Caddy')->andReturn(false);
$files->shouldReceive('mkdirAsUser')->with(VALET_HOME_PATH.'/Caddy');
$files->shouldReceive('touchAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep');
swap(Filesystem::class, $files);
$caddy = resolve(Caddy::class);
$caddy->installCaddyDirectory();
}
public function test_caddy_directory_is_never_created_if_it_already_exists()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Caddy')->andReturn(true);
$files->shouldReceive('mkdirAsUser')->never();
$files->shouldReceive('touchAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep');
swap(Filesystem::class, $files);
$caddy = resolve(Caddy::class);
$caddy->installCaddyDirectory();
}
public function test_caddy_daemon_is_placed_in_correct_location()
{
$files = Mockery::mock(Filesystem::class.'[put]');
swap(Filesystem::class, $files);
$caddy = resolve(Caddy::class);
$files->shouldReceive('put')->andReturnUsing(function ($path, $contents) use ($caddy) {
$this->assertEquals($caddy->daemonPath, $path);
$this->assertTrue(strpos($contents, VALET_HOME_PATH) !== false);
});
$caddy->installCaddyDaemon();
}
}

122
tests/ConfigurationTest.php Normal file
View File

@@ -0,0 +1,122 @@
<?php
use Valet\Filesystem;
use Valet\Configuration;
use Illuminate\Container\Container;
class ConfigurationTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
$_SERVER['SUDO_USER'] = 'Taylor';
Container::setInstance(new Container);
}
public function tearDown()
{
Mockery::close();
}
public function test_configuration_directory_is_created_if_it_doesnt_exist()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isDir')->with(VALET_HOME_PATH)->andReturn(false);
$files->shouldReceive('mkdirAsUser')->with(VALET_HOME_PATH);
swap(Filesystem::class, $files);
resolve(Configuration::class)->createConfigurationDirectory();
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isDir')->with(VALET_HOME_PATH)->andReturn(true);
$files->shouldReceive('mkdirAsUser')->never();
swap(Filesystem::class, $files);
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]');
$files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Drivers')->andReturn(false);
$files->shouldReceive('mkdirAsUser')->with(VALET_HOME_PATH.'/Drivers');
$files->shouldReceive('putAsUser');
swap(Filesystem::class, $files);
resolve(Configuration::class)->createDriversDirectory();
}
public function test_add_path_adds_a_path_to_the_paths_array_and_removes_duplicates()
{
$config = Mockery::mock(Configuration::class.'[read,write]', [new Filesystem]);
$config->shouldReceive('read')->andReturn([
'paths' => ['path-1', 'path-2'],
]);
$config->shouldReceive('write')->with([
'paths' => ['path-1', 'path-2', 'path-3'],
]);
$config->addPath('path-3');
$config = Mockery::mock(Configuration::class.'[read,write]', [new Filesystem]);
$config->shouldReceive('read')->andReturn([
'paths' => ['path-1', 'path-2', 'path-3'],
]);
$config->shouldReceive('write')->with([
'paths' => ['path-1', 'path-2', 'path-3'],
]);
$config->addPath('path-3');
}
public function test_paths_may_be_removed_from_the_configuration()
{
$config = Mockery::mock(Configuration::class.'[read,write]', [new Filesystem]);
$config->shouldReceive('read')->andReturn([
'paths' => ['path-1', 'path-2'],
]);
$config->shouldReceive('write')->with([
'paths' => ['path-1'],
]);
$config->removePath('path-2');
}
public function test_prune_removes_directories_from_paths_that_no_longer_exist()
{
$files = Mockery::mock(Filesystem::class.'[exists,isDir]');
swap(Filesystem::class, $files);
$files->shouldReceive('exists')->with(VALET_HOME_PATH.'/config.json')->andReturn(true);
$files->shouldReceive('isDir')->with('path-1')->andReturn(true);
$files->shouldReceive('isDir')->with('path-2')->andReturn(false);
$config = Mockery::mock(Configuration::class.'[read,write]', [$files]);
$config->shouldReceive('read')->andReturn([
'paths' => ['path-1', 'path-2'],
]);
$config->shouldReceive('write')->with([
'paths' => ['path-1'],
]);
$config->prune();
}
public function test_prune_doesnt_execute_if_configuration_directory_doesnt_exist()
{
$files = Mockery::mock(Filesystem::class.'[exists]');
swap(Filesystem::class, $files);
$files->shouldReceive('exists')->with(VALET_HOME_PATH.'/config.json')->andReturn(false);
$config = Mockery::mock(Configuration::class.'[read,write]', [$files]);
$config->shouldReceive('read')->never();
$config->shouldReceive('write')->never();
$config->prune();
}
public function test_update_key_updates_the_specified_configuration_key()
{
$config = Mockery::mock(Configuration::class.'[read,write]', [new Filesystem]);
$config->shouldReceive('read')->once()->andReturn(['foo' => 'bar']);
$config->shouldReceive('write')->once()->with(['foo' => 'bar', 'bar' => 'baz']);
$config->updateKey('bar', 'baz');
}
}