Skip to content

Commit

Permalink
Use installer to set the site URL in config
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 4, 2022
1 parent 3f0c843 commit d5f56ac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Commands/HydeInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class HydeInstallCommand extends Command
protected $description = 'Initialize a new Hyde project.';

public ?string $siteName = null;
public ?string $siteUrl = null;

public function handle(): int
{
Expand All @@ -37,6 +38,8 @@ public function handle(): int

$this->promptForSiteName();

$this->promptForSiteUrl();

$this->promptForHomepage();

return 0;
Expand All @@ -53,6 +56,18 @@ protected function promptForSiteName()
$this->line('Skipping site name.');
}

protected function promptForSiteUrl()
{
if ($this->siteUrl = $this->ask('What is the URL of your site? (leave blank to skip)')) {
$this->line('The URL is used to create permalinks which can improve SEO.');
$this->updateSiteUrl();
$this->info('Site URL set to: ' . $this->siteUrl);
return;
}

$this->line('Skipping site URL.');
}

protected function promptForHomepage()
{
$this->info('Hyde has a few different homepage options.');
Expand All @@ -73,4 +88,15 @@ protected function updateSiteName(): void
);
file_put_contents(Hyde::path('config/hyde.php'), $config);
}

protected function updateSiteUrl(): void
{
$config = file_get_contents(Hyde::path('config/hyde.php'));
$config = str_replace(
"'site_url' => env('SITE_URL', null),",
"'site_url' => env('SITE_URL', '".$this->siteUrl."'),",
$config
);
file_put_contents(Hyde::path('config/hyde.php'), $config);
}
}
34 changes: 34 additions & 0 deletions tests/Feature/Commands/HydeInstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function test_command_output()
->expectsOutput('Installing HydePHP...')
->doesntExpectOutput('Aborting installation.')
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsQuestion('What is the URL of your site? (leave blank to skip)', null)
->expectsOutput('Hyde has a few different homepage options.')
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);
Expand All @@ -39,6 +40,7 @@ public function test_prompt_for_site_name_saves_selected_site_name()
->expectsOutputToContain('Welcome to HydePHP!')
->expectsQuestion('Do you want to continue?', true)
->expectsQuestion('What is the name of your site? (leave blank to skip)', 'My Site')
->expectsQuestion('What is the URL of your site? (leave blank to skip)', null)
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);

Expand All @@ -52,13 +54,44 @@ public function test_prompt_for_site_name_does_nothing_if_user_skips()
->expectsOutputToContain('Welcome to HydePHP!')
->expectsQuestion('Do you want to continue?', true)
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsQuestion('What is the URL of your site? (leave blank to skip)', null)
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);

$this->assertStringNotContainsString('My Site',
file_get_contents(Hyde::path('config/hyde.php')));
}

// test prompt for site url saves selected site url
public function test_prompt_for_site_url_saves_selected_site_url()
{
$this->artisan('install')
->expectsOutputToContain('Welcome to HydePHP!')
->expectsQuestion('Do you want to continue?', true)
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsQuestion('What is the URL of your site? (leave blank to skip)', 'https://foo.example.com')
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);

$this->assertStringContainsString('https://foo.example.com',
file_get_contents(Hyde::path('config/hyde.php')));
}

// test prompt for site url does nothing if user skips
public function test_prompt_for_site_url_does_nothing_if_user_skips()
{
$this->artisan('install')
->expectsOutputToContain('Welcome to HydePHP!')
->expectsQuestion('Do you want to continue?', true)
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsQuestion('What is the URL of your site? (leave blank to skip)', null)
->expectsQuestion('Would you like to select one?', false)
->assertExitCode(0);

$this->assertStringContainsString("env('SITE_URL', null)",
file_get_contents(Hyde::path('config/hyde.php')));
}

public function test_command_calls_publish_homepage_command()
{
$this->artisan('install')
Expand All @@ -67,6 +100,7 @@ public function test_command_calls_publish_homepage_command()
->expectsOutput('Installing HydePHP...')
->doesntExpectOutput('Aborting installation.')
->expectsQuestion('What is the name of your site? (leave blank to skip)', null)
->expectsQuestion('What is the URL of your site? (leave blank to skip)', null)
->expectsOutput('Hyde has a few different homepage options.')
->expectsQuestion('Would you like to select one?', true)
->expectsQuestion('Which homepage do you want to publish?', 'default')
Expand Down

0 comments on commit d5f56ac

Please sign in to comment.