Skip to content

Commit

Permalink
Create build:sitemap command
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 20, 2022
1 parent e827132 commit 82c73a3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Commands/HydeBuildSitemapCommand.php
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);
}
}
39 changes: 39 additions & 0 deletions tests/Feature/Commands/HydeBuildSitemapCommandTest.php
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'));
}

}

0 comments on commit 82c73a3

Please sign in to comment.