-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e827132
commit 82c73a3
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Commands; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Services\SitemapService; | ||
use LaravelZero\Framework\Commands\Command; | ||
|
||
/** | ||
* Hyde Command to run the Build Process for the Sitemap. | ||
* | ||
* @see \Tests\Feature\Commands\HydeBuildSitemapCommandTest | ||
*/ | ||
class HydeBuildSitemapCommand extends Command | ||
{ | ||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'build:sitemap'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Generate the sitemap.xml'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle(): int | ||
{ | ||
$actionTime = microtime(true); | ||
|
||
$this->comment('Generating sitemap...'); | ||
file_put_contents(Hyde::getSiteOutputPath('sitemap.xml'), SitemapService::generateSitemap()); | ||
$this->line(' > Created <info>sitemap.xml</> in '.$this->getExecutionTimeInMs($actionTime)."ms\n"); | ||
|
||
return 0; | ||
} | ||
|
||
protected function getExecutionTimeInMs(float $timeStart): float | ||
{ | ||
return number_format(((microtime(true) - $timeStart) * 1000), 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Commands; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Tests\TestCase; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Commands\HydeBuildSitemapCommand | ||
*/ | ||
class HydeBuildSitemapCommandTest extends TestCase | ||
{ | ||
public function test_sitemap_is_not_generated_when_conditions_are_not_met() | ||
{ | ||
config(['hyde.site_url' => '']); | ||
config(['hyde.generateSitemap' => false]); | ||
|
||
unlinkIfExists(Hyde::path('_site/sitemap.xml')); | ||
$this->artisan('build') | ||
->assertExitCode(0); | ||
|
||
$this->assertFileDoesNotExist(Hyde::path('_site/sitemap.xml')); | ||
} | ||
|
||
public function test_sitemap_is_generated_when_conditions_are_met() | ||
{ | ||
config(['hyde.site_url' => 'https://example.com']); | ||
config(['hyde.generateSitemap' => true]); | ||
|
||
unlinkIfExists(Hyde::path('_site/sitemap.xml')); | ||
$this->artisan('build') | ||
->expectsOutput('Generating sitemap...') | ||
->assertExitCode(0); | ||
|
||
$this->assertFileExists(Hyde::path('_site/sitemap.xml')); | ||
unlink(Hyde::path('_site/sitemap.xml')); | ||
} | ||
|
||
} |