diff --git a/cli/Valet/Configuration.php b/cli/Valet/Configuration.php index b4b1824..d2cc849 100644 --- a/cli/Valet/Configuration.php +++ b/cli/Valet/Configuration.php @@ -2,6 +2,8 @@ namespace Valet; +use Exception; + class Configuration { public function __construct(public Filesystem $files) @@ -18,7 +20,7 @@ public function install(): void $this->createSitesDirectory(); $this->createLogDirectory(); $this->createCertificatesDirectory(); - $this->writeBaseConfiguration(); + $this->ensureBaseConfiguration(); $this->files->chown($this->path(), user()); } @@ -84,7 +86,23 @@ public function createCertificatesDirectory(): void } /** - * Write the base, initial configuration for Valet. + * Ensure the base initial configuration has been installed. + */ + public function ensureBaseConfiguration(): void + { + $this->writeBaseConfiguration(); + + if (empty($this->read()['tld'])) { + $this->updateKey('tld', 'test'); + } + + if (empty($this->read()['loopback'])) { + $this->updateKey('loopback', '127.0.0.1'); + } + } + + /** + * Write the base initial configuration for Valet. */ public function writeBaseConfiguration(): void { diff --git a/cli/app.php b/cli/app.php index e2fad8e..3d1356d 100644 --- a/cli/app.php +++ b/cli/app.php @@ -100,9 +100,7 @@ function (ConsoleCommandEvent $event) { /** * Upgrade helper: ensure the tld config exists and the loopback config exists. */ - if (empty(Configuration::read()['tld']) || empty(Configuration::read()['loopback'])) { - Configuration::writeBaseConfiguration(); - } + Configuration::ensureBaseConfiguration(); /** * Get or set the TLD currently being used by Valet. diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 6af29ab..187e302 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -132,4 +132,14 @@ public function test_trust_adds_the_sudoer_files() resolve(Brew::class)->createSudoersEntry(); resolve(Valet::class)->createSudoersEntry(); } + + public function test_ensure_configuration_exists_writes_tld_and_loopback_if_empty() + { + $config = Mockery::mock(Configuration::class.'[writeBaseConfiguration,read,updateKey]', [new Filesystem]); + $config->shouldReceive('writeBaseConfiguration')->once(); + $config->shouldReceive('read')->times(2)->andReturn([]); + $config->shouldReceive('updateKey')->with('tld', 'test'); + $config->shouldReceive('updateKey')->with('loopback', '127.0.0.1'); + $config->ensureBaseConfiguration(); + } }