From c669a03d8b00e346236bdc5c1ddffb7607f82d8f Mon Sep 17 00:00:00 2001 From: Joe Ferguson Date: Wed, 2 Aug 2017 09:02:07 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9D=84=EF=B8=8F=20=F0=9F=9A=91=20=E2=9C=85?= =?UTF-8?q?=20Make=20command=20should=20respect=20Homestead=20config=20exa?= =?UTF-8?q?mple=20file=20(#624)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ❄️ 🚑 ✅ Make command should respect Homestead config example file The make command should now correctly copy over all site options and folder options from an existing example file. * Apply fixes from StyleCI (#623) * Contributions to #624 (#627) * Restore docblocks formatting This allows to maintain consistency with the docblocks style from the repositories laravel/laravel and laravel/framework. * Add more tests for shared folders configuration --- src/MakeCommand.php | 8 ++-- src/Settings/HomesteadSettings.php | 58 ++++++++++++++++++++++------- tests/MakeCommandTest.php | 20 +++++----- tests/Settings/JsonSettingsTest.php | 45 +++++++++++++++++++--- tests/Settings/YamlSettingsTest.php | 45 +++++++++++++++++++--- 5 files changed, 136 insertions(+), 40 deletions(-) diff --git a/src/MakeCommand.php b/src/MakeCommand.php index 0ee3bebe6..867a89f10 100644 --- a/src/MakeCommand.php +++ b/src/MakeCommand.php @@ -202,13 +202,13 @@ protected function createSettingsFile($format, $options) if (! $this->exampleSettingsExists($format)) { $settings->updateName($options['name']) - ->updateHostname($options['hostname']); + ->updateHostname($options['hostname']); } $settings->updateIpAddress($options['ip']) - ->configureSites($this->projectName, $this->defaultName) - ->configureSharedFolders($this->basePath, $this->defaultName) - ->save("{$this->basePath}/Homestead.{$format}"); + ->configureSites($this->projectName, $this->defaultName) + ->configureSharedFolders($this->basePath, $this->defaultName) + ->save("{$this->basePath}/Homestead.{$format}"); } /** diff --git a/src/Settings/HomesteadSettings.php b/src/Settings/HomesteadSettings.php index eb6fb8ea8..b9dce7830 100644 --- a/src/Settings/HomesteadSettings.php +++ b/src/Settings/HomesteadSettings.php @@ -100,22 +100,38 @@ public function updateIpAddress($ip) */ public function configureSites($projectName, $projectDirectory) { - $site = [ - 'map' => "{$projectName}.app", - 'to' => "/home/vagrant/{$projectDirectory}/public", + $sites = [ + [ + 'map' => "{$projectName}.app", + 'to' => "/home/vagrant/{$projectDirectory}/public", + ], ]; if (isset($this->attributes['sites']) && ! empty($this->attributes['sites'])) { - if (isset($this->attributes['sites'][0]['type'])) { - $site['type'] = $this->attributes['sites'][0]['type']; - } - - if (isset($this->attributes['sites'][0]['schedule'])) { - $site['schedule'] = $this->attributes['sites'][0]['schedule']; + foreach ($this->attributes['sites'] as $index => $user_site) { + if (isset($user_site['map'])) { + $sites[$index]['map'] = $user_site['map']; + } + + if (isset($user_site['to'])) { + $sites[$index]['to'] = $user_site['to']; + } + + if (isset($user_site['type'])) { + $sites[$index]['type'] = $user_site['type']; + } + + if (isset($user_site['schedule'])) { + $sites[$index]['schedule'] = $user_site['schedule']; + } + + if (isset($user_site['php'])) { + $sites[$index]['php'] = $user_site['php']; + } } } - $this->update(['sites' => [$site]]); + $this->update(['sites' => $sites]); return $this; } @@ -129,12 +145,26 @@ public function configureSites($projectName, $projectDirectory) */ public function configureSharedFolders($projectPath, $projectDirectory) { - $folder = [ - 'map' => $projectPath, - 'to' => "/home/vagrant/{$projectDirectory}", + $folders = [ + [ + 'map' => $projectPath, + 'to' => "/home/vagrant/{$projectDirectory}", + ], ]; - $this->update(['folders' => [$folder]]); + if (isset($this->attributes['folders']) && ! empty($this->attributes['folders'])) { + foreach ($this->attributes['folders'] as $index => $user_folder) { + if (isset($user_folder['to'])) { + $folders[$index]['to'] = $user_folder['to']; + } + + if (isset($user_folder['type'])) { + $folders[$index]['type'] = $user_folder['type']; + } + } + } + + $this->update(['folders' => $folders]); return $this; } diff --git a/tests/MakeCommandTest.php b/tests/MakeCommandTest.php index 8651c54ba..6600b6ba8 100644 --- a/tests/MakeCommandTest.php +++ b/tests/MakeCommandTest.php @@ -431,13 +431,11 @@ public function a_homestead_yaml_settings_has_preconfigured_sites() $this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml'); - $projectDirectory = basename(getcwd()); - $projectName = $this->slug($projectDirectory); $settings = Yaml::parse(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.yaml')); $this->assertEquals([ - 'map' => "{$projectDirectory}.app", - 'to' => "/home/vagrant/{$projectName}/public", + 'map' => 'homestead.app', + 'to' => '/home/vagrant/Code/Laravel/public', ], $settings['sites'][0]); } @@ -452,13 +450,11 @@ public function a_homestead_json_settings_has_preconfigured_sites() $this->assertFileExists(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json'); - $projectDirectory = basename(getcwd()); - $projectName = $this->slug($projectDirectory); $settings = json_decode(file_get_contents(self::$testDirectory.DIRECTORY_SEPARATOR.'Homestead.json'), true); $this->assertEquals([ - 'map' => "{$projectDirectory}.app", - 'to' => "/home/vagrant/{$projectName}/public", + 'map' => 'homestead.app', + 'to' => '/home/vagrant/Code/Laravel/public', ], $settings['sites'][0]); } @@ -485,7 +481,9 @@ public function a_homestead_yaml_settings_has_preconfigured_shared_folders() // The curious thing is that both directories point to the same location. // $this->assertRegExp("/{$projectDirectory}/", $settings['folders'][0]['map']); - $this->assertEquals("/home/vagrant/{$projectName}", $settings['folders'][0]['to']); + $this->assertEquals('/home/vagrant/Code', $settings['folders'][0]['to']); + $this->assertEquals($projectName, $settings['name']); + $this->assertEquals($projectName, $settings['hostname']); } /** @test */ @@ -513,7 +511,9 @@ public function a_homestead_json_settings_has_preconfigured_shared_folders() // The curious thing is that both directories point to the same location. // $this->assertRegExp("/{$projectDirectory}/", $settings['folders'][0]['map']); - $this->assertEquals("/home/vagrant/{$projectName}", $settings['folders'][0]['to']); + $this->assertEquals('/home/vagrant/Code', $settings['folders'][0]['to']); + $this->assertEquals($projectName, $settings['name']); + $this->assertEquals($projectName, $settings['hostname']); } /** @test */ diff --git a/tests/Settings/JsonSettingsTest.php b/tests/Settings/JsonSettingsTest.php index 80cc03fa3..1c0e69fa3 100644 --- a/tests/Settings/JsonSettingsTest.php +++ b/tests/Settings/JsonSettingsTest.php @@ -116,7 +116,7 @@ public function it_can_update_its_ip_address() } /** @test */ - public function it_can_configure_its_sites() + public function it_can_configure_its_sites_from_existing_settings() { $settings = new JsonSettings([ 'sites' => [ @@ -125,6 +125,7 @@ public function it_can_configure_its_sites() 'to' => '/home/vagrant/Laravel/public', 'type' => 'laravel', 'schedule' => true, + 'php' => '5.6', ], ], ]); @@ -133,25 +134,57 @@ public function it_can_configure_its_sites() $attributes = $settings->toArray(); $this->assertEquals([ - 'map' => 'test.com.app', - 'to' => '/home/vagrant/test-com/public', + 'map' => 'homestead.app', + 'to' => '/home/vagrant/Laravel/public', 'type' => 'laravel', 'schedule' => true, + 'php' => '5.6', + ], $attributes['sites'][0]); + } + + /** @test */ + public function it_can_configure_its_sites_from_empty_settings() + { + $settings = new JsonSettings([]); + $settings->configureSites('test.com', 'test-com'); + + $attributes = $settings->toArray(); + $this->assertEquals([ + 'map' => 'test.com.app', + 'to' => '/home/vagrant/test-com/public', ], $attributes['sites'][0]); } /** @test */ - public function it_can_configure_its_shared_folders() + public function it_can_configure_its_shared_folders_from_existing_settings() { $settings = new JsonSettings([ 'folders' => [ - 'map' => '~/Code', - 'to' => '/home/vagrant/', + [ + 'map' => '~/Code', + 'to' => '/home/vagrant/Code', + 'type' => 'nfs', + ], ], ]); $settings->configureSharedFolders('/a/path/for/project_name', 'project_name'); + $attributes = $settings->toArray(); + $this->assertEquals([ + 'map' => '/a/path/for/project_name', + 'to' => '/home/vagrant/Code', + 'type' => 'nfs', + ], $attributes['folders'][0]); + } + + /** @test */ + public function it_can_configure_its_shared_folders_from_empty_settings() + { + $settings = new JsonSettings([]); + + $settings->configureSharedFolders('/a/path/for/project_name', 'project_name'); + $attributes = $settings->toArray(); $this->assertEquals([ 'map' => '/a/path/for/project_name', diff --git a/tests/Settings/YamlSettingsTest.php b/tests/Settings/YamlSettingsTest.php index d02ef2bbf..374d2f105 100644 --- a/tests/Settings/YamlSettingsTest.php +++ b/tests/Settings/YamlSettingsTest.php @@ -117,7 +117,7 @@ public function it_can_update_its_ip_address() } /** @test */ - public function it_can_configure_its_sites() + public function it_can_configure_its_sites_from_existing_settings() { $settings = new YamlSettings([ 'sites' => [ @@ -126,6 +126,7 @@ public function it_can_configure_its_sites() 'to' => '/home/vagrant/Laravel/public', 'type' => 'laravel', 'schedule' => true, + 'php' => '5.6', ], ], ]); @@ -134,25 +135,57 @@ public function it_can_configure_its_sites() $attributes = $settings->toArray(); $this->assertEquals([ - 'map' => 'test.com.app', - 'to' => '/home/vagrant/test-com/public', + 'map' => 'homestead.app', + 'to' => '/home/vagrant/Laravel/public', 'type' => 'laravel', 'schedule' => true, + 'php' => '5.6', + ], $attributes['sites'][0]); + } + + /** @test */ + public function it_can_configure_its_sites_from_empty_settings() + { + $settings = new YamlSettings([]); + $settings->configureSites('test.com', 'test-com'); + + $attributes = $settings->toArray(); + $this->assertEquals([ + 'map' => 'test.com.app', + 'to' => '/home/vagrant/test-com/public', ], $attributes['sites'][0]); } /** @test */ - public function it_can_configure_its_shared_folders() + public function it_can_configure_its_shared_folders_from_existing_settings() { $settings = new YamlSettings([ 'folders' => [ - 'map' => '~/Code', - 'to' => '/home/vagrant', + [ + 'map' => '~/Code', + 'to' => '/home/vagrant/Code', + 'type' => 'nfs', + ], ], ]); $settings->configureSharedFolders('/a/path/for/project_name', 'project_name'); + $attributes = $settings->toArray(); + $this->assertEquals([ + 'map' => '/a/path/for/project_name', + 'to' => '/home/vagrant/Code', + 'type' => 'nfs', + ], $attributes['folders'][0]); + } + + /** @test */ + public function it_can_configure_its_shared_folders_from_empty_settings() + { + $settings = new YamlSettings([]); + + $settings->configureSharedFolders('/a/path/for/project_name', 'project_name'); + $attributes = $settings->toArray(); $this->assertEquals([ 'map' => '/a/path/for/project_name',