generated from archtechx/template
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
113 additions
and
34 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
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ArchTech\SEO\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Intervention\Image\ImageManager; | ||
|
||
class GenerateFaviconsCommand extends Command | ||
{ | ||
protected $signature = 'seo:generate-favicons {from?}'; | ||
|
||
protected $description = 'Generate favicons based on a source file'; | ||
|
||
public function handle(): int | ||
{ | ||
$path = $this->argument('from') ?? public_path('assets/logo.png'); | ||
|
||
if (! is_string($path)) { | ||
$this->error('The `from` argument must be a string.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$this->info('Generating favicons...'); | ||
|
||
if (! class_exists(ImageManager::class)) { | ||
$this->error('Intervention not available, please run `composer require intervention/image`'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
if (! file_exists($path)) { | ||
$this->error("Given icon path `{$path}` does not exist."); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
// GD driver doesn't support .ico, that's why we use ImageMagick. | ||
$manager = new ImageManager(['driver' => 'imagick']); | ||
|
||
$this->comment('Generating ico...'); | ||
|
||
$manager | ||
->make($path) | ||
->resize(32, 32) | ||
->save(public_path('favicon.ico')); | ||
|
||
$this->comment('Generating png...'); | ||
|
||
$manager | ||
->make($path) | ||
->resize(32, 32) | ||
->save(public_path('favicon.png')); | ||
|
||
$this->info('All favicons have been generated!'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,14 +1,43 @@ | ||
<?php | ||
|
||
use ArchTech\SEO\Commands\GenerateFaviconsCommand; | ||
|
||
use function Pest\Laravel\artisan; | ||
use function PHPUnit\Framework\assertFileDoesNotExist; | ||
use function PHPUnit\Framework\assertFileExists; | ||
|
||
test("it should throw an exception if the given source icon doesn't exist", function () { | ||
seo()->favicon('i-dont-exist.png'); | ||
})->throws(Exception::class, 'Given icon path `i-dont-exist.png` does not exist.'); | ||
// Clean up generated files | ||
beforeEach(function () { | ||
$files = [ | ||
'favicon.ico', | ||
'favicon.png', | ||
]; | ||
|
||
foreach ($files as $file) { | ||
@unlink(public_path($file)); | ||
} | ||
}); | ||
|
||
test('it should generate two favicons', function () { | ||
seo()->favicon(__DIR__ . '/../stubs/logo.png'); | ||
seo()->favicon(); | ||
|
||
$from = __DIR__ . '/../stubs/logo.png'; | ||
|
||
artisan(GenerateFaviconsCommand::class, [ | ||
'from' => $from, | ||
])->assertSuccessful(); | ||
|
||
assertFileExists(public_path('favicon.ico')); | ||
assertFileExists(public_path('favicon.png')); | ||
}); | ||
|
||
test('it should fail because the from path is incorrect', function () { | ||
seo()->favicon(); | ||
|
||
artisan(GenerateFaviconsCommand::class, [ | ||
'from' => 'i/dont/exist.png', | ||
])->assertFailed(); | ||
|
||
assertFileDoesNotExist(public_path('favicon.ico')); | ||
assertFileDoesNotExist(public_path('favicon.png')); | ||
}); |